diff --git a/web/app/components/header/account-setting/ip-policies-page/__tests__/policy-dialog.spec.tsx b/web/app/components/header/account-setting/ip-policies-page/__tests__/policy-dialog.spec.tsx index d139e3a0a1b..15c17c3b606 100644 --- a/web/app/components/header/account-setting/ip-policies-page/__tests__/policy-dialog.spec.tsx +++ b/web/app/components/header/account-setting/ip-policies-page/__tests__/policy-dialog.spec.tsx @@ -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( + , + ) + await user.click(screen.getByRole('button', { name: mode === 'create' ? 'Create' : 'Save' })) + expect(onSubmit).toHaveBeenCalledWith({ + name: 'Office', + allowed_cidrs: ['::ffff:203.0.113.42/128'], + }) + }, + ) +}) diff --git a/web/app/components/header/account-setting/ip-policies-page/__tests__/validate-ip-entry.spec.ts b/web/app/components/header/account-setting/ip-policies-page/__tests__/validate-ip-entry.spec.ts index 7013432893b..c9256cde97a 100644 --- a/web/app/components/header/account-setting/ip-policies-page/__tests__/validate-ip-entry.spec.ts +++ b/web/app/components/header/account-setting/ip-policies-page/__tests__/validate-ip-entry.spec.ts @@ -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', () => { diff --git a/web/app/components/header/account-setting/ip-policies-page/validate-ip-entry.ts b/web/app/components/header/account-setting/ip-policies-page/validate-ip-entry.ts index fdcc592e013..6885d52c1a9 100644 --- a/web/app/components/header/account-setting/ip-policies-page/validate-ip-entry.ts +++ b/web/app/components/header/account-setting/ip-policies-page/validate-ip-entry.ts @@ -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