fix(browser): close request step edge cases from the second review

- Append query entries correctly after a path ending in "?", "&" or "#".
- Refuse 3xx expected statuses, which an unfollowed redirect can never
  report, and say a redirected write may already have been applied.
- Redact a page value also as fetch sends it, without surrounding spaces
  and tabs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Edgar ArakelyanandClaude Opus 5.5 committed 2026-10-23 21:01:07 -07:00
1 parent 3ecd6a6124
commit 74c2a12410
5 files changed
+51 -6

No files matched your search

+1 -1
View File
@@ -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")),
+3
View File
@@ -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}`;
+27 -1
View File
@@ -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");
+10 -3
View File
@@ -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 &&
+10 -1
View File
@@ -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(