mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-05 14:52:09 +03:00
captureCurrentProviderRequest mirrors every Bedrock Converse request into the pending-request log tracker right after openAIToBedrockConverse() builds it, including the decoded image.source.bytes Uint8Array. sanitizePayloadPII() and redactPayload() in src/lib/logPayloads.ts both gate their recursive walk on Array.isArray(), which is false for typed arrays, so each image fell into the generic-object branch and got enumerated one JS key per decoded byte (twice, once per function) before any truncation bound applied. For 3x ~1MB images this took ~4s of synchronous, event-loop-blocking work, matching the reporter's "1-2 images OK, 3+ fails" threshold and their --stack-size observation (data-width pressure, not call-depth). Add an opaque-binary short-circuit (ArrayBuffer.isView) ahead of the Array.isArray branch in both functions, returning a fixed-size placeholder instead of recursing. Apply the same guard to cloneBoundedForLog() in open-sse/utils/requestLogger.ts for defense-in-depth (same blind spot, only accidentally safe today via its own key-count slice). Regression test reproduces the exact reporter shape (3x 1MB images) through the real openAIToBedrockConverse() converter and protectPayloadForLog(), asserting completion well under the previous ~4s and that binary bytes are never expanded into per-byte object keys.
130 lines
3.5 KiB
TypeScript
130 lines
3.5 KiB
TypeScript
import { sanitizePII } from "./piiSanitizer";
|
|
|
|
const SENSITIVE_KEYS = new Set([
|
|
"api_key",
|
|
"apiKey",
|
|
"api-key",
|
|
"authorization",
|
|
"Authorization",
|
|
"x-api-key",
|
|
"X-Api-Key",
|
|
"x-goog-api-key",
|
|
"access_token",
|
|
"accessToken",
|
|
"refresh_token",
|
|
"refreshToken",
|
|
"password",
|
|
"secret",
|
|
"token",
|
|
]);
|
|
|
|
type JsonRecord = Record<string, unknown>;
|
|
|
|
/**
|
|
* True for any binary/opaque byte view (Uint8Array, Buffer, DataView, other
|
|
* typed arrays). `Array.isArray()` returns false for these, so callers that
|
|
* branch on it before recursing would otherwise fall into the generic-object
|
|
* branch and enumerate one JS property key per decoded byte (#7297).
|
|
*/
|
|
function isOpaqueBinary(value: unknown): value is ArrayBufferView {
|
|
return ArrayBuffer.isView(value);
|
|
}
|
|
|
|
function describeOpaqueBinary(value: ArrayBufferView): string {
|
|
const byteLength = value.byteLength;
|
|
return `[binary ${byteLength} bytes]`;
|
|
}
|
|
|
|
export function cloneLogPayload<T>(value: T): T {
|
|
if (value === null || value === undefined) return value;
|
|
if (typeof globalThis.structuredClone === "function") {
|
|
return globalThis.structuredClone(value);
|
|
}
|
|
return JSON.parse(JSON.stringify(value)) as T;
|
|
}
|
|
|
|
export function normalizePayloadForLog(payload: unknown): unknown {
|
|
if (typeof payload !== "string") return payload;
|
|
|
|
const trimmed = payload.trim();
|
|
if (!trimmed) return "";
|
|
|
|
try {
|
|
return JSON.parse(trimmed);
|
|
} catch {
|
|
return { _rawText: payload };
|
|
}
|
|
}
|
|
|
|
export function redactPayload(payload: unknown): unknown {
|
|
if (!payload || typeof payload !== "object") return payload;
|
|
if (isOpaqueBinary(payload)) return describeOpaqueBinary(payload);
|
|
if (Array.isArray(payload)) return payload.map(redactPayload);
|
|
|
|
const redacted: JsonRecord = {};
|
|
for (const [key, value] of Object.entries(payload)) {
|
|
if (SENSITIVE_KEYS.has(key)) {
|
|
redacted[key] = "[REDACTED]";
|
|
} else if (typeof value === "string" && value.startsWith("Bearer ")) {
|
|
redacted[key] = "Bearer [REDACTED]";
|
|
} else if (typeof value === "object" && value !== null) {
|
|
redacted[key] = redactPayload(value);
|
|
} else {
|
|
redacted[key] = value;
|
|
}
|
|
}
|
|
return redacted;
|
|
}
|
|
|
|
export function sanitizePayloadPII(payload: unknown): unknown {
|
|
if (typeof payload === "string") {
|
|
return sanitizePII(payload).text;
|
|
}
|
|
if (!payload || typeof payload !== "object") {
|
|
return payload;
|
|
}
|
|
if (isOpaqueBinary(payload)) {
|
|
return describeOpaqueBinary(payload);
|
|
}
|
|
if (Array.isArray(payload)) {
|
|
return payload.map(sanitizePayloadPII);
|
|
}
|
|
|
|
const sanitized: JsonRecord = {};
|
|
for (const [key, value] of Object.entries(payload)) {
|
|
sanitized[key] = sanitizePayloadPII(value);
|
|
}
|
|
return sanitized;
|
|
}
|
|
|
|
export function protectPayloadForLog(payload: unknown): unknown {
|
|
if (payload === null || payload === undefined) return null;
|
|
const normalized = normalizePayloadForLog(payload);
|
|
const piiSanitized = sanitizePayloadPII(normalized);
|
|
return redactPayload(piiSanitized);
|
|
}
|
|
|
|
export function serializePayloadForStorage(payload: unknown, maxLength = 65536): string | null {
|
|
if (payload === null || payload === undefined) return null;
|
|
|
|
const exact = JSON.stringify(payload);
|
|
if (exact.length <= maxLength) {
|
|
return exact;
|
|
}
|
|
|
|
return JSON.stringify({
|
|
_truncated: true,
|
|
_originalSize: exact.length,
|
|
_preview: exact.slice(0, maxLength),
|
|
});
|
|
}
|
|
|
|
export function parseStoredPayload(value: unknown): unknown | null {
|
|
if (typeof value !== "string" || value.trim().length === 0) return null;
|
|
try {
|
|
return JSON.parse(value);
|
|
} catch {
|
|
return { _rawText: value };
|
|
}
|
|
}
|