From 9ab18ba1dae99f690add39190ef964ba8be3232e Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 20 Nov 2026 08:01:38 -0500 Subject: [PATCH] test(browser): sharpen HAR malformed input and producer guard checks (#12805) * test(har): sharpen producer admission checks * test(browser): construct empty HAR failure markers explicitly --- .../history/HarCapture.failedRequests.test.ts | 15 +++--- src/browser/history/HarCapture.test.ts | 47 ++++++++----------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/browser/history/HarCapture.failedRequests.test.ts b/src/browser/history/HarCapture.failedRequests.test.ts index 6d25f814f9..90a355318d 100644 --- a/src/browser/history/HarCapture.failedRequests.test.ts +++ b/src/browser/history/HarCapture.failedRequests.test.ts @@ -70,7 +70,7 @@ it.each([ it.each([ "other-producer", "other-version", - "successful-response", + "empty-failure", "missing-failure", "invalid-duration", ])("keeps timing validation for %s", (variant) => { @@ -78,12 +78,15 @@ it.each([ const failed = fixture.log.entries[1]!; if (variant === "other-producer") fixture.log.creator.name = "unknown"; if (variant === "other-version") fixture.log.creator.version = "unknown"; - if (variant === "successful-response") { - failed.response.status = 200; - Reflect.deleteProperty(failed.response, "_failureText"); - } + if (variant === "empty-failure") + Object.assign(failed.response, { _failureText: "" }); if (variant === "missing-failure") Reflect.deleteProperty(failed.response, "_failureText"); if (variant === "invalid-duration") failed.timings.send = -2; - expect(() => decodeHarCapture(JSON.stringify(fixture), [])).toThrow(); + expect(() => decodeHarCapture(JSON.stringify(fixture), [])).toThrow( + expect.objectContaining({ + failure: expect.objectContaining({ kind: "malformed" }), + pointer: "/log/entries/1/timings/send", + }), + ); }); diff --git a/src/browser/history/HarCapture.test.ts b/src/browser/history/HarCapture.test.ts index fc0fa04369..067400b493 100644 --- a/src/browser/history/HarCapture.test.ts +++ b/src/browser/history/HarCapture.test.ts @@ -49,13 +49,7 @@ it("validates malformed timings before excluding an explicitly sensitive propert expect(() => decodeHarCapture(text, ["timings"])).toThrow(CaptureFormatError); }); -it.each([ - { cache: null }, - { cache: false }, - { cache: 0 }, - { cache: "cache" }, - { cache: [] }, -])("rejects a non-object HAR cache: $cache", ({ cache }) => { +it.each([null, false, []])("rejects a non-object HAR cache: %j", (cache) => { const fixture = historicalHar(); const entry = fixture.log.entries[0]!; expect(() => @@ -287,7 +281,6 @@ it("does not decode container lookalikes when excluding explicitly sensitive bod }); it.each([ - { values: [] }, { values: ["response"] }, { values: ["content"] }, { values: ["log"] }, @@ -444,18 +437,26 @@ it("rejects identical duplicate core fields before schema validation", () => { expect(() => decodeHarCapture(text, [])).toThrow("duplicate"); }); -it("names the violated HAR schema constraint without restating capture values", () => { - expect(() => - decodeHarCapture( - JSON.stringify({ - log: { creator: { name: "x", version: "1" }, entries: [] }, +it.each([ + { + log: { creator: { name: "x", version: "1" }, entries: [] }, + missing: "version", + }, + { log: { version: "1.2", entries: [] }, missing: "creator" }, +])( + "names a required HAR property $missing without disclosing producer values", + ({ log, missing }) => { + expect(() => decodeHarCapture(JSON.stringify({ log }), [])).toThrow( + expect.objectContaining({ + failure: expect.objectContaining({ + kind: "malformed", + field: "/log", + detail: `HAR schema validation failed: required (must have required property '${missing}').`, + }), }), - [], - ), - ).toThrow( - "HAR schema validation failed: required (must have required property 'version').", - ); -}); + ); + }, +); it("preserves prototype-named producer members through the capture boundary", () => { const text = JSON.stringify(historicalHar()).replace( @@ -729,14 +730,6 @@ it("redacts actual authentication fields and explicit binary values while preser }); }); -it.each([ - "{", - '{"log":{},"log":{}}', - JSON.stringify({ log: { version: "1.2", entries: [] } }), -])("rejects malformed producer data %s", (text) => { - expect(() => decodeHarCapture(text, [])).toThrow(); -}); - it("preserves observed SaveHar null post-data without broadening arbitrary HAR producers", () => { const fixture = historicalHar(); const first = fixture.log.entries[0];