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