@@ -10,7 +10,7 @@ import { describe, expect, test } from 'vitest'
1010
1111import type { ExternalIp , IpVersion , UnicastIpPool } from '~/api'
1212
13- import { getEphemeralIpSlots , parseIp , parseIpNet } from './ip'
13+ import { getEphemeralIpSlots , toUrlCheckableIpv6 , parseIp , parseIpNet } from './ip'
1414
1515const makePool = ( ipVersion : IpVersion , name = `pool-${ ipVersion } ` ) : UnicastIpPool => ( {
1616 id : `id-${ name } ` ,
@@ -183,6 +183,22 @@ describe('getEphemeralIpSlots', () => {
183183// and oxnet's Ipv4Net and Ipv6Net have the same validation behavior as our code.
184184// https://github.com/oxidecomputer/test-ip-validation
185185
186+ test . each ( [
187+ [ '2001:db8::1' , '2001:db8::1' ] ,
188+ [ '2001:db8:1:2::10.0.0.1' , '2001:db8:1:2::0:0' ] ,
189+ [ '1:2:3:4::10.0.0.1' , '1:2:3:4::0:0' ] ,
190+ [ '::ffff:255.255.255.255' , '::ffff:0:0' ] ,
191+ ] ) ( 'toUrlCheckableIpv6 converts %s to %s' , ( ip , expected ) => {
192+ expect ( toUrlCheckableIpv6 ( ip ) ) . toBe ( expected )
193+ } )
194+
195+ test . each ( [ '::ffff:1.2.3.04' , '::251.240.044.17' , '1::2:' , '::1:' , '1.2.3.4::' , '1.2.3.4' ] ) (
196+ 'toUrlCheckableIpv6 rejects %s' ,
197+ ( ip ) => {
198+ expect ( toUrlCheckableIpv6 ( ip ) ) . toBeNull ( )
199+ }
200+ )
201+
186202const v4 = [ '123.4.56.7' , '1.2.3.4' ]
187203
188204test . each ( v4 ) ( 'parseIp catches valid IPV4 / invalid IPV6: %s' , ( s ) => {
@@ -206,6 +222,18 @@ const v6 = [
206222 '::ffff:255.255.255.255' ,
207223 'fe08::7:8' ,
208224 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' ,
225+ // embedded IPv4 is allowed with :: in any position and in the full 6-group
226+ // form, not just after ::ffff: (console#1939)
227+ '2001:db8:3:4:5::192.0.2.33' ,
228+ '1:2:3:4:5:6:192.0.2.33' ,
229+ '::192.0.2.33' ,
230+ '2001:db8::192.0.2.33' ,
231+ '1:2:3:4::5:192.0.2.33' ,
232+ 'db8:db8:db8:db8:db8::192.0.2.33' ,
233+ // WebKit's URL parser wrongly rejects embedded IPv4 where :: expands to
234+ // exactly two groups; isIpv6 validates the quad itself so these still pass
235+ '2001:db8:1:2::10.0.0.1' ,
236+ '1:2:3:4::10.0.0.1' ,
209237]
210238
211239test . each ( v6 ) ( 'parseIp catches invalid IPV4 / valid IPV6: %s' , ( s ) => {
@@ -238,10 +266,33 @@ const invalid = [
238266 '::1:2:3:4:5:6:7:8' ,
239267 '1:2:3:4:5:6:7:8::' ,
240268 '1:2:3:4:5:6:7:88888' ,
241- '2001:db8:3:4:5::192.0.2.33' , // std::new::Ipv6Net allows this one
269+ '1:2:3:4:5:6:7:192.0.2.33' , // one group too many before the dotted quad
270+ '::1.2.3.4.5' ,
271+ '1:2:3:4:5:6:7:8:1.2.3.4' ,
242272 'fe08::7:8%' ,
243273 'fe08::7:8i' ,
244274 'fe08::7:8interface' ,
275+ // zone IDs are valid in some contexts but std::net::Ipv6Addr rejects them
276+ 'fe80::1%eth0' ,
277+ 'fe80::1%0' ,
278+ 'fe80::%eth0' ,
279+ '::1%lo0' ,
280+ // We validate v6 by handing the address to the URL parser as a bracketed
281+ // host, so a `]` plus a URL continuation could otherwise parse as port, path,
282+ // query, or fragment and come back valid. Zod's z.ipv6() has this bug.
283+ '::1]:80' ,
284+ '::1]:80/foo' ,
285+ '::1]#x' ,
286+ '::1]?q=1' ,
287+ '::1]:0/../x' ,
288+ '::1]@evil.com' ,
289+ '::1]' ,
290+ // engine quirks handled explicitly in isIpv6: Chrome's URL parser accepts
291+ // leading-zero octets in the embedded quad; WebKit's accepts a trailing colon
292+ '::ffff:1.2.3.04' ,
293+ '::251.240.044.17' ,
294+ '1::2:' ,
295+ '::1:' ,
245296]
246297
247298test . each ( invalid ) ( 'parseIp catches invalid IP: %s' , ( s ) => {
0 commit comments