Files
SnowLuma/packages/common/tests/log-sanitize.test.ts
DrSmoothl 0503af2fcc fix(webui): export logs without terminal control codes
Keep terminal styling confined to interactive console output while storing plain structured log lines for subscribers and file persistence.

Sanitize downloads again at the WebUI boundary so cached legacy entries and fallback-formatted records cannot leak CSI, OSC, DCS, or C1 control sequences. Add regression coverage for colored output, hyperlinks, control strings, malformed sequences, and preserved multiline text.
2026-07-16 19:20:48 +08:00

27 lines
972 B
TypeScript

import { describe, expect, it } from 'vitest';
import { sanitizeLogLine } from '../src/log-sanitize';
describe('sanitizeLogLine', () => {
it('removes SGR styling in seven-bit and eight-bit forms', () => {
expect(sanitizeLogLine('\x1b[31mred\x1b[0m / \u009b32mgreen\u009b0m')).toBe('red / green');
});
it('removes OSC hyperlinks without removing their visible label', () => {
expect(
sanitizeLogLine('\x1b]8;;https://example.com\x07visible\x1b]8;;\x07'),
).toBe('visible');
});
it('removes terminal control-string payloads', () => {
expect(sanitizeLogLine('before\x1bPprivate payload\x1b\\after')).toBe('beforeafter');
});
it('preserves tabs and newlines while removing other controls', () => {
expect(sanitizeLogLine('one\ttwo\nthree\r\x00')).toBe('one\ttwo\nthree');
});
it('does not leak unterminated terminal strings', () => {
expect(sanitizeLogLine('visible\x1b]8;;https://example.com')).toBe('visible');
});
});