JS: Ensure we never write outside the scratch dir

This commit is contained in:
Asger Feldthaus
2020-06-03 09:19:37 +01:00
parent ba5d6bb2e9
commit 6d15397fdc

View File

@@ -689,6 +689,19 @@ public class AutoBuild {
return null;
}
/**
* Gets a relative path from <code>from</code> to <code>to</code> provided
* the latter is contained in the former. Otherwise returns <code>null</code>.
* @return a path or null
*/
public static Path tryRelativize(Path from, Path to) {
Path relative = from.relativize(to);
if (relative.startsWith("..") || relative.isAbsolute()) {
return null;
}
return relative;
}
/**
* Installs dependencies for use by the TypeScript type checker.
* <p>
@@ -727,6 +740,9 @@ public class AutoBuild {
if (!(json instanceof JsonObject)) continue;
JsonObject jsonObject = (JsonObject) json;
file = file.toAbsolutePath();
if (tryRelativize(sourceRoot, file) == null) {
continue; // Ignore package.json files outside the source root.
}
packageJsonFiles.put(file, jsonObject);
String name = getChildAsString(jsonObject, "name");