Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([ansistrip-osc-alternative-unreachable]): stop greedy OSC body at first ST terminator

[._.]/ Adam Eivye1bf622f5d52
diff --git a/server/lib/ansiStrip.js b/server/lib/ansiStrip.js
index bbb4a12042..704878f808 100644
--- a/server/lib/ansiStrip.js
+++ b/server/lib/ansiStrip.js
@@ -16,7 +16,14 @@
// 0x5C-0x5F, which includes `]` (0x5D); if it ran first it would strip the
// `\x1B]` opener only and leak the body+terminator. The bare `\x1B]` (no
// terminator) case still falls through to the single-byte branch.
-export const ANSI_PATTERN = /\x1B(?:\][^\x07]*(?:\x07|\x1B\\)|[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g;
+//
+// Body alternation `[^\x07\x1B]|\x1B(?!\\)` stops greedy matching at the
+// FIRST `\x1B\\` (ST terminator). A naive `[^\x07]*` would otherwise prefer
+// the latest possible ST and swallow visible text between two adjacent
+// OSC sequences (e.g. `\x1B]a\x1B\\VISIBLE\x1B]b\x1B\\` → `` instead of
+// `VISIBLE`). Each character produces at most one alternative attempt, so
+// the alternation does not introduce catastrophic backtracking.
+export const ANSI_PATTERN = /\x1B(?:\](?:[^\x07\x1B]|\x1B(?!\\))*(?:\x07|\x1B\\)|[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g;
const INCOMPLETE_CSI = /^\x1B\[[0-?]*[ -/]*$/;
const INCOMPLETE_OSC = /^\x1B\][^\x07\x1B]*$/;
diff --git a/server/lib/ansiStrip.test.js b/server/lib/ansiStrip.test.js
index 68ad498a42..626004c97a 100644
--- a/server/lib/ansiStrip.test.js
+++ b/server/lib/ansiStrip.test.js
@@ -39,6 +39,23 @@ describe('stripAnsi (one-shot)', () => {
expect(stripAnsi('\x1B]8;;https://example.com\x07link\x1B]8;;\x07')).toBe('link');
});
+ it('does not swallow visible text between two adjacent ST-terminated OSC sequences', () => {
+ // Body alternation must stop at the FIRST `\x1B\\`. Without the
+ // `\x1B(?!\\)` guard, greedy `[^\x07]*` would match past the inner ST
+ // and consume `VISIBLE`.
+ expect(stripAnsi('\x1B]0;one\x1B\\VISIBLE\x1B]0;two\x1B\\after')).toBe('VISIBLEafter');
+ });
+
+ it('does not swallow visible text when an ST-OSC is followed by a BEL-OSC', () => {
+ expect(stripAnsi('\x1B]0;one\x1B\\VISIBLE\x1B]0;two\x07after')).toBe('VISIBLEafter');
+ });
+
+ it('treats a bare ESC inside an OSC body (not followed by `\\\\`) as part of the body', () => {
+ // `\x1B(?!\\)` lets a stray non-ST ESC stay in the body; the whole
+ // sequence still strips when a real terminator arrives.
+ expect(stripAnsi('\x1B]0;foo\x1Bbar\x07after')).toBe('after');
+ });
+
it('strips a bare `\\x1B]` (single-byte path) when nothing follows', () => {
expect(stripAnsi('a\x1B]b')).toBe('ab');
});