Skip to content

Commit 328eaa3

Browse files
fix(logging): keep redacted token hints UTF-16 safe (#103341)
1 parent 9ef833f commit 328eaa3

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/logging/redact.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,29 @@ describe("redactSensitiveText", () => {
309309
expect(redactSensitiveFieldValue("MONKEY", "banana")).toBe("banana");
310310
});
311311

312+
it("keeps Unicode token hints on valid UTF-16 boundaries", () => {
313+
const cases = [
314+
{
315+
secret: `abcde😀${"x".repeat(9)}wxyz`,
316+
expected: "abcde…wxyz",
317+
},
318+
{
319+
secret: `abcdef${"x".repeat(9)}😀abc`,
320+
expected: "abcdef…abc",
321+
},
322+
{
323+
secret: `abcd😀${"x".repeat(9)}😀ab`,
324+
expected: "abcd😀…😀ab",
325+
},
326+
];
327+
328+
for (const { secret, expected } of cases) {
329+
const redacted = redactSensitiveFieldValue("token", secret);
330+
expect(redacted).toBe(expected);
331+
expect(redacted).not.toMatch(/[\uD800-\uDFFF]/u);
332+
}
333+
});
334+
312335
it("masks bearer tokens", () => {
313336
const input = "Authorization: Bearer abcdef1234567890ghij";
314337
const output = redactSensitiveText(input, { mode: "tools" });

src/logging/redact.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Redaction helpers scrub secrets and sensitive identifiers from log output.
2+
import { sliceUtf16Safe } from "@openclaw/normalization-core/utf16-slice";
23
import type { OpenClawConfig } from "../config/types.openclaw.js";
34
import { compileConfigRegex } from "../security/config-regex.js";
45
import { readLoggingConfig } from "./config.js";
@@ -354,8 +355,8 @@ function maskToken(token: string): string {
354355
if (token.length < DEFAULT_REDACT_MIN_LENGTH) {
355356
return "***";
356357
}
357-
const start = token.slice(0, DEFAULT_REDACT_KEEP_START);
358-
const end = token.slice(-DEFAULT_REDACT_KEEP_END);
358+
const start = sliceUtf16Safe(token, 0, DEFAULT_REDACT_KEEP_START);
359+
const end = sliceUtf16Safe(token, -DEFAULT_REDACT_KEEP_END);
359360
return `${start}${end}`;
360361
}
361362

0 commit comments

Comments
 (0)