From 9f7cba07ab3777a2c11af93270ed5ab1d8225b0f Mon Sep 17 00:00:00 2001 From: Codeman maintainer Date: Wed, 28 Oct 2026 09:30:15 +0100 Subject: [PATCH] fix: strip nested paste markers until none is left (release review, #726) Ctrl+V's hand-built bracketed paste (#726) and the phone composer's dropped CSI 200~/201~ markers from the text in ONE pass, so a clipboard carrying ESC[20 ESC[201~ 1~ came out of it with an ESC[201~ that ended the frame early: everything after it was typed as keys and each CR ran. Both now strip until nothing matches. The composer's copy predates this release; it is the same one-liner. Co-Authored-By: Claude Opus 5.5 --- src/web/public/image-input.js | 7 +++++-- src/web/public/keyboard-accessory.js | 6 +++++- test/image-paste-trap.test.ts | 9 +++++++++ test/mobile-prompt-composer.test.ts | 10 ++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/web/public/image-input.js b/src/web/public/image-input.js index 58bf68ce..1f18526d 100644 --- a/src/web/public/image-input.js +++ b/src/web/public/image-input.js @@ -255,8 +255,11 @@ Object.assign(CodemanApp.prototype, { // onData as user input (scroll to the bottom, clear the selection). // The main pane's bracketed-paste branch and a tile's _onTerminalData // deliver it, including the local-echo flush (#219). Do not also write - // the frame from here. - const body = text.replace(/\x1b\[20[01]~/g, '').replace(/\r?\n/g, '\r'); + // the frame from here. Markers are stripped until none is left: one pass over + // `ESC[20` + `ESC[201~` + `1~` leaves an `ESC[201~` that would end the frame early. + let body = text; + while (/\x1b\[20[01]~/.test(body)) body = body.replace(/\x1b\[20[01]~/g, ''); + body = body.replace(/\r?\n/g, '\r'); terminal.input(`\x1b[200~${body}\x1b[201~`); } }); diff --git a/src/web/public/keyboard-accessory.js b/src/web/public/keyboard-accessory.js index 7ae3213c..a2596d03 100644 --- a/src/web/public/keyboard-accessory.js +++ b/src/web/public/keyboard-accessory.js @@ -1246,7 +1246,11 @@ const KeyboardAccessoryBar = { // the single-line input transport and is what terminal.paste() emitted. // Paste markers inside the text are dropped first: a clipboard carrying // `ESC[201~` would end the bracket early and run the rest without Enter. - const pasteText = text.replace(/\x1b\[20[01]~/g, '').replace(/\r?\n/g, '\r'); + // Strip until none is left: one pass over `ESC[20` + `ESC[201~` + `1~` leaves + // an `ESC[201~` behind. + let pasteText = text; + while (/\x1b\[20[01]~/.test(pasteText)) pasteText = pasteText.replace(/\x1b\[20[01]~/g, ''); + pasteText = pasteText.replace(/\r?\n/g, '\r'); if (pasteText.length > this._composerMaxLength) { app.showToast?.(`${label} is too long to send (maximum ${this._composerMaxLength.toLocaleString()} characters)`, 'error'); return false; diff --git a/test/image-paste-trap.test.ts b/test/image-paste-trap.test.ts index e1aaf32e..f117f539 100644 --- a/test/image-paste-trap.test.ts +++ b/test/image-paste-trap.test.ts @@ -259,6 +259,15 @@ describe('Ctrl+V paste trap', () => { expect(h.sentInputs).toEqual([]); }); + it('strips a marker hidden inside another, so the clipboard cannot end the frame early', () => { + const h = loadPasteHarness(); + + // One pass over this leaves `ESC[201~` between `ok` and the command. + h.firePaste({ text: 'ok\x1b[20\x1b[201~1~\nrm -rf ~/x\n' }); + + expect(h.inputted).toEqual(['\x1b[200~ok\rrm -rf ~/x\r\x1b[201~']); + }); + it('addresses the bracketed paste to the pane that received Ctrl+V', () => { const paneInputs: string[] = []; const pane: TargetTerminal = { diff --git a/test/mobile-prompt-composer.test.ts b/test/mobile-prompt-composer.test.ts index 77576db1..fe5bd548 100644 --- a/test/mobile-prompt-composer.test.ts +++ b/test/mobile-prompt-composer.test.ts @@ -725,6 +725,16 @@ describe('mobile shell Paste', () => { expect(app._sendInputAsync).toHaveBeenCalledWith('session-1', '\x1b[200~echo safe\rrm -rf ~/x\x1b[201~'); }); + it('drops a paste marker hidden inside another one', () => { + const { app, input, send } = openShellPaste(); + // One pass over this leaves `ESC[201~` between `ok` and the command. + input.value = 'ok\x1b[20\x1b[201~1~\nrm -rf ~/x'; + + send.click(); + + expect(app._sendInputAsync).toHaveBeenCalledWith('session-1', '\x1b[200~ok\rrm -rf ~/x\x1b[201~'); + }); + it('refuses a paste longer than one input frame and keeps the text for trimming', () => { const { app, bar, document, input, send } = openShellPaste(); input.value = 'x'.repeat(bar._composerMaxLength + 1);