Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
fix: conditionally opt out of request checksums for Aliyun OSS only
ikelvingo0432e180b43f
diff --git a/src/services/storage/s3.py b/src/services/storage/s3.pyindex 4a6c371f..e5f0c3d0 100644--- a/src/services/storage/s3.py+++ b/src/services/storage/s3.py@@ -19,6 +19,21 @@ def _is_aliyun_oss_endpoint(endpoint_url: Optional[str]) -> bool:return hostname.endswith('.aliyuncs.com')+def _botocore_supports_request_checksum() -> bool:+ """True if the installed botocore accepts request_checksum_calculation.++ The option was added in botocore 1.36.0. Passing it on older installs+ makes every S3 client fail with a TypeError, including AWS/MinIO users+ that never touch Aliyun OSS, so it must be applied conditionally.+ """+ try:+ import botocore+ version = tuple(int(x) for x in botocore.__version__.split('.')[:2])+ except Exception:+ return False+ return version >= (1, 36)++class S3StorageBackend:"""S3 storage backend with lazy boto3 initialization."""@@ -59,11 +74,22 @@ class S3StorageBackend:if self.session_token:client_kwargs['aws_session_token'] = self.session_token- if _is_aliyun_oss_endpoint(resolved_endpoint):- addressing_style = 'virtual'- else:- addressing_style = 'path' if self.use_path_style else 'auto'- client_kwargs['config'] = Config(signature_version='s3v4', s3={'addressing_style': addressing_style})+ config_kwargs = {+ 'signature_version': 's3v4',+ 's3': {+ 'addressing_style': (+ 'virtual' if _is_aliyun_oss_endpoint(resolved_endpoint)+ else 'path' if self.use_path_style else 'auto'+ ),+ },+ }+ # Aliyun OSS rejects the aws-chunked transfer encoding triggered by+ # request checksums, so opt out for OSS endpoints. AWS/MinIO keep the+ # botocore default. Guarded by the version that introduced the option.+ if (_is_aliyun_oss_endpoint(resolved_endpoint)+ and _botocore_supports_request_checksum()):+ config_kwargs['request_checksum_calculation'] = 'when_required'+ client_kwargs['config'] = Config(**config_kwargs)return boto3.client(**client_kwargs)diff --git a/tests/test_cov_storage_s3.py b/tests/test_cov_storage_s3.pyindex acf87f61..1ac4ecf2 100644--- a/tests/test_cov_storage_s3.py+++ b/tests/test_cov_storage_s3.py@@ -483,6 +483,45 @@ def test_get_client_uses_virtual_addressing_for_aliyun_oss():assert mock_config.call_args.kwargs['s3'] == {'addressing_style': 'virtual'}+def test_get_client_checksum_option_only_for_aliyun_oss():+ """request_checksum_calculation must NOT be set for generic AWS/MinIO."""+ backend = make_backend(endpoint_url='http://minio:9000')+ with patch('boto3.client', return_value=MagicMock()), patch(+ 'botocore.config.Config', return_value=MagicMock()+ ) as mock_config:+ backend._get_client()++ assert 'request_checksum_calculation' not in mock_config.call_args.kwargs+++def test_get_client_checksum_option_set_for_aliyun_oss_new_botocore():+ """Aliyun OSS + botocore >= 1.36 opts out of request checksums."""+ backend = make_backend(endpoint_url='https://oss-cn-shanghai.aliyuncs.com')+ with patch('boto3.client', return_value=MagicMock()), patch(+ 'botocore.config.Config', return_value=MagicMock()+ ) as mock_config, patch(+ 'src.services.storage.s3._botocore_supports_request_checksum',+ return_value=True,+ ):+ backend._get_client()++ assert mock_config.call_args.kwargs['request_checksum_calculation'] == 'when_required'+++def test_get_client_checksum_option_skipped_for_old_botocore():+ """botocore < 1.36 must not receive the unsupported option at all."""+ backend = make_backend(endpoint_url='https://oss-cn-shanghai.aliyuncs.com')+ with patch('boto3.client', return_value=MagicMock()), patch(+ 'botocore.config.Config', return_value=MagicMock()+ ) as mock_config, patch(+ 'src.services.storage.s3._botocore_supports_request_checksum',+ return_value=False,+ ):+ backend._get_client()++ assert 'request_checksum_calculation' not in mock_config.call_args.kwargs++def test_get_client_is_cached():backend = make_backend()fake_boto_client = MagicMock()