Skip to content

Commit 57405e9

Browse files
authored
feat: Support urllib3 >= 2.6.0 (#1658)
feat: Support urllib3 >= 2.6.0 **Context**: * This library implements a custom decoders ( `_GzipDecoder` , `_BrotliDecoder` ) which inherit from `urllib3.response.ContentDecoder` * Interface of `urllib3.response.ContentDecoder` was changed in [2.6.0](https://urllib3.readthedocs.io/en/stable/changelog.html#id1) to fix security vulnerability for highly compressed data reads. (Decompression bombs) Hence we need to change our interfaces as well. **Changes** * Add `max_length` param on decompress method, provide default value of -1 (same as urllib3's decompress) * Provide backwards compatibility ( ie urllib3 <= 2.5.0)
1 parent ddce7e5 commit 57405e9

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

google/cloud/storage/_media/requests/download.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ def __init__(self, checksum):
711711
super().__init__()
712712
self._checksum = checksum
713713

714-
def decompress(self, data):
714+
def decompress(self, data, max_length=-1):
715715
"""Decompress the bytes.
716716
717717
Args:
@@ -721,7 +721,11 @@ def decompress(self, data):
721721
bytes: The decompressed bytes from ``data``.
722722
"""
723723
self._checksum.update(data)
724-
return super().decompress(data)
724+
try:
725+
return super().decompress(data, max_length=max_length)
726+
except TypeError:
727+
# Fallback for urllib3 < 2.6.0 which lacks `max_length` support.
728+
return super().decompress(data)
725729

726730

727731
# urllib3.response.BrotliDecoder might not exist depending on whether brotli is
@@ -747,7 +751,7 @@ def __init__(self, checksum):
747751
self._decoder = urllib3.response.BrotliDecoder()
748752
self._checksum = checksum
749753

750-
def decompress(self, data):
754+
def decompress(self, data, max_length=-1):
751755
"""Decompress the bytes.
752756
753757
Args:
@@ -757,10 +761,19 @@ def decompress(self, data):
757761
bytes: The decompressed bytes from ``data``.
758762
"""
759763
self._checksum.update(data)
760-
return self._decoder.decompress(data)
764+
try:
765+
return self._decoder.decompress(data, max_length=max_length)
766+
except TypeError:
767+
# Fallback for urllib3 < 2.6.0 which lacks `max_length` support.
768+
return self._decoder.decompress(data)
761769

762770
def flush(self):
763771
return self._decoder.flush()
764772

773+
@property
774+
def has_unconsumed_tail(self) -> bool:
775+
return self._decoder.has_unconsumed_tail
776+
777+
765778
else: # pragma: NO COVER
766779
_BrotliDecoder = None # type: ignore # pragma: NO COVER

testing/constraints-3.12.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ grpcio
77
proto-plus
88
protobuf
99
grpc-google-iam-v1
10-
urllib3==2.5.0

0 commit comments

Comments
 (0)