Files
TREK/shared/src/maps/map-connect-hosts.spec.ts
T
Maurice dc008ad7a5 feat(shared): keywords for MapLibre's template and theme styles
The maplibre_style slot gains two values that name no style document:
trek:template, which has MapLibre draw the user's map template the way
Leaflet does, and trek:ofm-auto, OpenFreeMap Liberty in the light theme
and OpenFreeMap Dark in the dark one. No stored row holds either, so no
saved setting changes its meaning, and a client that predates them reads
them as a foreign style and draws the default.

mapConnectHostAllows reads a host-source entry the way the browser reads
the policy (a wildcard never covers its apex, no port means the default
one), so the client can ask whether a GL map may fetch a tile before it
tries.
2026-11-30 16:22:52 +01:00

119 lines
4.8 KiB
TypeScript

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);
});
});