mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-21 07:35:08 +00:00
parent
32ea104192
commit
458421af46
3 changed files with 101 additions and 17 deletions
|
|
@ -135,7 +135,7 @@ Finally, clients have the ability to *alias* MIME types when sending data to
|
|||
the clipboard. To do that, the client must send a ``type=walias`` packet of the
|
||||
form::
|
||||
|
||||
<OSC>5522;type=walias;mime=<base64 encoded target MIME type>;<base64 encoded, space separated list of aliases><ST>
|
||||
<OSC>5522;type=walias:mime=<base64 encoded target MIME type>;<base64 encoded, space separated list of aliases><ST>
|
||||
|
||||
The effect of an alias is that the system clipboard will make available all the
|
||||
aliased MIME types, with the same data as was transmitted for the target MIME
|
||||
|
|
|
|||
|
|
@ -382,13 +382,24 @@ class ClipboardRequestManager:
|
|||
except Exception:
|
||||
log_error('Malformed OSC 5522: metadata is not key=value pairs')
|
||||
return
|
||||
m[k] = decode_metadata_value(k, v)
|
||||
m[k] = v
|
||||
typ = m.get('type', '')
|
||||
try:
|
||||
m = {k: decode_metadata_value(k, v) for k, v in m.items()}
|
||||
except Exception as e:
|
||||
log_error(f'Malformed OSC 5522: could not decode a metadata value. Error: {e}')
|
||||
if typ in ('wdata', 'walias') and self.in_flight_write_request is not None:
|
||||
self.abort_write_request(self.in_flight_write_request, 'EINVAL')
|
||||
return
|
||||
if typ == 'read':
|
||||
payload = base64.standard_b64decode(epayload)
|
||||
try:
|
||||
mime_types = tuple(base64.standard_b64decode(epayload).decode('utf-8').split())
|
||||
except Exception as e:
|
||||
log_error(f'Malformed OSC 5522: read request payload is not valid base64 encoded UTF-8. Error: {e}')
|
||||
return
|
||||
rr = ReadRequest(
|
||||
is_primary_selection=m.get('loc', '') == 'primary',
|
||||
mime_types=tuple(payload.decode('utf-8').split()),
|
||||
mime_types=mime_types,
|
||||
protocol_type=ProtocolType.osc_5522,
|
||||
id=sanitize_id(m.get('id', '')),
|
||||
human_name=m.get('name', ''),
|
||||
|
|
@ -406,14 +417,23 @@ class ClipboardRequestManager:
|
|||
self.handle_write_request(self.in_flight_write_request)
|
||||
elif typ == 'walias':
|
||||
wr = self.in_flight_write_request
|
||||
if wr is None:
|
||||
return
|
||||
mime = m.get('mime', '')
|
||||
if mime and wr is not None:
|
||||
if not mime:
|
||||
log_error('Clipboard alias request has no MIME type, aborting the write request')
|
||||
self.abort_write_request(wr, 'EINVAL')
|
||||
return
|
||||
try:
|
||||
aliases = base64.standard_b64decode(epayload).decode('utf-8').split()
|
||||
for alias in aliases:
|
||||
wr.aliases[alias] = mime
|
||||
except Exception as e:
|
||||
log_error(f'Clipboard alias request payload is not valid base64 encoded UTF-8, aborting the write request. Error: {e}')
|
||||
self.abort_write_request(wr, 'EINVAL')
|
||||
return
|
||||
for alias in aliases:
|
||||
wr.aliases[alias] = mime
|
||||
elif typ == 'wdata':
|
||||
wr = self.in_flight_write_request
|
||||
w = get_boss().window_id_map.get(self.window_id)
|
||||
if wr is None:
|
||||
return
|
||||
mime = m.get('mime', '')
|
||||
|
|
@ -421,22 +441,22 @@ class ClipboardRequestManager:
|
|||
try:
|
||||
wr.add_base64_data(epayload, mime)
|
||||
except OSError:
|
||||
if w is not None:
|
||||
w.screen.send_escape_code_to_child(ESC_OSC, wr.encode_response(status='EIO'))
|
||||
self.in_flight_write_request = None
|
||||
self.abort_write_request(wr, 'EIO')
|
||||
raise
|
||||
except Exception:
|
||||
if w is not None:
|
||||
w.screen.send_escape_code_to_child(ESC_OSC, wr.encode_response(status='EINVAL'))
|
||||
self.in_flight_write_request = None
|
||||
self.abort_write_request(wr, 'EINVAL')
|
||||
raise
|
||||
if wr.max_size_exceeded:
|
||||
if w is not None:
|
||||
w.screen.send_escape_code_to_child(ESC_OSC, wr.encode_response(status='EFBIG'))
|
||||
self.in_flight_write_request = None
|
||||
self.abort_write_request(wr, 'EFBIG')
|
||||
else:
|
||||
self.commit_write_request(wr)
|
||||
|
||||
def abort_write_request(self, wr: WriteRequest, status: str) -> None:
|
||||
self.in_flight_write_request = None
|
||||
w = get_boss().window_id_map.get(self.window_id)
|
||||
if w is not None:
|
||||
w.screen.send_escape_code_to_child(ESC_OSC, wr.encode_response(status=status))
|
||||
|
||||
def commit_write_request(self, wr: WriteRequest, needs_flush: bool = True) -> None:
|
||||
if needs_flush:
|
||||
wr.flush_base64_data()
|
||||
|
|
|
|||
|
|
@ -97,3 +97,67 @@ class TestClipboard(BaseTest):
|
|||
self.ae(c.wtcbuf, b'')
|
||||
finally:
|
||||
set_boss(None)
|
||||
|
||||
def test_clipboard_malformed_write_packets(self):
|
||||
from kitty.clipboard import Clipboard, ClipboardRequestManager, ClipboardType, encode_mime
|
||||
from kitty.fast_data_types import set_boss
|
||||
|
||||
class Window:
|
||||
id = 1
|
||||
|
||||
def __init__(self, screen):
|
||||
self.screen = screen
|
||||
|
||||
class Boss:
|
||||
def __init__(self, window):
|
||||
self.clipboard = Clipboard()
|
||||
self.primary_selection = Clipboard(ClipboardType.primary_selection)
|
||||
self.window_id_map = {window.id: window}
|
||||
|
||||
s = self.create_screen()
|
||||
c = s.callbacks
|
||||
w = Window(s)
|
||||
set_boss(Boss(w))
|
||||
try:
|
||||
crm = ClipboardRequestManager(w.id)
|
||||
|
||||
def send(metadata, epayload=b''):
|
||||
data = metadata.encode('ascii')
|
||||
if epayload:
|
||||
data += b';' + epayload
|
||||
crm.parse_osc_5522(memoryview(data))
|
||||
|
||||
mime = f'mime={encode_mime("text/plain")}'
|
||||
|
||||
def t(*packets):
|
||||
c.clear()
|
||||
send('type=write:id=w1')
|
||||
send(f'type=wdata:{mime}', standard_b64encode(b'xxx'))
|
||||
c.clear()
|
||||
for packet in packets:
|
||||
send(*packet)
|
||||
self.ae(c.wtcbuf, b'\x1b]5522;type=write:status=EINVAL:id=w1\x1b\\')
|
||||
self.assertIsNone(crm.in_flight_write_request)
|
||||
# further packets for the aborted request must be ignored
|
||||
c.clear()
|
||||
send(f'type=wdata:{mime}', standard_b64encode(b'xxx'))
|
||||
send('type=wdata')
|
||||
self.ae(c.wtcbuf, b'')
|
||||
|
||||
# alias payload that is not valid base64
|
||||
t((f'type=walias:{mime}', b'AAA'))
|
||||
# alias payload that is not valid UTF-8
|
||||
t((f'type=walias:{mime}', b'/w=='))
|
||||
# alias packet without a MIME type
|
||||
t(('type=walias', standard_b64encode(b'text/rtf')))
|
||||
# metadata value that is not valid base64
|
||||
t(('type=walias:mime=AAA', standard_b64encode(b'text/rtf')))
|
||||
t(('type=wdata:mime=AAA', standard_b64encode(b'xxx')))
|
||||
# a malformed read packet must not abort the write request
|
||||
c.clear()
|
||||
send('type=write:id=w2')
|
||||
send('type=read:id=r1', b'AAA')
|
||||
self.ae(c.wtcbuf, b'')
|
||||
self.assertIsNotNone(crm.in_flight_write_request)
|
||||
finally:
|
||||
set_boss(None)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue