import { isMapConnectHost, mapConnectHostAllows, mapExtraConnectHostsSchema, parseMapConnectHosts, } from './map-connect-hosts'; import { describe, it, expect } from 'vitest'; describe('isMapConnectHost', () => { it.each([ 'tiles.example.com', 'a.b', 'maps.example.co.uk', '*.tiles.example.com', '*.example.com', 'tiles.example.com:8443', '*.example.com:443', 'tiles-1.example.com', '10.0.0.5', 'tiles.example.com:65535', 'tiles.example.com:1', ])('takes %s', (entry) => { expect(isMapConnectHost(entry)).toBe(true); }); it.each([ ['a bare label', 'localhost'], ['a wildcard over a top-level domain', '*.com'], ['a lone wildcard', '*'], ['a wildcard in the middle', 'tiles.*.example.com'], ['a scheme', 'https://tiles.example.com'], ['a path', 'tiles.example.com/styles'], ['a query', 'tiles.example.com?key=1'], ['credentials', 'user@tiles.example.com'], ['upper case', 'Tiles.Example.com'], ['an empty label', 'tiles..example.com'], ['a trailing dot', 'tiles.example.com.'], ['a leading hyphen', '-tiles.example.com'], ['a trailing hyphen', 'tiles-.example.com'], ['an underscore', 'my_tiles.example.com'], ['port 0', 'tiles.example.com:0'], ['a port past 65535', 'tiles.example.com:65536'], ['a port with a leading zero', 'tiles.example.com:0443'], ['an empty port', 'tiles.example.com:'], ['two ports', 'tiles.example.com:80:81'], ['a label past 63 characters', `${'a'.repeat(64)}.example.com`], ['a host past 253 characters', `${'a.'.repeat(126)}com`], ['an IPv6 address', '[::1]'], ['whitespace', 'tiles .example.com'], ])('refuses %s', (_case, entry) => { expect(isMapConnectHost(entry)).toBe(false); }); it('takes a label of exactly 63 characters', () => { expect(isMapConnectHost(`${'a'.repeat(63)}.example.com`)).toBe(true); }); }); describe('parseMapConnectHosts', () => { it('reads nothing from an unset or blank value', () => { expect(parseMapConnectHosts(undefined)).toEqual({ hosts: [], invalid: [] }); expect(parseMapConnectHosts(' ')).toEqual({ hosts: [], invalid: [] }); }); it('trims and lower-cases each entry, skips blank ones and lists a duplicate once', () => { expect(parseMapConnectHosts(' Tiles.Example.com , ,*.maps.example.org:8443,tiles.example.com,')).toEqual({ hosts: ['tiles.example.com', '*.maps.example.org:8443'], invalid: [], }); }); it('names every refused entry as it was written and keeps it out of the hosts', () => { expect(parseMapConnectHosts('tiles.example.com, https://evil.example.com, *.com')).toEqual({ hosts: ['tiles.example.com'], invalid: ['https://evil.example.com', '*.com'], }); }); }); describe('mapExtraConnectHostsSchema', () => { it('takes the canonical list the server sends', () => { expect(mapExtraConnectHostsSchema.parse(['tiles.example.com', '*.example.org:8443'])).toEqual([ 'tiles.example.com', '*.example.org:8443', ]); expect(mapExtraConnectHostsSchema.parse([])).toEqual([]); }); it('refuses an entry outside the grammar', () => { const result = mapExtraConnectHostsSchema.safeParse(['tiles.example.com', 'https://tiles.example.com']); expect(result.success).toBe(false); expect(result.error?.issues[0]?.message).toContain('tiles.example.com'); }); }); describe('mapConnectHostAllows', () => { it('MAP-HOSTS-ALLOW-001: a plain entry covers its own host on the default port only', () => { expect(mapConnectHostAllows('tiles.example.com', 'tiles.example.com')).toBe(true); expect(mapConnectHostAllows('tiles.example.com', 'tiles.example.com:443')).toBe(true); expect(mapConnectHostAllows('tiles.example.com', 'TILES.example.com')).toBe(true); expect(mapConnectHostAllows('tiles.example.com', 'tiles.example.com:8443')).toBe(false); expect(mapConnectHostAllows('tiles.example.com', 'a.tiles.example.com')).toBe(false); expect(mapConnectHostAllows('tiles.example.com', 'tiles.example.com.evil.test')).toBe(false); }); it('MAP-HOSTS-ALLOW-002: a port in the entry must be the URL port', () => { expect(mapConnectHostAllows('tiles.example.com:8443', 'tiles.example.com:8443')).toBe(true); expect(mapConnectHostAllows('tiles.example.com:8443', 'tiles.example.com')).toBe(false); }); it('MAP-HOSTS-ALLOW-003: a wildcard covers every subdomain and never the apex', () => { expect(mapConnectHostAllows('*.basemaps.cartocdn.com', 'a.basemaps.cartocdn.com')).toBe(true); expect(mapConnectHostAllows('*.basemaps.cartocdn.com', 'x.y.basemaps.cartocdn.com')).toBe(true); expect(mapConnectHostAllows('*.basemaps.cartocdn.com', 'basemaps.cartocdn.com')).toBe(false); expect(mapConnectHostAllows('*.basemaps.cartocdn.com', 'evilbasemaps.cartocdn.com')).toBe(false); }); });