From 550289154e35eaecc3cb9ebfebbc0ab1cbff249a Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 15:38:35 -0500 Subject: [PATCH] perf(browser): eliminate duplicate CaptureRedaction scans, per-event Buffer allocs, and QLOG flatMap churn (#13581) * perf(browser): eliminate duplicate CaptureRedaction scans, per-event Buffer allocs, and QLOG flatMap churn * fix(browser): preserve own __proto__ property in QlogCapture object projection * fix(browser): preserve capture extension and redaction semantics --- src/browser/history/CaptureRedaction.ts | 6 ++- src/browser/history/QlogCapture.test.ts | 9 +++++ src/browser/history/QlogCapture.ts | 50 ++++++++++++++++--------- src/browser/history/SazProjection.ts | 5 ++- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/browser/history/CaptureRedaction.ts b/src/browser/history/CaptureRedaction.ts index 0a4e0cb442..383f9f0b9d 100644 --- a/src/browser/history/CaptureRedaction.ts +++ b/src/browser/history/CaptureRedaction.ts @@ -67,8 +67,10 @@ export class CaptureRedaction { } } if ( - explicitValuesOccurIn(value, this.sensitiveValues) || - explicitValuesOccurIn(result, this.sensitiveValues) + this.sensitiveValues.length > 0 && + (explicitValuesOccurIn(value, this.sensitiveValues) || + (result !== value && + explicitValuesOccurIn(result, this.sensitiveValues))) ) { this.redactions.push({ pointer, reason: "explicit-sensitive-value" }); return null; diff --git a/src/browser/history/QlogCapture.test.ts b/src/browser/history/QlogCapture.test.ts index d6c1c8b735..2a79bdd3c3 100644 --- a/src/browser/history/QlogCapture.test.ts +++ b/src/browser/history/QlogCapture.test.ts @@ -56,6 +56,10 @@ it("derives interval unions per trace/direction while retaining event source ord enumerable: true, value: { ordinary: true }, }); + Object.defineProperty(original.traces[0].events[0].data, "__proto__", { + enumerable: true, + value: { eventExtension: "kept" }, + }); original.extra = "https://example.test/path?token=ordinary"; const { decoded, traces } = facts(JSON.stringify(original)); expect(traces[0]?.streams[0]).toMatchObject({ @@ -88,6 +92,11 @@ it("derives interval unions per trace/direction while retaining event source ord extra: original.extra, ["__proto__"]: { ordinary: true }, }); + const container = decoded.container.reported; + expect(Object.hasOwn(Object(container), "__proto__")).toBe(true); + expect(decoded.records[0]?.reported).toMatchObject({ + data: { ["__proto__"]: { eventExtension: "kept" } }, + }); expect(Reflect.get(Object.prototype, "ordinary")).toBeUndefined(); }); diff --git a/src/browser/history/QlogCapture.ts b/src/browser/history/QlogCapture.ts index 63757e675b..2784374a2a 100644 --- a/src/browser/history/QlogCapture.ts +++ b/src/browser/history/QlogCapture.ts @@ -411,6 +411,9 @@ const unsigned = (value: unknown, pointer: string): bigint | null => { return result; }; +const QLOG_HTTP_HEADER_VALUE_RE = /^\/data\/frame\/headers\/\d+\/value$/u; +const QLOG_TRANSPORT_URL_HEADERS = new Set(["location", "referer", "origin"]); + /** Preserve JSON values/lexemes and structurally exclude only transport credentials and declared values. */ const project = ( value: unknown, @@ -419,6 +422,7 @@ const project = ( const redactor = new CaptureRedaction(sensitiveValues); const numeric_literals: Projection["numeric_literals"] = []; const eventName = object(value) ? value.name : null; + const hasSensitiveValues = sensitiveValues.length > 0; const visit = ( item: unknown, pointer: string, @@ -427,7 +431,7 @@ const project = ( const header = (eventName === "http:frame_created" || eventName === "http:frame_parsed") && - /^\/data\/frame\/headers\/\d+\/value$/u.test(pointer) && + QLOG_HTTP_HEADER_VALUE_RE.test(pointer) && typeof parent?.name === "string"; if ( (header && isTransportCredentialHeader(String(parent?.name))) || @@ -449,9 +453,7 @@ const project = ( item, pointer, header && - ["location", "referer", "origin"].includes( - String(parent?.name).toLowerCase(), - ) + QLOG_TRANSPORT_URL_HEADERS.has(String(parent?.name).toLowerCase()) ? {} : undefined, ); @@ -460,20 +462,32 @@ const project = ( return item.map((child, index) => visit(child, capturePointer(pointer, String(index))), ); - if (object(item)) - return Object.fromEntries( - Object.entries(item).flatMap(([key, child]) => { - if (sensitiveValues.some((literal) => key.includes(literal))) { - redactor.redactions.push({ - pointer, - reason: "explicit-sensitive-value", - scope: "property-name", - }); - return []; - } - return [[key, visit(child, capturePointer(pointer, key), item)]]; - }), - ); + if (object(item)) { + const projected: Record = {}; + for (const [key, child] of Object.entries(item)) { + if ( + hasSensitiveValues && + sensitiveValues.some((literal) => key.includes(literal)) + ) { + redactor.redactions.push({ + pointer, + reason: "explicit-sensitive-value", + scope: "property-name", + }); + continue; + } + const projectedValue = visit(child, capturePointer(pointer, key), item); + if (key === "__proto__") + Object.defineProperty(projected, key, { + value: projectedValue, + enumerable: true, + writable: true, + configurable: true, + }); + else projected[key] = projectedValue; + } + return projected; + } return invalid("qlog parser returned a non-JSON value.", pointer); }; return { diff --git a/src/browser/history/SazProjection.ts b/src/browser/history/SazProjection.ts index 33b925434c..7c306f6e88 100644 --- a/src/browser/history/SazProjection.ts +++ b/src/browser/history/SazProjection.ts @@ -12,6 +12,8 @@ export type SazValue = | { readonly [key: string]: SazValue } | readonly SazValue[]; +const SAZ_HTTP_FIELD_POINTER_RE = /^\/(request|response)\//; + /** Project native byte fields with the same credential and explicit-literal exclusions as HAR. */ export const projectSazRecord = ( source: SazValue, @@ -73,7 +75,8 @@ export const projectSazRecord = ( // HTTP fields are Latin-1 views of bytes. A UTF-8 declaration must also // exclude its byte spelling, even when its display is mojibake. if ( - /^\/(request|response)\//.test(pointer) && + sensitiveValues.length > 0 && + SAZ_HTTP_FIELD_POINTER_RE.test(pointer) && !pointer.endsWith("/member") && redactor.sensitiveBytes(Buffer.from(value, "latin1")) ) {