Skip to content

Add experimental query: SSRF host guard missing IPv6-transition unwrap (CWE-918/CWE-1389)#21950

Open
tonghuaroot wants to merge 1 commit into
github:mainfrom
tonghuaroot:experimental-ssrf-ipv6-transition-js
Open

Add experimental query: SSRF host guard missing IPv6-transition unwrap (CWE-918/CWE-1389)#21950
tonghuaroot wants to merge 1 commit into
github:mainfrom
tonghuaroot:experimental-ssrf-ipv6-transition-js

Conversation

@tonghuaroot
Copy link
Copy Markdown

What

Adds a new experimental JavaScript query, javascript/ssrf-ipv6-transition-incomplete-guard, plus a .qhelp, a change note, and a test pack.

The query flags a hand-rolled SSRF host guard that rejects private/loopback IPv4 ranges but never unwraps IPv6-transition forms, so the guard can be bypassed by wrapping an internal IPv4 address in a transition literal.

Motivation

A common SSRF defense is to reject the request host against a denylist of private, loopback and cloud-metadata IPv4 ranges. When the guard inspects only the dotted-quad IPv4 form and never normalizes IPv6-transition representations, an attacker can wrap an internal address in a transition literal:

  • IPv4-mapped — ::ffff:169.254.169.254
  • NAT64 — 64:ff9b::a9fe:a9fe
  • 6to4 — 2002::

new URL("http://[::ffff:169.254.169.254]/") normalizes the host, but a dotted-quad denylist (private-ip, a hand-written 10./192.168./127. set, ip.isPrivate) never sees the embedded IPv4 and classifies the address as public, while the OS still routes to the internal endpoint. The Hono SSRF-protection middleware advisory GHSA-xrhx-7g5j-rcj5 is a public instance of this guard-completeness gap.

Why this is a separate, experimental query

The existing CWE-918 queries (js/request-forgery, experimental javascript/ssrf) are pure taint-flow: source = active threat-model source, sink = client request URL. They have no notion of a host-validation guard, so there is no notion of that guard being incomplete for IPv6-transition addresses. A target with a hand-rolled dotted-quad denylist is treated (correctly) as out of scope by the taint-flow queries; this query covers the orthogonal guard-completeness question. It is a standalone @kind problem query and does not touch any supported query.

Metadata follows the experimental rules: @tags includes experimental, security, external/cwe/cwe-918, external/cwe/cwe-1389; @security-severity and @precision are omitted (staff-assigned). The query matches on getName()/getValue() only — no toString regexp matching, no getAQlClass, no internal libraries (CONTRIBUTING §4).

Tests

javascript/ql/test/experimental/Security/CWE-918/SsrfIpv6TransitionIncompleteGuard/:

  • bad-private-ip-pkg.jsprivate-ip package guard, no unwrap — flagged (true positive)
  • bad-rfc1918-regex.js — hand-written RFC 1918 denylist — flagged (true positive)
  • good-ipaddr.jsipaddr.js .range() transition-aware classifier — not flagged
  • good-explicit-unwrap.js — explicit ::ffff: unwrap before the denylist — not flagged

codeql test run passes and the query compiles with no errors or warnings against the current javascript-all pack.

Add javascript/ssrf-ipv6-transition-incomplete-guard, an experimental
@kind problem query that flags hand-rolled SSRF host guards which reject
private/loopback IPv4 ranges but never unwrap IPv6-transition forms
(IPv4-mapped ::ffff:, NAT64 64:ff9b::, 6to4 2002::). Such guards can be
bypassed by wrapping an internal IPv4 address in a transition literal.

Includes a .qhelp with good/bad examples, a change note, and a test pack
with two true-positive fixtures (private-ip package guard and a
hand-written RFC 1918 denylist) and two negative-control fixtures
(ipaddr.js range classifier and an explicit ::ffff: unwrap).

Signed-off-by: tonghuaroot <23011166+tonghuaroot@users.noreply.github.com>
@tonghuaroot tonghuaroot requested a review from a team as a code owner June 6, 2026 13:48
@asgerf asgerf self-assigned this Jun 8, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 8, 2026

QHelp previews:

javascript/ql/src/experimental/Security/CWE-918/SsrfIpv6TransitionIncompleteGuard.qhelp

SSRF host guard does not reject IPv6-transition forms

Server-side request forgery (SSRF) guards frequently reject requests to internal addresses by checking the request host against a denylist of private, loopback and cloud-metadata IPv4 ranges. When such a guard inspects only the dotted-quad IPv4 form and never unwraps IPv6-transition representations, it can be bypassed: the host validator classifies the address as public, but the operating system routes the connection to the embedded internal IPv4 endpoint.

The affected forms include IPv4-mapped IPv6 (::ffff:169.254.169.254), NAT64 (64:ff9b::a9fe:a9fe) and 6to4 (2002::). A URL such as http://[::ffff:169.254.169.254]/ passes a dotted-quad denylist unchanged while still reaching the internal address.

Recommendation

Normalize the host before validating it: parse the address with a transition-aware library and unwrap IPv4-mapped, NAT64 and 6to4 forms to their embedded IPv4 address, then apply the private-range check to the normalized value. Libraries such as ipaddr.js classify these forms correctly via their range API, and SSRF-protection libraries such as request-filtering-agent apply the check after DNS resolution. Validate the resolved address rather than the textual host.

Example

The following guard rejects private IPv4 ranges using the private-ip package, which inspects the textual IPv4 form only. An attacker supplies ::ffff:169.254.169.254, which the guard classifies as public, but the request still reaches the internal metadata endpoint.

const isPrivate = require('private-ip');
const fetch = require('node-fetch');

// BAD: `private-ip` classifies the textual IPv4 form only, so it returns false
// for `::ffff:169.254.169.254`. The guard treats the wrapped internal address as
// public, but the request still reaches the metadata endpoint.
async function validateUrlHost(host) {
  if (isPrivate(host)) {
    throw new Error('blocked private host');
  }
  return fetch('http://' + host + '/');
}

module.exports = { validateUrlHost };

The following guard parses the host with a transition-aware classifier, so the embedded internal IPv4 address is detected regardless of the transition form used.

const ipaddr = require('ipaddr.js');
const fetch = require('node-fetch');

// GOOD: ipaddr.js parses the host and classifies it with `.range()`, which is
// transition-aware. `::ffff:169.254.169.254` parses as an IPv4-mapped address and
// is reported in the `linkLocal` range, so the guard is complete.
async function validateTargetHost(host) {
  const addr = ipaddr.parse(host);
  const range = addr.range();
  if (range === 'private' || range === 'loopback' || range === 'linkLocal') {
    throw new Error('blocked internal host');
  }
  return fetch('http://' + host + '/');
}

module.exports = { validateTargetHost };

References

@@ -0,0 +1 @@
experimental/Security/CWE-918/SsrfIpv6TransitionIncompleteGuard.ql No newline at end of file
Comment on lines +99 to +102
s.getValue().matches("%64:ff9b%") or
s.getValue().matches("%::ffff%") or
s.getValue().matches("%2002:%") or
s.getValue().matches("%2001:%")
@asgerf
Copy link
Copy Markdown
Contributor

asgerf commented Jun 8, 2026

Thanks for the submission! Looks great!

CI checks are currently failing since the new query filename needs to be added to this file (in sorted order):
javascript/ql/integration-tests/query-suite/not_included_in_qls.expected

Otherwise I'd say this is good to merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants