|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockDnsLookup, hostedFlag } = vi.hoisted(() => ({ |
| 7 | + mockDnsLookup: vi.fn(), |
| 8 | + hostedFlag: { value: false }, |
| 9 | +})) |
| 10 | + |
| 11 | +vi.mock('@/lib/core/config/feature-flags', () => ({ |
| 12 | + get isHosted() { |
| 13 | + return hostedFlag.value |
| 14 | + }, |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('dns/promises', () => ({ |
| 18 | + default: { lookup: mockDnsLookup }, |
| 19 | +})) |
| 20 | + |
| 21 | +import { validateConnectServerUrl } from '@/app/api/tools/onepassword/utils' |
| 22 | + |
| 23 | +describe('validateConnectServerUrl', () => { |
| 24 | + beforeEach(() => { |
| 25 | + vi.clearAllMocks() |
| 26 | + hostedFlag.value = false |
| 27 | + }) |
| 28 | + |
| 29 | + it('rejects a non-URL string', async () => { |
| 30 | + await expect(validateConnectServerUrl('not a url')).rejects.toThrow('is not a valid URL') |
| 31 | + }) |
| 32 | + |
| 33 | + describe('hosted deployment', () => { |
| 34 | + beforeEach(() => { |
| 35 | + hostedFlag.value = true |
| 36 | + }) |
| 37 | + |
| 38 | + it.each([ |
| 39 | + ['loopback', 'http://127.0.0.1:8080'], |
| 40 | + ['RFC1918 10.x', 'http://10.0.0.5'], |
| 41 | + ['RFC1918 192.168.x', 'http://192.168.1.1:8443'], |
| 42 | + ['RFC1918 172.16.x', 'http://172.16.0.9'], |
| 43 | + ['link-local metadata', 'http://169.254.169.254'], |
| 44 | + ['IPv4-mapped IPv6 private', 'http://[::ffff:10.0.0.1]'], |
| 45 | + ['IPv6 loopback', 'http://[::1]'], |
| 46 | + ])('blocks %s', async (_label, url) => { |
| 47 | + await expect(validateConnectServerUrl(url)).rejects.toThrow( |
| 48 | + 'cannot point to a private or reserved IP address' |
| 49 | + ) |
| 50 | + }) |
| 51 | + |
| 52 | + it('allows a public IP literal', async () => { |
| 53 | + await expect(validateConnectServerUrl('https://8.8.8.8')).resolves.toBe('8.8.8.8') |
| 54 | + }) |
| 55 | + |
| 56 | + it('blocks a hostname that resolves to a private IP', async () => { |
| 57 | + mockDnsLookup.mockResolvedValue({ address: '10.1.2.3', family: 4 }) |
| 58 | + await expect(validateConnectServerUrl('https://connect.internal')).rejects.toThrow( |
| 59 | + 'cannot point to a private or reserved IP address' |
| 60 | + ) |
| 61 | + }) |
| 62 | + |
| 63 | + it('allows a hostname that resolves to a public IP', async () => { |
| 64 | + mockDnsLookup.mockResolvedValue({ address: '93.184.216.34', family: 4 }) |
| 65 | + await expect(validateConnectServerUrl('https://connect.example.com')).resolves.toBe( |
| 66 | + '93.184.216.34' |
| 67 | + ) |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + describe('self-hosted deployment', () => { |
| 72 | + beforeEach(() => { |
| 73 | + hostedFlag.value = false |
| 74 | + }) |
| 75 | + |
| 76 | + it.each([ |
| 77 | + ['loopback', 'http://127.0.0.1:8080', '127.0.0.1'], |
| 78 | + ['RFC1918 10.x', 'http://10.0.0.5', '10.0.0.5'], |
| 79 | + ['RFC1918 192.168.x', 'http://192.168.1.1:8443', '192.168.1.1'], |
| 80 | + ])('allows %s (private Connect server)', async (_label, url, expected) => { |
| 81 | + await expect(validateConnectServerUrl(url)).resolves.toBe(expected) |
| 82 | + }) |
| 83 | + |
| 84 | + it('still blocks link-local metadata', async () => { |
| 85 | + await expect(validateConnectServerUrl('http://169.254.169.254')).rejects.toThrow( |
| 86 | + 'cannot point to a link-local address' |
| 87 | + ) |
| 88 | + }) |
| 89 | + |
| 90 | + it('still blocks IPv6 link-local', async () => { |
| 91 | + await expect(validateConnectServerUrl('http://[fe80::1]')).rejects.toThrow( |
| 92 | + 'cannot point to a link-local address' |
| 93 | + ) |
| 94 | + }) |
| 95 | + |
| 96 | + it('allows a hostname that resolves to a private IP', async () => { |
| 97 | + mockDnsLookup.mockResolvedValue({ address: '10.1.2.3', family: 4 }) |
| 98 | + await expect(validateConnectServerUrl('https://connect.internal')).resolves.toBe('10.1.2.3') |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + it('rejects when DNS resolution fails', async () => { |
| 103 | + mockDnsLookup.mockRejectedValue(new Error('ENOTFOUND')) |
| 104 | + await expect(validateConnectServerUrl('https://nope.invalid')).rejects.toThrow( |
| 105 | + 'could not be resolved' |
| 106 | + ) |
| 107 | + }) |
| 108 | +}) |
0 commit comments