Skip to content

fix(paste): neutralize bracketed-paste markers in pasted payloads - #280

Merged
charliek merged 4 commits into
mainfrom
fix/bracketed-paste-sanitize
Aug 3, 2026
Merged

fix(paste): neutralize bracketed-paste markers in pasted payloads#280
charliek merged 4 commits into
mainfrom
fix/bracketed-paste-sanitize

Conversation

@charliek

@charliek charliek commented Aug 3, 2026

Copy link
Copy Markdown
Owner

The bug (security, pre-existing, all three UIs)

When DEC mode 2004 is active, all three UIs wrapped pasted text in ESC[200~ … ESC[201~ without inspecting the payload. Clipboard or dropped content containing ESC[201~ terminates the bracketed region early, and everything after it reaches the shell as directly typed input — a trailing newline executes it. Copying crafted text from a web page is enough.

CodeRabbit flagged this on #279 (in code that PR merely relocated). A cross-UI audit found the same hole independently hand-rolled in each UI:

UI site state
iced app/interactions.rs paste_bytes vulnerable
GTK terminal_view.rs paste_text_into vulnerable
Swift TerminalView.swift sendBracketedPaste vulnerable

Secondary route: shell_escape (Rust + its Swift mirror) escaped shell metacharacters but not ESC, so a maliciously-named dropped file smuggled the same sequence through the file-drop path.

The fix

  • roost_ui_model::bracketed_paste::wrap(text, bracketed) — one shared implementation for both Rust UIs, replacing two hand-rolled wraps. Sanitization scans the output tail after each byte, not the input: an input-side pass lets a removal splice its neighbours into a fresh marker (ESC[20 + ESC[200~ + 0~ reassembles). Verified against six splice/nesting attack vectors.
  • mac/Sources/Roost/BracketedPaste.swift — hand-mirrored with byte-identical semantics and the same test vectors, following the established shell_escape.rsShellEscape.swift convention (cross-referencing doc comments both ways). Takes Data because Swift's image/multi-file paste paths join before wrapping.
  • shell_escape drops ESC (removal, not backslash — a backslash doesn't neutralize it for the VT parser), Rust and Swift, with the vector added to both suites. Defense in depth: file drops now get sanitized at both the escape and paste boundaries.

Semantics: empty → zero bytes; unbracketed → verbatim (no region to escape); bracketed → markers removed as contiguous 6-byte sequences, partial prefixes preserved as ordinary payload, UTF-8 either side untouched.

One behavior alignment: Swift previously emitted a bare ESC[200~ESC[201~ for an empty payload; it now emits nothing, matching Rust. All four Swift call sites already guard emptiness.

Testing

  • cargo test -p roost-ui-model 278 passed; -p roost-iced 137; -p roost-linux all suites green
  • cd mac && swift test — 698 tests, 29 suites passed (incl. new BracketedPasteTests, ShellEscapeTests.testDropsEscapeByte)
  • clippy -D warnings + cargo fmt --check clean (the 3 roost-linux clippy type_complexity errors are pre-existing on main — verified with these changes stashed)
  • Adversarial pass by the orchestrator: six splice/nesting vectors, no marker survives into the wrapped body

Note

Left open for your review rather than merged — it touches the Swift daily driver and wasn't a requested change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WLKWsLV45DAk61xG6Utj3e

Summary by CodeRabbit

  • New Features
    • Improved bracketed paste handling across desktop platforms.
    • Embedded paste control markers are safely removed to prevent pasted content from ending or altering the paste region.
    • Empty and unframed content is handled cleanly, with UTF-8 text preserved.
  • Bug Fixes
    • ESC characters are removed before shell escaping while shell-sensitive characters remain properly escaped.
  • Tests
    • Added coverage for paste framing, marker handling, UTF-8 content, and shell escaping.

All three UIs wrapped pasted text in ESC[200~ ... ESC[201~ when DEC mode
2004 is active without inspecting the payload, so clipboard or dropped
content containing ESC[201~ terminated the bracketed region early and the
tail reached the shell as directly-typed input — a newline then executes
it. Found by CodeRabbit on #279 (in relocated code); a cross-UI audit
showed iced, GTK, and Swift each hand-rolled the same unsafe wrap.

Adds roost_ui_model::bracketed_paste::wrap, used by both Rust UIs, and a
hand-mirrored Swift BracketedPaste.swift with shared test vectors
(shell_escape.rs <-> ShellEscape.swift precedent). Sanitization scans the
OUTPUT tail after each byte so a removal cannot splice its neighbours into
a fresh marker. shell_escape now drops ESC as well, closing the same
smuggling route through crafted filenames on the file-drop path.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WLKWsLV45DAk61xG6Utj3e
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 4 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5deef6fd-411c-46a9-8ff9-798313dfb35d

📥 Commits

Reviewing files that changed from the base of the PR and between 5da8490 and 77fc8ae.

📒 Files selected for processing (6)
  • crates/roost-ui-model/src/drop_content.rs
  • crates/roost-ui-model/src/shell_escape.rs
  • mac/Sources/Roost/ShellEscape.swift
  • mac/Sources/Roost/TerminalView.swift
  • mac/Tests/RoostTests/BracketedPasteTests.swift
  • mac/Tests/RoostTests/ShellEscapeTests.swift
📝 Walkthrough

Walkthrough

The change adds shared bracketed-paste wrappers for Rust and macOS, removes embedded paste markers, updates paste delivery paths, and removes ESC characters during shell escaping. Tests cover framing, marker handling, UTF-8 content, and ESC removal.

Changes

Paste sanitization and integration

Layer / File(s) Summary
Bracketed-paste wrapper implementations
crates/roost-ui-model/src/bracketed_paste.rs, crates/roost-ui-model/src/lib.rs, mac/Sources/Roost/BracketedPaste.swift, mac/Tests/RoostTests/BracketedPasteTests.swift
Rust wrap and Swift wrapBracketedPaste handle empty, unframed, and framed payloads. They remove embedded markers and prevent marker formation after removal. Tests cover these cases and UTF-8 content.
Terminal paste integration
crates/roost-iced/src/app/interactions.rs, crates/roost-linux/src/terminal_view.rs, mac/Sources/Roost/TerminalView.swift
Paste entry points now use the wrapper while preserving bracketed-mode selection and callback delivery.
Shell escape handling
crates/roost-ui-model/src/shell_escape.rs, mac/Sources/Roost/ShellEscape.swift, mac/Tests/RoostTests/ShellEscapeTests.swift
Rust and Swift shell escaping now removes ESC characters and continues escaping following metacharacters. Tests and the Swift reference path were updated.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant PasteEntryPoint
  participant BracketedPasteWrapper
  participant TerminalCallback
  PasteEntryPoint->>BracketedPasteWrapper: wrap payload for selected bracketed mode
  BracketedPasteWrapper-->>PasteEntryPoint: sanitized framed or plain bytes
  PasteEntryPoint->>TerminalCallback: deliver paste bytes
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: neutralizing bracketed-paste markers in pasted payloads.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/bracketed-paste-sanitize

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/roost-ui-model/src/shell_escape.rs`:
- Around line 15-27: Reject paths containing U+001B before shell escaping so
distinct filenames cannot collapse to identical PTY input. Update the
drop-content path handling in drop_content.rs and the macOS drop handling in
TerminalView.swift, and ensure the escape implementations in shell_escape.rs and
ShellEscape.swift are not relied on to silently remove ESC; apply the same
rejection behavior consistently at both platforms.

In `@mac/Tests/RoostTests/ShellEscapeTests.swift`:
- Around line 51-58: Update the added test in ShellEscapeTests to use
swift-testing rather than XCTest: convert the containing ShellEscapeTests suite
to `@Suite` and its test methods to `@Test` as needed, replacing XCTAssertEqual
assertions with `#expect` while preserving both escape-byte vectors and expected
results.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d483a101-8f57-4a7a-a466-99f4fa4e13a6

📥 Commits

Reviewing files that changed from the base of the PR and between 2c2de10 and 1ad1565.

📒 Files selected for processing (10)
  • crates/roost-iced/src/app/interactions.rs
  • crates/roost-linux/src/terminal_view.rs
  • crates/roost-ui-model/src/bracketed_paste.rs
  • crates/roost-ui-model/src/lib.rs
  • crates/roost-ui-model/src/shell_escape.rs
  • mac/Sources/Roost/BracketedPaste.swift
  • mac/Sources/Roost/ShellEscape.swift
  • mac/Sources/Roost/TerminalView.swift
  • mac/Tests/RoostTests/BracketedPasteTests.swift
  • mac/Tests/RoostTests/ShellEscapeTests.swift

Comment thread crates/roost-ui-model/src/shell_escape.rs Outdated
Comment thread mac/Tests/RoostTests/ShellEscapeTests.swift
charliek and others added 3 commits August 3, 2026 03:36
Left by removing a scratch adversarial test locally; caught by CI's
cargo fmt --check.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WLKWsLV45DAk61xG6Utj3e
CodeRabbit review on #280: silently dropping ESC in shell_escape meant
the escaped path no longer referred to the real file, and two distinct
filenames could collapse to identical PTY input. drop_content already
rejects newline-bearing paths, so ESC now follows that precedent on
both sides and escape() is a pure escaper again (bodies byte-identical
to main). bracketed_paste::wrap remains the paste-boundary defense, so
the injection fix is unaffected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WLKWsLV45DAk61xG6Utj3e
CI run 30796756743 aborted swiftpm-testing-helper with SIGABRT and no
failing test — the swift-testing runner bug ShellEscapeTests.swift's
header already documents for swarms of trivially fast value-checks
under Xcode 26.x. Same assertions and vectors, separate harness.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WLKWsLV45DAk61xG6Utj3e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant