mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 22:55:29 +00:00
dnd: Accept base64 payloads with trailing padding
libbase64's stream decoder fails every call made after it has seen padding, even an empty one, so the empty message that terminates a stream was rejected. Also size the decode buffers for pre-sent and drag item data correctly, so that very short padded chunks such as QQ== are not rejected. Fixes #10777
This commit is contained in:
parent
6adff8efff
commit
7fbe251750
4 changed files with 45 additions and 4 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue