mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-03 05:30:21 +00:00
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
37 lines
1.1 KiB
TypeScript
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;
|
|
}
|
|
}
|