Skip to content

Commit

Permalink
Fixed a bug in ArchiveExtractor that caused it to fail on case-insens…
Browse files Browse the repository at this point in the history
…itive file systems.
  • Loading branch information
MikeThomsen committed Aug 26, 2019
1 parent 5424679 commit 84a282c
Showing 1 changed file with 49 additions and 5 deletions.
Expand Up @@ -107,12 +107,15 @@ public void extract(String archive, String destinationDirectory) throws ArchiveE
while (tarEntry != null) {
// Create a file for this tarEntry
final File destPath = new File(destinationDirectory + File.separator + tarEntry.getName());
prepDestination(destPath, tarEntry.isDirectory());
if (!destPath.getCanonicalPath().startsWith(destinationDirectory)) {
throw new IOException(
"Expanding " + tarEntry.getName() + " would create file outside of " + destinationDirectory
);
prepDestination(destPath, tarEntry.isDirectory());


if (!startsWithPath(destPath.getCanonicalPath(), destinationDirectory)) {
throw new IOException(
"Expanding " + tarEntry.getName() + " would create file outside of " + destinationDirectory
);
}

if (!tarEntry.isDirectory()) {
destPath.createNewFile();
boolean isExecutable = (tarEntry.getMode() & 0100) > 0;
Expand All @@ -138,4 +141,45 @@ public void extract(String archive, String destinationDirectory) throws ArchiveE
+ "'", e);
}
}

/**
* Do multiple file system checks that should enable the plugin to work on any file system
* whether or not it's case sensitive or not.
* @param destPath
* @param destDir
* @return
*/
private boolean startsWithPath(String destPath, String destDir) {
if (destDir.startsWith(destDir)) {
return true;
} else if (destDir.length() > destPath.length()) {
return false;
} else {
/*
* The first check should handle case-sensitive file systems. We need this
* in order to weed out case-sensitive file systems with a non-existent path
* that slipped through the first test.
*/
if (new File(destPath).exists() && !(new File(destPath.toLowerCase()).exists())) {
return false;
}

boolean retVal = true;
for (int index = 0; index < destDir.length(); index++) {
char left = destPath.charAt(index);
char right = destDir.charAt(index);

if (left != right) {
char leftUc = Character.toUpperCase(left);
char rightUc = Character.toUpperCase(right);

if (leftUc != rightUc) {
retVal = false;
}
}
}

return retVal;
}
}
}

0 comments on commit 84a282c

Please sign in to comment.