diff --git a/java/ql/lib/change-notes/2026-11-27-path-sanitizer-related-guards.md b/java/ql/lib/change-notes/2026-11-27-path-sanitizer-related-guards.md new file mode 100644 index 000000000000..7bd7a809ed06 --- /dev/null +++ b/java/ql/lib/change-notes/2026-11-27-path-sanitizer-related-guards.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* Path injection sanitizers in `PathSanitizer.qll` that combine a prefix or blocklist check with a `..` traversal check now require both checks to apply to related values (connected by local taint flow), rather than allowing an unrelated check on another expression in the same method. This may result in additional true-positive alerts in `java/path-injection` and related queries. diff --git a/java/ql/lib/semmle/code/java/security/PathSanitizer.qll b/java/ql/lib/semmle/code/java/security/PathSanitizer.qll index 0d7e0af77385..a4c468187759 100644 --- a/java/ql/lib/semmle/code/java/security/PathSanitizer.qll +++ b/java/ql/lib/semmle/code/java/security/PathSanitizer.qll @@ -54,8 +54,116 @@ private predicate anyNode(DataFlow::Node n) { any() } private predicate pathGuardNode(DataFlow::Node n) { n.asExpr() = any(PathGuard g).getCheckedExpr() } +private module FlowToPathGuard = TaintTracking::LocalTaintFlow; + private predicate localTaintFlowToPathGuard(Expr e, PathGuard g) { - TaintTracking::LocalTaintFlow::hasExprFlow(e, g.getCheckedExpr()) + FlowToPathGuard::hasExprFlow(e, g.getCheckedExpr()) +} + +/** + * Holds if `n2` is a `String`, `File`, or `Path` representation of `n1` that + * preserves path-traversal components. + */ +private predicate pathRepresentationStep(DataFlow::Node n1, DataFlow::Node n2) { + exists(MethodCall mc, Method m | + n2.asExpr() = mc and + m = mc.getMethod() + | + n1.asExpr() = mc.getQualifier() and + mc.getNumArgument() = 0 and + ( + m.hasName("toString") and + exists(RefType qt | qt = mc.getQualifier().getType().(RefType).getAnAncestor() | + qt instanceof TypePath or + qt instanceof TypeFile or + qt instanceof TypeString + ) + or + m.getDeclaringType().getAnAncestor() instanceof TypePath and + m.hasName(["toAbsolutePath", "toFile"]) + or + m.getDeclaringType().getAnAncestor() instanceof TypeFile and + m.hasName(["getAbsoluteFile", "getAbsolutePath", "getPath", "toPath"]) + ) + or + n1.asExpr() = mc.getArgument(0) and + m.getParameter(0).getType() instanceof TypeString and + ( + mc.getNumArgument() = 1 + or + mc.getNumArgument() = 2 and + mc.getArgument(1).(ArrayCreationExpr).getFirstDimensionSize() = 0 + ) and + ( + m.getDeclaringType() instanceof TypePaths and m.hasName("get") + or + m.getDeclaringType() instanceof TypePath and m.hasName("of") + or + m.getDeclaringType().getAnAncestor() instanceof TypeFileSystem and m.hasName("getPath") + ) + ) + or + exists(ClassInstanceExpr cc | + n2.asExpr() = cc and + n1.asExpr() = cc.getArgument(0) and + cc.getConstructedType().getAnAncestor() instanceof TypeFile and + cc.getNumArgument() = 1 and + cc.getCallee().getParameter(0).getType() instanceof TypeString + ) +} + +private predicate localFlowToPathGuard(DataFlow::Node n, PathGuard g) { + n.asExpr() = g.getCheckedExpr() + or + exists(DataFlow::Node mid | + localFlowToPathGuard(mid, g) and + ( + DataFlow::localFlowStep(n, mid) or + pathRepresentationStep(n, mid) + ) + ) +} + +private predicate fileConstructorChildStep(DataFlow::Node n1, DataFlow::Node n2) { + exists(ConstructorCall cc | + cc.getConstructedType().getAnAncestor() instanceof TypeFile and + n1.asExpr() = cc.getArgument(1) and + n2.asExpr() = cc + ) +} + +private predicate reachesPathGuard(DataFlow::Node n) { + pathGuardNode(n) + or + exists(DataFlow::Node mid | + reachesPathGuard(mid) and + ( + TaintTracking::localTaintStep(n, mid) or + fileConstructorChildStep(n, mid) + ) + ) +} + +private predicate flowsFromGuard(DataFlow::Node n, PathGuard g) { + localFlowToPathGuard(n, g) + or + exists(DataFlow::Node mid | + flowsFromGuard(mid, g) and + reachesPathGuard(n) and + ( + TaintTracking::localTaintStep(mid, n) or + fileConstructorChildStep(mid, n) + ) + ) +} + +/** + * Holds if `e` (which itself flows to a `PathGuard`) and the expression checked by + * `previousGuard` are connected by local taint flow, possibly from a common value. + */ +private predicate checksRelatedValue(Expr e, PathGuard previousGuard) { + localTaintFlowToPathGuard(e, previousGuard) or + flowsFromGuard(DataFlow::exprNode(e), previousGuard) } private class AllowedPrefixGuard extends PathGuard instanceof MethodCall { @@ -82,12 +190,13 @@ private predicate allowedPrefixGuard(Guard g, Expr e, boolean branch) { // sink(file); g instanceof AllowedPrefixGuard and localTaintFlowToPathGuard(e, g) and - exists(Expr previousGuard | - localTaintFlowToPathGuard(previousGuard.(PathNormalizeSanitizer), g) + ( + localTaintFlowToPathGuard(any(PathNormalizeSanitizer p), g) or - previousGuard - .(PathTraversalGuard) - .controls(g.getBasicBlock(), previousGuard.(PathTraversalGuard).getBranch()) + exists(PathTraversalGuard previousGuard | + previousGuard.controls(g.getBasicBlock(), previousGuard.getBranch()) and + checksRelatedValue(e, previousGuard) + ) ) } @@ -108,12 +217,14 @@ private class FileGetNameCall extends MethodCall { */ private predicate dotDotCheckGuard(Guard g, Expr e, boolean branch) { pathTraversalGuard(g, e, branch) and - exists(Guard previousGuard | + exists(PathGuard previousGuard | previousGuard.(AllowedPrefixGuard).controls(g.getBasicBlock(), true) or previousGuard .(BlockListGuard) .controls(g.getBasicBlock(), previousGuard.(BlockListGuard).getBranch()) + | + checksRelatedValue(e, previousGuard) ) or // `File.getName` strips any directory prefix, returning only the final path @@ -137,7 +248,7 @@ private class BlockListGuard extends PathGuard { BlockListGuard() { exists(StringSubstringMatch match, boolean matchingBranch | this = match and - checkedExpr = match.getInputString() and + checkedExpr = match.getInputString().getUnderlyingExpr() and isDisallowedWord(match.getMatchExpression()) and match.hasSubstringOnBranch(matchingBranch) and branch = matchingBranch.booleanNot() @@ -172,12 +283,13 @@ private predicate blockListGuard(Guard g, Expr e, boolean branch) { // sink(file); g instanceof BlockListGuard and localTaintFlowToPathGuard(e, g) and - exists(Expr previousGuard | - localTaintFlowToPathGuard(previousGuard.(PathNormalizeSanitizer), g) + ( + localTaintFlowToPathGuard(any(PathNormalizeSanitizer p), g) or - previousGuard - .(PathTraversalGuard) - .controls(g.getBasicBlock(), previousGuard.(PathTraversalGuard).getBranch()) + exists(PathTraversalGuard previousGuard | + previousGuard.controls(g.getBasicBlock(), previousGuard.getBranch()) and + checksRelatedValue(e, previousGuard) + ) ) } @@ -250,7 +362,7 @@ private class PathTraversalGuard extends PathGuard { PathTraversalGuard() { exists(StringSubstringMatch match, boolean matchingBranch | this = match and - checkedExpr = match.getInputString() and + checkedExpr = match.getInputString().getUnderlyingExpr() and match.getMatchExpression().(CompileTimeConstantExpr).getStringValue() = ".." and match.hasSubstringOnBranch(matchingBranch) and branch = matchingBranch.booleanNot() diff --git a/java/ql/test/library-tests/pathsanitizer/Test.java b/java/ql/test/library-tests/pathsanitizer/Test.java index 44a1110d0a9a..cb43d2993f37 100644 --- a/java/ql/test/library-tests/pathsanitizer/Test.java +++ b/java/ql/test/library-tests/pathsanitizer/Test.java @@ -1,5 +1,6 @@ import java.io.File; import java.net.URI; +import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; import android.net.Uri; @@ -67,6 +68,11 @@ private void allowListGuardValidation(String path) throws Exception { throw new Exception(); } + private void allowListUnrelatedValidation(String other, String path) throws Exception { + if (other.contains("..") || !path.startsWith("/safe")) + throw new Exception(); + } + public void allowListGuard() throws Exception { // Prefix check by itself is not enough { @@ -171,6 +177,119 @@ public void allowListGuard() throws Exception { allowListGuardValidation(source); sink(source); // Safe } + { + String source = (String) source(); + String other = (String) source(); + allowListUnrelatedValidation(other, source); + sink(source); // $ hasTaintFlow + } + // Unrelated `..` guard does not sanitize + { + String source = (String) source(); + String other = (String) source(); + if (!other.contains("..") && source.startsWith("/safe")) + sink(source); // $ hasTaintFlow + else + sink(source); // $ hasTaintFlow + } + { + // Lossy `File.getName()` does not link `baseName` back to precomputed `resolved` + // (note: `sink(source)` is still sanitized because `localTaintFlowToPathGuard` allows taint steps). + String source = (String) source(); + String baseName = new File(source).getName(); + Path resolved = Paths.get("/safe/sub").resolve(source); + if (!baseName.contains("..") && resolved.startsWith("/safe")) + sink(resolved); // $ hasTaintFlow + } + // Derived values across both guards + { + String source = (String) source(); + Path resolved = Paths.get("/safe").resolve(source); + if (!source.contains("..") && resolved.startsWith("/safe")) { + sink(source); // Safe + sink(resolved); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(resolved); // $ hasTaintFlow + } + } + { + Path source = (Path) source(); + Path resolved = Paths.get("/safe").resolve(source); + if (!source.toString().contains("..") && resolved.startsWith("/safe")) { + sink(source); // Safe + sink(resolved); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(resolved); // $ hasTaintFlow + } + } + { + File source = (File) source(); + String str = source.getPath(); + Path resolved = Paths.get("/safe").resolve(source.toPath()); + if (!str.contains("..") && resolved.startsWith("/safe")) { + sink(source); // Safe + sink(resolved); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(resolved); // $ hasTaintFlow + } + } + { + File source = (File) source(); + String absPath = source.getAbsoluteFile().getAbsolutePath(); + Path resolved = Paths.get("/safe").resolve(source.toPath()); + if (!absPath.contains("..") && resolved.startsWith("/safe")) { + sink(source); // Safe + sink(resolved); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(resolved); // $ hasTaintFlow + } + } + { + File dir = new File("/safe"); + String name = (String) source(); + File file = new File(dir, name); + if (!name.contains("..") && file.toPath().startsWith(dir.toPath())) { + sink(file); // Safe + sink(name); // $ hasTaintFlow + } else { + sink(file); // $ hasTaintFlow + sink(name); // $ hasTaintFlow + } + } + { + String source = (String) source(); + if (!source.contains("..")) { + Path resolved = Paths.get("/safe").resolve(source); + if (resolved.startsWith("/safe")) { + sink(source); // Safe + sink(resolved); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(resolved); // $ hasTaintFlow + } + } + } + { + Path source = (Path) source(); + if (!source.toString().contains("..") && source.startsWith("/safe")) + sink(source); // Safe + else + sink(source); // $ hasTaintFlow + } + { + Object raw = source(); + if (!((String) raw).contains("..")) { + Path resolved = Paths.get("/safe").resolve((String) raw); + if (resolved.startsWith("/safe")) + sink(resolved); // Safe + else + sink(resolved); // $ hasTaintFlow + } + } // PathInjectionSanitizer + partial string match is considered unsafe { String source = (String) source(); @@ -212,6 +331,13 @@ private void dotDotCheckGuardValidation(String path) throws Exception { throw new Exception(); } + private void dotDotUnrelatedValidation(String route, String path) throws Exception { + // Use `indexOf` instead of `contains` because `ValidatedVariableAccess` + // treats any `void` helper calling `String.contains` on a parameter as a validator. + if (!route.startsWith("/safe") || path.indexOf("..") != -1) + throw new Exception(); + } + public void dotDotCheckGuard() throws Exception { // dot dot check by itself is not enough { @@ -292,6 +418,126 @@ public void dotDotCheckGuard() throws Exception { dotDotCheckGuardValidation(source); sink(source); // Safe } + { + String source = (String) source(); + String route = (String) source(); + dotDotUnrelatedValidation(route, source); + sink(source); // $ hasTaintFlow + } + // Unrelated prefix or blocklist guard does not sanitize + { + String source = (String) source(); + String route = (String) source(); + if (route.startsWith("/safe") && !source.contains("..")) + sink(source); // $ hasTaintFlow + else + sink(source); // $ hasTaintFlow + } + { + String source = (String) source(); + String other = (String) source(); + if (!other.startsWith("/data") && !source.contains("..")) + sink(source); // $ hasTaintFlow + else + sink(source); // $ hasTaintFlow + } + { + String s = (String) source(); + if (s.startsWith("/safe")) { + s = (String) source(); + if (!s.contains("..")) + sink(s); // $ hasTaintFlow + } + } + // Derived values across both guards + { + String source = (String) source(); + String trimmed = source.trim(); + if (source.startsWith("/safe") && !trimmed.contains("..")) { + sink(source); // Safe + sink(trimmed); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(trimmed); // $ hasTaintFlow + } + } + { + String source = (String) source(); + if (source.startsWith("/safe")) { + String trimmed = source.trim(); + if (!trimmed.contains("..")) { + sink(source); // Safe + sink(trimmed); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(trimmed); // $ hasTaintFlow + } + } + } + { + Object raw = source(); + if (!((String) raw).contains("/data")) { + String trimmed = ((String) raw).trim(); + if (!trimmed.contains("..")) + sink(trimmed); // Safe + else + sink(trimmed); // $ hasTaintFlow + } + } + { + String source = (String) source(); + String target = source + "/file"; + if (Paths.get(source).startsWith("/safe") && !target.contains("..")) { + sink(source); // Safe + sink(target); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(target); // $ hasTaintFlow + } + } + { + Path source = (Path) source(); + String str = source.toString(); + if (source.toString().startsWith("/safe") && !str.contains("..")) { + sink(source); // Safe + sink(str); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(str); // $ hasTaintFlow + } + } + { + String source = (String) source(); + Path checked = FileSystems.getDefault().getPath(source); + String target = source + "/file"; + if (checked.startsWith("/safe") && !target.contains("..")) { + sink(source); // Safe + sink(target); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(target); // $ hasTaintFlow + } + } + { + String source = (String) source(); + Path checked = FileSystems.getDefault().getPath(source, "/safe"); + String target = source + "/etc"; + if (checked.startsWith("/safe") && !target.contains("..")) { + sink(target); // $ hasTaintFlow + } + } + { + Path source = (Path) source(); + Path abs = source.toAbsolutePath(); + Path target = source.resolve("file"); + if (abs.startsWith("/safe") && !target.toString().contains("..")) { + sink(source); // Safe + sink(target); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(target); // $ hasTaintFlow + } + } } private void blockListGuardValidation(String path) throws Exception { @@ -403,6 +649,40 @@ public void blockListGuard() throws Exception { blockListGuardValidation(source); sink(source); // Safe } + // Unrelated `..` guard does not sanitize + { + String source = (String) source(); + String other = (String) source(); + if (!other.contains("..") && !source.startsWith("/data")) + sink(source); // $ hasTaintFlow + else + sink(source); // $ hasTaintFlow + } + // Derived values across both guards + { + String source = (String) source(); + Path resolved = Paths.get("/dir").resolve(source); + if (!source.contains("..") && !resolved.startsWith("/data")) { + sink(source); // Safe + sink(resolved); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(resolved); // $ hasTaintFlow + } + } + { + String source = (String) source(); + if (!source.contains("..")) { + Path resolved = Paths.get("/dir").resolve(source); + if (!resolved.startsWith("/data")) { + sink(source); // Safe + sink(resolved); // Safe + } else { + sink(source); // $ hasTaintFlow + sink(resolved); // $ hasTaintFlow + } + } + } // PathInjectionSanitizer + partial string match with disallowed words { String source = (String) source(); diff --git a/java/ql/test/library-tests/pathsanitizer/TestKt.kt b/java/ql/test/library-tests/pathsanitizer/TestKt.kt index d26f14ccd601..591304c3aa93 100644 --- a/java/ql/test/library-tests/pathsanitizer/TestKt.kt +++ b/java/ql/test/library-tests/pathsanitizer/TestKt.kt @@ -1,5 +1,6 @@ import java.io.File import java.net.URI +import java.nio.file.FileSystems import java.nio.file.Path import java.nio.file.Paths import android.net.Uri @@ -184,6 +185,75 @@ class TestKt { allowListGuardValidation(source) sink(source) // Safe } + // Unrelated `..` guard does not sanitize + run { + val source = source() as String? + val other = source() as String? + if (!other!!.contains("..") && source!!.startsWith("/safe")) + sink(source) // $ hasTaintFlow + else + sink(source) // $ hasTaintFlow + } + // Derived values across both guards + run { + val source = source() as String? + val resolved: Path = Paths.get("/safe").resolve(source!!) + if (!source.contains("..") && resolved.startsWith("/safe")) { + sink(source) // Safe + sink(resolved) // Safe + } else { + sink(source) // $ hasTaintFlow + sink(resolved) // $ hasTaintFlow + } + } + run { + val source = source() as Path? + val resolved: Path = Paths.get("/safe").resolve(source!!) + if (!source.toString().contains("..") && resolved.startsWith("/safe")) { + sink(source) // Safe + sink(resolved) // Safe + } else { + sink(source) // $ hasTaintFlow + sink(resolved) // $ hasTaintFlow + } + } + run { + val dir = File("/safe") + val name = source() as String? + val file = File(dir, name!!) + if (!name.contains("..") && file.toPath().startsWith(dir.toPath())) { + sink(file) // Safe + sink(name) // $ hasTaintFlow + } else { + sink(file) // $ hasTaintFlow + sink(name) // $ hasTaintFlow + } + } + run { + val source = source() as String? + val checked: Path = FileSystems.getDefault().getPath(source!!).toAbsolutePath() + val target = source + "/file" + if (checked.startsWith("/safe") && !target.contains("..")) { + sink(source) // Safe + sink(target) // Safe + } else { + sink(source) // $ hasTaintFlow + sink(target) // $ hasTaintFlow + } + } + run { + val source = source() as String? + if (!source!!.contains("..")) { + val resolved: Path = Paths.get("/safe").resolve(source) + if (resolved.startsWith("/safe")) { + sink(source) // Safe + sink(resolved) // Safe + } else { + sink(source) // $ hasTaintFlow + sink(resolved) // $ hasTaintFlow + } + } + } // PathInjectionSanitizer + partial string match is considered unsafe run { val source = source() as String? @@ -302,6 +372,48 @@ class TestKt { dotDotCheckGuardValidation(source) sink(source) // Safe } + // Unrelated prefix or blocklist guard does not sanitize + run { + val source = source() as String? + val route = source() as String? + if (route!!.startsWith("/safe") && !source!!.contains("..")) + sink(source) // $ hasTaintFlow + else + sink(source) // $ hasTaintFlow + } + run { + val source = source() as String? + val other = source() as String? + if (!other!!.startsWith("/data") && !source!!.contains("..")) + sink(source) // $ hasTaintFlow + else + sink(source) // $ hasTaintFlow + } + // Derived values across both guards + run { + val source = source() as String? + val withSuffix = source!! + "/file" + if (source.startsWith("/safe") && !withSuffix.contains("..")) { + sink(source) // Safe + sink(withSuffix) // Safe + } else { + sink(source) // $ hasTaintFlow + sink(withSuffix) // $ hasTaintFlow + } + } + run { + val source = source() as String? + if (source!!.startsWith("/safe")) { + val withSuffix = source + "/file" + if (!withSuffix.contains("..")) { + sink(source) // Safe + sink(withSuffix) // Safe + } else { + sink(source) // $ hasTaintFlow + sink(withSuffix) // $ hasTaintFlow + } + } + } } @Throws(Exception::class) @@ -428,6 +540,40 @@ class TestKt { blockListGuardValidation(source) sink(source) // Safe } + // Unrelated `..` guard does not sanitize + run { + val source = source() as String? + val other = source() as String? + if (!other!!.contains("..") && !source!!.startsWith("/data")) + sink(source) // $ hasTaintFlow + else + sink(source) // $ hasTaintFlow + } + // Derived values across both guards + run { + val source = source() as String? + val resolved: Path = Paths.get("/dir").resolve(source!!) + if (!source.contains("..") && !resolved.startsWith("/data")) { + sink(source) // Safe + sink(resolved) // Safe + } else { + sink(source) // $ hasTaintFlow + sink(resolved) // $ hasTaintFlow + } + } + run { + val source = source() as String? + if (!source!!.contains("..")) { + val resolved: Path = Paths.get("/dir").resolve(source) + if (!resolved.startsWith("/data")) { + sink(source) // Safe + sink(resolved) // Safe + } else { + sink(source) // $ hasTaintFlow + sink(resolved) // $ hasTaintFlow + } + } + } // PathInjectionSanitizer + partial string match with disallowed words run { val source = source() as String? diff --git a/java/ql/test/query-tests/security/CWE-022/semmle/tests/TaintedPath.expected b/java/ql/test/query-tests/security/CWE-022/semmle/tests/TaintedPath.expected index b44b294edc47..e742d492434c 100644 --- a/java/ql/test/query-tests/security/CWE-022/semmle/tests/TaintedPath.expected +++ b/java/ql/test/query-tests/security/CWE-022/semmle/tests/TaintedPath.expected @@ -2,6 +2,7 @@ | SanitizationTests2.java:16:59:16:62 | path | SanitizationTests2.java:14:59:14:91 | path : String | SanitizationTests2.java:16:59:16:62 | path | This path depends on a $@. | SanitizationTests2.java:14:59:14:91 | path | user-provided value | | TaintedPath.java:16:71:16:78 | filename | TaintedPath.java:13:58:13:78 | getInputStream(...) : InputStream | TaintedPath.java:16:71:16:78 | filename | This path depends on a $@. | TaintedPath.java:13:58:13:78 | getInputStream(...) | user-provided value | | TaintedPath.java:105:71:105:78 | baseName | TaintedPath.java:98:58:98:78 | getInputStream(...) : InputStream | TaintedPath.java:105:71:105:78 | baseName | This path depends on a $@. | TaintedPath.java:98:58:98:78 | getInputStream(...) | user-provided value | +| TaintedPath.java:141:75:141:105 | toFile(...) | TaintedPath.java:136:58:136:78 | getInputStream(...) : InputStream | TaintedPath.java:141:75:141:105 | toFile(...) | This path depends on a $@. | TaintedPath.java:136:58:136:78 | getInputStream(...) | user-provided value | | Test.java:37:52:37:68 | (...)... | Test.java:32:16:32:45 | getParameter(...) : String | Test.java:37:52:37:68 | (...)... | This path depends on a $@. | Test.java:32:16:32:45 | getParameter(...) | user-provided value | | Test.java:39:32:39:48 | (...)... | Test.java:32:16:32:45 | getParameter(...) : String | Test.java:39:32:39:48 | (...)... | This path depends on a $@. | Test.java:32:16:32:45 | getParameter(...) | user-provided value | | Test.java:41:47:41:63 | (...)... | Test.java:32:16:32:45 | getParameter(...) : String | Test.java:41:47:41:63 | (...)... | This path depends on a $@. | Test.java:32:16:32:45 | getParameter(...) | user-provided value | @@ -94,6 +95,13 @@ edges | TaintedPath.java:100:30:100:37 | filename : String | TaintedPath.java:100:21:100:38 | new File(...) : File | provenance | MaD:76 | | TaintedPath.java:101:27:101:30 | file : File | TaintedPath.java:101:27:101:40 | getName(...) : String | provenance | MaD:77 | | TaintedPath.java:101:27:101:40 | getName(...) : String | TaintedPath.java:105:71:105:78 | baseName | provenance | Sink:MaD:27 | +| TaintedPath.java:136:17:136:89 | new BufferedReader(...) : BufferedReader | TaintedPath.java:137:27:137:40 | filenameReader : BufferedReader | provenance | | +| TaintedPath.java:136:36:136:88 | new InputStreamReader(...) : InputStreamReader | TaintedPath.java:136:17:136:89 | new BufferedReader(...) : BufferedReader | provenance | MaD:74 | +| TaintedPath.java:136:58:136:78 | getInputStream(...) : InputStream | TaintedPath.java:136:36:136:88 | new InputStreamReader(...) : InputStreamReader | provenance | Src:MaD:72 MaD:78 | +| TaintedPath.java:137:27:137:40 | filenameReader : BufferedReader | TaintedPath.java:137:27:137:51 | readLine(...) : String | provenance | MaD:75 | +| TaintedPath.java:137:27:137:51 | readLine(...) : String | TaintedPath.java:141:88:141:95 | filename : String | provenance | | +| TaintedPath.java:141:75:141:96 | resolve(...) : Path | TaintedPath.java:141:75:141:105 | toFile(...) | provenance | MaD:79 Sink:MaD:24 | +| TaintedPath.java:141:88:141:95 | filename : String | TaintedPath.java:141:75:141:96 | resolve(...) : Path | provenance | MaD:80 | | Test.java:32:16:32:45 | getParameter(...) : String | Test.java:37:61:37:68 | source(...) : String | provenance | Src:MaD:73 | | Test.java:32:16:32:45 | getParameter(...) : String | Test.java:39:41:39:48 | source(...) : String | provenance | Src:MaD:73 | | Test.java:32:16:32:45 | getParameter(...) : String | Test.java:41:56:41:63 | source(...) : String | provenance | Src:MaD:73 | @@ -325,6 +333,8 @@ models | 76 | Summary: java.io; File; false; File; ; ; Argument[0]; Argument[this]; taint; manual | | 77 | Summary: java.io; File; true; getName; (); ; Argument[this]; ReturnValue; taint; manual | | 78 | Summary: java.io; InputStreamReader; false; InputStreamReader; ; ; Argument[0]; Argument[this]; taint; manual | +| 79 | Summary: java.nio.file; Path; false; toFile; ; ; Argument[this]; ReturnValue; taint; manual | +| 80 | Summary: java.nio.file; Path; true; resolve; ; ; Argument[0]; ReturnValue; taint; manual | nodes | SanitizationTests2.java:14:59:14:91 | path : String | semmle.label | path : String | | SanitizationTests2.java:16:59:16:62 | path | semmle.label | path | @@ -344,6 +354,14 @@ nodes | TaintedPath.java:101:27:101:30 | file : File | semmle.label | file : File | | TaintedPath.java:101:27:101:40 | getName(...) : String | semmle.label | getName(...) : String | | TaintedPath.java:105:71:105:78 | baseName | semmle.label | baseName | +| TaintedPath.java:136:17:136:89 | new BufferedReader(...) : BufferedReader | semmle.label | new BufferedReader(...) : BufferedReader | +| TaintedPath.java:136:36:136:88 | new InputStreamReader(...) : InputStreamReader | semmle.label | new InputStreamReader(...) : InputStreamReader | +| TaintedPath.java:136:58:136:78 | getInputStream(...) : InputStream | semmle.label | getInputStream(...) : InputStream | +| TaintedPath.java:137:27:137:40 | filenameReader : BufferedReader | semmle.label | filenameReader : BufferedReader | +| TaintedPath.java:137:27:137:51 | readLine(...) : String | semmle.label | readLine(...) : String | +| TaintedPath.java:141:75:141:96 | resolve(...) : Path | semmle.label | resolve(...) : Path | +| TaintedPath.java:141:75:141:105 | toFile(...) | semmle.label | toFile(...) | +| TaintedPath.java:141:88:141:95 | filename : String | semmle.label | filename : String | | Test.java:32:16:32:45 | getParameter(...) : String | semmle.label | getParameter(...) : String | | Test.java:37:52:37:68 | (...)... | semmle.label | (...)... | | Test.java:37:61:37:68 | source(...) : String | semmle.label | source(...) : String | diff --git a/java/ql/test/query-tests/security/CWE-022/semmle/tests/TaintedPath.java b/java/ql/test/query-tests/security/CWE-022/semmle/tests/TaintedPath.java index fc01e1d23568..1bf685a8e6e7 100644 --- a/java/ql/test/query-tests/security/CWE-022/semmle/tests/TaintedPath.java +++ b/java/ql/test/query-tests/security/CWE-022/semmle/tests/TaintedPath.java @@ -127,4 +127,19 @@ public void sendUserFileGood4(Socket sock, String user) throws IOException { } } } + + public void sendUserFileBadUnrelatedPrefix(Socket sock, String route) throws IOException { + if (!route.startsWith("/download/")) { + return; + } + BufferedReader filenameReader = + new BufferedReader(new InputStreamReader(sock.getInputStream(), "UTF-8")); // $ Source[java/path-injection] + String filename = filenameReader.readLine(); + // BAD: routing prefix check on `route` does not prevent `filename` from being an absolute path. + if (!filename.contains("..")) { + Path base = Paths.get("/srv/app/files"); + BufferedReader fileReader = new BufferedReader(new FileReader(base.resolve(filename).toFile())); // $ Alert[java/path-injection] + fileReader.close(); + } + } }