Skip to content
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

Resolve CodeQL zip slip warning #13183

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,16 @@ private URL findJarResource(String name) {
private URL getJarEntryUrl(JarEntry jarEntry) {
if (jarEntry != null) {
try {
return new URL(jarBase, jarEntry.getName());
} catch (MalformedURLException e) {
String entryName = jarEntry.getName();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent of the warning is that when you extract jar entries into a directory and there is a malicious entry that contains .. you may up overwriting something outside of the directory where you were extracting the jar. Imo the codeql warning here isn't justified as the jar entry name isn't really used for an io operation. I think it should be possible to circumvent this. Since we look up the JarEntry by name we don't need to use getName() but can remember the name that we used to look it up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative implementation #13193

// normalize the path and check for directory traversal
// in order to resolve CodeQL zip slip warning
File entryFile = new File(jarBase.getPath(), entryName).getCanonicalFile();
File baseDir = new File(jarBase.getPath()).getCanonicalFile();
if (!entryFile.toPath().startsWith(baseDir.toPath())) {
throw new IllegalStateException("Bad zip entry: " + entryName);
}
return new URL(jarBase, entryName);
} catch (IOException e) {
throw new IllegalStateException(
"Failed to construct url for jar entry " + jarEntry.getName(), e);
}
Expand Down