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 = / ^ : : f f f f : ( .+ ) $ / . exec ( addr ) ;
152- if ( mapped ) {
153- const tail = mapped [ 1 ] ;
154- if ( isIP ( tail ) === 4 ) return isBlockedAddress ( tail ) ;
155- const hex = / ^ ( [ 0 - 9 a - f ] { 1 , 4 } ) : ( [ 0 - 9 a - 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 ( / ^ f e [ 8 9 a b ] / . 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