Skip to content

Commit 73e49aa

Browse files
fix(server): terminal history strips queries with embedded controls (pingdotgg#36027)
1 parent 1acee5b commit 73e49aa

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎apps/server/src/terminal/Manager.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,28 @@ it.layer(
14191419
}),
14201420
);
14211421

1422+
// Ghostty executes 0x00-0x0F inside a CSI and ignores DEL and 0x10-0x1F; none change the query.
1423+
it.effect.each([
1424+
{ name: "NUL in DA", data: "A\u001b[0\u0000cB", history: "AB" },
1425+
{ name: "DEL in DA", data: "A\u001b[0\u007fcB", history: "AB" },
1426+
{ name: "NUL in DECRQM", data: "A\u001b[?2026\u0000$pB", history: "AB" },
1427+
{ name: "CR in DSR", data: "AB\u001b[5\rnC", history: "AB\rC" },
1428+
{ name: "DLE in DSR", data: "AB\u001b[5\u0010nC", history: "ABC" },
1429+
])(
1430+
"strips a query with an embedded control but keeps what the terminal ran ($name)",
1431+
({ data, history }) =>
1432+
Effect.gen(function* () {
1433+
const { manager, ptyAdapter } = yield* createManager();
1434+
yield* manager.open(openInput());
1435+
ptyAdapter.processes[0]!.emitData(data);
1436+
yield* manager.close({ threadId: "thread-1" });
1437+
1438+
const reopened = yield* manager.open(openInput());
1439+
const restored = reopened.history.replaceAll("\u0000", "");
1440+
expect(restored.split("\u001b[?1049h")[0]).toBe(history);
1441+
}),
1442+
);
1443+
14221444
it.effect.each([
14231445
{ name: "OSC", sequence: "\u001b]0;unfinished" },
14241446
{ name: "DCS", sequence: "\u001bPunfinished" },

‎apps/server/src/terminal/Manager.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,17 +1274,30 @@ function sanitizeTerminalHistoryChunk(
12741274

12751275
if (nextCodePoint === 0x5b) {
12761276
let cursor = index + 2;
1277+
// Inside a CSI Ghostty executes 0x00-0x0F and ignores DEL and 0x10-0x1F, which it would
1278+
// print in ground state. None join the parameters; only executed ones survive a strip.
1279+
let body = "";
1280+
let executedControls = "";
12771281
while (cursor < input.length) {
1278-
const cancelled = isControlSequenceCancel(input.charCodeAt(cursor));
1279-
if (cancelled || isCsiFinalByte(input.charCodeAt(cursor))) {
1280-
const sequence = input.slice(index, cursor + 1);
1281-
const body = input.slice(index + 2, cursor);
1282+
const cursorCodePoint = input.charCodeAt(cursor);
1283+
const cancelled = isControlSequenceCancel(cursorCodePoint);
1284+
if (cancelled || isCsiFinalByte(cursorCodePoint)) {
12821285
if (cancelled || !shouldStripCsiSequence(body, input[cursor] ?? "")) {
1283-
append(sequence);
1286+
append(input.slice(index, cursor + 1));
1287+
} else {
1288+
append(executedControls);
12841289
}
12851290
index = cursor + 1;
12861291
break;
12871292
}
1293+
if (cursorCodePoint < 0x10) {
1294+
executedControls += input[cursor];
1295+
} else if (
1296+
(cursorCodePoint >= 0x20 || cursorCodePoint === 0x1b) &&
1297+
cursorCodePoint !== 0x7f
1298+
) {
1299+
body += input[cursor];
1300+
}
12881301
cursor += 1;
12891302
}
12901303
if (cursor >= input.length) {

0 commit comments

Comments
 (0)