@@ -150,10 +150,12 @@ def _parse_message(frame: str) -> dict[str, object]:
150150 return message
151151
152152
153- def _integer_field (message : dict [str , object ], name : str , default : int | None = None ) -> int :
153+ def _integer_field (
154+ message : dict [str , object ], name : str , default : int | None = None , maximum : int | None = None
155+ ) -> int :
154156 value = message .get (name , default )
155- if isinstance (value , bool ) or not isinstance (value , int ) or value < 0 :
156- raise CopyProtocolError (f"cp header { name } must be a non-negative integer" )
157+ if isinstance (value , bool ) or not isinstance (value , int ) or value < 0 or ( maximum is not None and value > maximum ) :
158+ raise CopyProtocolError (f"cp header { name } has an invalid integer value " )
157159 return value
158160
159161
@@ -371,7 +373,7 @@ def _start(self, message: dict[str, object]) -> None:
371373 if self .header is not None :
372374 raise CopyProtocolError ("cp sent a new header before ending the previous entry" )
373375 target = self ._safe_target (message .get ("path" ))
374- mode = _integer_field (message , "mode" )
376+ mode = _integer_field (message , "mode" , maximum = 0o777 )
375377 size = _integer_field (message , "size" )
376378 mtime = _integer_field (message , "mtime" )
377379 uid = _integer_field (message , "uid" , 0 )
@@ -532,7 +534,7 @@ async def cp_from_instance_async(
532534 """Asynchronous counterpart to :func:`cp_from_instance`."""
533535
534536 url , headers = connection_settings (client , instance_id , "cp" )
535- state = await asyncio . to_thread ( _DownloadState , Path (dst_path ), archive , callbacks )
537+ state = _DownloadState ( Path (dst_path ), archive , callbacks )
536538 try :
537539 async with connector (url , additional_headers = headers , max_size = None ) as websocket :
538540 await websocket .send (_download_request (src_path , follow_symlinks ))
@@ -541,7 +543,7 @@ async def cp_from_instance_async(
541543 frame = await websocket .recv ()
542544 except Exception as exc :
543545 raise CopyProtocolError ("cp download ended before the final marker" ) from exc
544- await asyncio . to_thread ( state .consume , frame )
546+ state .consume ( frame )
545547 except BaseException :
546- await asyncio . to_thread ( state .abort )
548+ state .abort ( )
547549 raise
0 commit comments