Skip to content

Commit c8d619f

Browse files
cliffhallclaude
andcommitted
fix(server): block IPv4-mapped IPv6 form of metadata IP in /fetch guard
The IPv4-mapped IPv6 branch of isBlockedProxyAddress only matched the dotted-decimal form (::ffff:169.254.169.254), but the WHATWG URL parser serializes IPv4-mapped addresses in hex (::ffff:a9fe:a9fe), so http://[::ffff:169.254.169.254]/ resolved past the guard and still reached the cloud metadata service on dual-stack hosts — the exact vector this guard targets. Extract the embedded IPv4 from both the hex and dotted forms and re-check it. Adds a regression test for the IPv4-mapped IPv6 metadata address. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01HiccCEh9mwCfVzE8qYcop1
1 parent 0db2b16 commit c8d619f

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎client/src/__tests__/proxyFetchEndpoint.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,25 @@ describe("POST /fetch endpoint", () => {
249249
expect(body.error).toMatch(/blocked address/i);
250250
});
251251

252+
it("returns 403 for the metadata IP in IPv4-mapped IPv6 form (SSRF guard)", async () => {
253+
// The WHATWG URL parser serializes this host to ::ffff:a9fe:a9fe, which must
254+
// still be recognized as 169.254.169.254 and blocked.
255+
const res = await fetch(`${baseUrl}/fetch`, {
256+
method: "POST",
257+
headers: {
258+
"Content-Type": "application/json",
259+
"X-MCP-Proxy-Auth": `Bearer ${TEST_TOKEN}`,
260+
},
261+
body: JSON.stringify({
262+
url: "http://[::ffff:169.254.169.254]/latest/meta-data/",
263+
init: { method: "GET" },
264+
}),
265+
});
266+
expect(res.status).toBe(403);
267+
const body = (await res.json()) as { error: string };
268+
expect(body.error).toMatch(/blocked address/i);
269+
});
270+
252271
it("blocks a redirect that points at a blocked address (SSRF guard)", async () => {
253272
await withLocalUpstream(
254273
(req, res) => {

‎server/src/index.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,10 +886,24 @@ function isBlockedProxyAddress(ip: string): boolean {
886886
}
887887
if (kind === 6) {
888888
const addr = ip.toLowerCase();
889-
// IPv4-mapped IPv6 (e.g. ::ffff:169.254.169.254) — check the IPv4 form
890-
const mapped = addr.match(/::ffff:(\d+\.\d+\.\d+\.\d+)$/);
889+
// IPv4-mapped IPv6 (::ffff:x). The WHATWG URL parser serializes the embedded
890+
// IPv4 in hex (::ffff:a9fe:a9fe for 169.254.169.254), while DNS and other
891+
// sources may use the dotted form (::ffff:169.254.169.254) — handle both by
892+
// extracting the IPv4 and re-checking it.
893+
const mapped = addr.match(/^::ffff:(.+)$/);
891894
if (mapped) {
892-
return isBlockedProxyAddress(mapped[1]);
895+
const tail = mapped[1];
896+
if (isIP(tail) === 4) {
897+
return isBlockedProxyAddress(tail);
898+
}
899+
const hex = tail.match(/^([0-9a-f]{1,4}):([0-9a-f]{1,4})$/);
900+
if (hex) {
901+
const high = parseInt(hex[1], 16);
902+
const low = parseInt(hex[2], 16);
903+
return isBlockedProxyAddress(
904+
`${high >> 8}.${high & 0xff}.${low >> 8}.${low & 0xff}`,
905+
);
906+
}
893907
}
894908
// fe80::/10 — IPv6 link-local
895909
if (/^fe[89ab]/.test(addr)) {

0 commit comments

Comments
 (0)