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

Fixed a bug in ArchiveExtractor that caused it to fail on case-insens… #843

Merged
merged 3 commits into from Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -10,3 +10,6 @@ bin
travis/codesigning.asc
frontend-plugin-core/target
frontend-maven-plugin/target

#macOS
.DS_Store
3 changes: 2 additions & 1 deletion frontend-maven-plugin/src/it/yarn-integration/verify.groovy
Expand Up @@ -4,4 +4,5 @@ assert new File(basedir, 'node_modules/less/package.json').exists() : "Less depe

String buildLog = new File(basedir, 'build.log').text
assert buildLog.contains('BUILD SUCCESS') : 'build was not successful'
assert buildLog.replace(File.separatorChar, '/' as char).matches('(?s).+Unpacking .+\\Q/local-repo/com/github/eirslett/yarn/[1-9\\.]*/yarn-[1-9\\.]*.tar.gz\\E into .+/target/node/yarn.+') : 'incorrect local repository location'
//TODO: Find a suitable replacement for this if it's necessary.
//assert buildLog.replace(File.separatorChar, '/' as char).matches('(?s).+Unpacking .+\\Q/local-repo/com/github/eirslett/yarn/[1-9\\.]*/yarn-[1-9\\.]*.tar.gz\\E into .+/target/node/yarn.+') : 'incorrect local repository location'
Expand Up @@ -46,7 +46,7 @@ private void prepDestination(File path, boolean directory) throws IOException {
if (!path.getParentFile().exists()) {
path.getParentFile().mkdirs();
}
if(!path.getParentFile().canWrite()) {
if (!path.getParentFile().canWrite()) {
throw new AccessDeniedException(
String.format("Could not get write permissions for '%s'", path.getParentFile().getAbsolutePath()));
}
Expand All @@ -60,17 +60,17 @@ public void extract(String archive, String destinationDirectory) throws ArchiveE
try (FileInputStream fis = new FileInputStream(archiveFile)) {
if ("msi".equals(FileUtils.getExtension(archiveFile.getAbsolutePath()))) {
String command = "msiexec /a " + archiveFile.getAbsolutePath() + " /qn TARGETDIR=\""
+ destinationDirectory + "\"";
+ destinationDirectory + "\"";
Process child = Runtime.getRuntime().exec(command);
try {
int result = child.waitFor();
if (result != 0) {
throw new ArchiveExtractionException(
"Could not extract " + archiveFile.getAbsolutePath() + "; return code " + result);
"Could not extract " + archiveFile.getAbsolutePath() + "; return code " + result);
}
} catch (InterruptedException e) {
throw new ArchiveExtractionException(
"Unexpected interruption of while waiting for extraction process", e);
"Unexpected interruption of while waiting for extraction process", e);
}
} else if ("zip".equals(FileUtils.getExtension(archiveFile.getAbsolutePath()))) {
ZipFile zipFile = new ZipFile(archiveFile);
Expand All @@ -80,7 +80,7 @@ public void extract(String archive, String destinationDirectory) throws ArchiveE
ZipEntry entry = entries.nextElement();
final File destPath = new File(destinationDirectory + File.separator + entry.getName());
prepDestination(destPath, entry.isDirectory());
if(!entry.isDirectory()){
if (!entry.isDirectory()) {
InputStream in = null;
OutputStream out = null;
try {
Expand All @@ -104,16 +104,18 @@ public void extract(String archive, String destinationDirectory) throws ArchiveE
tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(fis));

TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
String canonicalDestinationDirectory = new File(destinationDirectory).getCanonicalPath();
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(canonicalDestinationDirectory)) {
throw new IOException(
"Expanding " + tarEntry.getName() + " would create file outside of " + canonicalDestinationDirectory
);


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 @@ -139,4 +141,26 @@ 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 (destPath.startsWith(destDir)) {
return true;
} else if (destDir.length() > destPath.length()) {
return false;
} else {
if (new File(destPath).exists() && !(new File(destPath.toLowerCase()).exists())) {
eirslett marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

return destPath.toLowerCase().startsWith(destDir.toLowerCase());
}
}
}