diff --git a/apps/api/src/routes/__tests__/maptiler.test.ts b/apps/api/src/routes/__tests__/maptiler.test.ts index 66df55134..95a382dd4 100644 --- a/apps/api/src/routes/__tests__/maptiler.test.ts +++ b/apps/api/src/routes/__tests__/maptiler.test.ts @@ -487,6 +487,139 @@ describe("GET /maptiler/*", () => { }); }); +describe("MapTiler key never reaches the client", () => { + const FAKE_KEY = "fake-maptiler-key-for-tests"; + + function stubJson(document: unknown) { + vi.stubEnv("MAPTILER_KEY", FAKE_KEY); + vi.stubEnv("PUBLIC_BASE_URL", "https://api.example.test"); + vi.stubGlobal( + "fetch", + vi.fn(async () => Response.json(document)), + ); + } + + it("rewrites the array form of sprite and keeps each id", async () => { + stubJson({ + version: 8, + sprite: [ + { id: "default", url: `https://api.maptiler.com/maps/bright-v2/sprite?key=${FAKE_KEY}` }, + ], + sources: {}, + layers: [], + }); + + const res = await app.inject("/maptiler/maps/bright-v2/style.json"); + + expect(res.statusCode).toBe(200); + expect(res.json().sprite).toEqual([ + { id: "default", url: "https://api.example.test/api/maptiler/maps/bright-v2/sprite" }, + ]); + expect(res.body).not.toContain(FAKE_KEY); + }); + + it("strips the key from MapTiler URLs in fields the rewrite does not know", async () => { + stubJson({ + version: 8, + metadata: { extra: `https://cdn.maptiler.com/x/{z}/y.json?lang=en&key=${FAKE_KEY}#top` }, + sources: { + s: { type: "geojson", data: `https://api.maptiler.com/data/abc.geojson?key=${FAKE_KEY}` }, + }, + layers: [], + }); + + const res = await app.inject("/maptiler/maps/bright-v2/style.json"); + + expect(res.statusCode).toBe(200); + expect(res.headers["cache-control"]).toContain("public"); + const body = res.json(); + expect(body.metadata.extra).toBe("https://cdn.maptiler.com/x/{z}/y.json?lang=en#top"); + expect(body.sources.s.data).toBe("https://api.maptiler.com/data/abc.geojson"); + expect(res.body).not.toContain(FAKE_KEY); + }); + + it("strips the configured key on other hosts but leaves unrelated key parameters", async () => { + stubJson({ + tiles: [`https://api.maptiler.com/tiles/v3/{z}/{x}/{y}.pbf?key=${FAKE_KEY}`], + legend: `https://example.org/l.png?key=${FAKE_KEY}&size=2`, + data: ["https://example.org/d.json?key=someone-elses-value"], + }); + + const res = await app.inject("/maptiler/tiles/v3/tiles.json"); + + expect(res.statusCode).toBe(200); + expect(res.json()).toMatchObject({ + tiles: ["https://api.example.test/api/maptiler/tiles/v3/{z}/{x}/{y}.pbf"], + legend: "https://example.org/l.png?size=2", + data: ["https://example.org/d.json?key=someone-elses-value"], + }); + expect(res.body).not.toContain(FAKE_KEY); + }); + + it("refuses a document that still contains the key, without logging or returning it", async () => { + vi.stubEnv("MAPTILER_KEY", FAKE_KEY); + vi.stubGlobal( + "fetch", + vi.fn(async () => + Response.json({ + tiles: ["https://api.maptiler.com/tiles/v3/{z}/{x}/{y}.pbf"], + attribution: `MapTiler`, + }), + ), + ); + const chunks: string[] = []; + const stream = new Writable({ + write(chunk, _encoding, callback) { + chunks.push(String(chunk)); + callback(); + }, + }); + const loggerApp = Fastify( + controlledRequestLoggingOptions(pino(createSafePinoOptions("info"), stream)), + ); + const { maptilerRoute } = await import("../maptiler.js"); + await loggerApp.register(maptilerRoute); + await loggerApp.ready(); + + const res = await loggerApp.inject("/maptiler/tiles/v3/tiles.json"); + await loggerApp.close(); + + expect(res.statusCode).toBe(502); + expect(res.headers["cache-control"]).toBe("no-store"); + expect(res.json()).toEqual({ message: "MapTiler provider returned an invalid response" }); + expect(res.body).not.toContain(FAKE_KEY); + const logs = chunks.join(""); + expect(logs).not.toContain(FAKE_KEY); + const warning = logs + .split("\n") + .filter(Boolean) + .map((line) => JSON.parse(line) as Record) + .find((record) => record.msg === "MapTiler proxy response rejected"); + expect(warning).toMatchObject({ errorClass: "MaptilerKeyLeakError" }); + }); + + it("refuses sprite JSON that contains the key", async () => { + stubJson({ icon: { x: 0, y: 0, width: 1, height: 1, note: FAKE_KEY } }); + + const res = await app.inject("/maptiler/maps/bright-v2/sprite.json"); + + expect(res.statusCode).toBe(502); + expect(res.headers["cache-control"]).toBe("no-store"); + expect(res.body).not.toContain(FAKE_KEY); + }); + + it("serves ordinary sprite JSON unchanged", async () => { + const sprite = { icon: { x: 0, y: 0, width: 16, height: 16, pixelRatio: 1 } }; + stubJson(sprite); + + const res = await app.inject("/maptiler/maps/bright-v2/sprite@2x.json"); + + expect(res.statusCode).toBe(200); + expect(res.headers["content-type"]).toContain("application/json"); + expect(res.json()).toEqual(sprite); + }); +}); + describe("admin-configured MapTiler credentials", () => { it("authenticates upstream requests with the effective database key without returning it", async () => { vi.stubEnv("MAPTILER_KEY", ""); diff --git a/apps/api/src/routes/maptiler.ts b/apps/api/src/routes/maptiler.ts index 1971c4fbb..b210cffca 100644 --- a/apps/api/src/routes/maptiler.ts +++ b/apps/api/src/routes/maptiler.ts @@ -25,10 +25,10 @@ import { const MAPTILER_ORIGIN = "https://api.maptiler.com"; const MAPTILER_MAX_ZOOM = 22; const MAPTILER_TIMEOUT_MS = 15_000; -const JSON_MEDIA_TYPES = ["application/json"] as const; const STYLE_RE = /^maps\/[a-z0-9_-]+\/style\.json$/i; const SPRITE_RE = /^maps\/[a-z0-9_-]+\/sprite(?:(?:@2x)?\.(?:json|png))?$/i; +const SPRITE_JSON_RE = /^maps\/[a-z0-9_-]+\/sprite(?:@2x)?\.json$/i; const TILEJSON_RE = /^tiles\/[a-z0-9_-]+\/tiles\.json$/i; const TILE_RE = /^tiles\/[a-z0-9_-]+\/(?\d{1,2})\/(?\d+)\/(?\d+)\.(?pbf|mvt|png|jpe?g|webp)$/i; @@ -84,13 +84,6 @@ function binaryResponseOptions(path: string): { label: "MapTiler vector asset", }; } - if (extension === "json") { - return { - maxBytes: MAX_VECTOR_TILE_BYTES, - allowedContentTypes: JSON_MEDIA_TYPES, - label: "MapTiler sprite JSON", - }; - } if (extension === "jpg" || extension === "jpeg") { return { maxBytes: MAX_RASTER_TILE_BYTES, @@ -207,6 +200,13 @@ function rewriteStyle(style: JsonRecord, base: string): JsonRecord { } if (typeof rewritten.sprite === "string") { rewritten.sprite = rewriteMaptilerUrl(rewritten.sprite, base); + } else if (Array.isArray(rewritten.sprite)) { + // The style spec also allows a list of `{ id, url }` sprite sheets. + rewritten.sprite = rewritten.sprite.map((entry) => + entry && typeof entry === "object" && typeof (entry as JsonRecord).url === "string" + ? { ...entry, url: rewriteMaptilerUrl((entry as JsonRecord).url as string, base) } + : entry, + ); } if (rewritten.sources && typeof rewritten.sources === "object") { @@ -232,6 +232,91 @@ function rewriteStyle(style: JsonRecord, base: string): JsonRecord { return rewritten; } +const ABSOLUTE_URL_RE = /^(?:https?:)?\/\//i; + +function isMaptilerHost(hostname: string): boolean { + const host = hostname.toLowerCase(); + return host === "maptiler.com" || host.endsWith(".maptiler.com"); +} + +function decodeQueryComponent(value: string): string { + try { + return decodeURIComponent(value.replace(/\+/g, " ")); + } catch { + return value; + } +} + +/** + * Remove the `key` query parameter from a URL string that either points at a + * MapTiler host or carries the configured key. The query is edited as text + * because `URL` and `URLSearchParams` re-encode `{z}`-style template + * placeholders, which would break tile and glyph templates. + */ +function stripKeyParam(value: string, key: string): string { + if (!ABSOLUTE_URL_RE.test(value)) return value; + let parsed: URL; + try { + parsed = new URL(value.startsWith("//") ? `https:${value}` : value); + } catch { + return value; + } + const onMaptiler = isMaptilerHost(parsed.hostname); + const hashIndex = value.indexOf("#"); + const beforeHash = hashIndex === -1 ? value : value.slice(0, hashIndex); + const hash = hashIndex === -1 ? "" : value.slice(hashIndex); + const queryIndex = beforeHash.indexOf("?"); + if (queryIndex === -1) return value; + + const parts = beforeHash.slice(queryIndex + 1).split("&"); + const kept = parts.filter((part) => { + const separator = part.indexOf("="); + const name = decodeQueryComponent(separator === -1 ? part : part.slice(0, separator)); + if (name !== "key") return true; + if (onMaptiler) return false; + return decodeQueryComponent(separator === -1 ? "" : part.slice(separator + 1)) !== key; + }); + if (kept.length === parts.length) return value; + const query = kept.join("&"); + return `${beforeHash.slice(0, queryIndex)}${query ? `?${query}` : ""}${hash}`; +} + +/** + * Walk a whole JSON document and strip the key from every URL string, so + * fields the targeted rewrite does not know about cannot pass it through. + */ +function stripMaptilerKey(value: unknown, key: string, counter: { stripped: number }): unknown { + if (typeof value === "string") { + const stripped = stripKeyParam(value, key); + if (stripped !== value) counter.stripped += 1; + return stripped; + } + if (Array.isArray(value)) return value.map((item) => stripMaptilerKey(item, key, counter)); + if (value && typeof value === "object") { + return Object.fromEntries( + Object.entries(value).map(([name, item]) => [name, stripMaptilerKey(item, key, counter)]), + ); + } + return value; +} + +class MaptilerKeyLeakError extends Error { + override name = "MaptilerKeyLeakError"; +} + +/** + * Serialize a response document and refuse it if the key is still present + * anywhere, for example inside HTML or a string that is not a URL. + */ +function serializeKeySafe(value: unknown, key: string): string { + const serialized = JSON.stringify(value); + const forms = [key, encodeURIComponent(key), JSON.stringify(key).slice(1, -1)]; + if (forms.some((form) => serialized.includes(form))) { + throw new MaptilerKeyLeakError("MapTiler response contained the API key"); + } + return serialized; +} + function getRequestedMaptilerPath(req: FastifyRequest): string | null { const pathname = new URL(req.url, "http://openmapx.local").pathname; const marker = "/maptiler/"; @@ -310,6 +395,20 @@ export const maptilerRoute: FastifyPluginAsync<{ budget?: ProviderDailyBudget | reply.header("Cache-Control", "public, max-age=604800, s-maxage=604800"); reply.header("Cross-Origin-Resource-Policy", "cross-origin"); + // Every JSON body is served through here: strip the key from all URL + // strings, then refuse the document if the key survives anywhere. + const sendKeySafeJson = (document: unknown) => { + const counter = { stripped: 0 }; + const body = serializeKeySafe(stripMaptilerKey(document, key, counter), key); + if (counter.stripped > 0) { + req.log.warn( + { upstream: summarizeExternalUrl(upstreamUrl.href), strippedCount: counter.stripped }, + "MapTiler key stripped from URLs the proxy cannot rewrite", + ); + } + return reply.type("application/json").send(body); + }; + try { const base = proxyBase(req); if (STYLE_RE.test(path)) { @@ -319,7 +418,7 @@ export const maptilerRoute: FastifyPluginAsync<{ budget?: ProviderDailyBudget | label: "MapTiler style JSON", signal, }); - return reply.type("application/json").send(rewriteStyle(style, base)); + return sendKeySafeJson(rewriteStyle(style, base)); } if (TILEJSON_RE.test(path)) { requireMaptilerJsonContentType(upstream, "MapTiler TileJSON"); @@ -328,7 +427,16 @@ export const maptilerRoute: FastifyPluginAsync<{ budget?: ProviderDailyBudget | label: "MapTiler TileJSON", signal, }); - return reply.type("application/json").send(rewriteTileJson(tileJson, base)); + return sendKeySafeJson(rewriteTileJson(tileJson, base)); + } + if (SPRITE_JSON_RE.test(path)) { + requireMaptilerJsonContentType(upstream, "MapTiler sprite JSON"); + const sprite = await readBoundedJsonResponse(upstream, { + maxBytes: MAX_VECTOR_TILE_BYTES, + label: "MapTiler sprite JSON", + signal, + }); + return sendKeySafeJson(sprite); } const proxy = createBoundedBinaryProxyStream(upstream, binaryResponseOptions(path));