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