-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Fix zip slip check in StructContext on Windows #3357
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -139,8 +139,8 @@ private void addArchive(String path, File file, int type, boolean isOwn) throws | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| String name = entry.getName(); | ||||||||||||||||||||||||
| File test = new File(file.getAbsolutePath(), name); | ||||||||||||||||||||||||
| if (!test.getCanonicalPath().startsWith(file.getCanonicalPath() + File.separator)) { // check for zip slip exploit | ||||||||||||||||||||||||
| String normalizedName = name.replace('\\', '/'); | ||||||||||||||||||||||||
| if (normalizedName.startsWith("/") || normalizedName.startsWith(".." + "/") || normalizedName.contains("/" + ".." + "/")) { // check for zip slip exploit | ||||||||||||||||||||||||
| throw new RuntimeException("Zip entry '" + entry.getName() + "' tries to escape target directory"); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+143
to
145
|
||||||||||||||||||||||||
| if (normalizedName.startsWith("/") || normalizedName.startsWith(".." + "/") || normalizedName.contains("/" + ".." + "/")) { // check for zip slip exploit | |
| throw new RuntimeException("Zip entry '" + entry.getName() + "' tries to escape target directory"); | |
| } | |
| if (normalizedName.startsWith("/")) { // check for zip slip exploit: absolute path | |
| throw new RuntimeException("Zip entry '" + entry.getName() + "' tries to escape target directory"); | |
| } | |
| for (String component : normalizedName.split("/")) { // check for zip slip exploit: parent directory traversal | |
| if ("..".equals(component)) { | |
| throw new RuntimeException("Zip entry '" + entry.getName() + "' tries to escape target directory"); | |
| } | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The string concatenation using the
+operator should be replaced with string literals for better performance and readability. Instead of".." + "/"and"/" + ".." + "/", use"../"and"/../"directly. While the compiler may optimize simple concatenations, using string literals is clearer and avoids any potential runtime concatenation overhead.