feat(maps): one shared list of the shipped tile hosts for the CSP and the client

A GL map fetches its style and its tiles, so it can only draw from a host
the server's connect-src names. The raster presets and OpenFreeMap were
written out in the middleware while the client kept its own idea of them,
and nothing held the two together.

The hosts, their reasons and the default OpenFreeMap style now live in
shared/src/maps/map-defaults.ts. The middleware builds those sources from
the list, and the client takes the default style from it, so the renderer
choice the next steps add reads the same hosts the policy allows.

The policy an install sends stays byte for byte the same: a test pins the
whole connect-src in its order.
This commit is contained in:
Maurice
2026-11-30 14:33:05 +01:00
parent b27b3f6563
commit d8a93af2f5
6 changed files with 150 additions and 51 deletions
+19
View File
@@ -0,0 +1,19 @@
import { isMapConnectHost } from './map-connect-hosts';
import { OPENFREEMAP_DEFAULT_STYLE, OPENFREEMAP_HOST, RASTER_TILE_HOSTS } from './map-defaults';
import { describe, expect, it } from 'vitest';
describe('map defaults', () => {
it('MAP-DEFAULTS-001: every shipped host reads in the grammar the operator list uses', () => {
for (const host of [...RASTER_TILE_HOSTS, OPENFREEMAP_HOST]) expect(isMapConnectHost(host), host).toBe(true);
});
it('MAP-DEFAULTS-002: lists each raster host once', () => {
expect(new Set(RASTER_TILE_HOSTS).size).toBe(RASTER_TILE_HOSTS.length);
});
it('MAP-DEFAULTS-003: the default style is OpenFreeMap Liberty on the shared host', () => {
expect(OPENFREEMAP_DEFAULT_STYLE).toBe('https://tiles.openfreemap.org/styles/liberty');
expect(new URL(OPENFREEMAP_DEFAULT_STYLE).host).toBe(OPENFREEMAP_HOST);
});
});
+64
View File
@@ -0,0 +1,64 @@
/**
* The map hosts and defaults the client and the server must agree on.
*
* A GL map reads its style and its tiles through fetch, so every host it may
* draw from has to be in the server's Content-Security-Policy connect-src, and
* the client may only ask a GL renderer for what that policy allows. Both sides
* read the hosts from here, so the policy and the renderer choice cannot drift
* apart. Entries are host names in the grammar of map-connect-hosts.ts (https
* implied); the server prefixes the scheme.
*/
/** OpenFreeMap: the styles, vector tiles, sprites and glyphs of the default basemap. */
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`;
/**
* The raster tile hosts TREK ships, in the order the policy lists them.
*
* Leaflet loads a tile as an image, which img-src allows from every https host,
* so a host missing here still drew there. A GL map and the offline prefetch
* fetch the tile instead, and both are refused without the host in connect-src.
*/
export const RASTER_TILE_HOSTS: readonly string[] = [
// Both forms: CARTO documents the apex host on its key page, so that is the
// template users paste in, and the {s} sharded form is what TREK ships (#2054).
'basemaps.cartocdn.com',
'*.basemaps.cartocdn.com',
// Both forms: a CSP wildcard host never matches the apex, and OSM serves
// everything from the bare tile.openstreetmap.org since it retired the
// a/b/c/d shards (#1733). The sharded hosts stay listed for tile templates
// users saved before that.
'tile.openstreetmap.org',
'*.tile.openstreetmap.org',
// The other two raster presets. `mode: 'no-cors'` relaxes CORS, not CSP, so
// without these the tile prefetch is refused in the document and never
// reaches the Service Worker that would cache it (#2180).
'tile.openstreetmap.de',
'tiles.stadiamaps.com',
// OpenTopoMap, the key-free raster layer of the Tours planner. Leaflet uses its
// a/b/c shards; the apex is named too so a future unsharded template does not
// repeat the OSM gap above.
'tile.opentopomap.org',
'*.tile.opentopomap.org',
// The imagery host. The satellite view worked on Leaflet with this host missing,
// while a GL map and the prefetch were refused and no tile ever arrived (#2307).
'server.arcgisonline.com',
// OpenSeaMap's transparent seamark layer, offered only to a paddle tour (PAD-10).
'tiles.openseamap.org',
// OpenSnowMap's transparent piste layer (SKI-01).
'tiles.opensnowmap.org',
// OpenHikingMap, the hiking base map of the Tours planner (MAP-01).
'tile.openmaps.fr',
// CyclOSM, the cycling base map of a bike tour (BIK-06); its three shards are
// named, so no wildcard opens the whole domain.
'a.tile-cyclosm.openstreetmap.fr',
'b.tile-cyclosm.openstreetmap.fr',
'c.tile-cyclosm.openstreetmap.fr',
// Amap's raster tiles, for an install whose users are in China. Road
// (webrd01..04) and satellite (webst01..04) are numbered shards of one
// domain, so the wildcard is the whole list.
'*.is.autonavi.com',
];