From b43f6269f4356f8336fed2b5cc68dc47a16cd55c Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Fri, 27 Nov 2026 10:16:45 +0000 Subject: [PATCH] fix(storage)!: require v4 signing for response and generation parameters generate_signed_url_v2 now raises ValueError when response_type, response_disposition, or generation is specified, requiring V4 signing for those parameters. Existing V2 signed URL generation without these parameters and all V4 signed URL behavior remain unchanged. Updated unit tests in packages/google-cloud-storage/tests/unit/test__signing.py. BREAKING CHANGE: V2 signed URL generation raises ValueError when response_type, response_disposition, or generation is specified. [Generated-by: AI] --- .../google/cloud/storage/_signing.py | 36 +++++++++++------- .../google/cloud/storage/blob.py | 10 +++-- .../tests/unit/test__signing.py | 38 ++++++++----------- 3 files changed, 44 insertions(+), 40 deletions(-) diff --git a/packages/google-cloud-storage/google/cloud/storage/_signing.py b/packages/google-cloud-storage/google/cloud/storage/_signing.py index 256d60f68426..04f99bf576e7 100644 --- a/packages/google-cloud-storage/google/cloud/storage/_signing.py +++ b/packages/google-cloud-storage/google/cloud/storage/_signing.py @@ -324,17 +324,19 @@ def generate_signed_url_v2( by ``resource``. :type response_type: str - :param response_type: (Optional) Content type of responses to requests for - the signed URL. Ignored if content_type is set on - object/blob metadata. + :param response_type: (Optional) Only supported with V4 signed URLs + (``version="v4"``). Passing a non-``None`` value + raises :exc:`ValueError`. :type response_disposition: str - :param response_disposition: (Optional) Content disposition of responses to - requests for the signed URL. + :param response_disposition: (Optional) Only supported with V4 signed URLs + (``version="v4"``). Passing a non-``None`` + value raises :exc:`ValueError`. :type generation: str - :param generation: (Optional) A value that indicates which generation of - the resource to fetch. + :param generation: (Optional) Only supported with V4 signed URLs + (``version="v4"``). Passing a non-``None`` value raises + :exc:`ValueError`. :type headers: Union[dict|List(Tuple(str,str))] :param headers: @@ -356,6 +358,8 @@ def generate_signed_url_v2( signed URLs. See: https://cloud.google.com/storage/docs/xml-api/reference-headers#query + :raises: :exc:`ValueError` when ``response_type``, + ``response_disposition``, or ``generation`` is provided. :raises: :exc:`TypeError` when expiration is not a valid type. :raises: :exc:`AttributeError` if credentials is not an instance of :class:`google.auth.credentials.Signing`. @@ -364,6 +368,17 @@ def generate_signed_url_v2( :returns: A signed URL you can use to access the resource until expiration. """ + if ( + response_type is not None + or response_disposition is not None + or generation is not None + ): + raise ValueError( + "The 'response_type', 'response_disposition', and 'generation' " + "parameters are only supported with V4 signed URLs. " + "Use version='v4' instead." + ) + expiration_stamp = get_expiration_seconds_v2(expiration) canonical = canonicalize_v2(method, resource, query_parameters, headers) @@ -396,13 +411,6 @@ def generate_signed_url_v2( credentials, expiration_stamp, string_to_sign ) - if response_type is not None: - signed_query_params["response-content-type"] = response_type - if response_disposition is not None: - signed_query_params["response-content-disposition"] = response_disposition - if generation is not None: - signed_query_params["generation"] = generation - signed_query_params.update(canonical.query_parameters) sorted_signed_query_params = sorted(signed_query_params.items()) diff --git a/packages/google-cloud-storage/google/cloud/storage/blob.py b/packages/google-cloud-storage/google/cloud/storage/blob.py index 87493a30dfc0..47a1f7ce4906 100644 --- a/packages/google-cloud-storage/google/cloud/storage/blob.py +++ b/packages/google-cloud-storage/google/cloud/storage/blob.py @@ -523,17 +523,18 @@ def generate_signed_url( (Optional) Content disposition of responses to requests for the signed URL. For example, to enable the signed URL to initiate a file of ``blog.png``, use the value ``'attachment; - filename=blob.png'``. + filename=blob.png'``. Requires ``version="v4"``. :type response_type: str :param response_type: (Optional) Content type of responses to requests for the signed URL. Ignored if content_type is set on object/blob metadata. + Requires ``version="v4"``. :type generation: str :param generation: (Optional) A value that indicates which generation of the resource - to fetch. + to fetch. Requires ``version="v4"``. :type headers: dict :param headers: @@ -592,7 +593,10 @@ def generate_signed_url( hostname, use this value as the scheme. ``https`` will work only when using a CDN. Defaults to ``"http"``. - :raises: :exc:`ValueError` when version is invalid or mutually exclusive arguments are used. + :raises: :exc:`ValueError` when version is invalid, mutually exclusive + arguments are used, or ``response_type``, + ``response_disposition``, or ``generation`` is provided without + ``version="v4"``. :raises: :exc:`TypeError` when expiration is not a valid type. :raises: :exc:`AttributeError` if credentials is not an instance of :class:`google.auth.credentials.Signing`. diff --git a/packages/google-cloud-storage/tests/unit/test__signing.py b/packages/google-cloud-storage/tests/unit/test__signing.py index 073d46ea8ab3..396dfa992e7a 100644 --- a/packages/google-cloud-storage/tests/unit/test__signing.py +++ b/packages/google-cloud-storage/tests/unit/test__signing.py @@ -337,9 +337,6 @@ def _generate_helper( method="GET", content_md5=None, content_type=None, - response_type=None, - response_disposition=None, - generation=None, headers=None, query_parameters=None, ): @@ -361,9 +358,6 @@ def _generate_helper( method=method, content_md5=content_md5, content_type=content_type, - response_type=response_type, - response_disposition=response_disposition, - generation=generation, headers=headers, query_parameters=query_parameters, service_account_email=None, @@ -420,17 +414,6 @@ def _generate_helper( self.assertEqual(params["Expires"], str(expiration)) self.assertEqual(params["Signature"], signed) - if response_type is not None: - self.assertEqual(params["response-content-type"], response_type) - - if response_disposition is not None: - self.assertEqual( - params["response-content-disposition"], response_disposition - ) - - if generation is not None: - self.assertEqual(params["generation"], str(generation)) - if query_parameters is not None: for key, value in query_parameters.items(): value = value.strip() if value else "" @@ -451,17 +434,26 @@ def test_w_method_resumable(self): method = "RESUMABLE" self._generate_helper(method=method) + def _assert_v4_required(self, **kwargs): + credentials = _make_credentials(signer_email="service@example.com") + credentials.sign_bytes.return_value = b"DEADBEEF" + with self.assertRaisesRegex(ValueError, r"version='v4'"): + self._call_fut( + credentials, + resource="/name/path", + expiration=1000, + **kwargs, + ) + credentials.sign_bytes.assert_not_called() + def test_w_response_type(self): - response_type = "text/plain" - self._generate_helper(response_type=response_type) + self._assert_v4_required(response_type="text/plain") def test_w_response_disposition(self): - response_disposition = "attachment; filename=blob.png" - self._generate_helper(response_disposition=response_disposition) + self._assert_v4_required(response_disposition="attachment; filename=blob.png") def test_w_generation(self): - generation = "123" - self._generate_helper(generation=generation) + self._assert_v4_required(generation="123") def test_w_custom_headers_dict(self): self._generate_helper(headers={"x-goog-foo": "bar"})