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

An app host's redirect and socket doors refuse what they should: `to=/\host` is not a same-origin path, and a read-only token cannot open a WebSocket #14918

Merged opened this

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

Merged

scout/forge:scout/14897-an-app-host-s-redirect-and-socket-doors → main 1 commit fork

  • Merged. scout/14897-an-app-host-s-redirect-and-socket-doors was merged into main as 10ef998.

Review

Sign in to review this change.
2 files changed +147 −6
src/apps/proxy.ts +20 −6
@@ -882,11 +882,12 @@ function admit(
882882 }),
883883 );
884884 // A read-only token reads: the forge's API refuses it writes, and the
885 // app's door is no wider.
885 // app's door is no wider. A socket is a two-way channel opened by a GET:
886 // it is refused the way a POST is (#14897).
886887 if (
887888 bearer?.readOnly === true &&
888 request.method !== "GET" &&
889 request.method !== "HEAD"
889 ((request.method !== "GET" && request.method !== "HEAD") ||
890 isWebSocketHandshake(request))
890891 )
891892 return err(
892893 new Response("this token is read-only", {
@@ -1941,9 +1942,22 @@ function grant(
19411942 });
19421943 }
19431944
1944 /** A `to` a redirect may follow: this site's own paths, nothing else. */
1945 function safePath(to: string | null): string {
1946 return to !== null && to.startsWith("/") && !to.startsWith("//") ? to : "/";
1945 /**
1946 * A `to` a redirect may follow: this site's own paths, nothing else. A
1947 * browser reads `/\host` as `//host` and drops a tab or newline before it
1948 * resolves, so a backslash or a control character is refused outright, and
1949 * what is left must still resolve to the same origin (#14897).
1950 */
1951 export function safePath(to: string | null): string {
1952 if (to === null || !to.startsWith("/") || to.startsWith("//")) return "/";
1953 if (/[\\\u0000-\u001f\u007f]/.test(to)) return "/";
1954 const base = "http://same.invalid";
1955 try {
1956 if (new URL(to, base).origin !== base) return "/";
1957 } catch {
1958 return "/";
1959 }
1960 return to;
19471961 }
19481962
19491963 function cookieValue(request: Request, name: string): string | undefined {
test/apps-proxy-doors.test.ts added +127
@@ -0,0 +1,127 @@
1 /**
2 * An app host's own doors refuse what they should (#14897): a `to` that a
3 * browser would read as another origin collapses to `/`, and a read-only or
4 * run bearer token is refused a WebSocket the way it is refused a POST.
5 * Nothing deploys: both answers come before the app is asked.
6 */
7
8 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
9 import { newApp } from "../src/actions/apps.ts";
10 import { safePath } from "../src/apps/proxy.ts";
11 import type { Repo } from "../src/core/repos.ts";
12 import { mintRunToken } from "../src/core/run_token.ts";
13 import { createToken } from "../src/core/tokens.ts";
14 import {
15 fetchAsHost,
16 startAppsForge,
17 stopAppsForge,
18 type AppsForge,
19 } from "./helpers/apps-forge.ts";
20
21 describe("safePath", () => {
22 test("keeps a same-origin path with its query", () => {
23 expect(safePath("/a?b")).toBe("/a?b");
24 expect(safePath("/who?q=1#top")).toBe("/who?q=1#top");
25 });
26
27 test("refuses what a browser reads as another host", () => {
28 for (const to of [
29 "/\\evil.example",
30 "/\t/evil.example",
31 "/\n/evil.example",
32 "//evil.example",
33 "http://evil.example",
34 "evil.example",
35 "",
36 null,
37 ])
38 expect(safePath(to)).toBe("/");
39 });
40 });
41
42 describe("an app host's doors", () => {
43 let forge: AppsForge;
44 let repo: Repo;
45 const host = () => `vault.${forge.runtime.options.domain}`;
46
47 beforeAll(async () => {
48 forge = await startAppsForge({ start: false });
49 const made = await newApp(forge.app, forge.alice, {
50 name: "vault",
51 description: "Private",
52 visibility: "private",
53 });
54 if (!made.ok) throw new Error(String(made.error));
55 repo = made.value.repo;
56 }, 60_000);
57
58 afterAll(async () => {
59 await stopAppsForge(forge);
60 });
61
62 test("sign out's `to` keeps a deep link and drops an escape", async () => {
63 const where = async (to: string) =>
64 (
65 await fetchAsHost(
66 forge.servers,
67 host(),
68 `/__forge/signout?to=${encodeURIComponent(to)}`,
69 )
70 ).headers.get("location");
71 expect(await where("/a?b")).toBe("/a?b");
72 expect(await where("/\\evil.example")).toBe("/");
73 expect(await where("/\t/evil.example")).toBe("/");
74 // The raw form a link carries: `%09` decodes to a tab.
75 const raw = await fetchAsHost(
76 forge.servers,
77 host(),
78 "/__forge/signout?to=/%09/evil.example",
79 );
80 expect(raw.headers.get("location")).toBe("/");
81 });
82
83 const handshake = (token: string) =>
84 fetchAsHost(forge.servers, host(), "/ws", {
85 headers: {
86 authorization: `Bearer ${token}`,
87 upgrade: "websocket",
88 connection: "Upgrade",
89 "sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
90 "sec-websocket-version": "13",
91 },
92 });
93 const post = (token: string) =>
94 fetchAsHost(forge.servers, host(), "/", {
95 method: "POST",
96 headers: { authorization: `Bearer ${token}` },
97 body: "x",
98 });
99
100 test("a read-only token's handshake gets its POST's answer", async () => {
101 const ro = createToken(forge.app.db, forge.alice.id, "t", "ro").token;
102 const refused = await post(ro);
103 const socket = await handshake(ro);
104 expect(socket.status).toBe(403);
105 expect(socket.status).toBe(refused.status);
106 expect(await socket.text()).toBe(await refused.text());
107 });
108
109 test("a run's token's handshake gets its POST's answer", async () => {
110 const run = mintRunToken(
111 forge.app.db,
112 forge.alice.id,
113 "run1",
114 { repoId: repo.id, forkId: repo.id },
115 new Date(Date.now() + 3_600_000).toISOString(),
116 ).token;
117 const refused = await post(run);
118 const socket = await handshake(run);
119 expect(socket.status).toBe(403);
120 expect(await socket.text()).toBe(await refused.text());
121 });
122
123 test("a read-write token's handshake is not refused at the door", async () => {
124 const rw = createToken(forge.app.db, forge.alice.id, "t", "rw").token;
125 expect((await handshake(rw)).status).not.toBe(403);
126 });
127 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs