fix(web): accept IPv4-mapped IPv6 policy entries (SNP-830)

This commit is contained in:
CodingOnStar committed 2026-09-20 17:28:35 +08:00
1 parent e58d4f3ff6
commit e44f95b2fd
3 files changed
+60 -2

No files matched your search

@@ -111,3 +111,28 @@ describe('IpPolicyDialog', () => {
})
})
})
describe('trusted current IP and allowlist editing', () => {
it.each(['create', 'edit'] as const)(
'accepts a mapped IPv6 address in %s mode and submits it to the server',
async (mode) => {
const user = userEvent.setup()
const onSubmit = vi.fn()
render(
<IpPolicyDialog
open
mode={mode}
initialName="Office"
initialEntries={['::ffff:203.0.113.42/128']}
onOpenChange={vi.fn()}
onSubmit={onSubmit}
/>,
)
await user.click(screen.getByRole('button', { name: mode === 'create' ? 'Create' : 'Save' }))
expect(onSubmit).toHaveBeenCalledWith({
name: 'Office',
allowed_cidrs: ['::ffff:203.0.113.42/128'],
})
},
)
})
@@ -71,6 +71,32 @@ describe('validateIpEntry', () => {
expect(validateIpEntry('10.0.0.*')).toEqual({ kind: 'invalid', code: 'unsupported' })
expect(validateIpEntry('office.example.com')).toEqual({ kind: 'invalid', code: 'unsupported' })
})
it.each([
'::ffff:203.0.113.42/128',
'::ffff:203.0.113.0/120',
'0:0:0:0:0:ffff:203.0.113.42',
'::ffff:cb00:712a/128',
'2001:db8::192.0.2.1/128',
'::/0',
])('accepts valid IPv6 notation also accepted by the backend: %s', (entry) => {
expect(validateIpEntry(entry)).toEqual({ kind: 'valid' })
expect(canSubmitIpPolicy('Office', [entry])).toBe(true)
})
it.each([
'::ffff:203.0.113.256',
'::ffff:203.0.113.01',
'::ffff:203.0.113',
'::ffff::203.0.113.42',
'1:2:3:4:5:6:7:203.0.113.42',
'::ffff:203.0.113.42:1',
'::ffff:203.0.113.42/129',
'::ffff:203.0.113.42/128/1',
])('blocks malformed embedded IPv4 or CIDR: %s', (entry) => {
expect(validateIpEntry(entry).kind).toBe('invalid')
expect(canSubmitIpPolicy('Office', [entry])).toBe(false)
})
})
describe('canSubmitIpPolicy', () => {
@@ -37,7 +37,15 @@ function parseIpv4(address: string): 'valid' | 'leadingZeros' | 'octetRange' | '
}
function parseIpv6(address: string): boolean {
if (address.includes('.')) return false
if (address.includes('.')) {
const lastColon = address.lastIndexOf(':')
const embeddedIpv4 = address.slice(lastColon + 1)
if (parseIpv4(embeddedIpv4) !== 'valid') return false
const octets = embeddedIpv4.split('.').map(Number)
const high = ((octets[0] ?? 0) * 256 + (octets[1] ?? 0)).toString(16)
const low = ((octets[2] ?? 0) * 256 + (octets[3] ?? 0)).toString(16)
address = `${address.slice(0, lastColon + 1)}${high}:${low}`
}
const sides = address.split('::')
if (sides.length > 2) return false
@@ -138,7 +146,6 @@ export function splitPolicySummary(addresses: readonly string[]): {
if (addresses.length <= 2) return { listed: addresses, moreCount: 0 }
return { listed: addresses.slice(0, 2), moreCount: addresses.length - 2 }
}
function ipv4ToInt(ip: string): number | null {
const parts = ip.split('.')
if (parts.length !== 4) return null