mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
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:
@@ -1,3 +1,9 @@
|
||||
import { OPENFREEMAP_DEFAULT_STYLE } from '@trek/shared'
|
||||
|
||||
// The server builds its CSP from the same shared map defaults, so the style a
|
||||
// MapLibre map falls back to is always one the policy lets it fetch.
|
||||
export { OPENFREEMAP_DEFAULT_STYLE }
|
||||
|
||||
export type GlMapProvider = 'mapbox-gl' | 'maplibre-gl'
|
||||
|
||||
export interface GlStylePreset {
|
||||
@@ -7,7 +13,6 @@ export interface GlStylePreset {
|
||||
}
|
||||
|
||||
export const MAPBOX_DEFAULT_STYLE = 'mapbox://styles/mapbox/standard'
|
||||
export const OPENFREEMAP_DEFAULT_STYLE = 'https://tiles.openfreemap.org/styles/liberty'
|
||||
|
||||
export const MAPBOX_STYLE_PRESETS: GlStylePreset[] = [
|
||||
{ name: 'Mapbox Standard', url: MAPBOX_DEFAULT_STYLE, tags: ['3D', 'Apple-like'] },
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
} from '../nest/common/request-correlation';
|
||||
import { isSameHostOrigin } from '../nest/common/same-origin';
|
||||
import type { ConfigType } from '@nestjs/config';
|
||||
import { OPENFREEMAP_HOST, RASTER_TILE_HOSTS } from '@trek/shared';
|
||||
|
||||
import compression from 'compression';
|
||||
import cookieParser from 'cookie-parser';
|
||||
@@ -298,55 +299,10 @@ export function applyGlobalMiddleware(
|
||||
'https://api.openweathermap.org',
|
||||
'https://en.wikipedia.org',
|
||||
'https://commons.wikimedia.org',
|
||||
// Both forms here too: 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).
|
||||
'https://basemaps.cartocdn.com',
|
||||
'https://*.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.
|
||||
'https://tile.openstreetmap.org',
|
||||
'https://*.tile.openstreetmap.org',
|
||||
// The other two raster presets TREK ships. `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). routing.openstreetmap.de below is a different host.
|
||||
'https://tile.openstreetmap.de',
|
||||
'https://tiles.stadiamaps.com',
|
||||
// OpenTopoMap is the key-free raster layer offered only by 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 CSP gap above.
|
||||
'https://tile.opentopomap.org',
|
||||
'https://*.tile.opentopomap.org',
|
||||
// The imagery host, for the same reason and one more. Leaflet fetches a tile
|
||||
// as an <img>, which img-src's blanket `https:` waves through, so the satellite
|
||||
// view worked on Leaflet with this host missing. A GL map reads the raster
|
||||
// through fetch to hand it to WebGL, and the prefetch does too, so both were
|
||||
// refused here while nothing in the app could see it: the switch flipped, the
|
||||
// layer went on, and no tile ever arrived (#2307).
|
||||
'https://server.arcgisonline.com',
|
||||
// OpenSeaMap's transparent seamark layer, offered only to a paddle tour in the
|
||||
// Tours planner (PAD-10). Leaflet loads it as an <img>; the GL map reads it
|
||||
// through fetch and needs it here.
|
||||
'https://tiles.openseamap.org',
|
||||
// OpenSnowMap's transparent piste layer (SKI-01), offered in the Tours planner with
|
||||
// no tour open and for a ski tour; the GL map reads it through fetch.
|
||||
'https://tiles.opensnowmap.org',
|
||||
// OpenHikingMap, the hiking base map of the Tours planner (MAP-01), loaded only
|
||||
// while a user shows it and the admin allows it; GL reads it through fetch.
|
||||
'https://tile.openmaps.fr',
|
||||
// CyclOSM, the cycling base map of a bike tour in the Tours planner (BIK-06), on the
|
||||
// same terms; its three shards are named, so no wildcard opens the whole domain.
|
||||
'https://a.tile-cyclosm.openstreetmap.fr',
|
||||
'https://b.tile-cyclosm.openstreetmap.fr',
|
||||
'https://c.tile-cyclosm.openstreetmap.fr',
|
||||
// Amap's raster tiles, for an install whose users are in China, for the
|
||||
// same reason as the hosts above (#2180). Road (webrd01..04) and
|
||||
// satellite (webst01..04) are numbered shards of one domain, so the
|
||||
// wildcard is the whole list.
|
||||
'https://*.is.autonavi.com',
|
||||
// The raster tile hosts TREK ships, from the list the client reads too, so the
|
||||
// policy and the renderer choice agree (shared/src/maps/map-defaults.ts says
|
||||
// why each one is there).
|
||||
...RASTER_TILE_HOSTS.map((host) => `https://${host}`),
|
||||
'https://unpkg.com',
|
||||
'https://open-meteo.com',
|
||||
'https://api.open-meteo.com',
|
||||
@@ -363,7 +319,7 @@ export function applyGlobalMiddleware(
|
||||
'https://api.mapbox.com',
|
||||
'https://*.tiles.mapbox.com',
|
||||
'https://events.mapbox.com',
|
||||
'https://tiles.openfreemap.org',
|
||||
`https://${OPENFREEMAP_HOST}`,
|
||||
// A self-hosted routing engine, when the instance has one configured. Without
|
||||
// this the browser blocks it silently: no error the app can catch, just legs
|
||||
// that never route (#1797).
|
||||
|
||||
@@ -305,3 +305,57 @@ describe("connect-src with the operator's own map hosts (MAP_EXTRA_CONNECT_HOSTS
|
||||
expect(sources).not.toContain("'unsafe-inline'");
|
||||
});
|
||||
});
|
||||
|
||||
describe('connect-src as shipped', () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it('CSP-SHIPPED-001: lists exactly the shipped sources, in the order they always had', async () => {
|
||||
// The tile hosts come from the list the client shares (shared/src/maps/map-defaults.ts);
|
||||
// moving them there must not change one byte of the policy an install sends.
|
||||
vi.stubEnv('MAP_EXTRA_CONNECT_HOSTS', '');
|
||||
expect((await connectSrcSources()).join(' ')).toBe(
|
||||
[
|
||||
"'self'",
|
||||
'ws:',
|
||||
'wss:',
|
||||
'https://nominatim.openstreetmap.org',
|
||||
'https://elevation-tiles-prod.s3.amazonaws.com',
|
||||
'https://overpass-api.de',
|
||||
'https://places.googleapis.com',
|
||||
'https://api.openweathermap.org',
|
||||
'https://en.wikipedia.org',
|
||||
'https://commons.wikimedia.org',
|
||||
'https://basemaps.cartocdn.com',
|
||||
'https://*.basemaps.cartocdn.com',
|
||||
'https://tile.openstreetmap.org',
|
||||
'https://*.tile.openstreetmap.org',
|
||||
'https://tile.openstreetmap.de',
|
||||
'https://tiles.stadiamaps.com',
|
||||
'https://tile.opentopomap.org',
|
||||
'https://*.tile.opentopomap.org',
|
||||
'https://server.arcgisonline.com',
|
||||
'https://tiles.openseamap.org',
|
||||
'https://tiles.opensnowmap.org',
|
||||
'https://tile.openmaps.fr',
|
||||
'https://a.tile-cyclosm.openstreetmap.fr',
|
||||
'https://b.tile-cyclosm.openstreetmap.fr',
|
||||
'https://c.tile-cyclosm.openstreetmap.fr',
|
||||
'https://*.is.autonavi.com',
|
||||
'https://unpkg.com',
|
||||
'https://open-meteo.com',
|
||||
'https://api.open-meteo.com',
|
||||
'https://geocoding-api.open-meteo.com',
|
||||
'https://api.frankfurter.dev',
|
||||
'https://router.project-osrm.org/route/v1/',
|
||||
'https://routing.openstreetmap.de/',
|
||||
'https://valhalla1.openstreetmap.de',
|
||||
'https://api.mapbox.com',
|
||||
'https://*.tiles.mapbox.com',
|
||||
'https://events.mapbox.com',
|
||||
'https://tiles.openfreemap.org',
|
||||
].join(' '),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -27,6 +27,7 @@ export * from './config/config.schema';
|
||||
export * from './system-notice/system-notice.schema';
|
||||
export * from './maps/maps.schema';
|
||||
export * from './maps/map-connect-hosts';
|
||||
export * from './maps/map-defaults';
|
||||
export * from './maps/place-shadow.schema';
|
||||
export * from './maps/route-usage.schema';
|
||||
export * from './category/category.schema';
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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',
|
||||
];
|
||||
Reference in New Issue
Block a user