diff --git a/scripts/lib/browser-request-e2e.mjs b/scripts/lib/browser-request-e2e.mjs index f288b2226e..9b5813acbc 100644 --- a/scripts/lib/browser-request-e2e.mjs +++ b/scripts/lib/browser-request-e2e.mjs @@ -225,7 +225,7 @@ const assertUnexpectedStatusFails = async (entrypoint, scenario, site) => { ({ step_id }) => step_id === "elsewhere", ); assert.equal(step.status, "failed"); - assert.match(step.error ?? "", /follow no redirects/u); + assert.match(step.error ?? "", /request steps do not follow/u); // Chrome reports the redirected request and aborts it; the other origin must never receive it. assert.ok( site.hosts.every((line) => !line.startsWith("localhost")), diff --git a/src/browser/BrowserScenarioSecrets.ts b/src/browser/BrowserScenarioSecrets.ts index c10f6446e1..18c238342c 100644 --- a/src/browser/BrowserScenarioSecrets.ts +++ b/src/browser/BrowserScenarioSecrets.ts @@ -271,6 +271,9 @@ export class BrowserScenarioSecrets { * {@link MIN_REDACTED_PAGE_VALUE_LENGTH} is not redacted. */ admitPageValue(label: string, value: string): void { + // fetch sends a header value without surrounding spaces and tabs, so that is the spelling records hold. + const sent = value.replace(/^[\t ]+|[\t ]+$/gu, ""); + if (sent !== value) this.admitPageValue(label, sent); if (value.length < MIN_REDACTED_PAGE_VALUE_LENGTH) return; for (const held of this.values.values()) if (held === value) return; let id = `page:${label}`; diff --git a/src/browser/PlaywrightScenarioRequest.test.ts b/src/browser/PlaywrightScenarioRequest.test.ts index 040804d33d..6d8754a4b0 100644 --- a/src/browser/PlaywrightScenarioRequest.test.ts +++ b/src/browser/PlaywrightScenarioRequest.test.ts @@ -60,6 +60,9 @@ describe("request step schema", () => { }), ]), ).toContainEqual(expect.stringContaining("cannot set")); + expect( + issues([requestStep({ expected_status: [200, 303] })]), + ).toContainEqual(expect.stringContaining("3xx")); for (const header of [ { name: "X-A", value: literal("a\r\nX-B: b") }, { name: "X-A", value: literal("a"), prefix: "Bearer\n" }, @@ -321,7 +324,7 @@ describe("request step confinement", () => { const parsed = scenario([requestStep({})]); await expect( perform(fakePage({}), parsed.actions[0]!, resolved(parsed)), - ).rejects.toThrow("follow no redirects"); + ).rejects.toThrow("request steps do not follow"); expect(calls[0]!.init.redirect).toBe("manual"); await expect( perform( @@ -375,6 +378,21 @@ describe("request step confinement", () => { expect(calls[0]!.url).toBe( `${ORIGIN}/api/items?flag&a=b%20c&s=x;y&q=a%20b`, ); + for (const [path, sent] of [ + ["/api/items?", "/api/items?q=1"], + ["/api/items#", "/api/items?q=1"], + ["/api/items?a=1&", "/api/items?a=1&q=1"], + ["/api/items?a=1#top", "/api/items?a=1&q=1"], + ] as const) { + const edge = scenario([ + requestStep({ + path, + query: [{ name: "q", value: { source: "literal", value: "1" } }], + }), + ]); + await perform(fakePage({}), edge.actions[0]!, resolved(edge)); + expect(calls.at(-1)!.url, path).toBe(`${ORIGIN}${sent}`); + } }); it("refuses page values a header cannot carry, and stored values it cannot read", async () => { @@ -443,6 +461,14 @@ describe("page value redaction", () => { ); }); + it("redacts the value fetch sends when the stored one has surrounding spaces", () => { + const held = secrets(); + held.admitPageValue("s.X-Tok", " padded-token-4f9e2a\t"); + expect(held.redact("X-Tok: padded-token-4f9e2a")).not.toContain( + "padded-token-4f9e2a", + ); + }); + it("does not redact values too short to tell from ids and ports", () => { const held = secrets(); held.admitPageValue("s.X-Org-Id", "1"); diff --git a/src/browser/PlaywrightScenarioRequest.ts b/src/browser/PlaywrightScenarioRequest.ts index a0cbcd66a9..586c708543 100644 --- a/src/browser/PlaywrightScenarioRequest.ts +++ b/src/browser/PlaywrightScenarioRequest.ts @@ -119,8 +119,15 @@ const withQuery = ( `${encodeURIComponent(name)}=${encodeURIComponent(value)}`, ) .join("&"); - const base = url.href.slice(0, url.href.length - url.hash.length); - return `${base}${url.search === "" ? "?" : "&"}${added}${url.hash}`; + // fetch drops the fragment; cut it at its "#" even when nothing follows it. + const hash = url.href.indexOf("#"); + const base = hash < 0 ? url.href : url.href.slice(0, hash); + const separator = !base.includes("?") + ? "?" + : base.endsWith("?") || base.endsWith("&") + ? "" + : "&"; + return `${base}${separator}${added}`; }; /** What a request step sends: the URL, the origin it must be sent from, and the call. */ @@ -240,7 +247,7 @@ export const performScenarioRequest = async (input: { ); if ("redirected" in response) throw new ScenarioRequestError( - `Request step ${action.step_id} was answered with a redirect; request steps follow no redirects, so send it to the path the redirect names.`, + `Request step ${action.step_id} was answered with a redirect, which request steps do not follow; a write may already have been applied. The capture records the redirect and, with header values selected, its Location.`, ); if ( action.expected_status !== undefined && diff --git a/src/domain/browserScenarioValues.ts b/src/domain/browserScenarioValues.ts index 630f60fc51..8c748dfcc8 100644 --- a/src/domain/browserScenarioValues.ts +++ b/src/domain/browserScenarioValues.ts @@ -331,7 +331,16 @@ export const browserScenarioActionSchema = z.discriminatedUnion("action", [ "JSON request body, sent with Content-Type application/json unless a header sets one; not allowed for GET or HEAD.", ), expected_status: z - .array(z.int().min(100).max(599)) + .array( + z + .int() + .min(100) + .max(599) + .refine((status) => status < 300 || status > 399, { + message: + "Request steps follow no redirects, so a 3xx status cannot complete one", + }), + ) .min(1) .optional() .describe(