|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { describe, it } from "node:test"; |
| 3 | +import { |
| 4 | + MAX_REDIRECT_HOPS, |
| 5 | + isAllowedUpstreamUrl, |
| 6 | + proxyViewerRequest, |
| 7 | + sanitizeViewerPath, |
| 8 | +} from "../workers/viewer/src/proxy"; |
| 9 | +import { |
| 10 | + TILES_MAX_REDIRECT_HOPS, |
| 11 | + fetchAllowlistedUpstream, |
| 12 | + isAllowedTilesUpstreamUrl, |
| 13 | +} from "../workers/tiles/src/allowlisted-fetch"; |
| 14 | + |
| 15 | +describe("viewer proxy path sanitization", () => { |
| 16 | + it("accepts normal asset paths and rejects traversal", () => { |
| 17 | + assert.equal(sanitizeViewerPath("/assets/index.js"), "/assets/index.js"); |
| 18 | + assert.equal(sanitizeViewerPath("/"), "/"); |
| 19 | + assert.equal(sanitizeViewerPath("/../secret"), null); |
| 20 | + assert.equal(sanitizeViewerPath("/foo/../../etc/passwd"), null); |
| 21 | + assert.equal(sanitizeViewerPath("/foo%2e%2e/bar"), null); |
| 22 | + assert.equal(sanitizeViewerPath("/foo%2f..%2fsecret"), null); |
| 23 | + assert.equal(sanitizeViewerPath("/foo%2F..%2Fsecret"), null); |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +describe("viewer upstream allowlist", () => { |
| 28 | + it("keeps fetches under https://geolibre.app/demo", () => { |
| 29 | + assert.equal(isAllowedUpstreamUrl("https://geolibre.app/demo"), true); |
| 30 | + assert.equal(isAllowedUpstreamUrl("https://geolibre.app/demo/"), true); |
| 31 | + assert.equal(isAllowedUpstreamUrl("https://geolibre.app/demo/assets/a.js"), true); |
| 32 | + assert.equal(isAllowedUpstreamUrl("https://geolibre.app/"), false); |
| 33 | + assert.equal(isAllowedUpstreamUrl("https://evil.example/demo"), false); |
| 34 | + assert.equal(isAllowedUpstreamUrl("http://geolibre.app/demo"), false); |
| 35 | + assert.equal(isAllowedUpstreamUrl("https://geolibre.app:8443/demo/assets/a.js"), false); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("viewer redirect policy", () => { |
| 40 | + it("follows an in-prefix redirect, strips cookies, and refuses cross-origin", async () => { |
| 41 | + const calls: string[] = []; |
| 42 | + const fetchImpl: typeof fetch = async (input) => { |
| 43 | + const url = String(input); |
| 44 | + calls.push(url); |
| 45 | + if (url.endsWith("/demo/old")) { |
| 46 | + return new Response(null, { |
| 47 | + status: 302, |
| 48 | + headers: { location: "https://geolibre.app/demo/new" }, |
| 49 | + }); |
| 50 | + } |
| 51 | + if (url.endsWith("/demo/new")) { |
| 52 | + return new Response("ok", { |
| 53 | + status: 200, |
| 54 | + headers: { |
| 55 | + "set-cookie": "session=evil", |
| 56 | + "set-cookie2": "also=evil", |
| 57 | + "content-type": "text/plain", |
| 58 | + }, |
| 59 | + }); |
| 60 | + } |
| 61 | + return new Response("unexpected", { status: 500 }); |
| 62 | + }; |
| 63 | + |
| 64 | + const ok = await proxyViewerRequest(new Request("https://web.geolibre.app/old"), fetchImpl); |
| 65 | + assert.equal(ok.status, 200); |
| 66 | + assert.equal(await ok.text(), "ok"); |
| 67 | + assert.equal(ok.headers.get("set-cookie"), null); |
| 68 | + assert.equal(ok.headers.get("set-cookie2"), null); |
| 69 | + assert.deepEqual(calls, ["https://geolibre.app/demo/old", "https://geolibre.app/demo/new"]); |
| 70 | + |
| 71 | + const evilFetch: typeof fetch = async () => |
| 72 | + new Response(null, { |
| 73 | + status: 302, |
| 74 | + headers: { location: "https://evil.example/steal" }, |
| 75 | + }); |
| 76 | + const blocked = await proxyViewerRequest(new Request("https://web.geolibre.app/"), evilFetch); |
| 77 | + assert.equal(blocked.status, 502); |
| 78 | + }); |
| 79 | + |
| 80 | + it("passes through 304 Not Modified instead of treating it as a broken redirect", async () => { |
| 81 | + const fetchImpl: typeof fetch = async () => |
| 82 | + new Response(null, { |
| 83 | + status: 304, |
| 84 | + headers: { etag: '"abc"' }, |
| 85 | + }); |
| 86 | + const response = await proxyViewerRequest( |
| 87 | + new Request("https://web.geolibre.app/assets/app.js", { |
| 88 | + headers: { "if-none-match": '"abc"' }, |
| 89 | + }), |
| 90 | + fetchImpl, |
| 91 | + ); |
| 92 | + assert.equal(response.status, 304); |
| 93 | + assert.equal(response.headers.get("etag"), '"abc"'); |
| 94 | + }); |
| 95 | + |
| 96 | + it("rejects non-GET methods and caps redirect hops", async () => { |
| 97 | + const method = await proxyViewerRequest( |
| 98 | + new Request("https://web.geolibre.app/", { method: "POST" }), |
| 99 | + ); |
| 100 | + assert.equal(method.status, 405); |
| 101 | + |
| 102 | + let hops = 0; |
| 103 | + const looping: typeof fetch = async (input) => { |
| 104 | + hops += 1; |
| 105 | + const url = String(input); |
| 106 | + return new Response(null, { |
| 107 | + status: 302, |
| 108 | + headers: { location: `${url}?n=${hops}` }, |
| 109 | + }); |
| 110 | + }; |
| 111 | + const capped = await proxyViewerRequest(new Request("https://web.geolibre.app/loop"), looping); |
| 112 | + assert.equal(capped.status, 502); |
| 113 | + assert.equal(hops, MAX_REDIRECT_HOPS + 1); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe("tiles allowlisted fetch", () => { |
| 118 | + it("refuses off-host and off-prefix S3 redirects", async () => { |
| 119 | + assert.equal(isAllowedTilesUpstreamUrl("https://api.openaerialmap.org/meta"), true); |
| 120 | + assert.equal(isAllowedTilesUpstreamUrl("https://evil.example/meta"), false); |
| 121 | + assert.equal( |
| 122 | + isAllowedTilesUpstreamUrl( |
| 123 | + "https://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/mola-color/0/0/0.png", |
| 124 | + ), |
| 125 | + true, |
| 126 | + ); |
| 127 | + assert.equal( |
| 128 | + isAllowedTilesUpstreamUrl("https://s3-eu-west-1.amazonaws.com/other-bucket/secret"), |
| 129 | + false, |
| 130 | + ); |
| 131 | + |
| 132 | + const fetchImpl: typeof fetch = async () => |
| 133 | + new Response(null, { |
| 134 | + status: 302, |
| 135 | + headers: { location: "https://evil.example/payload" }, |
| 136 | + }); |
| 137 | + |
| 138 | + await assert.rejects( |
| 139 | + () => fetchAllowlistedUpstream("https://api.openaerialmap.org/meta", {}, fetchImpl), |
| 140 | + /non-allowlisted/, |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | + it("follows a same-host HTTPS redirect and caps hops", async () => { |
| 145 | + const fetchImpl: typeof fetch = async (input) => { |
| 146 | + const url = String(input); |
| 147 | + if (url.endsWith("/meta")) { |
| 148 | + return new Response(null, { |
| 149 | + status: 301, |
| 150 | + headers: { location: "https://api.openaerialmap.org/meta/" }, |
| 151 | + }); |
| 152 | + } |
| 153 | + return new Response('{"ok":true}', { |
| 154 | + status: 200, |
| 155 | + headers: { "content-type": "application/json" }, |
| 156 | + }); |
| 157 | + }; |
| 158 | + const response = await fetchAllowlistedUpstream( |
| 159 | + "https://api.openaerialmap.org/meta", |
| 160 | + {}, |
| 161 | + fetchImpl, |
| 162 | + ); |
| 163 | + assert.equal(response.status, 200); |
| 164 | + assert.equal(await response.text(), '{"ok":true}'); |
| 165 | + |
| 166 | + let hops = 0; |
| 167 | + const looping: typeof fetch = async (input) => { |
| 168 | + hops += 1; |
| 169 | + const url = String(input); |
| 170 | + return new Response(null, { |
| 171 | + status: 302, |
| 172 | + headers: { location: `${url}?n=${hops}` }, |
| 173 | + }); |
| 174 | + }; |
| 175 | + await assert.rejects( |
| 176 | + () => fetchAllowlistedUpstream("https://api.openaerialmap.org/meta", {}, looping), |
| 177 | + /Too many upstream redirects/, |
| 178 | + ); |
| 179 | + assert.equal(hops, TILES_MAX_REDIRECT_HOPS + 1); |
| 180 | + }); |
| 181 | + |
| 182 | + it("passes through 304 Not Modified with its ETag", async () => { |
| 183 | + const fetchImpl: typeof fetch = async () => |
| 184 | + new Response(null, { |
| 185 | + status: 304, |
| 186 | + headers: { etag: '"tile-abc"' }, |
| 187 | + }); |
| 188 | + const response = await fetchAllowlistedUpstream( |
| 189 | + "https://api.openaerialmap.org/meta", |
| 190 | + { headers: { "if-none-match": '"tile-abc"' } }, |
| 191 | + fetchImpl, |
| 192 | + ); |
| 193 | + assert.equal(response.status, 304); |
| 194 | + assert.equal(response.headers.get("etag"), '"tile-abc"'); |
| 195 | + }); |
| 196 | +}); |
0 commit comments