Skip to content

Commit 5532850

Browse files
authored
Merge pull request #3050 from modelcontextprotocol/v2/fix/3048-nat64-ssrf
fix(fetchGuard): block IPv4-embedding IPv6 forms, incl. NAT64
2 parents f97624a + 3469894 commit 5532850

2 files changed

Lines changed: 159 additions & 17 deletions

File tree

‎clients/web/src/test/core/mcp/node/fetchGuard.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,40 @@ describe("isBlockedAddress", () => {
4343
"192.0.0.192",
4444
"::ffff:192.0.0.192",
4545
"::ffff:c000:c0",
46+
// Non-canonical spellings `isIP` accepts are matched on their value.
47+
"0:0:0:0:0:ffff:a9fe:a9fe",
48+
"0000::FFFF:169.254.169.254",
49+
"fe80::1%eth0",
50+
// The top of fe80::/10.
51+
"febf::1",
52+
"fd00:0ec2:0:0:0:0:0:0254",
53+
// NAT64 (RFC 6052 well-known prefix) forms of every metadata address, in
54+
// the hex form WHATWG URL serializes to and the dotted form.
55+
"64:ff9b::a9fe:a9fe",
56+
"64:ff9b::169.254.169.254",
57+
"64:ff9b::6464:64c8",
58+
"64:ff9b::100.100.100.200",
59+
"64:ff9b::c000:c0",
60+
"64:ff9b::192.0.0.192",
61+
// ...and their non-canonical spellings.
62+
"64:ff9b:0:0:0:0:a9fe:a9fe",
63+
"0064:ff9b:0000::a9fe:a9fe",
64+
"64:FF9B::A9FE:A9FE",
65+
"64:ff9b:0:0:0:0:169.254.169.254",
66+
// The RFC 8215 local-use prefix, /96 layout.
67+
"64:ff9b:1::a9fe:a9fe",
68+
"64:ff9b:1:abcd::169.254.169.254",
69+
// The local-use prefix under each shorter RFC 6052 layout, worked from
70+
// the §2.2 table (byte 8 is the reserved `u` octet).
71+
// /48: a9.fe in bytes 6-7, a9.fe in bytes 9-10.
72+
"64:ff9b:1:a9fe:a9:fe00::",
73+
"64:ff9b:1:6464:64:c800::", // 100.100.100.200
74+
// /56: a9 in byte 7, fe.a9.fe in bytes 9-11 (byte 6 is prefix).
75+
"64:ff9b:1:a9:fe:a9fe::",
76+
"64:ff9b:1:12a9:fe:a9fe::",
77+
// /64: a9.fe.a9.fe in bytes 9-12 (bytes 6-7 are prefix).
78+
"64:ff9b:1:0:a9:fea9:fe00:0",
79+
"64:ff9b:1:beef:a9:fea9:fe00:0",
4680
])("blocks %s", (ip) => {
4781
expect(isBlockedAddress(ip)).toBe(true);
4882
});
@@ -68,9 +102,30 @@ describe("isBlockedAddress", () => {
68102
"::ffff:100.100.100.100",
69103
"::ffff:6464:6464",
70104
"192.0.0.193",
105+
// NAT64 of a public address is an ordinary destination.
106+
"64:ff9b::808:808",
107+
"64:ff9b::8.8.8.8",
108+
"64:ff9b:1::808:808",
109+
// Not a NAT64 prefix: the IPv4-looking tail means nothing here.
110+
"64:ff9c::a9fe:a9fe",
111+
"64:ff9b:2::a9fe:a9fe",
112+
"64:ff9b:0:0:1::a9fe:a9fe",
113+
"2001:db8::a9fe:a9fe",
114+
"fd00:ec2::",
115+
// fe8::1 is group 0x0fe8, below fe80::/10.
116+
"fe8::1",
71117
])("allows %s", (ip) => {
72118
expect(isBlockedAddress(ip)).toBe(false);
73119
});
120+
121+
// 8.8.8.8 under the /48 layout. Its decodings under every layout the guard
122+
// reads (/48, /56, /64, /96) are all public, so it stays reachable.
123+
it("allows a public address under the local-use /48 layout", () => {
124+
expect(isBlockedAddress("64:ff9b:1:808:8:800::")).toBe(false);
125+
for (const decoded of ["8.8.8.8", "8.8.8.0", "8.8.0.0", "0.0.0.0"]) {
126+
expect(isBlockedAddress(decoded)).toBe(false);
127+
}
128+
});
74129
});
75130

76131
describe("normalizeFetchHeaders", () => {

‎core/mcp/node/fetchGuard.ts‎

Lines changed: 104 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
* - **Scheme:** `http:` and `https:` only, on every hop.
1212
* - **Destinations:** link-local and metadata addresses only —
1313
* `169.254.0.0/16`, the cloud metadata services that sit outside it
14-
* (`100.100.100.200`, `192.0.0.192`), the IPv4-mapped IPv6 forms of all of
15-
* those, `fe80::/10` and `fd00:ec2::254` ({@link isBlockedAddress}).
14+
* (`100.100.100.200`, `192.0.0.192`), the IPv4-mapped and NAT64 IPv6
15+
* forms of all of those, `fe80::/10` and `fd00:ec2::254`
16+
* ({@link isBlockedAddress}).
1617
* Loopback, RFC 1918 and CGNAT space stay reachable on purpose: local and
1718
* LAN MCP servers and authorization servers are legitimate targets.
1819
* - **Redirects:** followed here, with `redirect: "manual"`, so each hop is
@@ -128,6 +129,10 @@ const METADATA_IPV4_ADDRESSES: ReadonlySet<string> = new Set([
128129
* fixed address *outside* link-local, so the range above misses them
129130
* - `::ffff:` IPv4-mapped forms of every IPv4 address above, in both the
130131
* dotted and the hex serialization
132+
* - NAT64 forms of the same addresses, under the RFC 6052 well-known prefix
133+
* `64:ff9b::/96` and the RFC 8215 local-use prefix `64:ff9b:1::/48`
134+
* ({@link nat64EmbeddedIpv4s}): on a host with NAT64 egress,
135+
* `64:ff9b::a9fe:a9fe` *is* `169.254.169.254`
131136
* - `fe80::/10` (IPv6 link-local)
132137
* - `fd00:ec2::254` (the AWS IPv6 IMDS)
133138
*
@@ -136,6 +141,11 @@ const METADATA_IPV4_ADDRESSES: ReadonlySet<string> = new Set([
136141
* Tailscale also uses (its MagicDNS resolver is `100.100.100.100`), and a
137142
* tailnet MCP or authorization server is as legitimate a target as a LAN one.
138143
*
144+
* Every IPv6 form is matched on its parsed value ({@link ipv6Groups}), not
145+
* its spelling, so the non-canonical spellings `isIP` accepts — leading
146+
* zeros, an uncompressed run of zero groups, uppercase, a dotted tail — are
147+
* caught as well as the canonical one.
148+
*
139149
* Anything that is not an IP address returns `false`; names are resolved
140150
* before they are checked.
141151
*/
@@ -147,22 +157,99 @@ export function isBlockedAddress(ip: string): boolean {
147157
}
148158
if (kind !== 6) return false;
149159

150-
const addr = ip.toLowerCase();
151-
const mapped = /^::ffff:(.+)$/.exec(addr);
152-
if (mapped) {
153-
const tail = mapped[1];
154-
if (isIP(tail) === 4) return isBlockedAddress(tail);
155-
const hex = /^([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(tail);
156-
if (hex) {
157-
const high = parseInt(hex[1], 16);
158-
const low = parseInt(hex[2], 16);
159-
return isBlockedAddress(
160-
`${high >> 8}.${high & 0xff}.${low >> 8}.${low & 0xff}`,
161-
);
162-
}
160+
const groups = ipv6Groups(ip);
161+
const [g0, g1, g2, g3, g4, g5, g6, g7] = groups;
162+
// ::ffff:0:0/96, the IPv4-mapped range, carries an IPv4 address in the low
163+
// 32 bits; the verdict is that address's.
164+
const highZero = g0 === 0 && g1 === 0 && g2 === 0 && g3 === 0 && g4 === 0;
165+
if (highZero && g5 === 0xffff) {
166+
return isBlockedAddress(`${g6 >> 8}.${g6 & 0xff}.${g7 >> 8}.${g7 & 0xff}`);
167+
}
168+
// A NAT64 address is blocked if any IPv4 address it may embed is.
169+
const embedded = nat64EmbeddedIpv4s(groups);
170+
if (embedded !== undefined) {
171+
return embedded.some((ipv4) => isBlockedAddress(ipv4));
172+
}
173+
if ((g0 & 0xffc0) === 0xfe80) return true; // fe80::/10
174+
// fd00:ec2::254
175+
return (
176+
g0 === 0xfd00 &&
177+
g1 === 0xec2 &&
178+
g2 === 0 &&
179+
g3 === 0 &&
180+
g4 === 0 &&
181+
g5 === 0 &&
182+
g6 === 0 &&
183+
g7 === 0x254
184+
);
185+
}
186+
187+
/**
188+
* Where RFC 6052 §2.2 puts the embedded IPv4 address, as byte offsets into
189+
* the IPv6 address, for a NAT64 prefix of each length. Byte 8 (bits 64-71)
190+
* is the reserved `u` octet, which every layout skips.
191+
*/
192+
const NAT64_LAYOUT_96: readonly number[] = [12, 13, 14, 15];
193+
const NAT64_LOCAL_USE_LAYOUTS: readonly (readonly number[])[] = [
194+
[6, 7, 9, 10], // /48: two octets before `u`, two after
195+
[7, 9, 10, 11], // /56: one octet before `u`, three after
196+
[9, 10, 11, 12], // /64: all four after `u`
197+
NAT64_LAYOUT_96, // /96: the low 32 bits
198+
];
199+
200+
/**
201+
* The IPv4 addresses a NAT64 address (as {@link ipv6Groups}) may embed, or
202+
* `undefined` when it is not a NAT64 address.
203+
*
204+
* - `64:ff9b::/96`, the RFC 6052 well-known prefix, is a /96 by definition,
205+
* so it embeds one address, in the low 32 bits.
206+
* - `64:ff9b:1::/48`, the RFC 8215 local-use prefix, exists for exactly this
207+
* translation and has no other use. An operator carves a NAT64 prefix of
208+
* any RFC 6052 length from /48 to /96 out of it, and which one it chose is
209+
* not recoverable from the address, so every layout is decoded and the
210+
* caller blocks the address if any decoding is blocked. That fails closed
211+
* on the ambiguity without blocking the whole /48, which would cut off
212+
* every destination behind such a translator. The cost is a rare false
213+
* refusal, where another layout's decoding of a public destination lands
214+
* in a blocked range (`a.169.254.d` under /48 reads as `169.254.d.0`
215+
* under /56).
216+
*/
217+
function nat64EmbeddedIpv4s(groups: readonly number[]): string[] | undefined {
218+
const [g0, g1, g2, g3, g4, g5] = groups;
219+
if (g0 !== 0x64 || g1 !== 0xff9b) return undefined;
220+
const bytes = groups.flatMap((g) => [g >> 8, g & 0xff]);
221+
const decode = (offsets: readonly number[]): string =>
222+
offsets.map((offset) => bytes[offset]).join(".");
223+
if (g2 === 1) return NAT64_LOCAL_USE_LAYOUTS.map(decode);
224+
if (g2 === 0 && g3 === 0 && g4 === 0 && g5 === 0) {
225+
return [decode(NAT64_LAYOUT_96)];
226+
}
227+
return undefined;
228+
}
229+
230+
/**
231+
* The eight 16-bit groups of an address `isIP` has already accepted as IPv6,
232+
* whatever its spelling: a zone index is dropped, a dotted IPv4 tail becomes
233+
* the last two groups, and a `::` expands to the zero groups it stands for.
234+
*/
235+
function ipv6Groups(ip: string): number[] {
236+
let addr = ip.replace(/%.*$/, "");
237+
const lastColon = addr.lastIndexOf(":");
238+
const tail = addr.slice(lastColon + 1);
239+
if (tail.includes(".")) {
240+
const [a, b, c, d] = tail.split(".").map(Number);
241+
const high = ((a << 8) | b).toString(16);
242+
const low = ((c << 8) | d).toString(16);
243+
addr = `${addr.slice(0, lastColon + 1)}${high}:${low}`;
163244
}
164-
if (/^fe[89ab]/.test(addr)) return true; // fe80::/10
165-
return addr === "fd00:ec2::254";
245+
const toGroups = (part: string): number[] =>
246+
part === "" ? [] : part.split(":").map((g) => parseInt(g, 16));
247+
const halves = addr.split("::");
248+
if (halves.length === 1) return toGroups(addr);
249+
const head = toGroups(halves[0]);
250+
const rest = toGroups(halves[1]);
251+
const zeros = new Array<number>(8 - head.length - rest.length).fill(0);
252+
return [...head, ...zeros, ...rest];
166253
}
167254

168255
/** Throws a {@link FetchTargetError} naming the first blocked address. */

0 commit comments

Comments
 (0)