import { z } from 'zod'; /** * The map hosts an operator adds to the ones TREK ships, through * MAP_EXTRA_CONNECT_HOSTS: a self-hosted tile server, a vector style on the * operator's own domain. * * A GL map reads its tiles and its style with fetch, so a host has to be in the * browser's connect-src; Leaflet loads raster tiles as images, which img-src * already allows from every https host. The server adds each entry to its * Content-Security-Policy and hands the same list to the client in the app * config, so the map only asks a GL renderer to draw what the policy lets it * fetch. One grammar for both sides keeps the two lists from disagreeing. * * An entry is a host name, `tiles.example.com`, optionally with a port, * `tiles.example.com:8443`, or a wildcard over its subdomains, * `*.tiles.example.com`. https is implied: no scheme, path, query or user. * The host needs at least two labels and a wildcard at least two below it, so * neither `localhost` nor `*.com` can open the policy to a whole namespace. */ /** The longest host name DNS can carry. */ const MAX_HOST_LENGTH = 253; const LABEL = /^[a-z0-9-]{1,63}$/; const PORT = /^[1-9]\d{0,4}$/; /** One DNS label: letters, digits and inner hyphens. */ function isLabel(label: string): boolean { return LABEL.test(label) && !label.startsWith('-') && !label.endsWith('-'); } function isPort(port: string): boolean { return PORT.test(port) && Number(port) <= 65535; } /** * Whether `entry` is one host in the canonical spelling the list holds: lower * case, as described above. Read piece by piece rather than with one pattern, * so a long hostile value costs a single pass. */ export function isMapConnectHost(entry: string): boolean { const rest = entry.startsWith('*.') ? entry.slice(2) : entry; const colon = rest.indexOf(':'); const host = colon === -1 ? rest : rest.slice(0, colon); if (colon !== -1 && !isPort(rest.slice(colon + 1))) return false; const labels = host.split('.'); return host.length <= MAX_HOST_LENGTH && labels.length >= 2 && labels.every(isLabel); } /** * MAP_EXTRA_CONNECT_HOSTS read as a list: comma-separated, each entry trimmed * and lower-cased, blank entries skipped. `invalid` names what was refused, * for the boot check; `hosts` is what may be used, so a bad entry allows * nothing and a duplicate is listed once. */ export function parseMapConnectHosts(raw: string | undefined): { hosts: string[]; invalid: string[] } { const hosts = new Set(); const invalid: string[] = []; for (const entry of (raw ?? '').split(',')) { const trimmed = entry.trim(); if (trimmed === '') continue; const host = trimmed.toLowerCase(); if (isMapConnectHost(host)) hosts.add(host); else invalid.push(trimmed); } return { hosts: [...hosts], invalid }; } /** * Whether an https URL's host (`host` or `host:port`, as `URL.host` spells it) * falls under one entry of the grammar above, read the way a CSP host-source * reads it: a wildcard covers every subdomain but never the apex, and an entry * without a port covers only the default one. Lets the client ask the same * question of a tile URL that the browser will ask of the policy. */ export function mapConnectHostAllows(entry: string, urlHost: string): boolean { const [entryHost = '', entryPort = ''] = entry.toLowerCase().split(':'); const [host = '', port = ''] = urlHost.toLowerCase().split(':'); if (entryPort !== (port === '443' ? '' : port)) return false; if (entryHost.startsWith('*.')) return host.endsWith(entryHost.slice(1)); return host === entryHost; } /** One entry of the list, as the app config carries it. */ export const mapConnectHostSchema = z.string().refine(isMapConnectHost, { message: 'Expected a host name such as tiles.example.com, *.tiles.example.com or tiles.example.com:8443', }); /** `map_extra_connect_hosts` in the app config: the hosts a GL map may fetch from beyond the ones TREK ships. */ export const mapExtraConnectHostsSchema = z.array(mapConnectHostSchema); export type MapExtraConnectHosts = z.infer;