-
Notifications
You must be signed in to change notification settings - Fork 6k
feat: prevent path traversal attacks #12611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
62f2eb9
feat: prevent path traversal attacks
ewaostrowska e791cb8
Merge branch 'master' into security-issues-for-swagger-codegen
daniel-kmiecik 5e28337
fix pr comments
djankows 3fba44e
Add logs, remove unnecessary path validation in DefaultGenerator
djankows File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
modules/swagger-codegen/src/main/java/io/swagger/codegen/utils/SecureFileUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.swagger.codegen.utils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
|
||
/** | ||
* Utility class for secure file operations that prevent path traversal attacks. | ||
* Uses a simplified approach focusing on canonical path validation and allowlist-based security. | ||
*/ | ||
public class SecureFileUtils { | ||
|
||
private SecureFileUtils() { | ||
daniel-kmiecik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Utility class | ||
} | ||
|
||
public static void validatePath(File file) { | ||
if (file == null) { | ||
throw new IllegalArgumentException("File cannot be null"); | ||
} | ||
|
||
try { | ||
String absolutePath = file.getAbsolutePath(); | ||
String canonicalPath = file.getCanonicalPath(); | ||
|
||
if (absolutePath.contains("..") || absolutePath.contains("\0")) { | ||
throw new SecurityException("Path contains suspicious characters: " + absolutePath); | ||
} | ||
|
||
if (canonicalPath.contains("..") || canonicalPath.contains("\0")) { | ||
throw new SecurityException("Path contains suspicious characters: " + canonicalPath); | ||
} | ||
|
||
} catch (IOException e) { | ||
throw new SecurityException("Unable to resolve canonical path for: " + file.getAbsolutePath(), e); | ||
} | ||
} | ||
|
||
public static void validatePath(String path) { | ||
if (path == null || path.trim().isEmpty()) { | ||
throw new IllegalArgumentException("Path cannot be null or empty"); | ||
} | ||
|
||
if (path.contains("..") || path.contains("\0")) { | ||
throw new SecurityException("Path contains suspicious characters: " + path); | ||
} | ||
|
||
validatePath(new File(path)); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
modules/swagger-codegen/src/test/java/io/swagger/codegen/AbstractGeneratorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.swagger.codegen; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
|
||
public class AbstractGeneratorTest { | ||
|
||
|
||
private static class TestableAbstractGenerator extends AbstractGenerator { | ||
} | ||
|
||
@Test(expectedExceptions = SecurityException.class) | ||
public void testWriteToFileWithPathTraversal() throws IOException { | ||
TestableAbstractGenerator generator = new TestableAbstractGenerator(); | ||
generator.writeToFile("../../../etc/passwd", "malicious content"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...r-codegen/src/test/java/io/swagger/codegen/ignore/CodegenIgnoreProcessorSecurityTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.swagger.codegen.ignore; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.File; | ||
|
||
public class CodegenIgnoreProcessorSecurityTest { | ||
|
||
@Test | ||
public void testConstructorWithPathTraversal() { | ||
Assert.assertThrows( | ||
"shouldOverwrite should throw SecurityException for suspicious path", | ||
SecurityException.class, | ||
() -> new CodegenIgnoreProcessor("../../../etc") | ||
); | ||
} | ||
|
||
@Test | ||
public void testFileConstructorWithPathTraversal() { | ||
File maliciousFile = new File("../../../etc/passwd"); | ||
|
||
Assert.assertThrows( | ||
"shouldOverwrite should throw SecurityException for suspicious path", | ||
SecurityException.class, | ||
() -> new CodegenIgnoreProcessor(maliciousFile) | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...les/swagger-codegen/src/test/java/io/swagger/codegen/languages/RubyClientCodegenTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.swagger.codegen.languages; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
public class RubyClientCodegenTest { | ||
|
||
@Test | ||
public void testShouldOverwriteWithPathTraversal() { | ||
RubyClientCodegen codegen = new RubyClientCodegen(); | ||
Assert.assertThrows( | ||
"shouldOverwrite should throw SecurityException for suspicious path", | ||
SecurityException.class, | ||
() -> codegen.shouldOverwrite("../../../etc/passwd") | ||
); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
modules/swagger-codegen/src/test/java/io/swagger/codegen/languages/SwaggerGeneratorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.swagger.codegen.languages; | ||
|
||
import io.swagger.models.Swagger; | ||
import io.swagger.models.Info; | ||
import org.testng.annotations.Test; | ||
|
||
public class SwaggerGeneratorTest { | ||
|
||
@Test | ||
public void testProcessSwaggerWithPathTraversal() { | ||
SwaggerGenerator generator = new SwaggerGenerator(); | ||
generator.setOutputFile("../../../etc/passwd"); | ||
|
||
Swagger swagger = new Swagger(); | ||
swagger.setInfo(new Info().title("Test API").version("1.0.0")); | ||
|
||
generator.processSwagger(swagger); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
.../swagger-codegen/src/test/java/io/swagger/codegen/languages/SwaggerYamlGeneratorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.swagger.codegen.languages; | ||
|
||
import io.swagger.models.Swagger; | ||
import io.swagger.models.Info; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
public class SwaggerYamlGeneratorTest { | ||
|
||
@Test | ||
public void testProcessSwaggerWithPathTraversal() { | ||
SwaggerYamlGenerator generator = new SwaggerYamlGenerator(); | ||
|
||
generator.setOutputFile("../../../etc/passwd"); | ||
|
||
Swagger swagger = new Swagger(); | ||
swagger.setInfo(new Info().title("Test API").version("1.0.0")); | ||
|
||
try { | ||
generator.processSwagger(swagger); | ||
} catch (Exception e) { | ||
Assert.fail("processSwagger should handle SecurityException gracefully: " + e.getMessage()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.