-
Notifications
You must be signed in to change notification settings - Fork 261
fix(core): classify IPv6 literals by parsed bytes in the private-range SSRF gate #3502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Astro-Han
merged 4 commits into
apache:main
from
GabrielDrapor:fix/private-range-ipv6-spellings
Aug 23, 2026
+294
−11
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
109cbfc
fix(core): classify IPv6 literals by parsed bytes in the private-rang…
GabrielDrapor ba4f36d
fix(core): classify deprecated IPv4-compatible ::/96 embeddings too
GabrielDrapor f0707b1
fix(core): close the remaining IPv4-embedding and parser gaps in the …
GabrielDrapor 4e00843
fix(core): treat exactly 0.0.0.0 as machine-local in the plain IPv4 b…
GabrielDrapor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
packages/core/src/__tests__/mcp-host-classification.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
| import { isLoopbackHost, isPrivateRangeHost } from '../mcp.js'; | ||
|
|
||
| describe('MCP host classification', () => { | ||
| it('classifies IPv6 literals by parsed bytes, not spelling', () => { | ||
| // WHATWG URL parsing canonicalizes hostnames, so the same address | ||
| // arrives in different spellings depending on how it was written; the | ||
| // classifier must agree across all of them. | ||
| for (const spelling of [ | ||
| '[::ffff:127.0.0.1]', | ||
| '[::FFFF:127.0.0.1]', | ||
| '[::ffff:7f00:1]', | ||
| '[0:0:0:0:0:ffff:7f00:1]', | ||
| ]) { | ||
| assert.equal(isPrivateRangeHost(spelling), true, spelling); | ||
| // The asymmetry is deliberate: teaching isLoopbackHost the mapped | ||
| // spelling would flip its fail-closed cleartext refusal into | ||
| // fail-open. The mapped loopback must land on the PRIVATE side. | ||
| assert.equal(isLoopbackHost(spelling), false, spelling); | ||
| } | ||
| // The canonical URL round-trip agrees with the raw spellings. | ||
| assert.equal(isPrivateRangeHost(new URL('https://[::ffff:192.168.1.1]/').hostname), true); | ||
| assert.equal(isPrivateRangeHost(new URL('https://[64:ff9b::10.0.0.5]/').hostname), true); | ||
| }); | ||
|
|
||
| it('covers the whole fe80::/10 link-local range, not just the fe8 spelling', () => { | ||
| for (const inner of ['fe80::1', 'fe9a::1', 'feaf::1', 'febf::1']) { | ||
| assert.equal(isPrivateRangeHost(`[${inner}]`), true, inner); | ||
| } | ||
| assert.equal(isPrivateRangeHost('[fec0::1]'), false); | ||
| }); | ||
|
|
||
| it('classifies deprecated IPv4-compatible ::/96 embeddings by their bytes', () => { | ||
| // RFC 4291 §2.5.5.1 — deprecated, but a byte-level embedding the | ||
| // classifier must know regardless of whether modern stacks still | ||
| // translate it: the gate's correctness must not rest on the other | ||
| // end's network stack. | ||
| assert.equal(isPrivateRangeHost(new URL('https://[::192.168.1.1]/').hostname), true); | ||
| assert.equal(isPrivateRangeHost(new URL('https://[::127.0.0.1]/').hostname), true); | ||
| assert.equal(isPrivateRangeHost('[::c0a8:101]'), true); | ||
| assert.equal(isPrivateRangeHost('[::7f00:1]'), true); | ||
| assert.equal(isPrivateRangeHost('[::a00:5]'), true); // ::10.0.0.5 | ||
| // Public v4-compatible stays global; the unspecified address reaches | ||
| // the local machine on common stacks and is private (fail closed). | ||
| assert.equal(isPrivateRangeHost('[::808:808]'), false); | ||
| assert.equal(isPrivateRangeHost('[::]'), true); | ||
| assert.equal(isPrivateRangeHost('[::2]'), false); | ||
| }); | ||
|
|
||
| it('classifies the remaining translation embeddings and the mapped unspecified address', () => { | ||
| // Mapped unspecified: connecting to ::ffff:0.0.0.0 reaches a | ||
| // 127.0.0.1-bound listener on common stacks — private, fail closed. | ||
| assert.equal(isPrivateRangeHost('[::ffff:0:0]'), true); | ||
| // RFC 8215 local-use NAT64 space (64:ff9b:1::/48) is reserved for | ||
| // in-network translation; deployments carve arbitrary RFC 6052 prefix | ||
| // lengths out of it, so the whole /48 fails closed. | ||
| assert.equal(isPrivateRangeHost('[64:ff9b:1:c0a8:1:100::]'), true); | ||
| assert.equal(isPrivateRangeHost('[64:ff9b:1::1]'), true); | ||
| // RFC 2765 SIIT (::ffff:0:0/96) classifies by its embedded IPv4. | ||
| assert.equal(isPrivateRangeHost(new URL('https://[0::ffff:0:192.168.1.1]/').hostname), true); | ||
| assert.equal(isPrivateRangeHost('[::ffff:0:808:808]'), false); // SIIT 8.8.8.8 | ||
| }); | ||
|
|
||
| it('treats exactly 0.0.0.0 as machine-local in the plain IPv4 branch too', () => { | ||
| // `https://0/` canonicalizes to 0.0.0.0, and connecting to it reaches | ||
| // the local machine — the same rationale as the embedded checks, which | ||
| // this branch previously never consulted. Only the exact address: | ||
| // 0.0.0.1 does not reach a local listener. | ||
| assert.equal(isPrivateRangeHost('0.0.0.0'), true); | ||
| assert.equal(isPrivateRangeHost(new URL('https://0/').hostname), true); | ||
| assert.equal(isPrivateRangeHost('0.0.0.1'), false); | ||
| }); | ||
|
|
||
| it('rejects literals RFC 4291 does not permit instead of mis-parsing them', () => { | ||
| // Dotted IPv4 belongs only in the low-order 32 bits; an empty zone id | ||
| // is not a literal. Both fail closed as private. | ||
| assert.equal(isPrivateRangeHost('[192.168.1.1::]'), true); | ||
| assert.equal(isPrivateRangeHost('[1:2.2.2.2:3::]'), true); | ||
| assert.equal(isPrivateRangeHost('[2606:4700::1%]'), true); | ||
| }); | ||
|
|
||
| it('keeps global addresses reachable and fails closed on garbage', () => { | ||
| assert.equal(isPrivateRangeHost('[2606:4700::1]'), false); | ||
| assert.equal(isPrivateRangeHost('[::ffff:808:808]'), false); // mapped 8.8.8.8 | ||
| assert.equal(isPrivateRangeHost('[64:ff9b::808:808]'), false); | ||
| assert.equal(isPrivateRangeHost('[::1]'), false); // isLoopbackHost's positive case | ||
| assert.equal(isLoopbackHost('[::1]'), true); | ||
| // An unparseable bracketed literal is treated as private: the SSRF | ||
| // gate fails closed on spellings it cannot classify. | ||
| assert.equal(isPrivateRangeHost('[not-an-address]'), true); | ||
| assert.equal(isPrivateRangeHost('[1::2::3]'), true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] IPv4-compatible
::/96is not classified by its embedded IPv4.classifyIpv6recognizes two ways of embedding an IPv4 address —::ffff:/96and64:ff9b::/96— and hands both toisPrivateIpv4. It does not recognize the third,::/96, so those addresses reach the finalreturn 'global'.Reproduction, using the URL parser this gate sits behind:
Trace
[::7f00:1]through the function: bytes 0–11 are zero somappedis false (byte 10 is not0xff) andnat64is false (byte 1 is not0x64); the loopback check requires bytes 0–14 to be zero and byte 12 is0x7f;fc00::/7andfe80::/10both test byte 0, which is zero. Result:global.isPrivateRangeHost('[::7f00:1]')returnsfalse, so the remote-HTTPS branch intransport-security.ts:59-75does not reject it, and a remote server's metadata or a redirect can still hand back a target spelled this way.On severity. We tried to connect to
::127.0.0.1on macOS and it times out rather than reaching the loopback listener, so on a current stack this is unlikely to be an exploitable path to a live service — IPv4-compatible addresses are deprecated by RFC 4291 and most stacks no longer translate them. That is why this is a P2 and not a P1. But it should still be closed, for a reason that does not depend on reachability: this PR's whole premise is that classification happens on parsed bytes rather than on how an address is written, and::/96is the one byte-level IPv4 embedding the classifier does not know about. Leaving it means the gate's correctness rests on an assumption about the other end's network stack, which is exactly the kind of assumption this change set out to remove.Smallest fix: treat
::/96alongside the other two — bytes 0–11 all zero, excluding::and::1which the surrounding checks already own — and reuse the sameisPrivateIpv4/127.logic. Worth a regression case that comes in through the remote-provenance path rather than only calling the classifier directly, since that is where a real one would arrive.