Files
TREK/shared/src/maps/map-connect-hosts.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

92 lines
4.0 KiB
TypeScript

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<string>();
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<typeof mapExtraConnectHostsSchema>;