diff --git a/b2sdk/_internal/transfer/outbound/upload_source.py b/b2sdk/_internal/transfer/outbound/upload_source.py index 647336a2..50265330 100644 --- a/b2sdk/_internal/transfer/outbound/upload_source.py +++ b/b2sdk/_internal/transfer/outbound/upload_source.py @@ -89,13 +89,7 @@ def __init__( self.content_sha1 = content_sha1 def __repr__(self) -> str: - return '<{classname} data={data} id={id}>'.format( - classname=self.__class__.__name__, - data=str(self.data_bytes[:20]) + '...' - if len(self.data_bytes) > 20 - else self.data_bytes, - id=id(self), - ) + return f'<{self.__class__.__name__} content_length={len(self.data_bytes)} id={id(self)}>' def get_content_length(self) -> int: return len(self.data_bytes) @@ -178,6 +172,8 @@ def __init__( """ super().__init__(local_path, content_sha1) self.file_size = self.content_length + if offset < 0 or offset > self.file_size: + raise ValueError('Range offset outside file size') self.offset = offset if length is None: self.content_length = self.file_size - self.offset @@ -316,7 +312,8 @@ def open(self): return self.stream_opener() def _set_content_length_and_sha1(self) -> None: - sha1, content_length = hex_sha1_of_unlimited_stream(self.open()) + with self.open() as stream: + sha1, content_length = hex_sha1_of_unlimited_stream(stream) self._content_length = content_length self._content_sha1 = sha1 diff --git a/changelog.d/+upload-source-safety.fixed.md b/changelog.d/+upload-source-safety.fixed.md new file mode 100644 index 00000000..7bfbbe7b --- /dev/null +++ b/changelog.d/+upload-source-safety.fixed.md @@ -0,0 +1 @@ +Protect upload data in representations, close checksum streams, and validate local file range offsets. diff --git a/test/unit/stream/test_regression_streams.py b/test/unit/stream/test_regression_streams.py index bdffe4a0..626faa8e 100644 --- a/test/unit/stream/test_regression_streams.py +++ b/test/unit/stream/test_regression_streams.py @@ -123,11 +123,6 @@ def test_c15_stream_range_without_length_computes_length(self): length = source.get_content_length() assert length == 10, 'wrong computed length' - @known_defect( - 'C14', - 'UploadSourceStream does not close the stream it opened to compute the checksum', - raises=DefectStillPresent, - ) def test_c14_stream_is_closed_after_checksum_computation(self): """C14: computing the checksum must close the stream it opened.""" opened = [] @@ -145,17 +140,13 @@ def opener(): class TestUploadSourceLocalFileRange: @pytest.mark.parametrize('offset', [-3, 15]) - @known_defect( - 'C52', - 'UploadSourceLocalFileRange accepts a negative offset or an offset past the end of the file', - raises=pytest.fail.Exception, - ) - def test_c52_invalid_offset_is_rejected(self, tmp_path, offset): + @pytest.mark.parametrize('length', [None, 0, 1]) + def test_c52_invalid_offset_is_rejected(self, tmp_path, offset, length): """C52: negative offset or offset past end (file is 10 bytes) must be refused.""" path = tmp_path / 'f.bin' path.write_bytes(b'0123456789') with pytest.raises(ValueError): - UploadSourceLocalFileRange(str(path), offset=offset) + UploadSourceLocalFileRange(str(path), offset=offset, length=length) class TestUnboundWriteIntentMemory: diff --git a/test/unit/stream/test_upload_source_safety.py b/test/unit/stream/test_upload_source_safety.py new file mode 100644 index 00000000..fb6f2545 --- /dev/null +++ b/test/unit/stream/test_upload_source_safety.py @@ -0,0 +1,57 @@ +###################################################################### +# +# File: test/unit/stream/test_upload_source_safety.py +# +# Copyright 2026 Backblaze Inc. All Rights Reserved. +# +# License https://www.backblaze.com/using_b2_code.html +# +###################################################################### +import io +import logging + +import pytest + +from b2sdk._internal.transfer.outbound.upload_source import ( + UploadSourceBytes, + UploadSourceLocalFileRange, + UploadSourceStream, +) + + +@pytest.mark.parametrize( + 'payload', [b'sample-data', b'sample-data-' * 5, bytearray(b'sample-data')] +) +def test_bytes_source_repr_and_logs_do_not_expose_payload(payload, caplog): + source = UploadSourceBytes(payload) + logger = logging.getLogger('test.upload-source') + with caplog.at_level(logging.DEBUG, logger=logger.name): + logger.debug('upload source: %r', source) + assert 'sample-data' not in repr(source) + assert caplog.records + assert all('sample-data' not in record.getMessage() for record in caplog.records) + assert source.get_content_length() == len(payload) + + +def test_stream_is_closed_if_checksum_read_fails(): + class FailingStream(io.BytesIO): + def read(self, size=-1): + raise OSError('read failed') + + stream = FailingStream(b'content') + source = UploadSourceStream(lambda: stream) + with pytest.raises(OSError, match='read failed'): + source.get_content_sha1() + assert stream.closed + + +@pytest.mark.parametrize( + 'offset,length,expected', [(0, None, b'abc'), (3, None, b''), (3, 0, b''), (1, 2, b'bc')] +) +def test_file_range_accepts_valid_boundaries(tmp_path, offset, length, expected): + path = tmp_path / 'data.bin' + path.write_bytes(b'abc') + source = UploadSourceLocalFileRange(path, offset=offset, length=length) + assert source.get_content_length() == len(expected) + with source.open() as stream: + assert stream.read() == expected