From 44b9aa166e836fb22b10a386b4e05cb32aa07cbc Mon Sep 17 00:00:00 2001 From: BlackAngelTV Date: Wed, 11 Nov 2026 21:32:09 +0100 Subject: [PATCH] fix(clipboard): record streamed clipboard writes in the history OSC 52 writes are committed as a chunker factory rather than bytes, so the history silently dropped every text copy made by a program. Extract the text from either representation, bounded so a huge write is not materialized. --- kitty/clipboard.py | 9 ++++---- kitty/clipboard_history.py | 28 +++++++++++++++++++++++++ kitty_tests/clipboard_history.py | 36 +++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/kitty/clipboard.py b/kitty/clipboard.py index 2cd0cf7..b807004 100644 --- a/kitty/clipboard.py +++ b/kitty/clipboard.py @@ -9,6 +9,7 @@ from gettext import gettext as _ from tempfile import TemporaryFile from typing import IO, NamedTuple, Union +from .clipboard_history import text_from_mime_data from .conf.utils import uniq from .constants import supports_primary_selection from .fast_data_types import ( @@ -113,15 +114,15 @@ class Clipboard: # history itself, so that secrets never reach it. if self.clipboard_type is not ClipboardType.clipboard: return - data = self.data.get('text/plain') - if not isinstance(data, bytes): + if 'text/plain' not in self.data: return try: - text = data.decode('utf-8') + # The payload can be a chunker rather than bytes, as writes are streamed from a temp file + text = text_from_mime_data(self.data['text/plain']) history = getattr(get_boss(), 'clipboard_history', None) except Exception: return - if history is not None: + if text and history is not None: history.add(text, private=private) def get_text(self) -> str: diff --git a/kitty/clipboard_history.py b/kitty/clipboard_history.py index 5b00c28..6e71029 100644 --- a/kitty/clipboard_history.py +++ b/kitty/clipboard_history.py @@ -15,6 +15,34 @@ MAX_ENTRY_CHARS = 64 * 1024 MAX_TOTAL_CHARS = 1024 * 1024 +def text_from_mime_data(data: object, max_chars: int = MAX_ENTRY_CHARS) -> str: + """Extract text from a clipboard MIME payload, or an empty string when it is not text. + + Clipboard payloads are either raw bytes or a factory returning a chunker, as the data can be + streamed from a temporary file. Only the start of the payload is materialized, since an entry + longer than max_chars is not kept anyway. + """ + limit = 4 * max_chars # a UTF-8 character takes at most four bytes + if isinstance(data, bytes | bytearray | memoryview): + raw = bytes(data[:limit]) + elif callable(data): + chunks: list[bytes] = [] + size = 0 + chunker = data() + while True: + q = chunker() + if not q: + break + chunks.append(q) + size += len(q) + if size >= limit: + break + raw = b''.join(chunks)[:limit] + else: + return '' + return raw.decode('utf-8', 'replace') + + def match_score(needle: str, haystack: str) -> int: """Return a score, lower is better, or -1 when haystack does not contain needle as a subsequence. diff --git a/kitty_tests/clipboard_history.py b/kitty_tests/clipboard_history.py index 3ad2daf..0499011 100644 --- a/kitty_tests/clipboard_history.py +++ b/kitty_tests/clipboard_history.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # License: GPL v3 Copyright: 2026, Kovid Goyal -from kitty.clipboard_history import ClipboardHistory, match_score +from kitty.clipboard_history import ClipboardHistory, match_score, text_from_mime_data from .base import BaseTest @@ -91,6 +91,40 @@ class TestClipboardHistory(BaseTest): self.ae(match_score('abc', 'cba'), -1) self.ae(match_score('Abc', 'xxabc'), 2) # matching is case insensitive + def test_text_from_bytes(self) -> None: + self.ae(text_from_mime_data(b'hello'), 'hello') + self.ae(text_from_mime_data('not bytes'), '') + self.ae(text_from_mime_data(None), '') + self.ae(text_from_mime_data(b'a\xffb'), 'a\ufffdb') + + def test_text_from_chunker(self) -> None: + # OSC 52 writes are streamed, so the clipboard stores a chunker factory not bytes + chunks = [b'first ', b'second', b''] + + def factory(): + it = iter(chunks) + + def chunker(): + return next(it, b'') + + return chunker + + self.ae(text_from_mime_data(factory), 'first second') + + def test_text_from_chunker_is_bounded(self) -> None: + def factory(): + remaining = [b'x' * 1000] * 1000 + it = iter(remaining) + + def chunker(): + return next(it, b'') + + return chunker + + ans = text_from_mime_data(factory, max_chars=10) + self.assertLessEqual(len(ans), 40) + self.assertTrue(ans) + def test_no_persistence_api(self) -> None: # The history is deliberately memory only: there is no save/load entry point to call h = ClipboardHistory()