|
| 1 | +import java |
| 2 | +import semmle.code.java.controlflow.Guards |
| 3 | +import semmle.code.java.dataflow.FlowSources |
| 4 | + |
| 5 | +/** A barrier guard that protects against path traversal vulnerabilities. */ |
| 6 | +abstract class PathTraversalBarrierGuard extends DataFlow::BarrierGuard { } |
| 7 | + |
| 8 | +/** |
| 9 | + * A guard that considers safe a string being exactly compared to a trusted value. |
| 10 | + */ |
| 11 | +private class ExactStringPathMatchGuard extends PathTraversalBarrierGuard instanceof MethodAccess { |
| 12 | + ExactStringPathMatchGuard() { |
| 13 | + super.getMethod().getDeclaringType() instanceof TypeString and |
| 14 | + super.getMethod().getName() = ["equals", "equalsIgnoreCase"] |
| 15 | + } |
| 16 | + |
| 17 | + override predicate checks(Expr e, boolean branch) { |
| 18 | + e = super.getQualifier() and |
| 19 | + branch = true |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +private class AllowListGuard extends Guard instanceof MethodAccess { |
| 24 | + AllowListGuard() { |
| 25 | + (isStringPartialMatch(this) or isPathPartialMatch(this)) and |
| 26 | + not isDisallowedWord(super.getAnArgument()) |
| 27 | + } |
| 28 | + |
| 29 | + Expr getCheckedExpr() { result = super.getQualifier() } |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * A guard that considers a path safe because it is checked against an allowlist of partial trusted values. |
| 34 | + * This requires additional protection against path traversal, either another guard (`PathTraversalGuard`) |
| 35 | + * or a sanitizer (`PathNormalizeSanitizer`), to ensure any internal `..` components are removed from the path. |
| 36 | + */ |
| 37 | +private class AllowListBarrierGuard extends PathTraversalBarrierGuard instanceof AllowListGuard { |
| 38 | + override predicate checks(Expr e, boolean branch) { |
| 39 | + e = super.getCheckedExpr() and |
| 40 | + branch = true and |
| 41 | + ( |
| 42 | + // Either a path normalization sanitizer comes before the guard, |
| 43 | + exists(PathNormalizeSanitizer sanitizer | DataFlow::localExprFlow(sanitizer, e)) |
| 44 | + or |
| 45 | + // or a check like `!path.contains("..")` comes before the guard |
| 46 | + exists(PathTraversalGuard previousGuard | |
| 47 | + DataFlow::localExprFlow(previousGuard.getCheckedExpr(), e) and |
| 48 | + previousGuard.controls(this.getBasicBlock().(ConditionBlock), false) |
| 49 | + ) |
| 50 | + ) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * A guard that considers a path safe because it is checked for `..` components, having previously |
| 56 | + * been checked for a trusted prefix. |
| 57 | + */ |
| 58 | +private class DotDotCheckBarrierGuard extends PathTraversalBarrierGuard instanceof PathTraversalGuard { |
| 59 | + override predicate checks(Expr e, boolean branch) { |
| 60 | + e = super.getCheckedExpr() and |
| 61 | + branch = false and |
| 62 | + // The same value has previously been checked against a list of allowed prefixes: |
| 63 | + exists(AllowListGuard previousGuard | |
| 64 | + DataFlow::localExprFlow(previousGuard.getCheckedExpr(), e) and |
| 65 | + previousGuard.controls(this.getBasicBlock().(ConditionBlock), true) |
| 66 | + ) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +private class BlockListGuard extends Guard instanceof MethodAccess { |
| 71 | + BlockListGuard() { |
| 72 | + (isStringPartialMatch(this) or isPathPartialMatch(this)) and |
| 73 | + isDisallowedWord(super.getAnArgument()) |
| 74 | + } |
| 75 | + |
| 76 | + Expr getCheckedExpr() { result = super.getQualifier() } |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * A guard that considers a string safe because it is checked against a blocklist of known dangerous values. |
| 81 | + * This requires a prior check for URL encoding concealing a forbidden value, either a guard (`UrlEncodingGuard`) |
| 82 | + * or a sanitizer (`UrlDecodeSanitizer`). |
| 83 | + */ |
| 84 | +private class BlockListBarrierGuard extends PathTraversalBarrierGuard instanceof BlockListGuard { |
| 85 | + override predicate checks(Expr e, boolean branch) { |
| 86 | + e = super.getCheckedExpr() and |
| 87 | + branch = false and |
| 88 | + ( |
| 89 | + // Either `e` has been URL decoded: |
| 90 | + exists(UrlDecodeSanitizer sanitizer | DataFlow::localExprFlow(sanitizer, e)) |
| 91 | + or |
| 92 | + // or `e` has previously been checked for URL encoding sequences: |
| 93 | + exists(UrlEncodingGuard previousGuard | |
| 94 | + DataFlow::localExprFlow(previousGuard.getCheckedExpr(), e) and |
| 95 | + previousGuard.controls(this.getBasicBlock(), false) |
| 96 | + ) |
| 97 | + ) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * A guard that considers a string safe because it is checked for URL encoding sequences, |
| 103 | + * having previously been checked against a block-list of forbidden values. |
| 104 | + */ |
| 105 | +private class URLEncodingBarrierGuard extends PathTraversalBarrierGuard instanceof UrlEncodingGuard { |
| 106 | + override predicate checks(Expr e, boolean branch) { |
| 107 | + e = super.getCheckedExpr() and |
| 108 | + branch = false and |
| 109 | + exists(BlockListGuard previousGuard | |
| 110 | + DataFlow::localExprFlow(previousGuard.getCheckedExpr(), e) and |
| 111 | + previousGuard.controls(this.getBasicBlock(), false) |
| 112 | + ) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Holds if `ma` is a call to a method that checks a partial string match. |
| 118 | + */ |
| 119 | +private predicate isStringPartialMatch(MethodAccess ma) { |
| 120 | + ma.getMethod().getDeclaringType() instanceof TypeString and |
| 121 | + ma.getMethod().getName() = |
| 122 | + ["contains", "startsWith", "matches", "regionMatches", "indexOf", "lastIndexOf"] |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Holds if `ma` is a call to a method of `java.nio.file.Path` that checks a partial path match. |
| 127 | + */ |
| 128 | +private predicate isPathPartialMatch(MethodAccess ma) { |
| 129 | + ma.getMethod().getDeclaringType() instanceof TypePath and |
| 130 | + ma.getMethod().getName() = "startsWith" |
| 131 | +} |
| 132 | + |
| 133 | +private predicate isDisallowedWord(CompileTimeConstantExpr word) { |
| 134 | + word.getStringValue().matches(["%WEB-INF%", "%META-INF%", "%..%"]) |
| 135 | +} |
| 136 | + |
| 137 | +/** A complementary guard that protects against path traversal, by looking for the literal `..`. */ |
| 138 | +class PathTraversalGuard extends Guard instanceof MethodAccess { |
| 139 | + Expr checked; |
| 140 | + |
| 141 | + PathTraversalGuard() { |
| 142 | + super.getMethod().getDeclaringType() instanceof TypeString and |
| 143 | + super.getMethod().hasName(["contains", "indexOf"]) and |
| 144 | + super.getAnArgument().(CompileTimeConstantExpr).getStringValue() = ".." |
| 145 | + } |
| 146 | + |
| 147 | + Expr getCheckedExpr() { result = super.getQualifier() } |
| 148 | +} |
| 149 | + |
| 150 | +/** A complementary sanitizer that protects against path traversal using path normalization. */ |
| 151 | +private class PathNormalizeSanitizer extends MethodAccess { |
| 152 | + PathNormalizeSanitizer() { |
| 153 | + this.getMethod().getDeclaringType().hasQualifiedName("java.nio.file", "Path") and |
| 154 | + this.getMethod().hasName("normalize") |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +/** A complementary guard that protects against double URL encoding, by looking for the literal `%`. */ |
| 159 | +private class UrlEncodingGuard extends Guard instanceof MethodAccess { |
| 160 | + UrlEncodingGuard() { |
| 161 | + super.getMethod().getDeclaringType() instanceof TypeString and |
| 162 | + super.getMethod().hasName(["contains", "indexOf"]) and |
| 163 | + super.getAnArgument().(CompileTimeConstantExpr).getStringValue() = "%" |
| 164 | + } |
| 165 | + |
| 166 | + Expr getCheckedExpr() { result = super.getQualifier() } |
| 167 | +} |
| 168 | + |
| 169 | +/** A complementary sanitizer that protects against double URL encoding using URL decoding. */ |
| 170 | +private class UrlDecodeSanitizer extends MethodAccess { |
| 171 | + UrlDecodeSanitizer() { |
| 172 | + this.getMethod().getDeclaringType().hasQualifiedName("java.net", "URLDecoder") and |
| 173 | + this.getMethod().hasName("decode") |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +/** A node with path normalization. */ |
| 178 | +class NormalizedPathNode extends DataFlow::Node { |
| 179 | + NormalizedPathNode() { |
| 180 | + TaintTracking::localExprTaint(this.asExpr(), any(PathNormalizeSanitizer ma)) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +/** Data model related to `java.nio.file.Path`. */ |
| 185 | +private class PathDataModel extends SummaryModelCsv { |
| 186 | + override predicate row(string row) { |
| 187 | + row = |
| 188 | + [ |
| 189 | + "java.nio.file;Paths;true;get;;;Argument[0];ReturnValue;taint", |
| 190 | + "java.nio.file;Path;true;normalize;;;Argument[-1];ReturnValue;taint" |
| 191 | + ] |
| 192 | + } |
| 193 | +} |
0 commit comments