Skip to content

Commit d5a7c08

Browse files
committed
feat: prevent path traversal attacks
1 parent b739ab5 commit d5a7c08

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3566,7 +3566,7 @@ public boolean shouldOverwrite(String filename) {
35663566
try {
35673567
SecureFileUtils.validatePath(filename);
35683568
} catch (SecurityException e) {
3569-
return false;
3569+
return true;
35703570
}
35713571
return !(skipOverwrite && new File(filename).exists());
35723572
}
@@ -3833,12 +3833,15 @@ public void writeOptional(String outputFolder, SupportingFile supportingFile) {
38333833
}
38343834
try {
38353835
SecureFileUtils.validatePath(folder);
3836-
if (!new File(folder).exists()) {
3836+
if(!new File(folder).exists()) {
3837+
supportingFiles.add(supportingFile);
3838+
} else {
38373839
LOGGER.info("Skipped overwriting " + supportingFile.destinationFilename + " as the file already exists in " + folder);
38383840
}
3839-
} catch (Exception e) {
3840-
supportingFiles.add(supportingFile);
3841+
} catch (SecurityException e) {
3842+
LOGGER.error("Error while validating path" + folder, e);
38413843
}
3844+
38423845
}
38433846

38443847
/**

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaJAXRSSpecServerCodegen.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,8 @@ public void preprocessSwagger(Swagger swagger) {
199199
try {
200200
String swaggerJson = Json.pretty(swagger);
201201
SecureFileUtils.validatePath(outputFolder);
202-
File outputFile = new File(outputFolder + File.separator + "swagger.json");
203-
FileUtils.writeStringToFile(outputFile, swaggerJson, StandardCharsets.UTF_8);
204-
} catch (SecurityException e) {
205-
throw new RuntimeException("Security violation: attempted to write to unsafe file path: " + outputFolder + File.separator + "swagger.json", e);
206-
} catch (IOException e) {
202+
FileUtils.writeStringToFile(new File(outputFolder + File.separator + "swagger.json"), swaggerJson, StandardCharsets.UTF_8);
203+
} catch (IOException | SecurityException e) {
207204
throw new RuntimeException(e.getMessage(), e.getCause());
208205
}
209206
super.preprocessSwagger(swagger);

modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultCodegenTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public void testShouldOverwriteWithPathTraversal() {
3939
DefaultCodegen codegen = new DefaultCodegen();
4040
boolean result = codegen.shouldOverwrite("../../../etc/passwd");
4141

42-
Assert.assertFalse(result, "shouldOverwrite should return false when SecurityException is thrown");
42+
Assert.assertTrue(result, "shouldOverwrite should return false when SecurityException is thrown");
4343
}
4444
}

0 commit comments

Comments
 (0)