Files
openclaw/src/config/redact-snapshot.raw.ts
solodmd 8ae8a5c174 config: skip empty string in raw redaction to avoid corrupting snapshot (#28214)
Merged via squash.

Prepared head SHA: 07ec5b77b1
Co-authored-by: solodmd <51304754+solodmd@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
2026-04-03 17:11:06 -04:00

37 lines
1.1 KiB
TypeScript

import { isDeepStrictEqual } from "node:util";
import JSON5 from "json5";
export function replaceSensitiveValuesInRaw(params: {
raw: string;
sensitiveValues: string[];
redactedSentinel: string;
}): string {
// Empty string is not a valid replacement token here: replaceAll("", x)
// matches every character boundary and corrupts the whole raw snapshot.
const values = [...new Set(params.sensitiveValues)]
.filter((value) => value !== "")
.toSorted((a, b) => b.length - a.length);
let result = params.raw;
for (const value of values) {
result = result.replaceAll(value, params.redactedSentinel);
}
return result;
}
export function shouldFallbackToStructuredRawRedaction(params: {
redactedRaw: string;
originalConfig: unknown;
restoreParsed: (parsed: unknown) => { ok: boolean; result?: unknown };
}): boolean {
try {
const parsed = JSON5.parse(params.redactedRaw);
const restored = params.restoreParsed(parsed);
if (!restored.ok) {
return true;
}
return !isDeepStrictEqual(restored.result, params.originalConfig);
} catch {
return true;
}
}