Debian Codegraph @

docs/howto/security.rst

raw
1 .. include:: ../header.rst
2
3 =============================
4 Deploying Docutils Securely
5 =============================
6
7 :Author: David Goodger
8 :Contact: [email protected]
9 :Date: $Date: 2025-12-14 14:20:59 +0100 (So, 14. Dez 2025) $
10 :Revision: $Revision: 10272 $
11 :Copyright: This document has been placed in the public domain.
12
13 .. contents::
14
15 Introduction
16 ============
17
18 Initially, Docutils was intended for command-line tools and
19 single-user applications. Through-the-web editing and processing was
20 not envisaged, therefore web security was not a consideration. Once
21 Docutils/reStructuredText started being incorporated into an
22 ever-increasing number of web applications (blogs__, wikis__, content
23 management systems, and others), several security issues arose and
24 have been addressed. Still, **Docutils does not come in a
25 through-the-web secure state**, because this would inconvenience
26 ordinary users. This document provides pointers to help you secure
27 the Docutils software in your applications.
28
29 __ ../../FAQ.html#are-there-any-weblog-blog-projects-that-use-restructuredtext-syntax
30 __ ../../FAQ.html#are-there-any-wikis-that-use-restructuredtext-syntax
31
32
33 The Issues
34 ==========
35
36 File Creation
37 -------------
38
39 Docutils does not do any checks before writing to a file:
40
41 * Existing **files are overwritten** without asking!
42 * Files may be **written to any location** accessible to the process.
43 * There are **no restrictions to** the **file names**.
44
45 Special care must be taken when allowing users to configure the `output`_
46 (or `_destination`_), `warning_stream`_, or `record_dependencies`_ settings.
47
48 .. _output: ../user/config.html#output
49 .. _`_destination`: ../user/config.html#destination
50 .. _warning_stream: ../user/config.html#warning-stream
51 .. _record_dependencies: ../user/config.html#record-dependencies
52
53
54 External Data Insertion
55 -----------------------
56
57 There are several `reStructuredText directives`_ that can insert external
58 data (files or content fetched from a URL) into the output document.
59 These directives are:
60
61 * "include_", by its very nature,
62 * "raw_", through its ``:file:`` and ``:url:`` options,
63 * "csv-table_", through its ``:file:`` and ``:url:`` options,
64 * "image_" through its ``:loading:`` option.
65
66 The "include_" directive and the other directives' file insertion
67 features can be disabled by setting "file_insertion_enabled_" to
68 "false__".
69
70 __ ../user/config.html#configuration-file-syntax
71 .. _reStructuredText directives: ../ref/rst/directives.html
72 .. _include: ../ref/rst/directives.html#include
73 .. _raw: ../ref/rst/directives.html#raw
74 .. _csv-table: ../ref/rst/directives.html#csv-table
75 .. _image: ../ref/rst/directives.html#image
76 .. _embed_images: ../user/config.html#embed-images
77 .. _file_insertion_enabled: ../user/config.html#file-insertion-enabled
78
79
80 Raw HTML Insertion
81 ------------------
82
83 Content of the "raw_" directive is passed untouched to the Writer.
84 Docutils standard writers insert "raw" content with matching
85 format_ into the output document without any checks or changes.
86 This can be abused to bypass site features or insert malicious
87 script code into a web page.
88
89 A web-secure Writer must ignore or sanitize "raw" content.
90 Alternatively, the "raw" directive can be disabled by setting
91 "raw_enabled_" to "false".
92
93 .. _raw_enabled: ../user/config.html#raw-enabled
94 .. _format: ../ref/doctree.html#format
95
96 CPU and memory utilization
97 --------------------------
98
99 Parsing **complex reStructuredText documents may require high
100 processing resources**. This enables `Denial of Service` attacks using
101 specially crafted input.
102
103 It is recommended to enforce limits for the computation time and
104 resource utilization of the Docutils process when processing
105 untrusted input. In addition, the "line_length_limit_" can be
106 adapted.
107
108 .. _line_length_limit: ../user/config.html#line-length-limit
109
110
111 Securing Docutils
112 =================
113
114 Programmatically Via Application Default Settings
115 -------------------------------------------------
116
117 If your application calls Docutils via one of the `convenience
118 functions`_, you can pass a dictionary of default settings that
119 override the component defaults::
120
121 defaults = {'file_insertion_enabled': False,
122 'raw_enabled': False}
123 output = docutils.core.publish_string(
124 ..., settings_overrides=defaults)
125
126 Note that these defaults can be overridden by configuration files (and
127 command-line options if applicable). If this is not desired, you can
128 disable configuration file processing with the ``_disable_config``
129 setting::
130
131 defaults = {'file_insertion_enabled': False,
132 'raw_enabled': False,
133 '_disable_config': True}
134 output = docutils.core.publish_string(
135 ..., settings_overrides=defaults)
136
137 .. _convenience functions: ../api/publisher.html
138
139
140 Via a Configuration File
141 ------------------------
142
143 You may secure Docutils via a configuration file:
144
145 * if your application executes one of the `Docutils front-end tools`_
146 as a separate process;
147 * if you cannot or choose not to alter the source code of your
148 application or the component that calls Docutils; or
149 * if you want to secure all Docutils deployments system-wide.
150
151 If you call Docutils programmatically, it may be preferable to use the
152 methods described in the section above.
153
154 Docutils automatically looks in three places for a configuration file:
155
156 * ``/etc/docutils.conf``, for system-wide configuration,
157 * ``./docutils.conf`` (in the current working directory), for
158 project-specific configuration, and
159 * ``~/.docutils`` (in the user's home directory), for user-specific
160 configuration.
161
162 These locations can be overridden by the ``DOCUTILSCONFIG``
163 environment variable. Details about configuration files, the purpose
164 of the various locations, and ``DOCUTILSCONFIG`` are available in the
165 `"Configuration Files"`_ section of `Docutils Configuration`_.
166
167 To fully secure a recent Docutils installation, the configuration file
168 should contain the following lines ::
169
170 [general]
171 file-insertion-enabled: off
172 raw-enabled: no
173
174 and untrusted users must be prevented to modify or create local
175 configuration files that overwrite these settings.
176
177 .. _Docutils front-end tools: ../user/tools.html
178 .. _"Configuration Files": ../user/config.html#configuration-files
179 .. _Docutils Configuration: ../user/config.html
180
181
182 Version Applicability
183 =====================
184
185 The "file_insertion_enabled_" and "raw_enabled_" settings were added
186 to Docutils 0.3.9; previous versions will ignore these settings.
187
188 A bug existed in the configuration file handling of these settings in
189 Docutils 0.4 and earlier: the right-hand-side needed to be left blank
190 (no values)::
191
192 [general]
193 file-insertion-enabled:
194 raw-enabled:
195
196 The bug was fixed with the 0.4.1 release on 2006-11-12.
197
198 The "line_length_limit_" is new in Docutils 0.17.
199
200
201 Related Documents
202 =================
203
204 `Docutils Runtime Settings`_ explains the relationship between
205 component settings specifications, application settings
206 specifications, configuration files, and command-line options
207
208 `Docutils Configuration`_ describes configuration files (locations,
209 structure, and syntax), and lists all settings and command-line
210 options.
211
212 .. _Docutils Runtime Settings: ../api/runtime-settings.html