fix(clipboard): neutralize termflow OSC 52 clipboard hijacking at the source#484
Merged
Conversation
termflow's RenderFeatures.clipboard defaults to True, causing it to emit OSC 52 escape sequences that silently overwrite the user's clipboard with every rendered code block. The patch replaces Renderer._copy_to_clipboard with a no-op to prevent this behavior at the source, ensuring robust protection regardless of per-call feature flags or future code paths. - Add patch_termflow_clipboard() to monkey-patch Renderer._copy_to_clipboard as a no-op - Integrate the patch into apply_all_patches() for automatic activation - Update module docstring to reflect broader third-party library patching scope - Ensure all changes are idempotent and fail silently if dependencies are absent
- Verify that _copy_to_clipboard is a no-op and produces no output - End-to-end test confirming no OSC 52 escape sequences appear in rendered output - Guard against crashes when termflow is not installed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
termflow's
RenderFeatures.clipboarddefaults toTrue. When the rendererfinishes a code block, it emits an OSC 52 escape sequence
(
\x1b]52;c;<base64>\x07) that modern terminals (iTerm2, Kitty, Alacritty,WezTerm, tmux) silently interpret as a clipboard-write command — clobbering
whatever the user had copied, with no prompt or consent.
PR #335 added explicit
RenderFeatures(clipboard=False)at the two knownTermflowRendererinstantiation sites, but this is whack-a-mole: any futurecode path, a new termflow version with changed defaults, or a missed
instantiation site reintroduces the bug.
Fix
Added
patch_termflow_clipboard()topydantic_patches.py. It replacestermflow.render.renderer.Renderer._copy_to_clipboardwith a no-op atstartup (via the existing
apply_all_patches()call incli_runner.py).This kills the behaviour at the source regardless of:
RenderFeatures(clipboard=...)flags~/.config/termflow/config.toml)The existing per-site
clipboard=Falseflags are left in place asdefense-in-depth and for documentation value.
Testing
test_copy_to_clipboard_is_noop— confirms the patched method writes nothingtest_code_block_render_emits_no_osc52— end-to-end: renders a code blockwith
clipboard=Trueand asserts no\x1b]52sequence leaks into outputtest_patch_does_not_crash_without_termflow— verifies graceful failure iftermflow is not installed
All 26 tests in
test_pydantic_patches_full_coverage.pypass.Files Changed
code_puppy/pydantic_patches.pypatch_termflow_clipboard()no-op patch, registered inapply_all_patches()tests/test_pydantic_patches_full_coverage.pyTestPatchTermflowClipboardclass with 3 regression tests