mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
feat(shared): host grammar for the operator's own map hosts
MAP_EXTRA_CONNECT_HOSTS lets an operator name tile and style hosts beyond the ones TREK ships. The server adds them to the CSP connect-src and the client will only ask a GL renderer to draw what that list names, so both sides read entries through one grammar: a lower-case host name with at least two labels, an optional port and an optional leading wildcard over two or more labels. No scheme, path or user, so an entry can never widen the policy past a single host family. The parser returns the accepted hosts and the refused entries, for the boot check, and the app config field gets a Zod contract.
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import { isMapConnectHost, 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');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,76 @@
|
||||
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 };
|
||||
}
|
||||
|
||||
/** 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>;
|
||||
Reference in New Issue
Block a user