summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgoodger <goodger@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2005-03-21 21:26:21 +0000
committergoodger <goodger@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2005-03-21 21:26:21 +0000
commit74d2d3afebad25d1eff7b9bd795ac02c974fa238 (patch)
tree188b23a66f535ebfdcf36361a87a4b9e8bb94761
parentcec4eb950d8c2390f07d6b463041f53853bab00f (diff)
downloaddocutils-74d2d3afebad25d1eff7b9bd795ac02c974fa238.tar.gz
Added settings: ``file_insertion_enabled`` & ``raw_enabled``.
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@3071 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
-rw-r--r--docutils/HISTORY.txt6
-rw-r--r--docutils/docs/user/config.txt17
-rw-r--r--docutils/docutils/parsers/rst/__init__.py18
-rw-r--r--docutils/docutils/parsers/rst/directives/misc.py12
-rw-r--r--docutils/test/functional/expected/dangerous.html50
-rw-r--r--docutils/test/functional/input/dangerous.txt13
-rw-r--r--docutils/test/functional/tests/dangerous.py12
7 files changed, 127 insertions, 1 deletions
diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt
index 6de976038..94bca94db 100644
--- a/docutils/HISTORY.txt
+++ b/docutils/HISTORY.txt
@@ -33,6 +33,10 @@ Changes Since 0.3.7
* docutils/languages/nl.py: Added to project; Dutch mappings by
Martijn Pieters.
+* docutils/parsers/rst/__init__.py:
+
+ - Added settings: ``file_insertion_enabled`` & ``raw_enabled``.
+
* docutils/parsers/rst/states.py:
- Added check for escaped at-mark to prevent email address recognition.
@@ -54,6 +58,8 @@ Changes Since 0.3.7
- Fixed "include" and "raw" directives to catch text decoding
errors.
- Allowed whitespace in "include" & "raw" directive paths.
+ - Added support for ``file_insertion_enabled`` & ``raw_enabled``
+ settings in "include" & "raw" directives
* docutils/parsers/rst/directives/tables.py:
diff --git a/docutils/docs/user/config.txt b/docutils/docs/user/config.txt
index 458801347..0e74cc0b3 100644
--- a/docutils/docs/user/config.txt
+++ b/docutils/docs/user/config.txt
@@ -228,6 +228,17 @@ _`expose_internals`
Default: don't (None). Options: ``--expose-internal-attribute``
(hidden, for development use only).
+_`file_insertion_enabled`
+ Enable or disable directives that insert the contents of external
+ files, such as the "include__" & "raw__". A "warning" system
+ message (including the directive text) is inserted instead.
+
+ Default: enabled (1). Options: ``--file-insertion-enabled,
+ --no-file-insertion``.
+
+ .. _include: ../ref/rst/directives.html#include
+ .. _raw: ../ref/rst/directives.html#raw
+
_`footnote_backlinks`
Enable or disable backlinks from footnotes and citations to their
references.
@@ -309,6 +320,12 @@ _`output_encoding_error_handler`
Default: "strict". Options: ``--output-encoding-error-handler,
--output-encoding, -o``.
+_`raw_enabled`
+ Enable or disable the "raw_" directive. A "warning" system
+ message (including the directive text) is inserted instead.
+
+ Default: enabled (1). Options: ``--raw-enabled, --no-raw``.
+
_`report_level`
Verbosity threshold at or above which system messages are
reported.
diff --git a/docutils/docutils/parsers/rst/__init__.py b/docutils/docutils/parsers/rst/__init__.py
index 88795a830..8d7935be6 100644
--- a/docutils/docutils/parsers/rst/__init__.py
+++ b/docutils/docutils/parsers/rst/__init__.py
@@ -112,7 +112,23 @@ class Parser(docutils.parsers.Parser):
('Leave spaces before footnote references.',
['--leave-footnote-reference-space'],
{'action': 'store_false', 'dest': 'trim_footnote_reference_space',
- 'validator': frontend.validate_boolean}),))
+ 'validator': frontend.validate_boolean}),
+ ('Disable directives that insert the contents of external file '
+ '("include" & "raw"); replaced with a "warning" system message.',
+ ['--no-file-insertion'],
+ {'action': 'store_false', 'default': 1,
+ 'dest': 'file_insertion_enabled'}),
+ ('Enable directives that insert the contents of external file '
+ '("include" & "raw"). Enabled by default.',
+ ['--file-insertion-enabled'],
+ {'action': 'store_true', 'dest': 'file_insertion_enabled'}),
+ ('Disable the "raw" directives; replaced with a "warning" '
+ 'system message.',
+ ['--no-raw'],
+ {'action': 'store_false', 'default': 1, 'dest': 'raw_enabled'}),
+ ('Enable the "raw" directive. Enabled by default.',
+ ['--raw-enabled'],
+ {'action': 'store_true', 'dest': 'raw_enabled'}),))
config_section = 'restructuredtext parser'
config_section_dependencies = ('parsers',)
diff --git a/docutils/docutils/parsers/rst/directives/misc.py b/docutils/docutils/parsers/rst/directives/misc.py
index 1ea4699f3..1a6cbc17c 100644
--- a/docutils/docutils/parsers/rst/directives/misc.py
+++ b/docutils/docutils/parsers/rst/directives/misc.py
@@ -24,6 +24,11 @@ except ImportError:
def include(name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
"""Include a reST file as part of the content of this reST file."""
+ if not state.document.settings.file_insertion_enabled:
+ warning = state_machine.reporter.warning(
+ '"%s" directive disabled.' % name,
+ nodes.literal_block(block_text, block_text), line=lineno)
+ return [warning]
source = state_machine.input_lines.source(
lineno - state_machine.input_offset - 1)
source_dir = os.path.dirname(os.path.abspath(source))
@@ -76,6 +81,13 @@ def raw(name, arguments, options, content, lineno,
Content may be included inline (content section of directive) or
imported from a file or url.
"""
+ if ( not state.document.settings.raw_enabled
+ or (not state.document.settings.file_insertion_enabled
+ and (options.has_key('file') or options.has_key('url'))) ):
+ warning = state_machine.reporter.warning(
+ '"%s" directive disabled.' % name,
+ nodes.literal_block(block_text, block_text), line=lineno)
+ return [warning]
attributes = {'format': ' '.join(arguments[0].lower().split())}
encoding = options.get('encoding', state.document.settings.input_encoding)
if content:
diff --git a/docutils/test/functional/expected/dangerous.html b/docutils/test/functional/expected/dangerous.html
new file mode 100644
index 000000000..9f0b58a54
--- /dev/null
+++ b/docutils/test/functional/expected/dangerous.html
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<meta name="generator" content="Docutils 0.3.8: http://docutils.sourceforge.net/" />
+<title></title>
+<link rel="stylesheet" href="default.css" type="text/css" />
+</head>
+<body>
+<div class="document">
+<p>Potentially dangerous features (security holes):</p>
+<div class="system-message">
+<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils">functional/input/dangerous.txt</tt>, line 3)</p>
+<p>&quot;include&quot; directive disabled.</p>
+<pre class="literal-block">
+.. include:: /etc/passwd
+</pre>
+</div>
+<div class="system-message">
+<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils">functional/input/dangerous.txt</tt>, line 4)</p>
+<p>&quot;raw&quot; directive disabled.</p>
+<pre class="literal-block">
+.. raw:: html
+ :file: /etc/passwd
+</pre>
+</div>
+<div class="system-message">
+<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils">functional/input/dangerous.txt</tt>, line 6)</p>
+<p>&quot;raw&quot; directive disabled.</p>
+<pre class="literal-block">
+.. raw:: html
+ :url: file:///etc/passwd
+</pre>
+</div>
+<div class="system-message">
+<p class="system-message-title">System Message: WARNING/2 (<tt class="docutils">functional/input/dangerous.txt</tt>, line 8)</p>
+<p>&quot;raw&quot; directive disabled.</p>
+<pre class="literal-block">
+.. raw:: html
+
+ &lt;script&gt;
+ that does something really nasty
+ &lt;/script&gt;
+
+</pre>
+</div>
+</div>
+</body>
+</html>
diff --git a/docutils/test/functional/input/dangerous.txt b/docutils/test/functional/input/dangerous.txt
new file mode 100644
index 000000000..2c1e55251
--- /dev/null
+++ b/docutils/test/functional/input/dangerous.txt
@@ -0,0 +1,13 @@
+Potentially dangerous features (security holes):
+
+.. include:: /etc/passwd
+.. raw:: html
+ :file: /etc/passwd
+.. raw:: html
+ :url: file:///etc/passwd
+.. raw:: html
+
+ <script>
+ that does something really nasty
+ </script>
+
diff --git a/docutils/test/functional/tests/dangerous.py b/docutils/test/functional/tests/dangerous.py
new file mode 100644
index 000000000..620a927ba
--- /dev/null
+++ b/docutils/test/functional/tests/dangerous.py
@@ -0,0 +1,12 @@
+# Source and destination file names.
+test_source = "dangerous.txt"
+test_destination = "dangerous.html"
+
+# Keyword parameters passed to publish_file.
+reader_name = "standalone"
+parser_name = "rst"
+writer_name = "html"
+
+# Settings
+settings_overrides['file_insertion_enabled'] = 0
+settings_overrides['raw_enabled'] = 0