Skip to content

Commit 750a5bd

Browse files
committed
Disable external file access by default
1 parent a828b00 commit 750a5bd

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

NEWS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
* Fix conversion of unmerged table cells.
66

7-
* Support disabling external file accesses using the external_file_access argument.
7+
* Disable external file accesses by default. External file access can be enabled
8+
using the external_file_access argument.
89

910
* Handle numbering levels defined without an index.
1011

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ Converts the source document to HTML.
291291
pass `include_default_style_map=False`.
292292

293293
* Source documents may reference files outside of the source document.
294-
To disable access to any such external files during the conversion process,
295-
pass `external_file_access=False`.
296-
This is highly recommended when converting untrusted user input.
294+
Access to any such external files is disabled by default.
295+
To enable access when converting trusted source documents,
296+
pass `external_file_access=True`.
297297

298298
* `convert_image`: by default, images are converted to `<img>` elements with the source included inline in the `src` attribute.
299299
Set this argument to an [image converter](#image-converters) to override the default behaviour.
@@ -420,8 +420,9 @@ For instance:
420420
and embed the HTML into your website,
421421
this may allow arbitrary files on the server to be read and exfiltrated.
422422

423-
To disable access to any such external files during the conversion process,
424-
pass `external_file_access=False`.
423+
To avoid this issue, access to any such external files is disabled by default.
424+
To enable access when converting trusted source documents,
425+
pass `external_file_access=True`.
425426

426427
### Document transforms
427428

mammoth/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def convert(
3434
kwargs["embedded_style_map"] = read_style_map(fileobj)
3535

3636
if external_file_access is _undefined:
37-
external_file_access = True
37+
external_file_access = False
3838

3939
return options.read_options(kwargs).bind(lambda convert_options:
4040
docx.read(fileobj, external_file_access=external_file_access).map(transform_document).bind(lambda document:

tests/mammoth_tests.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,40 +110,40 @@ def test_inline_images_referenced_by_path_relative_to_base_are_included_in_outpu
110110
assert_equal([], result.messages)
111111

112112

113-
def test_images_stored_outside_of_document_are_included_in_output():
113+
def test_when_external_file_access_is_enabled_images_stored_outside_of_document_are_included_in_output():
114114
with open(generate_test_path("external-picture.docx"), "rb") as fileobj:
115-
result = mammoth.convert_to_html(fileobj=fileobj)
115+
result = mammoth.convert_to_html(fileobj=fileobj, external_file_access=True)
116116
assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value)
117117
assert_equal([], result.messages)
118118

119119

120-
def test_warn_if_images_stored_outside_of_document_are_specified_when_passing_fileobj_without_name():
120+
def test_when_external_file_access_is_enabled_warn_if_images_stored_outside_of_document_are_specified_when_passing_fileobj_without_name():
121121
fileobj = io.BytesIO()
122122
with open(generate_test_path("external-picture.docx"), "rb") as source_fileobj:
123123
shutil.copyfileobj(source_fileobj, fileobj)
124124

125-
result = mammoth.convert_to_html(fileobj=fileobj)
125+
result = mammoth.convert_to_html(fileobj=fileobj, external_file_access=True)
126126
assert_equal("", result.value)
127127
assert_equal([results.warning("could not find external image 'tiny-picture.png', fileobj has no name")], result.messages)
128128

129129

130-
def test_warn_if_images_stored_outside_of_document_are_specified_when_external_file_access_is_disabled():
130+
def test_given_external_file_access_is_disabled_by_default_then_warn_if_images_stored_outside_of_document_are_specified():
131131
with open(generate_test_path("external-picture.docx"), "rb") as fileobj:
132-
result = mammoth.convert_to_html(fileobj=fileobj, external_file_access=False)
132+
result = mammoth.convert_to_html(fileobj=fileobj)
133133

134134
assert_equal("", result.value)
135135
assert_equal([results.warning("could not open external image 'tiny-picture.png', external file access is disabled")], result.messages)
136136

137137

138-
def test_warn_if_images_stored_outside_of_document_are_not_found():
138+
def test_when_external_file_access_is_enabled_warn_if_images_stored_outside_of_document_are_not_found():
139139
with tempman.create_temp_dir() as temp_dir:
140140
document_path = os.path.join(temp_dir.path, "document.docx")
141141
with open(document_path, "wb") as fileobj:
142142
with open(generate_test_path("external-picture.docx"), "rb") as source_fileobj:
143143
shutil.copyfileobj(source_fileobj, fileobj)
144144

145145
with open(document_path, "rb") as fileobj:
146-
result = mammoth.convert_to_html(fileobj=fileobj)
146+
result = mammoth.convert_to_html(fileobj=fileobj, external_file_access=True)
147147
assert_equal("", result.value)
148148
expected_warning = "could not open external image: 'tiny-picture.png'"
149149
assert_equal("warning", result.messages[0].type)

0 commit comments

Comments
 (0)