Skip to content

Commit 23dd3f9

Browse files
committed
Fixed: Implement path traversal protection in OutputServices
1 parent 5039f72 commit 23dd3f9

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

applications/content/config/ContentUiLabels.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,9 @@
20892089
<value xml:lang="zh">生成${contentType}是出错:${errorString}</value>
20902090
<value xml:lang="zh-TW">產生${contentType}時出錯:${errorString}</value>
20912091
</property>
2092+
<property key="ContentRenderingPathTraversalError">
2093+
<value xml:lang="en">Rejected output path: "${fileName}" resolves outside the allowed directory "${filePath}"</value>
2094+
</property>
20922095
<property key="ContentRequiredField">
20932096
<value xml:lang="en">Required field ${requiredField} is missing in simple method call ${resourceDescription}</value>
20942097
<value xml:lang="fr">Le champ requis ${requiredField} est manquant dans l'appel d'une méthode simple : ${resourceDescription}</value>

applications/content/src/main/java/org/apache/ofbiz/content/output/OutputServices.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public static Map<String, Object> createFileFromScreen(DispatchContext dctx, Map
218218
Map<String, Object> screenContext = UtilGenerics.cast(serviceContext.remove("screenContext"));
219219
String contentType = (String) serviceContext.remove("contentType");
220220
String filePath = (String) serviceContext.remove("filePath");
221-
String fileName = (String) serviceContext.remove("fileName");
221+
String fileName = new File((String) serviceContext.remove("fileName")).getName();
222222

223223
if (UtilValidate.isEmpty(screenContext)) {
224224
screenContext = new HashMap<>();
@@ -265,7 +265,12 @@ public static Map<String, Object> createFileFromScreen(DispatchContext dctx, Map
265265
if (UtilValidate.isEmpty(filePath)) {
266266
filePath = EntityUtilProperties.getPropertyValue("content", "content.output.path", "/output", delegator);
267267
}
268-
File file = new File(filePath, fileName);
268+
File baseDir = new File(filePath).getCanonicalFile();
269+
File file = new File(baseDir, fileName).getCanonicalFile();
270+
if (!file.toPath().startsWith(baseDir.toPath())) {
271+
return ServiceUtil.returnError(UtilProperties.getMessage(RESOURCE, "ContentRenderingPathTraversalError",
272+
UtilMisc.toMap("fileName", fileName, "filePath", filePath), locale));
273+
}
269274

270275
FileOutputStream fos = new FileOutputStream(file);
271276
fos.write(baos.toByteArray());

0 commit comments

Comments
 (0)