From dc008ad7a55c230ece2eb36bed76d2b9ecc843cb Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 30 Nov 2026 15:00:09 +0100 Subject: [PATCH] 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. --- shared/src/maps/map-connect-hosts.spec.ts | 30 ++++++++++++++++++++++- shared/src/maps/map-connect-hosts.ts | 15 ++++++++++++ shared/src/maps/map-defaults.spec.ts | 14 ++++++++++- shared/src/maps/map-defaults.ts | 13 ++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/shared/src/maps/map-connect-hosts.spec.ts b/shared/src/maps/map-connect-hosts.spec.ts index a2c6ff3ce..210c60abf 100644 --- a/shared/src/maps/map-connect-hosts.spec.ts +++ b/shared/src/maps/map-connect-hosts.spec.ts @@ -1,4 +1,9 @@ -import { isMapConnectHost, mapExtraConnectHostsSchema, parseMapConnectHosts } from './map-connect-hosts'; +import { + isMapConnectHost, + mapConnectHostAllows, + mapExtraConnectHostsSchema, + parseMapConnectHosts, +} from './map-connect-hosts'; import { describe, it, expect } from 'vitest'; @@ -88,3 +93,26 @@ describe('mapExtraConnectHostsSchema', () => { 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); + }); +}); diff --git a/shared/src/maps/map-connect-hosts.ts b/shared/src/maps/map-connect-hosts.ts index a8b199190..42a4f34e5 100644 --- a/shared/src/maps/map-connect-hosts.ts +++ b/shared/src/maps/map-connect-hosts.ts @@ -66,6 +66,21 @@ export function parseMapConnectHosts(raw: string | undefined): { hosts: string[] 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', diff --git a/shared/src/maps/map-defaults.spec.ts b/shared/src/maps/map-defaults.spec.ts index b25a13ca8..03f8b9908 100644 --- a/shared/src/maps/map-defaults.spec.ts +++ b/shared/src/maps/map-defaults.spec.ts @@ -1,5 +1,11 @@ import { isMapConnectHost } from './map-connect-hosts'; -import { OPENFREEMAP_DEFAULT_STYLE, OPENFREEMAP_HOST, RASTER_TILE_HOSTS } from './map-defaults'; +import { + MAPLIBRE_FROM_TEMPLATE, + MAPLIBRE_OFM_AUTO, + OPENFREEMAP_DEFAULT_STYLE, + OPENFREEMAP_HOST, + RASTER_TILE_HOSTS, +} from './map-defaults'; import { describe, expect, it } from 'vitest'; @@ -16,4 +22,10 @@ describe('map defaults', () => { expect(OPENFREEMAP_DEFAULT_STYLE).toBe('https://tiles.openfreemap.org/styles/liberty'); expect(new URL(OPENFREEMAP_DEFAULT_STYLE).host).toBe(OPENFREEMAP_HOST); }); + + it('MAP-DEFAULTS-004: the MapLibre keywords are distinct and can never be read as a style URL', () => { + expect(MAPLIBRE_FROM_TEMPLATE).toBe('trek:template'); + expect(MAPLIBRE_OFM_AUTO).toBe('trek:ofm-auto'); + for (const keyword of [MAPLIBRE_FROM_TEMPLATE, MAPLIBRE_OFM_AUTO]) expect(keyword).not.toMatch(/^(https?|mapbox):/); + }); }); diff --git a/shared/src/maps/map-defaults.ts b/shared/src/maps/map-defaults.ts index 4fd75e401..58531b9f9 100644 --- a/shared/src/maps/map-defaults.ts +++ b/shared/src/maps/map-defaults.ts @@ -15,6 +15,19 @@ export const OPENFREEMAP_HOST = 'tiles.openfreemap.org'; /** The style a MapLibre map draws when nobody chose one. */ export const OPENFREEMAP_DEFAULT_STYLE = `https://${OPENFREEMAP_HOST}/styles/liberty`; +/** + * Values of the `maplibre_style` setting that name no style document. No row + * held either before they were introduced, so neither changes what a saved + * setting meant, and a client that does not know them reads them as a foreign + * style and draws the default. + */ + +/** MapLibre draws the user's map template (`map_tile_url`), the one Leaflet draws. */ +export const MAPLIBRE_FROM_TEMPLATE = 'trek:template'; + +/** MapLibre draws OpenFreeMap Liberty in the light theme and OpenFreeMap Dark in the dark one. */ +export const MAPLIBRE_OFM_AUTO = 'trek:ofm-auto'; + /** * The raster tile hosts TREK ships, in the order the policy lists them. *