Public: anyone can read this; the crew can change it.

An app's page views count behind the operator's edge #16476

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

sold/measure-behind-edge → main 0 commits

  • Merged. sold/measure-behind-edge was merged into main as d0c6770.

Review

Sign in to review this change.
2 files changed +12 −3
src/apps/measure.ts +10 −3
@@ -11,7 +11,7 @@
1111 import type { App } from "../app.ts";
1212 import { RateLimiter } from "../core/ratelimit.ts";
1313 import { readBounded } from "../http/limits.ts";
14 import { clientAddress, clientBucket } from "../http/peer.ts";
14 import { clientAddress, clientBucket, wasViaEdge } from "../http/peer.ts";
1515 import { optedOut, parseReport, REPORT_MAX } from "../web/measure.ts";
1616 import type { AppTarget } from "./proxy.ts";
1717 import { originOf } from "../perf/sources.ts";
@@ -29,13 +29,20 @@ const limits = new WeakMap<App, { client: RateLimiter; apps: Map<string, RateLim
2929 const refuse = (status: number, headers?: Record<string, string>): Response =>
3030 new Response(null, { status, headers: { "cache-control": "no-store", ...headers } });
3131
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 */
3337 function sameOrigin(request: Request): boolean {
3438 const origin = request.headers.get("origin");
3539 const host = request.headers.get("host");
3640 if (origin === null || host === null) return false;
3741 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;
3946 } catch {
4047 return false;
4148 }
test/apps-measure.test.ts +2
@@ -121,6 +121,8 @@ describe("a report through the proxy", () => {
121121
122122 test("keeps no address and refuses a foreign origin, a bad body and an opted-out visitor", async () => {
123123 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);
124126 expect((await report("notes", "nope")).status).toBe(400);
125127 expect((await report("notes", { ...good, load: -1 })).status).toBe(400);
126128 const before = app.db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM app_vital").get()!.n;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs