Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/unfurl-ipv6-ssrf-guard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"dotflowy": patch
---

Link titles are no longer fetched for reserved IPv4 addresses or IPv6 addresses that embed an internal IPv4 address.
86 changes: 59 additions & 27 deletions worker/unfurl-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export function canonicalYouTubeVideoUrl(raw: string): string | null {
: null;
}

/** An IPv4 literal in a private / loopback / link-local / reserved range. Used
/** An IPv4 literal outside public unicast space: private, loopback,
* link-local, CGNAT, documentation, benchmarking, multicast, or reserved. Used
* to deny obvious internal targets even though Cloudflare already blocks raw-IP
* fetch -- belt and suspenders. Non-IPv4 hostnames return false here (handled
* by the name checks in isAllowedUnfurlTarget). */
Expand All @@ -67,33 +68,72 @@ function isPrivateIpv4(host: string): boolean {
const o = m.slice(1).map(Number);
if (o.some((n) => n > 255)) return false;
// SAFETY: the regex captured exactly four groups and map preserves length.
const [a, b] = o as [number, number, number, number];
const [a, b, c] = o as [number, number, number, number];
return (
a === 0 || // 0.0.0.0/8 "this network"
a === 10 || // 10/8 private
a === 127 || // 127/8 loopback
(a === 169 && b === 254) || // 169.254/16 link-local (cloud metadata)
(a === 172 && b >= 16 && b <= 31) || // 172.16/12 private
(a === 192 && b === 168) || // 192.168/16 private
(a === 100 && b >= 64 && b <= 127) // 100.64/10 CGNAT
(a === 100 && b >= 64 && b <= 127) || // 100.64/10 CGNAT
(a === 192 && b === 0 && (c === 0 || c === 2)) || // 192.0.0/24 IETF, 192.0.2/24 TEST-NET-1
(a === 198 && (b === 18 || b === 19)) || // 198.18/15 benchmarking
(a === 198 && b === 51 && c === 100) || // 198.51.100/24 TEST-NET-2
(a === 203 && b === 0 && c === 113) || // 203.0.113/24 TEST-NET-3
a >= 224 // 224/4 multicast, 240/4 reserved, 255.255.255.255 broadcast
);
}

/** Decode an IPv4-mapped IPv6 host (`::ffff:127.0.0.1` or its canonicalized hex
* form `::ffff:7f00:1`) to its dotted IPv4 string, else null. WHATWG `new URL`
* canonicalizes the dotted form to hex, so the hex tail is what the guard
* actually sees — both are handled. Lets isPrivateIpv4 catch a loopback/private
* target smuggled through the v6 mapping (#232, item 4). */
function mappedIpv4(host: string): string | null {
const m = /^::ffff:(.+)$/i.exec(host);
if (!m) return null;
const tail = m[1]!;
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(tail)) return tail; // dotted
const hex = /^([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i.exec(tail); // h:h (two 16-bit groups)
if (!hex) return null;
const hi = parseInt(hex[1]!, 16);
const lo = parseInt(hex[2]!, 16);
return `${(hi >> 8) & 255}.${hi & 255}.${(lo >> 8) & 255}.${lo & 255}`;
/** Parse an IPv6 literal (no brackets) into its eight 16-bit groups, else
* null. Accepts `::` compression and a trailing dotted-IPv4 tail. */
function parseIpv6(host: string): number[] | null {
if (!host.includes(":")) return null;
let text = host;
const tail: number[] = [];
const dotted = /^(.*:)(\d{1,3}(?:\.\d{1,3}){3})$/.exec(text);
if (dotted) {
const o = dotted[2]!.split(".").map(Number);
if (o.some((n) => n > 255)) return null;
tail.push((o[0]! << 8) | o[1]!, (o[2]! << 8) | o[3]!);
text = dotted[1]!.endsWith("::") ? dotted[1]! : dotted[1]!.slice(0, -1);
}
const halves = text.split("::");
if (halves.length > 2) return null;
const parseGroups = (part: string): number[] | null => {
if (part === "") return [];
const groups = part.split(":");
if (groups.some((g) => !/^[0-9a-f]{1,4}$/i.test(g))) return null;
return groups.map((g) => parseInt(g, 16));
};
const head = parseGroups(halves[0]!);
const rest = halves.length === 2 ? parseGroups(halves[1]!) : [];
if (!head || !rest) return null;
const explicit = head.length + rest.length + tail.length;
if (halves.length === 1) return explicit === 8 ? [...head, ...tail] : null;
if (explicit > 7) return null;
return [...head, ...Array<number>(8 - explicit).fill(0), ...rest, ...tail];
}

/** True iff an IPv6 literal may be fetched. Only global unicast (2000::/3) is
* allowed, minus the ranges that embed or tunnel to an IPv4 address (6to4
* 2002::/16, Teredo 2001::/32) and documentation space. This denies loopback,
* unspecified, link-local, ULA, multicast, NAT64 64:ff9b::/96, and the
* deprecated IPv4-compatible ::/96. IPv4-mapped ::ffff:0:0/96 is judged by
* its embedded IPv4 address (#232). An unparseable literal is denied. */
function isAllowedIpv6(host: string): boolean {
const g = parseIpv6(host);
if (!g) return false;
if (g.slice(0, 5).every((x) => x === 0) && g[5] === 0xffff) {
const [hi, lo] = [g[6]!, g[7]!];
return !isPrivateIpv4(`${hi >> 8}.${hi & 255}.${lo >> 8}.${lo & 255}`);
}
const [g0, g1] = [g[0]!, g[1]!];
if ((g0 & 0xe000) !== 0x2000) return false; // not 2000::/3 global unicast
if (g0 === 0x2002) return false; // 6to4
if (g0 === 0x2001 && (g1 === 0 || g1 === 0x0db8)) return false; // Teredo, docs
if ((g0 & 0xfff0) === 0x3ff0 && g1 < 0x1000) return false; // 3fff::/20 docs
return true;
}

/** The outbound target guard: exclude internal destinations and browser-only
Expand All @@ -119,16 +159,8 @@ export function isAllowedUnfurlTarget(raw: string): boolean {
return false;
if (host === "localhost" || host.endsWith(".localhost")) return false;
if (host.endsWith(".local") || host.endsWith(".internal")) return false;
if (host === "0.0.0.0" || host === "::" || host === "::1") return false;
if (
host.startsWith("fe80:") ||
host.startsWith("fc") ||
host.startsWith("fd")
)
return false; // IPv6 link-local / ULA
if (host.includes(":")) return isAllowedIpv6(host);
if (isPrivateIpv4(host)) return false;
const mapped = mappedIpv4(host); // IPv4-mapped IPv6 smuggling a private target
if (mapped && isPrivateIpv4(mapped)) return false;
return true;
}

Expand Down
32 changes: 32 additions & 0 deletions worker/unfurl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,38 @@ test.each([
["http://[::ffff:7f00:1]/", false],
["http://[::ffff:10.0.0.1]/", false],
["http://[::ffff:169.254.169.254]/", false],
// IPv6 forms that embed or tunnel to IPv4, and other non-global-unicast v6.
["http://[64:ff9b::7f00:1]/", false], // NAT64 -> 127.0.0.1
["http://[64:ff9b::a9fe:a9fe]/", false], // NAT64 -> 169.254.169.254
["http://[64:ff9b::8.8.8.8]/", false], // NAT64 is denied outright
["http://[2002:7f00:1::]/", false], // 6to4 -> 127.0.0.1
["http://[2002:808:808::1]/", false], // 6to4 is denied outright
["http://[::7f00:1]/", false], // IPv4-compatible -> 127.0.0.1
["http://[::127.0.0.1]/", false],
["http://[::]/", false],
["http://[2001::1]/", false], // Teredo
["http://[2001:db8::1]/", false], // documentation
["http://[ff02::1]/", false], // multicast
["http://[fc00::1]/", false], // ULA
["http://[fec0::1]/", false], // deprecated site-local
["http://[2606:4700:4700::1111]/", true], // global unicast
["http://[2001:4860:4860::8888]/", true],
// Names that merely start with v6-looking prefixes are hostnames, not literals.
["https://fcbarcelona.com/", true],
["https://fdic.gov/", true],
// Reserved, benchmarking, documentation, multicast, and broadcast IPv4.
["http://198.18.0.1/", false],
["http://198.19.255.255/", false],
["http://192.0.0.1/", false],
["http://192.0.2.1/", false],
["http://198.51.100.1/", false],
["http://203.0.113.1/", false],
["http://224.0.0.1/", false],
["http://240.0.0.1/", false],
["http://255.255.255.255/", false],
["http://0x7f.1/", false], // WHATWG normalizes to 127.0.0.1
["http://8.8.8.8/", true],
["http://[::ffff:198.18.0.1]/", false],
])("isAllowedUnfurlTarget(%p) is %p (SSRF guard)", (url, ok) => {
expect(isAllowedUnfurlTarget(url)).toBe(ok);
});
Expand Down
Loading