diff --git a/docs/changelog.rst b/docs/changelog.rst index 4d5d59507..ace8c330e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -439,6 +439,9 @@ Detailed list of changes file. No longer rename remotely dropped files whose names differ only in case on case sensitive filesystems (:pull:`10769`, :pull:`10772`) +- Drag and drop protocol: Fix base64 data with padding at the end being + rejected, as allowed by the protocol (:iss:`10777`) + - Clipboard kitten: Fix only the last of several outputs that use the same MIME type being saved, for example with :code:`kitten clipboard -g picture.png picture.jpg`, the data is now diff --git a/kitty/base64.h b/kitty/base64.h index 1d36a1881..be4625aaa 100644 --- a/kitty/base64.h +++ b/kitty/base64.h @@ -28,6 +28,13 @@ base64_init_stream_decoder(base64_state *state) { static inline bool base64_decode_stream(base64_state *state, const uint8_t *src, size_t src_sz, uint8_t *dest, size_t *dest_sz) { + // Decoding nothing always succeeds. libbase64 fails any call made after + // it has seen padding, even an empty one, which breaks protocols that + // terminate streams with an empty chunk. + if (!src_sz) { + *dest_sz = 0; + return true; + } if (*dest_sz < required_buffer_size_for_base64_decode(src_sz)) return false; return base64_stream_decode(state, (const char *)src, src_sz, (char *)dest, dest_sz) == 1; } diff --git a/kitty/dnd.c b/kitty/dnd.c index aec24eadb..200ab5bfa 100644 --- a/kitty/dnd.c +++ b/kitty/dnd.c @@ -1601,8 +1601,9 @@ drag_add_pre_sent_data(Window *w, unsigned idx, const uint8_t *payload, size_t s item.data_decode_initialized = true; base64_init_stream_decoder(&item.base64_state); } - if (item.data_capacity < sz + item.data_size) { - size_t newcap = MAX(item.data_capacity * 2, sz + item.data_size); + const size_t needed = MAX(sz, required_buffer_size_for_base64_decode(sz)) + item.data_size; + if (item.data_capacity < needed) { + size_t newcap = MAX(item.data_capacity * 2, needed); uint8_t *tmp = realloc(item.optional_data, newcap); if (!tmp) abrt(ENOMEM, ""); item.optional_data = tmp; @@ -2145,12 +2146,12 @@ drag_process_item_data(Window *w, size_t idx, int has_more, const uint8_t *paylo // Decode and write payload data if (payload_sz > 0) { - RAII_ALLOC(uint8_t, decoded, malloc(payload_sz)); + size_t outlen = MAX(payload_sz, required_buffer_size_for_base64_decode(payload_sz)); + RAII_ALLOC(uint8_t, decoded, malloc(outlen)); if (!decoded) { cancel_drag(w, ENOMEM, "out of memory processing drag source item data"); return; } - size_t outlen = payload_sz; if (!base64_decode_stream(&ds.items[idx].base64_state, payload, payload_sz, decoded, &outlen)) { cancel_drag(w, EINVAL, "failed to base64 decode drag source item data"); return; diff --git a/kitty_tests/dnd.py b/kitty_tests/dnd.py index e223f48e0..0f5790892 100644 --- a/kitty_tests/dnd.py +++ b/kitty_tests/dnd.py @@ -1801,6 +1801,36 @@ class TestDnDProtocol(BaseTest): parse_bytes(screen, client_drag_pre_send(0, chunk2_b64, more=False)) self._assert_no_output(cap) + def test_drag_padded_base64_then_end_of_data(self) -> None: + """base64 padding at the end of the last chunk is accepted, followed by the empty terminator.""" + for raw in (b'hello from the repro', b'drag image', b'A'): + with self.subTest(raw=raw), dnd_test_window() as (screen, cap): + self._setup_drag_offer(screen, cap, 'text/plain') + padded = standard_b64encode(raw).decode() + self.assertTrue(padded.endswith('=')) + parse_bytes(screen, client_drag_pre_send(0, padded)) + parse_bytes(screen, client_drag_pre_send(0, '')) + self._assert_no_output(cap) + parse_bytes(screen, client_drag_add_image(1, 0, 1, 1, padded)) + parse_bytes(screen, _osc(f'{DND_CODE};t=p:x=-1:y=0:X=1:Y=1')) + self._assert_no_output(cap) + + def test_remote_drag_process_item_data_padded(self) -> None: + """MIME data whose last chunk carries base64 padding is accepted.""" + for data in (b'A', b'test data!'): + with self.subTest(data=data), dnd_test_window() as (screen, cap): + parse_bytes(screen, _osc(f'{DND_CODE};t=o:x=1;{machine_id()}')) + parse_bytes(screen, client_drag_offer_mimes(1, 'text/plain')) + cap.consume() + dnd_test_force_drag_dropped(cap.window_id) + with self.assertRaises(OSError): + dnd_test_drag_get_data(cap.window_id, 'text/plain') + cap.consume() + parse_bytes(screen, client_drag_send_data(0, standard_b64encode(data).decode(), more=True)) + parse_bytes(screen, client_drag_send_data(0, '')) + self._assert_no_output(cap) + self.assertEqual(dnd_test_drag_get_data(cap.window_id, 'text/plain'), data) + def test_drag_add_image_chunked(self) -> None: """Image data can be chunked across multiple escape codes.""" with dnd_test_window() as (screen, cap):