xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #2072: clipboard-size.patch

File clipboard-size.patch, 4.6 KB (added by Antoine Martin, 3 years ago)

improved patch

  • xpra/clipboard/clipboard_base.py

     
    2828
    2929MIN_CLIPBOARD_COMPRESSION_SIZE = 512
    3030MAX_CLIPBOARD_PACKET_SIZE = 4*1024*1024
     31MAX_CLIPBOARD_RECEIVE_SIZE = envint("XPRA_MAX_CLIPBOARD_RECEIVE_SIZE", -1)
     32MAX_CLIPBOARD_SEND_SIZE = envint("XPRA_MAX_CLIPBOARD_SEND_SIZE", -1)
    3133
    3234from xpra.platform.features import CLIPBOARDS as PLATFORM_CLIPBOARDS
    3335ALL_CLIPBOARDS = [strtobytes(x) for x in PLATFORM_CLIPBOARDS]
     
    107109        self.can_send = d.boolget("can-send", True)
    108110        self.can_receive = d.boolget("can-receive", True)
    109111        self.max_clipboard_packet_size = MAX_CLIPBOARD_PACKET_SIZE
     112        self.max_clipboard_receive_size = MAX_CLIPBOARD_RECEIVE_SIZE
     113        self.max_clipboard_send_size = MAX_CLIPBOARD_SEND_SIZE
    110114        self.disabled_by_loop = []
    111115        self.filter_res = []
    112116        filter_res = d.strlistget("filters")
     
    131135
    132136    def get_info(self):
    133137        info = {
    134                 "type"      : str(self).replace("ClipboardProtocolHelper", ""),
    135                 "max_size"  : self.max_clipboard_packet_size,
    136                 "filters"   : [x.pattern for x in self.filter_res],
    137                 "requests"  : self._clipboard_request_counter,
    138                 "pending"   : tuple(self._clipboard_outstanding_requests.keys()),
     138                "type"          : str(self).replace("ClipboardProtocolHelper", ""),
     139                "max_size"      : self.max_clipboard_packet_size,
     140                "max_recv_size" : self.max_clipboard_receive_size,
     141                "max_send_size" : self.max_clipboard_send_size,
     142                "filters"       : [x.pattern for x in self.filter_res],
     143                "requests"      : self._clipboard_request_counter,
     144                "pending"       : tuple(self._clipboard_outstanding_requests.keys()),
    139145                "can-send"      : self.can_send,
    140146                "can-receive"   : self.can_receive,
    141147                "want_targets"  : self._want_targets,
     
    199205                        if selection not in self.disabled_by_loop:
    200206                            self.disabled_by_loop.append(selection)
    201207
    202     def set_direction(self, can_send, can_receive):
     208    def set_direction(self, can_send, can_receive, max_send_size=-1, max_receive_size=-1):
    203209        self.can_send = can_send
    204210        self.can_receive = can_receive
     211        if max_send_size >= 0:
     212            self.max_clipboard_send_size = max_send_size
     213        if max_receive_size >= 0:
     214            self.max_clipboard_receive_size = max_receive_size
    205215        for proxy in self._clipboard_proxies.values():
    206216            proxy.set_direction(can_send, can_receive)
    207217
     
    435445
    436446    def _munge_wire_selection_to_raw(self, encoding, dtype, dformat, data):
    437447        log("wire selection to raw, encoding=%s, type=%s, format=%s, len(data)=%s", encoding, dtype, dformat, len(data or ""))
     448        max_recv_datalen = self.max_clipboard_receive_size * 8 // max(8, dformat)
     449        if len(data) > max_recv_datalen:
     450            olen = len(data)
     451            data = data[:max_recv_datalen]
     452            log.info("Data copied out truncated because of clipboard policy %d to %d", olen, max_recv_datalen)
    438453        if encoding == b"bytes":
    439454            return data
    440455        elif encoding == b"integers":
     
    490505            if dtype is None or data is None or (dformat==0 and data==b""):
    491506                no_contents()
    492507                return
     508            log("perform security checking - datasize - %d, %d", len(data), self.max_clipboard_send_size)
     509            max_send_datalen = self.max_clipboard_send_size * 8 // max(8, dformat)
     510            if len(data) > max_send_datalen:
     511                truncated = len(data) - max_send_datalen
     512                data = data[:max_send_datalen]
     513            else:
     514                truncated = 0
    493515            munged = self._munge_raw_selection_to_wire(target, dtype, dformat, data)
    494516            wire_encoding, wire_data = munged
    495517            log("clipboard raw -> wire: %r -> %r", (dtype, dformat, data), munged)
     
    499521            wire_data = self._may_compress(dtype, dformat, wire_data)
    500522            if wire_data is not None:
    501523                self.send("clipboard-contents", request_id, selection,
    502                        dtype, dformat, wire_encoding, wire_data)
     524                       dtype, dformat, wire_encoding, wire_data, truncated)
    503525        proxy.get_contents(target, got_contents)
    504526
    505527    def _may_compress(self, dtype, dformat, wire_data):