Skip to content

Commit 6d15397

Browse files
committed
JS: Ensure we never write outside the scratch dir
1 parent ba5d6bb commit 6d15397

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,19 @@ private String getChildAsString(JsonObject obj, String name) {
689689
return null;
690690
}
691691

692+
/**
693+
* Gets a relative path from <code>from</code> to <code>to</code> provided
694+
* the latter is contained in the former. Otherwise returns <code>null</code>.
695+
* @return a path or null
696+
*/
697+
public static Path tryRelativize(Path from, Path to) {
698+
Path relative = from.relativize(to);
699+
if (relative.startsWith("..") || relative.isAbsolute()) {
700+
return null;
701+
}
702+
return relative;
703+
}
704+
692705
/**
693706
* Installs dependencies for use by the TypeScript type checker.
694707
* <p>
@@ -727,6 +740,9 @@ protected DependencyInstallationResult installDependencies(Set<Path> filesToExtr
727740
if (!(json instanceof JsonObject)) continue;
728741
JsonObject jsonObject = (JsonObject) json;
729742
file = file.toAbsolutePath();
743+
if (tryRelativize(sourceRoot, file) == null) {
744+
continue; // Ignore package.json files outside the source root.
745+
}
730746
packageJsonFiles.put(file, jsonObject);
731747

732748
String name = getChildAsString(jsonObject, "name");

0 commit comments

Comments
 (0)