Skip to content

Commit a929dc7

Browse files
committed
Disable external file access by default
1 parent 98c9854 commit a929dc7

5 files changed

Lines changed: 39 additions & 30 deletions

File tree

NEWS

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

33
* Ignore style definitions using a style ID that has already been used.
44

5-
* Support disabling external file accesses using the external_file_access argument.
5+
* Disable external file accesses by default. External file access can be enabled
6+
using enableExternalFileAccess().
67

78
* Handle numbering levels defined without an index.
89

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ Methods:
255255
if the document contains an embedded style map, then it is combined with the default style map.
256256
Call this to ignore any embedded style maps.
257257

258-
* `DocumentConvert disableExternalFileAccess()`: Source documents may reference files outside of the source document.
259-
Call this to disable access to any such external files during the conversion process.
260-
This is highly recommended when converting untrusted user input.
258+
* `DocumentConverter enableExternalFileAccess()`: Source documents may reference files outside of the source document.
259+
Access to any such external files is disabled by default.
260+
Call this to enable access when converting trusted source documents.
261261

262262
* `DocumentConverter preserveEmptyParagraphs()`: by default, empty paragraphs are ignored.
263263
Call this to preserve empty paragraphs in the output.
@@ -330,6 +330,10 @@ For instance:
330330
automatically convert the document into HTML on the server,
331331
and embed the HTML into your website,
332332
this may allow arbitrary files on the server to be read and exfiltrated.
333+
*
334+
To avoid this issue, access to any such external files is disabled by default.
335+
To enable access when converting trusted source documents,
336+
call `enableExternalFileAccess()`.
333337

334338
## Writing style maps
335339

src/main/java/org/zwobble/mammoth/DocumentConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ public DocumentConverter disableEmbeddedStyleMap() {
6262

6363
/**
6464
* Source documents may reference files outside of the source document.
65-
* Call this to disable access to any such external files during the conversion process.
66-
* This is highly recommended when converting untrusted user input.
65+
* Access to any such external files is disabled by default.
66+
* Call this to enable access when converting trusted source documents.
6767
*/
68-
public DocumentConverter disableExternalFileAccess() {
69-
return new DocumentConverter(options.disableExternalFileAccess());
68+
public DocumentConverter enableExternalFileAccess() {
69+
return new DocumentConverter(options.enableExternalFileAccess());
7070
}
7171

7272
/**

src/main/java/org/zwobble/mammoth/internal/conversion/DocumentToHtmlOptions.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class DocumentToHtmlOptions {
3131
private final StyleMap embeddedStyleMap;
3232
private final boolean disableDefaultStyleMap;
3333
private final boolean disableEmbeddedStyleMap;
34-
private final boolean disableExternalFileAccess;
34+
private final boolean enableExternalFileAccess;
3535
private final InternalImageConverter imageConverter;
3636

3737
public DocumentToHtmlOptions(
@@ -41,7 +41,7 @@ public DocumentToHtmlOptions(
4141
StyleMap embeddedStyleMap,
4242
boolean disableDefaultStyleMap,
4343
boolean disableEmbeddedStyleMap,
44-
boolean disableExternalFileAccess,
44+
boolean enableExternalFileAccess,
4545
InternalImageConverter imageConverter
4646
) {
4747
this.idPrefix = idPrefix;
@@ -50,7 +50,7 @@ public DocumentToHtmlOptions(
5050
this.embeddedStyleMap = embeddedStyleMap;
5151
this.disableDefaultStyleMap = disableDefaultStyleMap;
5252
this.disableEmbeddedStyleMap = disableEmbeddedStyleMap;
53-
this.disableExternalFileAccess = disableExternalFileAccess;
53+
this.enableExternalFileAccess = enableExternalFileAccess;
5454
this.imageConverter = imageConverter;
5555
}
5656

@@ -62,7 +62,7 @@ public DocumentToHtmlOptions idPrefix(String prefix) {
6262
embeddedStyleMap,
6363
disableDefaultStyleMap,
6464
disableEmbeddedStyleMap,
65-
disableExternalFileAccess,
65+
enableExternalFileAccess,
6666
imageConverter
6767
);
6868
}
@@ -75,7 +75,7 @@ public DocumentToHtmlOptions preserveEmptyParagraphs() {
7575
embeddedStyleMap,
7676
disableDefaultStyleMap,
7777
disableEmbeddedStyleMap,
78-
disableExternalFileAccess,
78+
enableExternalFileAccess,
7979
imageConverter
8080
);
8181
}
@@ -92,7 +92,7 @@ public DocumentToHtmlOptions addStyleMap(StyleMap styleMap) {
9292
embeddedStyleMap,
9393
disableDefaultStyleMap,
9494
disableEmbeddedStyleMap,
95-
disableExternalFileAccess,
95+
enableExternalFileAccess,
9696
imageConverter
9797
);
9898
}
@@ -105,7 +105,7 @@ public DocumentToHtmlOptions disableDefaultStyleMap() {
105105
embeddedStyleMap,
106106
true,
107107
disableEmbeddedStyleMap,
108-
disableExternalFileAccess,
108+
enableExternalFileAccess,
109109
imageConverter
110110
);
111111
}
@@ -118,12 +118,12 @@ public DocumentToHtmlOptions disableEmbeddedStyleMap() {
118118
embeddedStyleMap,
119119
disableDefaultStyleMap,
120120
true,
121-
disableExternalFileAccess,
121+
enableExternalFileAccess,
122122
imageConverter
123123
);
124124
}
125125

126-
public DocumentToHtmlOptions disableExternalFileAccess() {
126+
public DocumentToHtmlOptions enableExternalFileAccess() {
127127
return new DocumentToHtmlOptions(
128128
idPrefix,
129129
preserveEmptyParagraphs,
@@ -144,7 +144,7 @@ public DocumentToHtmlOptions addEmbeddedStyleMap(StyleMap embeddedStyleMap) {
144144
embeddedStyleMap,
145145
disableDefaultStyleMap,
146146
disableEmbeddedStyleMap,
147-
disableExternalFileAccess,
147+
enableExternalFileAccess,
148148
imageConverter
149149
);
150150
}
@@ -157,7 +157,7 @@ public DocumentToHtmlOptions imageConverter(ImageConverter.ImgElement imageConve
157157
embeddedStyleMap,
158158
disableDefaultStyleMap,
159159
disableEmbeddedStyleMap,
160-
disableExternalFileAccess,
160+
enableExternalFileAccess,
161161
InternalImageConverter.imgElement(imageConverter)
162162
);
163163
}
@@ -183,7 +183,7 @@ public StyleMap styleMap() {
183183
}
184184

185185
public boolean externalFileAccess() {
186-
return !this.disableExternalFileAccess;
186+
return this.enableExternalFileAccess;
187187
}
188188

189189
public InternalImageConverter imageConverter() {

src/test/java/org/zwobble/mammoth/tests/MammothTests.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,32 @@ public void inlineImagesReferencedByPathRelativeToBaseAreIncludedInOutput() thro
8989
}
9090

9191
@Test
92-
public void imagesStoredOutsideOfDocumentAreIncludedInOutput() throws IOException {
92+
public void whenExternalFileAccessIsEnabledThenImagesStoredOutsideOfDocumentAreIncludedInOutput() throws IOException {
9393
Path tempDirectory = Files.createTempDirectory("mammoth-");
9494
try {
9595
Path documentPath = tempDirectory.resolve("external-picture.docx");
9696
Files.copy(TestData.file("external-picture.docx").toPath(), documentPath);
9797
Files.copy(TestData.file("tiny-picture.png").toPath(), tempDirectory.resolve("tiny-picture.png"));
9898
assertThat(
99-
new DocumentConverter().convertToHtml(documentPath.toFile()),
99+
new DocumentConverter()
100+
.enableExternalFileAccess()
101+
.convertToHtml(documentPath.toFile()),
100102
isSuccess("<p><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>"));
101103
} finally {
102104
tempDirectory.toFile().delete();
103105
}
104106
}
105107

106108
@Test
107-
public void warnIfDocumentHasImagesStoredOutsideOfDocumentWhenPathOfDocumentIsUnknown() throws IOException {
109+
public void whenExternalFileAccessIsEnabledThenWarnIfDocumentHasImagesStoredOutsideOfDocumentWhenPathOfDocumentIsUnknown() throws IOException {
108110
Path tempDirectory = Files.createTempDirectory("mammoth-");
109111
try {
110112
Path documentPath = tempDirectory.resolve("external-picture.docx");
111113
Files.copy(TestData.file("external-picture.docx").toPath(), documentPath);
112114
assertThat(
113-
new DocumentConverter().convertToHtml(documentPath.toUri().toURL().openStream()),
115+
new DocumentConverter()
116+
.enableExternalFileAccess()
117+
.convertToHtml(documentPath.toUri().toURL().openStream()),
114118
allOf(
115119
hasProperty("value", equalTo("")),
116120
hasProperty("warnings", contains(
@@ -121,16 +125,14 @@ public void warnIfDocumentHasImagesStoredOutsideOfDocumentWhenPathOfDocumentIsUn
121125
}
122126

123127
@Test
124-
public void warnIfDocumentHasImagesStoredOutsideOfDocumentWhenExternalFileAccessIsDisabled() throws IOException {
128+
public void givenExternalFileAccessIsDisabledByDefaultThenWarnIfDocumentHasImagesStoredOutsideOfDocument() throws IOException {
125129
Path tempDirectory = Files.createTempDirectory("mammoth-");
126130
try {
127131
Path documentPath = tempDirectory.resolve("external-picture.docx");
128132
Files.copy(TestData.file("external-picture.docx").toPath(), documentPath);
129133
Files.copy(TestData.file("tiny-picture.png").toPath(), tempDirectory.resolve("tiny-picture.png"));
130134
assertThat(
131-
new DocumentConverter()
132-
.disableExternalFileAccess()
133-
.convertToHtml(documentPath.toFile()),
135+
new DocumentConverter().convertToHtml(documentPath.toFile()),
134136
allOf(
135137
hasProperty("value", equalTo("")),
136138
hasProperty("warnings", contains(
@@ -144,13 +146,15 @@ public void warnIfDocumentHasImagesStoredOutsideOfDocumentWhenExternalFileAccess
144146
}
145147

146148
@Test
147-
public void warnIfImagesStoredOutsideOfDocumentAreNotFound() throws IOException {
149+
public void whenExternalFileAccessIsEnabledWarnIfImagesStoredOutsideOfDocumentAreNotFound() throws IOException {
148150
Path tempDirectory = Files.createTempDirectory("mammoth-");
149151
try {
150152
Path documentPath = tempDirectory.resolve("external-picture.docx");
151153
Files.copy(TestData.file("external-picture.docx").toPath(), documentPath);
152154
assertThat(
153-
new DocumentConverter().convertToHtml(documentPath.toFile()),
155+
new DocumentConverter()
156+
.enableExternalFileAccess()
157+
.convertToHtml(documentPath.toFile()),
154158
allOf(
155159
hasProperty("value", equalTo("")),
156160
hasProperty("warnings", contains(

0 commit comments

Comments
 (0)