145 lines
4.0 KiB
TypeScript
145 lines
4.0 KiB
TypeScript
/**
|
|
* Spec §28. Redaction before buffering.
|
|
*
|
|
* The sanitizer copies only plain own data into fresh objects/arrays: it never
|
|
* preserves prototypes and never invokes getters or `toJSON` on untrusted
|
|
* payloads. Traversal is iterative and capped by a structural budget, so an
|
|
* adversarial payload cannot make telemetry itself unbounded.
|
|
*/
|
|
export type TelemetryPayloadBudget = Readonly<{
|
|
maxDepth: number;
|
|
maxKeys: number;
|
|
maxArrayItems: number;
|
|
maxStringLength: number;
|
|
}>;
|
|
|
|
export const DEFAULT_TELEMETRY_PAYLOAD_BUDGET: TelemetryPayloadBudget =
|
|
Object.freeze({
|
|
maxDepth: 6,
|
|
maxKeys: 64,
|
|
maxArrayItems: 32,
|
|
maxStringLength: 256,
|
|
});
|
|
|
|
export const REDACTED = "[redacted]";
|
|
export const TRUNCATED = "[truncated]";
|
|
|
|
/**
|
|
* Case/separator-insensitive secret key detection. `accessToken`,
|
|
* `access_token` and `ACCESS-TOKEN` all normalize to `accesstoken`.
|
|
*/
|
|
const SECRET_KEY_FRAGMENTS = Object.freeze([
|
|
"password",
|
|
"passwd",
|
|
"secret",
|
|
"authorization",
|
|
"cookie",
|
|
"token",
|
|
"apikey",
|
|
"credential",
|
|
"session",
|
|
"privatekey",
|
|
"signature",
|
|
]);
|
|
|
|
export function isSecretTelemetryKey(key: string): boolean {
|
|
const normalized = key.toLowerCase().replace(/[^a-z0-9]/gu, "");
|
|
return SECRET_KEY_FRAGMENTS.some((fragment) => normalized.includes(fragment));
|
|
}
|
|
|
|
/** Values that look like bearer credentials are redacted wherever they occur. */
|
|
const SECRET_VALUE = /^(?:bearer|basic)\s+\S+|^eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\./iu;
|
|
|
|
function readOwnData(
|
|
source: object,
|
|
key: string,
|
|
): Readonly<{ ok: true; value: unknown }> | Readonly<{ ok: false }> {
|
|
const descriptor = Object.getOwnPropertyDescriptor(source, key);
|
|
if (!descriptor || !("value" in descriptor)) return { ok: false };
|
|
return { ok: true, value: descriptor.value };
|
|
}
|
|
|
|
function sanitizeLeaf(value: unknown, budget: TelemetryPayloadBudget): unknown {
|
|
switch (typeof value) {
|
|
case "string":
|
|
if (SECRET_VALUE.test(value)) return REDACTED;
|
|
return value.length > budget.maxStringLength
|
|
? `${value.slice(0, budget.maxStringLength)}${TRUNCATED}`
|
|
: value;
|
|
case "number":
|
|
return Number.isFinite(value) ? value : null;
|
|
case "boolean":
|
|
return value;
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
type Pending = Readonly<{
|
|
source: unknown;
|
|
depth: number;
|
|
assign: (value: unknown) => void;
|
|
}>;
|
|
|
|
export function redactTelemetryPayload(
|
|
payload: unknown,
|
|
budget: TelemetryPayloadBudget = DEFAULT_TELEMETRY_PAYLOAD_BUDGET,
|
|
): unknown {
|
|
let result: unknown = null;
|
|
const stack: Pending[] = [
|
|
{
|
|
source: payload,
|
|
depth: 0,
|
|
assign: (value) => {
|
|
result = value;
|
|
},
|
|
},
|
|
];
|
|
while (stack.length > 0) {
|
|
const { source, depth, assign } = stack.pop() as Pending;
|
|
if (source === null || typeof source !== "object") {
|
|
assign(source === undefined ? null : sanitizeLeaf(source, budget));
|
|
continue;
|
|
}
|
|
if (depth >= budget.maxDepth) {
|
|
assign(TRUNCATED);
|
|
continue;
|
|
}
|
|
if (Array.isArray(source)) {
|
|
const length = Math.min(source.length, budget.maxArrayItems);
|
|
const copy: unknown[] = new Array<unknown>(length).fill(null);
|
|
assign(copy);
|
|
for (let index = 0; index < length; index += 1) {
|
|
const item = readOwnData(source, String(index));
|
|
if (!item.ok) continue;
|
|
stack.push({
|
|
source: item.value,
|
|
depth: depth + 1,
|
|
assign: (value) => {
|
|
copy[index] = value;
|
|
},
|
|
});
|
|
}
|
|
continue;
|
|
}
|
|
const copy: Record<string, unknown> = {};
|
|
assign(copy);
|
|
for (const key of Object.keys(source).slice(0, budget.maxKeys)) {
|
|
if (key === "__proto__" || key === "constructor" || key === "prototype") {
|
|
continue;
|
|
}
|
|
if (isSecretTelemetryKey(key)) continue;
|
|
const entry = readOwnData(source, key);
|
|
if (!entry.ok) continue;
|
|
stack.push({
|
|
source: entry.value,
|
|
depth: depth + 1,
|
|
assign: (value) => {
|
|
copy[key] = value;
|
|
},
|
|
});
|
|
}
|
|
}
|
|
return result;
|
|
}
|