mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 11:24:58 +00:00
CLI: dedupe config validate errors and expose allowed values
This commit is contained in:
12
src/terminal/safe-text.test.ts
Normal file
12
src/terminal/safe-text.test.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { sanitizeTerminalText } from "./safe-text.js";
|
||||
|
||||
describe("sanitizeTerminalText", () => {
|
||||
it("removes C1 control characters", () => {
|
||||
expect(sanitizeTerminalText("a\u009bb\u0085c")).toBe("abc");
|
||||
});
|
||||
|
||||
it("escapes line controls while preserving printable text", () => {
|
||||
expect(sanitizeTerminalText("a\tb\nc\rd")).toBe("a\\tb\\nc\\rd");
|
||||
});
|
||||
});
|
||||
20
src/terminal/safe-text.ts
Normal file
20
src/terminal/safe-text.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { stripAnsi } from "./ansi.js";
|
||||
|
||||
/**
|
||||
* Normalize untrusted text for single-line terminal/log rendering.
|
||||
*/
|
||||
export function sanitizeTerminalText(input: string): string {
|
||||
const normalized = stripAnsi(input)
|
||||
.replace(/\r/g, "\\r")
|
||||
.replace(/\n/g, "\\n")
|
||||
.replace(/\t/g, "\\t");
|
||||
let sanitized = "";
|
||||
for (const char of normalized) {
|
||||
const code = char.charCodeAt(0);
|
||||
const isControl = (code >= 0x00 && code <= 0x1f) || (code >= 0x7f && code <= 0x9f);
|
||||
if (!isControl) {
|
||||
sanitized += char;
|
||||
}
|
||||
}
|
||||
return sanitized;
|
||||
}
|
||||
Reference in New Issue
Block a user