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

An app.looks path with a backslash never sends the page fetch to another host #14981

Merged opened this

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

Merged

scout/forge:scout/14957-an-app-looks-path-with-a-backslash-never → main 4 commits fork 4 threads · 4 unresolved

  • Merged. scout/14957-an-app-looks-path-with-a-backslash-never was merged into main as 799bde0.

Review

Sign in to review this change.
7 files changed +74 −10
src/apps/manifest.ts +4
@@ -458,6 +458,10 @@ function pathList(
458458 return invalid(
459459 `forge.json: ${field} entries must be a path starting with / (no scheme, no host)`,
460460 );
461 if (item.includes("\\"))
462 return invalid(
463 `forge.json: ${field} entries must not contain \\ (it can name another host)`,
464 );
461465 if (item.includes(".."))
462466 return invalid(`forge.json: ${field} entries must not contain ..`);
463467 if (/[\x00-\x1f\x7f]/.test(item))
src/kernel/same_path.ts added +25
@@ -0,0 +1,25 @@
1 /**
2 * A page path an app names (`app.looks`, `app.looksAsOwner`) is the app's own,
3 * never another host's: `/\evil.example` and `//evil.example` resolve to a
4 * different origin under the WHATWG URL parser (#14957). One rule, used where
5 * forge.json is read and again by every fetcher of those paths.
6 */
7
8 /** True when `path` starts with exactly one `/` and holds no `\` or control character. */
9 export function samePath(path: string): boolean {

This line is over 100 columns, and the gate reports it. Wrap it. The \u0000-\u001f range is also written differently from the \x00-\x1f form in manifest.ts. Use one form.

10 return path.startsWith("/") && !path.startsWith("//") && !/[\\\x00-\x1f\x7f]/.test(path);
11 }
12
13 /**
14 * The URL `path` names on `base`, or null when the path breaks `samePath` or the URL leaves
15 * `base`'s origin.
16 */
17 export function sameOriginUrl(path: string, base: string): string | null {
18 if (!samePath(path)) return null;
19 try {
20 const url = new URL(path, base);
21 return url.origin === new URL(base).origin ? url.href : null;
22 } catch {
23 return null;
24 }
25 }
src/release/axe.ts +10 −5
@@ -17,6 +17,7 @@
1717 import { findChromium } from "../looks/capture.ts";
1818 import { withPage } from "./devtools.ts";
1919 import { s, type Finding } from "./webspec.ts";
20 import { sameOriginUrl } from "../kernel/same_path.ts";
2021
2122 const SCHEMES = ["light", "dark"] as const;
2223
@@ -54,8 +55,10 @@ export function noScriptProblems(html: string): string[] {
5455 export async function noScript(base: string, paths: string[], fetcher: typeof fetch = fetch): Promise<Finding[]> {
5556 const findings: Finding[] = [];
5657 for (const path of paths) {
58 const url = sameOriginUrl(path, base);
59 if (url === null) continue; // webspec() reports a path that is not the app's (#14957)
5760 try {
58 const res = await fetcher(new URL(path, base).href, { headers: { accept: "text/html" }, redirect: "manual", signal: AbortSignal.timeout(10000) });
61 const res = await fetcher(url, { headers: { accept: "text/html" }, redirect: "manual", signal: AbortSignal.timeout(10000) });
5962 if (!res.ok) continue; // webspec() already reports the status
6063 for (const message of noScriptProblems(await res.text()))
6164 findings.push({ path, check: "no-js", message, section: s("progressive-enhancement") });
@@ -75,16 +78,18 @@ export async function axe(base: string, paths: string[]): Promise<Finding[]> {
7578 try {
7679 await withPage(binary, async (send, once) => {
7780 await send("Page.enable");
78 const load = async (path: string) => {
81 const load = async (url: string) => {
7982 const loaded = once("Page.loadEventFired");
8083 loaded.catch(() => {});
81 await send("Page.navigate", { url: new URL(path, base).href });
84 await send("Page.navigate", { url });
8285 await loaded;
outdated

The for loop already skips a path where sameOriginUrl is null, so this throw cannot happen. Delete the throw and the second call: have load take the URL, or compute it once in the loop.

8386 };
8487 for (const path of paths) {
88 const url = sameOriginUrl(path, base);
89 if (url === null) continue; // webspec() reports it (#14957)
8590 try {
8691 await send("Emulation.setScriptExecutionDisabled", { value: true });
87 await load(path);
92 await load(url);
8893 const dom = await send("Runtime.evaluate", { expression: "document.documentElement.outerHTML", returnByValue: true });
8994 for (const message of noScriptProblems(`<!doctype html>${dom.result.value}`))
9095 findings.push({ path, check: "no-js", message, section: s("progressive-enhancement") });
@@ -96,7 +101,7 @@ export async function axe(base: string, paths: string[]): Promise<Finding[]> {
96101 for (const scheme of SCHEMES) {
97102 try {
98103 await send("Emulation.setEmulatedMedia", { features: [{ name: "prefers-color-scheme", value: scheme }] });
99 await load(path);
104 await load(url);
100105 const res = await send("Runtime.evaluate", {
101106 expression: `${source};axe.run(document,{resultTypes:["violations"]}).then(r=>JSON.stringify(r.violations))`,
102107 awaitPromise: true,
src/release/webspec.ts +5 −5
@@ -7,6 +7,7 @@
77 */
88
99 import { siteFindings } from "./webspec-site.ts";
10 import { sameOriginUrl } from "../kernel/same_path.ts";
1011
1112 export interface Finding {
1213 path: string;
@@ -221,11 +222,10 @@ export async function webspec(base: string, paths: string[], fetcher: Fetcher =
221222 const report = (ps: Problem[]) => {
222223 for (const p of ps) findings.push({ path, ...p });
223224 };
224 let url: string;
225 try {
226 url = new URL(path, base).href;
227 } catch {
228 report([{ check: "fetch", message: `${path} is not a URL`, section: SPEC }]);
225 // A path is the app's to name, never another host's (#14957).
226 const url = sameOriginUrl(path, base);
227 if (url === null) {
228 report([{ check: "fetch", message: `${JSON.stringify(path)} is not a path on this app; it was not fetched`, section: SPEC }]);
229229 continue;
230230 }
231231 let res: Response;
test/apps-adversarial.test.ts +12
@@ -401,6 +401,18 @@ describe("manifest: parseManifest edge cases", () => {
401401 }
402402 });
403403
404 test("an app.looks or app.looksAsOwner entry with a backslash is refused: /\\evil.example is another host (#14957)", () => {
405 for (const app of [
406 { run: "x", looks: ["/\\evil.example"] },
407 { run: "x", looks: ["/", "/a\\b"] },
408 { run: "x", looks: ["/"], looksAsOwner: ["/\\evil.example"] },
409 ]) {
410 const result = parseManifest(JSON.stringify({ app }), undefined);
411 expect(result.ok).toBe(false);
412 if (!result.ok && result.error.kind === "invalid") expect(result.error.message).toContain("must not contain \\");
413 }
414 });
415
404416 test("an over-long app.looks entry is refused", () => {
405417 const result = parseManifest(
406418 JSON.stringify({ app: { run: "x", looks: [`/${"a".repeat(300)}`] } }),
test/axe.test.ts +7
@@ -24,6 +24,13 @@ test("a page that fills <main> from a script is a no-js finding; a rendered one
2424 expect(found[0]!.section).toContain("specification.website");
2525 });
2626
27 test("noScript never fetches a path that leaves the app's origin (#14957)", async () => {
28 const asked: string[] = [];
29 const f = (async (input: string) => (asked.push(input), new Response(PAGES["/script"]!))) as unknown as typeof fetch;
30 expect(await noScript(base, ["/\\evil.example", "//evil.example", "https://evil.example/"], f)).toEqual([]);
31 expect(asked).toEqual([]);
32 });
33
2734 test("noScriptProblems names a missing main and heading", () => {
2835 expect(noScriptProblems("<body>x</body>")).toEqual(["the page has no <main>", "no main heading (<h1>) without JavaScript"]);
2936 });
test/webspec.test.ts +11
@@ -241,3 +241,14 @@ test("#14755: a preview's HTML cannot aim the checker at a private or metadata a
241241 expect(off.join("\n")).toContain("169.254.169.254");
242242 expect(found.some((f) => f.check === "head-links" && f.message.includes("169.254"))).toBe(false);
243243 });
244
245 test("a looks path that would leave the app's origin is refused, never fetched (#14957)", async () => {
246 const asked: string[] = [];
247 const f = async (input: string) => (asked.push(input), new Response(GOOD_HTML, { headers: GOOD_HEADERS }));
248 for (const path of ["/\\evil.example", "//evil.example", "https://evil.example/", "evil", "/a\nb", "/a\tb"]) {
249 const found = await webspec("https://x.test", [path], f);
250 expect(found.map((x) => x.check)).toEqual(["fetch"]);
251 expect(found[0]!.message).toContain("not a path on this app");
252 }
253 expect(asked).toEqual([]);
254 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs