Skip to content

Commit e8a8b5c

Browse files
committed
fix pr comments
1 parent e791cb8 commit e8a8b5c

File tree

10 files changed

+49
-66
lines changed

10 files changed

+49
-66
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ public abstract class AbstractGenerator {
2222
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractGenerator.class);
2323

2424
@SuppressWarnings("static-method")
25-
public File writeToFile(String filename, String contents) throws IOException, SecurityException {
25+
public File writeToFile(String filename, String contents) throws IOException {
2626
LOGGER.info("writing file " + filename);
2727
SecureFileUtils.validatePath(filename);
2828
File output = new File(filename);
2929

3030
if (output.getParent() != null && !new File(output.getParent()).exists()) {
31-
SecureFileUtils.validatePath(output.getParent());
3231
File parent = new File(output.getParent());
3332
parent.mkdirs();
3433
}
@@ -109,7 +108,7 @@ public String getFullTemplateFile(CodegenConfig config, String templateFile) {
109108
return embeddedLibTemplateFile;
110109
}
111110
}
112-
111+
113112
// Fall back to the template file embedded/packaged in the JAR file...
114113
return config.embeddedTemplateDir() + File.separator + templateFile;
115114
}

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3563,11 +3563,7 @@ public String apiTestFilename(String templateName, String tag) {
35633563
}
35643564

35653565
public boolean shouldOverwrite(String filename) {
3566-
try {
3567-
SecureFileUtils.validatePath(filename);
3568-
} catch (SecurityException e) {
3569-
return true;
3570-
}
3566+
SecureFileUtils.validatePath(filename);
35713567
return !(skipOverwrite && new File(filename).exists());
35723568
}
35733569

@@ -3831,17 +3827,12 @@ public void writeOptional(String outputFolder, SupportingFile supportingFile) {
38313827
else {
38323828
folder = supportingFile.destinationFilename;
38333829
}
3834-
try {
3835-
SecureFileUtils.validatePath(folder);
3836-
if(!new File(folder).exists()) {
3837-
supportingFiles.add(supportingFile);
3838-
} else {
3839-
LOGGER.info("Skipped overwriting " + supportingFile.destinationFilename + " as the file already exists in " + folder);
3840-
}
3841-
} catch (SecurityException e) {
3842-
LOGGER.error("Error while validating path" + folder, e);
3830+
SecureFileUtils.validatePath(folder);
3831+
if(!new File(folder).exists()) {
3832+
supportingFiles.add(supportingFile);
3833+
} else {
3834+
LOGGER.info("Skipped overwriting " + supportingFile.destinationFilename + " as the file already exists in " + folder);
38433835
}
3844-
38453836
}
38463837

38473838
/**

modules/swagger-codegen/src/main/java/io/swagger/codegen/ignore/CodegenIgnoreProcessor.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,14 @@ public CodegenIgnoreProcessor(final String baseDirectory) {
4141
*/
4242
@SuppressWarnings("WeakerAccess")
4343
public CodegenIgnoreProcessor(final String baseDirectory, final String ignoreFile) {
44-
try {
45-
SecureFileUtils.validatePath(baseDirectory);
46-
final File directory = new File(baseDirectory);
47-
SecureFileUtils.validatePath(directory);
48-
SecureFileUtils.validatePath(ignoreFile);
49-
final File targetIgnoreFile = new File(directory, ignoreFile);
50-
if (directory.exists() && directory.isDirectory()) {
51-
loadFromFile(targetIgnoreFile);
52-
} else {
53-
LOGGER.warn("Output directory does not exist, or is inaccessible. No file (.swagger-codegen-ignore) will be evaluated.");
54-
}
55-
} catch (SecurityException e) {
56-
LOGGER.error("Security violation: attempted to access unsafe ignore file path ", e);
44+
SecureFileUtils.validatePath(baseDirectory);
45+
final File directory = new File(baseDirectory);
46+
SecureFileUtils.validatePath(ignoreFile);
47+
final File targetIgnoreFile = new File(directory, ignoreFile);
48+
if (directory.exists() && directory.isDirectory()) {
49+
loadFromFile(targetIgnoreFile);
50+
} else {
51+
LOGGER.warn("Output directory does not exist, or is inaccessible. No file (.swagger-codegen-ignore) will be evaluated.");
5752
}
5853
}
5954

@@ -63,12 +58,8 @@ public CodegenIgnoreProcessor(final String baseDirectory, final String ignoreFil
6358
* @param targetIgnoreFile The ignore file location.
6459
*/
6560
public CodegenIgnoreProcessor(final File targetIgnoreFile) {
66-
try {
67-
SecureFileUtils.validatePath(targetIgnoreFile);
68-
loadFromFile(targetIgnoreFile);
69-
} catch (SecurityException e) {
70-
LOGGER.error("Security violation: attempted to access unsafe ignore file path: " + targetIgnoreFile.getAbsolutePath(), e);
71-
}
61+
SecureFileUtils.validatePath(targetIgnoreFile);
62+
loadFromFile(targetIgnoreFile);
7263
}
7364

7465
private void loadFromFile(File targetIgnoreFile) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public void preprocessSwagger(Swagger swagger) {
200200
String swaggerJson = Json.pretty(swagger);
201201
SecureFileUtils.validatePath(outputFolder);
202202
FileUtils.writeStringToFile(new File(outputFolder + File.separator + "swagger.json"), swaggerJson, StandardCharsets.UTF_8);
203-
} catch (IOException | SecurityException e) {
203+
} catch (IOException e) {
204204
throw new RuntimeException(e.getMessage(), e.getCause());
205205
}
206206
super.preprocessSwagger(swagger);

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public String generateGemName(String moduleName) {
327327
}
328328

329329
@Override
330-
public String escapeReservedWord(String name) {
330+
public String escapeReservedWord(String name) {
331331
if(this.reservedWordsMappings().containsKey(name)) {
332332
return this.reservedWordsMappings().get(name);
333333
}
@@ -742,13 +742,8 @@ public void setGemAuthorEmail(String gemAuthorEmail) {
742742
@Override
743743
public boolean shouldOverwrite(String filename) {
744744
// skip spec file as the file might have been updated with new test cases
745-
try {
746-
SecureFileUtils.validatePath(filename);
747-
return !(skipOverwrite && new File(filename).exists());
748-
} catch (SecurityException e) {
749-
LOGGER.warn("Security violation: attempted to check unsafe file path: " + filename, e);
750-
return false;
751-
}
745+
SecureFileUtils.validatePath(filename);
746+
return !(skipOverwrite && new File(filename).exists());
752747
//
753748
//return super.shouldOverwrite(filename) && !filename.endsWith("_spec.rb");
754749
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/utils/SecureFileUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
*/
1111
public class SecureFileUtils {
1212

13-
public static void validatePath(File file) throws SecurityException {
13+
private SecureFileUtils() {
14+
// Utility class
15+
}
16+
17+
public static void validatePath(File file) {
1418
if (file == null) {
1519
throw new IllegalArgumentException("File cannot be null");
1620
}
@@ -32,7 +36,7 @@ public static void validatePath(File file) throws SecurityException {
3236
}
3337
}
3438

35-
public static void validatePath(String path) throws SecurityException {
39+
public static void validatePath(String path) {
3640
if (path == null || path.trim().isEmpty()) {
3741
throw new IllegalArgumentException("Path cannot be null or empty");
3842
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {
3737
@Test
3838
public void testShouldOverwriteWithPathTraversal() {
3939
DefaultCodegen codegen = new DefaultCodegen();
40-
boolean result = codegen.shouldOverwrite("../../../etc/passwd");
41-
42-
Assert.assertTrue(result, "shouldOverwrite should return false when SecurityException is thrown");
40+
Assert.assertThrows(
41+
"shouldOverwrite should throw SecurityException for suspicious path",
42+
SecurityException.class,
43+
() -> codegen.shouldOverwrite("../../../etc/passwd")
44+
);
4345
}
4446
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/ignore/CodegenIgnoreProcessorSecurityTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ public class CodegenIgnoreProcessorSecurityTest {
99

1010
@Test
1111
public void testConstructorWithPathTraversal() {
12-
CodegenIgnoreProcessor processor = new CodegenIgnoreProcessor("../../../etc");
13-
14-
Assert.assertNotNull(processor, "Processor should be created despite security violation");
15-
Assert.assertTrue(processor.getExclusionRules().isEmpty(), "No exclusion rules should be loaded");
16-
Assert.assertTrue(processor.getInclusionRules().isEmpty(), "No inclusion rules should be loaded");
12+
Assert.assertThrows(
13+
"shouldOverwrite should throw SecurityException for suspicious path",
14+
SecurityException.class,
15+
() -> new CodegenIgnoreProcessor("../../../etc")
16+
);
1717
}
1818

1919
@Test
2020
public void testFileConstructorWithPathTraversal() {
2121
File maliciousFile = new File("../../../etc/passwd");
22-
CodegenIgnoreProcessor processor = new CodegenIgnoreProcessor(maliciousFile);
2322

24-
Assert.assertNotNull(processor, "Processor should be created despite security violation");
25-
Assert.assertTrue(processor.getExclusionRules().isEmpty(), "No exclusion rules should be loaded");
26-
Assert.assertTrue(processor.getInclusionRules().isEmpty(), "No inclusion rules should be loaded");
23+
Assert.assertThrows(
24+
"shouldOverwrite should throw SecurityException for suspicious path",
25+
SecurityException.class,
26+
() -> new CodegenIgnoreProcessor(maliciousFile)
27+
);
2728
}
2829
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/languages/RubyClientCodegenTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ public class RubyClientCodegenTest {
88
@Test
99
public void testShouldOverwriteWithPathTraversal() {
1010
RubyClientCodegen codegen = new RubyClientCodegen();
11-
12-
boolean result = codegen.shouldOverwrite("../../../etc/passwd");
13-
14-
Assert.assertFalse(result, "shouldOverwrite should return false when SecurityException is thrown");
11+
Assert.assertThrows(
12+
"shouldOverwrite should throw SecurityException for suspicious path",
13+
SecurityException.class,
14+
() -> codegen.shouldOverwrite("../../../etc/passwd")
15+
);
1516
}
1617
}

modules/swagger-generator/src/main/java/io/swagger/generator/resource/SwaggerResource.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,11 @@ public Response downloadFile(@PathParam("fileId") String fileId) throws Exceptio
6060
System.out.println("looking for fileId " + fileId);
6161
System.out.println("got filename " + g.getFilename());
6262
if (g.getFilename() != null) {
63-
// SecureFileUtils.validatePath(g.getFilename());
63+
SecureFileUtils.validatePath(g.getFilename());
6464
File file = new java.io.File(g.getFilename());
6565
byte[] bytes = org.apache.commons.io.FileUtils.readFileToByteArray(file);
6666

6767
try {
68-
// SecureFileUtils.validatePath(file.getParentFile());
6968
FileUtils.deleteDirectory(file.getParentFile());
7069
} catch (Exception e) {
7170
System.out.println("failed to delete file " + file.getAbsolutePath());

0 commit comments

Comments
 (0)