|
| 1 | +/** |
| 2 | + * @name SSRF host guard does not reject IPv6-transition forms |
| 3 | + * @description An SSRF host guard that rejects private or loopback IPv4 ranges but never |
| 4 | + * unwraps IPv6-transition forms (IPv4-mapped `::ffff:`, NAT64 `64:ff9b::`, |
| 5 | + * 6to4 `2002::`) can be bypassed by wrapping an internal IPv4 address in a |
| 6 | + * transition literal, allowing requests to reach internal endpoints. |
| 7 | + * @kind problem |
| 8 | + * @problem.severity warning |
| 9 | + * @id javascript/ssrf-ipv6-transition-incomplete-guard |
| 10 | + * @tags security |
| 11 | + * experimental |
| 12 | + * external/cwe/cwe-918 |
| 13 | + * external/cwe/cwe-1389 |
| 14 | + */ |
| 15 | + |
| 16 | +import javascript |
| 17 | + |
| 18 | +/** |
| 19 | + * Holds if `f` imports a dotted-quad-oriented private-IP guard package whose |
| 20 | + * classification is performed on the textual IPv4 form and therefore returns |
| 21 | + * `false` for an internal address wrapped in an IPv6-transition literal. |
| 22 | + */ |
| 23 | +predicate importsHandRolledIpGuard(File f) { |
| 24 | + exists(DataFlow::SourceNode mod | |
| 25 | + mod.getFile() = f and |
| 26 | + mod = DataFlow::moduleImport(["private-ip", "is-ip", "ip", "ip-range-check"]) |
| 27 | + ) |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Holds if `f` contains a call to an `isPrivate`-style host classifier, the |
| 32 | + * common name for a hand-rolled SSRF guard. |
| 33 | + */ |
| 34 | +predicate hasIsPrivateCall(File f) { |
| 35 | + exists(DataFlow::CallNode c | |
| 36 | + c.getFile() = f and |
| 37 | + c.getCalleeName().regexpMatch("(?i)^is_?private(ip|address|host)?$") |
| 38 | + ) |
| 39 | + or |
| 40 | + exists(DataFlow::MethodCallNode m | |
| 41 | + m.getFile() = f and |
| 42 | + m.getMethodName().regexpMatch("(?i)^is_?private(ip|address|host)?$") |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Holds if `f` contains a hand-written RFC 1918, loopback or cloud-metadata IPv4 |
| 48 | + * literal used as a denylist entry. |
| 49 | + */ |
| 50 | +predicate hasRfc1918Literal(File f) { |
| 51 | + exists(StringLiteral s | |
| 52 | + s.getFile() = f and |
| 53 | + s.getValue() |
| 54 | + .regexpMatch("(?i).*(127\\.0\\.0\\.1|169\\.254\\.169\\.254|10\\.|192\\.168|172\\.1[6-9]|::1|fc00|fd00|metadata\\.google).*") |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +/** Holds if `f` carries any hand-rolled, dotted-quad-oriented SSRF guard signal. */ |
| 59 | +predicate hasUnsafeGuardSignal(File f) { |
| 60 | + importsHandRolledIpGuard(f) or |
| 61 | + hasIsPrivateCall(f) or |
| 62 | + hasRfc1918Literal(f) |
| 63 | +} |
| 64 | + |
| 65 | +/** Holds if `func` has a name that reads as an SSRF host or URL validator. */ |
| 66 | +predicate isSsrfValidatorFunction(Function func) { |
| 67 | + func.getName() |
| 68 | + .regexpMatch("(?i).*(validate|check|guard|reject|deny|block|allow|is_?safe|sanitiz)e?_?.*(url|host|ip|address|target|endpoint|webhook|origin).*") |
| 69 | + or |
| 70 | + func.getName() |
| 71 | + .regexpMatch("(?i).*(is_?)?(private|internal|loopback|reserved|external)_?(ip|address|host|url).*") |
| 72 | + or |
| 73 | + func.getName().regexpMatch("(?i).*(ssrf|metadata).*") |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Holds if `f` imports a maturity-hardened, transition-aware address classifier |
| 78 | + * or SSRF-protection library that does unwrap IPv6-transition forms. |
| 79 | + */ |
| 80 | +predicate importsSafeClassifier(File f) { |
| 81 | + exists(DataFlow::SourceNode mod | |
| 82 | + mod.getFile() = f and |
| 83 | + mod = |
| 84 | + DataFlow::moduleImport([ |
| 85 | + "ipaddr.js", "ssrf-req-filter", "request-filtering-agent", "ssrf-agent", "netmask", |
| 86 | + "ip-cidr", "cidr-matcher", "blocked-at" |
| 87 | + ]) |
| 88 | + ) |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Holds if `f` already performs an explicit IPv6-transition unwrap or |
| 93 | + * canonicalization, so the guard does see the embedded IPv4 address. |
| 94 | + */ |
| 95 | +predicate hasTransitionUnwrap(File f) { |
| 96 | + exists(StringLiteral s | |
| 97 | + s.getFile() = f and |
| 98 | + ( |
| 99 | + s.getValue().matches("%64:ff9b%") or |
| 100 | + s.getValue().matches("%::ffff%") or |
| 101 | + s.getValue().matches("%2002:%") or |
| 102 | + s.getValue().matches("%2001:%") |
| 103 | + ) |
| 104 | + ) |
| 105 | + or |
| 106 | + exists(Identifier id | |
| 107 | + id.getFile() = f and |
| 108 | + id.getName() |
| 109 | + .regexpMatch("(?i).*(ipv4mapped|v4mapped|mappedipv4|ipv4inipv6|embeddedipv4|unwrap.*ip|toipv4|canonicaliz|isipv4compat).*") |
| 110 | + ) |
| 111 | + or |
| 112 | + exists(DataFlow::MethodCallNode m | m.getFile() = f and m.getMethodName() = ["range", "kind"]) |
| 113 | +} |
| 114 | + |
| 115 | +/** Holds if `f` is treated as safe (transition-aware), suppressing the alert. */ |
| 116 | +predicate isSafe(File f) { importsSafeClassifier(f) or hasTransitionUnwrap(f) } |
| 117 | + |
| 118 | +from Function guard, File f |
| 119 | +where |
| 120 | + guard.getFile() = f and |
| 121 | + isSsrfValidatorFunction(guard) and |
| 122 | + hasUnsafeGuardSignal(f) and |
| 123 | + not isSafe(f) and |
| 124 | + not f.getRelativePath() |
| 125 | + .regexpMatch("(?i).*/(tests?|specs?|examples?|__tests__|e2e|node_modules)/.*") |
| 126 | +select guard, |
| 127 | + "This SSRF host guard rejects private IPv4 ranges but never unwraps IPv6-transition forms " + |
| 128 | + "(IPv4-mapped '::ffff:', NAT64 '64:ff9b::', 6to4 '2002::'); an attacker can wrap an internal " + |
| 129 | + "IPv4 address in a transition literal to bypass it and reach internal endpoints." |
0 commit comments