Public: anyone can read this; the crew can change it.
An app's page views count behind the operator's edge #16476
Check · Review · Land · Live done — Landed and live in production.
sold/measure-behind-edge → main 0 commits
2 files
changed +12
−3
src/apps/measure.ts +10 −3
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | import type { App } from "../app.ts"; |
| 12 | 12 | import { RateLimiter } from "../core/ratelimit.ts"; |
| 13 | 13 | import { readBounded } from "../http/limits.ts"; |
| 14 | import { clientAddress, clientBucket } from "../http/peer.ts"; | |
| 14 | import { clientAddress, clientBucket, wasViaEdge } from "../http/peer.ts"; | |
| 15 | 15 | import { optedOut, parseReport, REPORT_MAX } from "../web/measure.ts"; |
| 16 | 16 | import type { AppTarget } from "./proxy.ts"; |
| 17 | 17 | import { originOf } from "../perf/sources.ts"; |
| @@ -29,13 +29,20 @@ const limits = new WeakMap<App, { client: RateLimiter; apps: Map<string, RateLim | ||
| 29 | 29 | const refuse = (status: number, headers?: Record<string, string>): Response => |
| 30 | 30 | new Response(null, { status, headers: { "cache-control": "no-store", ...headers } }); |
| 31 | 31 | |
| 32 | /** The page's own origin: the request's Origin names the host it was sent to. */ | |
| 32 | /** | |
| 33 | * The page's own origin: the request's Origin names the host it was sent to. Behind the operator's | |
| 34 | * edge an app's public name (www.open-platform.sh) reaches the forge with the app's forge hostname as | |
| 35 | * Host and the public name in X-Forwarded-Host; that name counts only when a trusted proxy sent it. | |
| 36 | */ | |
| 33 | 37 | function sameOrigin(request: Request): boolean { |
| 34 | 38 | const origin = request.headers.get("origin"); |
| 35 | 39 | const host = request.headers.get("host"); |
| 36 | 40 | if (origin === null || host === null) return false; |
| 37 | 41 | try { |
| 38 | return new URL(origin).host === host; | |
| 42 | const from = new URL(origin).host; | |
| 43 | if (from === host) return true; | |
| 44 | const forwarded = request.headers.get("x-forwarded-host"); | |
| 45 | return forwarded !== null && wasViaEdge(request) && from === forwarded; | |
| 39 | 46 | } catch { |
| 40 | 47 | return false; |
| 41 | 48 | } |
test/apps-measure.test.ts +2
| @@ -121,6 +121,8 @@ describe("a report through the proxy", () => { | ||
| 121 | 121 | |
| 122 | 122 | test("keeps no address and refuses a foreign origin, a bad body and an opted-out visitor", async () => { |
| 123 | 123 | expect((await report("notes", good, { origin: "http://evil.test" })).status).toBe(403); |
| 124 | // A forwarded public name counts only from the operator's trusted edge, never from a visitor. | |
| 125 | expect((await report("notes", good, { origin: "https://www.notes.example", "x-forwarded-host": "www.notes.example" })).status).toBe(403); | |
| 124 | 126 | expect((await report("notes", "nope")).status).toBe(400); |
| 125 | 127 | expect((await report("notes", { ...good, load: -1 })).status).toBe(400); |
| 126 | 128 | const before = app.db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM app_vital").get()!.n; |
new above
offline