From f609aa3393fba07dbd27e7ad5de6c7a0e930fc34 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sat, 21 Nov 2026 11:44:11 -0500 Subject: [PATCH] test(web): consolidate browser artifact privacy checks (#13150) --- .../PlaywrightScenarioArtifacts.test.ts | 198 +++++++----------- 1 file changed, 71 insertions(+), 127 deletions(-) diff --git a/src/browser/PlaywrightScenarioArtifacts.test.ts b/src/browser/PlaywrightScenarioArtifacts.test.ts index 343de7e7e3..f0d56bd762 100644 --- a/src/browser/PlaywrightScenarioArtifacts.test.ts +++ b/src/browser/PlaywrightScenarioArtifacts.test.ts @@ -17,10 +17,8 @@ const secrets = resolveScenarioSecrets(scenario, {}); // Text artifacts have no inline size ceiling; the content is returned whole. it("returns a large requested text artifact complete", async () => { - const page = { - url: () => "https://app.example.test/", - content: () => Promise.resolve("x".repeat(16 * 1_024 * 1_024 + 1)), - } as unknown as Page; + const text = `${"x".repeat(16 * 1_024 * 1_024)}!`; + const page = { content: async () => text } as unknown as Page; const result = await capturePlaywrightStepArtifacts({ context: {} as BrowserContext, @@ -28,16 +26,16 @@ it("returns a large requested text artifact complete", async () => { secrets, requested: new Set(["dom"]), }); - expect(result.dom).toMatchObject({ - state: "captured", - value: { bytes: 16 * 1_024 * 1_024 + 1 }, - }); + if (result.dom.state !== "captured") throw new Error("Expected captured DOM"); + expect(result.dom.value.bytes).toBe(16 * 1_024 * 1_024 + 1); + expect(result.dom.value.text.length).toBe(text.length); + expect(result.dom.value.text.at(-1)).toBe("!"); }); it("redacts declared secrets from current and historical URL artifacts", async () => { const secretValue = "rea browser+verifier"; const scenarioWithSecretUrl = browserScenarioSchema.parse({ - browser: { mode: "launch", executable_path: "/opt/chromium" }, + ...scenario, start_url: { url: "https://app.example.test/", query: [ @@ -47,7 +45,6 @@ it("redacts declared secrets from current and historical URL artifacts", async ( }, ], }, - actions: [{ step_id: "wait", action: "wait_for_timeout", duration_ms: 1 }], secrets: [ { secret_id: "url_token", @@ -79,28 +76,17 @@ it("redacts declared secrets from current and historical URL artifacts", async ( expect( JSON.stringify({ url: result.url, history: result.history }), ).not.toContain(secretValue); - expect(result.url).toMatchObject({ - state: "captured", - value: { - redacted: true, - url: "https://app.example.test/?token=[REDACTED:url_token]#[REDACTED:url_token]", - }, - }); + const sanitizedUrl = { + redacted: true, + url: "https://app.example.test/?token=[REDACTED:url_token]#[REDACTED:url_token]", + }; + expect(result.url).toMatchObject({ state: "captured", value: sanitizedUrl }); expect(result.history).toMatchObject({ state: "captured", value: { - current_url: { - redacted: true, - url: "https://app.example.test/?token=[REDACTED:url_token]#[REDACTED:url_token]", - }, - navigation_entries: [ - { - name: { - redacted: true, - url: "https://app.example.test/?token=[REDACTED:url_token]#[REDACTED:url_token]", - }, - }, - ], + length: 1, + current_url: sanitizedUrl, + navigation_entries: [{ type: "navigate", name: sanitizedUrl }], }, }); }); @@ -108,9 +94,7 @@ it("redacts declared secrets from current and historical URL artifacts", async ( it("redacts a declared secret used as a cookie or storage name", async () => { const secretValue = "session-token"; const scenarioWithSecret = browserScenarioSchema.parse({ - browser: { mode: "launch", executable_path: "/opt/chromium" }, - start_url: { url: "https://app.example.test/" }, - actions: [{ step_id: "wait", action: "wait_for_timeout", duration_ms: 1 }], + ...scenario, secrets: [ { secret_id: "storage_key", environment_variable: "REA_STORAGE_KEY" }, ], @@ -161,28 +145,69 @@ it("redacts a declared secret used as a cookie or storage name", async () => { }); }); -it.each(["declared", "saved"])( - "withholds selected screenshots with %s sensitive values before requesting pixels", - async (kind) => { +const request = { + step_id: "request", + action: "request", + method: "POST", + path: "/api/items", +}; +const savedToken = [{ name: "token", json_pointer: "/token", secret: true }]; +const ui = { + step_id: "ui", + locator: { kind: "css", selector: "#save" }, + save_response: { method: "POST", path: "/api/items", save: savedToken }, +}; +const screenshotPrivacyCases: readonly { + readonly source: string; + readonly action?: unknown; + readonly supplied?: "declared" | "saved"; +}[] = [ + { source: "a declared value", supplied: "declared" }, + { source: "a saved value", supplied: "saved" }, + { + source: "a planned request response", + action: { ...request, save: savedToken }, + }, + { source: "a planned click response", action: { ...ui, action: "click" } }, + { + source: "a planned press response", + action: { ...ui, action: "press", key: "Enter" }, + }, + ...[ + { source: "cookie", name: "session" }, + { source: "local_storage", key: "session" }, + { + source: "basic_credentials", + parts: [ + { source: "literal", value: "user" }, + { source: "cookie", name: "password" }, + ], + }, + ].map((value) => ({ + source: `planned ${value.source} authentication`, + action: { ...request, headers: [{ name: "Authorization", value }] }, + })), +]; + +it.each(screenshotPrivacyCases)( + "withholds requested pixels for $source", + async ({ action, supplied }) => { const selected = browserScenarioSchema.parse({ ...scenario, + actions: action === undefined ? scenario.actions : [action], secrets: - kind === "declared" + supplied === "declared" ? [{ secret_id: "token", environment_variable: "TOKEN" }] : [], }); + const value = "pixel-token-729a"; const protectedSecrets = resolveScenarioSecrets( selected, - kind === "declared" ? { TOKEN: "pixel-token-729a" } : {}, + supplied === "declared" ? { TOKEN: value } : {}, ); - if (kind === "saved") - protectedSecrets.save( - "response.token", - "token", - "pixel-token-729a", - true, - ); - const screenshot = vi.fn(async () => Buffer.from("private pixels")); + if (supplied === "saved") + protectedSecrets.save("response.token", "token", value, true); + const screenshot = vi.fn(); const result = await capturePlaywrightStepArtifacts({ page: { screenshot } as unknown as Page, context: {} as BrowserContext, @@ -197,93 +222,12 @@ it.each(["declared", "saved"])( }, ); -it.each(["request", "click", "press"])( - "withholds pixels from the first capture when a future %s response is marked sensitive", - async (action) => { - const save = [{ name: "token", json_pointer: "/token", secret: true }]; - const selected = browserScenarioSchema.parse({ - ...scenario, - actions: [ - action === "request" - ? { - step_id: "request", - action, - method: "POST", - path: "/api/items", - save, - } - : { - step_id: "ui", - action, - locator: { kind: "css", selector: "#save" }, - ...(action === "press" ? { key: "Enter" } : {}), - save_response: { method: "POST", path: "/api/items", save }, - }, - ], - }); - const screenshot = vi.fn(async () => Buffer.from("private pixels")); - const result = await capturePlaywrightStepArtifacts({ - page: { screenshot } as unknown as Page, - context: {} as BrowserContext, - secrets: resolveScenarioSecrets(selected, {}), - requested: new Set(["screenshot"]), - }); - expect(result.screenshot).toMatchObject({ - state: "missing", - reason: expect.stringContaining("sensitive"), - }); - expect(screenshot).not.toHaveBeenCalled(); - }, -); - -it.each(["cookie", "local_storage", "basic_credentials"])( - "withholds pixels before planned %s authentication values are read", - async (source) => { - const value = - source === "basic_credentials" - ? { - source, - parts: [ - { source: "literal", value: "user" }, - { source: "cookie", name: "password" }, - ], - } - : source === "local_storage" - ? { source, key: "session" } - : { source, name: "session" }; - const selected = browserScenarioSchema.parse({ - ...scenario, - actions: [ - { - step_id: "request", - action: "request", - method: "POST", - path: "/api/items", - headers: [{ name: "Authorization", value }], - }, - ], - }); - const screenshot = vi.fn(async () => Buffer.from("private pixels")); - const result = await capturePlaywrightStepArtifacts({ - page: { screenshot } as unknown as Page, - context: {} as BrowserContext, - secrets: resolveScenarioSecrets(selected, {}), - requested: new Set(["screenshot"]), - }); - expect(result.screenshot).toMatchObject({ state: "missing" }); - expect(screenshot).not.toHaveBeenCalled(); - }, -); - it("preserves requested pixels when saved response values are not marked sensitive", async () => { const selected = browserScenarioSchema.parse({ ...scenario, actions: [ { - step_id: "request", - action: "request", - method: "POST", - path: "/api/items", + ...request, save: [{ name: "id", json_pointer: "/id", secret: false }], }, ],