Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion apps/geolibre-desktop/src/lib/data-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ export interface ServiceUrlParameter {
styleUrl: string | null;
}

/** The service kinds addressed by a `{z}/{x}/{y}` tile template. */
const TILE_TEMPLATE_KINDS = new Set(["xyz", "wmts", "ogc-vector-tiles"]);

export function serviceUrlParameter(search: string): ServiceUrlParameter | null {
const params = new URLSearchParams(search);
const kind = params.get("add");
const rawUrl = params.get("serviceUrl");
const url = httpUrl(rawUrl)?.replace(/%7B/gi, "{").replace(/%7D/gi, "}") ?? null;
// Only a tile template carries braces to restore. Rewriting them for every
// kind would corrupt a WMS or ArcGIS URL whose query legitimately encodes a
// brace, in a signed parameter or token, into one the server never sent.
const parsed = httpUrl(rawUrl);
const url =
parsed && kind && TILE_TEMPLATE_KINDS.has(kind)
? parsed.replace(/%7B/gi, "{").replace(/%7D/gi, "}")
: parsed;
if (!kind || !SERVICE_KINDS.has(kind) || !url) return null;
return {
kind,
Expand Down
Binary file modified extensions/geolibre-chrome/background.mjs
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/data-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ describe("serviceUrlParameter", () => {
);
});

it("restores tile-template braces only for the kinds that use them", () => {
// A WMS token that legitimately encodes a brace must reach the server as it
// was signed, not as a literal brace.
assert.equal(
serviceUrlParameter(
"?add=wms&serviceUrl=https%3A%2F%2Fmaps.example.com%2Fwms%3Ftoken%3Da%257Bb%257Dc",
)?.url,
"https://maps.example.com/wms?token=a%7Bb%7Dc",
);
assert.equal(
serviceUrlParameter(
"?add=wmts&serviceUrl=https%3A%2F%2Ftiles.example.com%2Fwmts%3FTileMatrix%3D%257Bz%257D",
)?.url,
"https://tiles.example.com/wmts?TileMatrix={z}",
);
});

it("ignores a blank layer and a non-web style link", () => {
assert.deepEqual(
serviceUrlParameter(
Expand Down
Loading