From fe8ef06eb88b36f9ca04925fc5d9c0fad38608c7 Mon Sep 17 00:00:00 2001 From: PanpanZou <865287328@qq.com> Date: Fri, 30 Oct 2026 11:53:54 +0800 Subject: [PATCH 1/3] Default signature V2 to HMAC-SHA256 with configurable SHA1 fallback --- .../auth/algorithm/hmac_sha256.py | 46 +++++ .../auth/composer/roa_signature_composer.py | 13 +- .../auth/composer/rpc_signature_composer.py | 8 +- .../aliyunsdkcore/client.py | 11 +- .../aliyunsdkcore/request.py | 15 +- .../composer/test_roa_signature_composer.py | 8 +- aliyun-python-sdk-core/tests/test_request.py | 20 +-- .../tests/test_signature_v2.py | 163 ++++++++++++++++++ 8 files changed, 254 insertions(+), 30 deletions(-) create mode 100644 aliyun-python-sdk-core/aliyunsdkcore/auth/algorithm/hmac_sha256.py create mode 100644 aliyun-python-sdk-core/tests/test_signature_v2.py diff --git a/aliyun-python-sdk-core/aliyunsdkcore/auth/algorithm/hmac_sha256.py b/aliyun-python-sdk-core/aliyunsdkcore/auth/algorithm/hmac_sha256.py new file mode 100644 index 0000000000..938bba7159 --- /dev/null +++ b/aliyun-python-sdk-core/aliyunsdkcore/auth/algorithm/hmac_sha256.py @@ -0,0 +1,46 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# coding=utf-8 + + +import hashlib +import hmac + +from aliyunsdkcore.compat import ensure_string +from aliyunsdkcore.compat import ensure_bytes +from aliyunsdkcore.compat import b64_encode_bytes + + +def get_sign_string(source, secret): + source = ensure_bytes(source) + secret = ensure_bytes(secret) + h = hmac.new(secret, source, hashlib.sha256) + signature = ensure_string(b64_encode_bytes(h.digest()).strip()) + return signature + + +def get_signer_name(): + return "HMAC-SHA256" + + +def get_signer_version(): + return "1.0" + + +def get_signer_type(): + return "" diff --git a/aliyun-python-sdk-core/aliyunsdkcore/auth/composer/roa_signature_composer.py b/aliyun-python-sdk-core/aliyunsdkcore/auth/composer/roa_signature_composer.py index 55e64ec725..aac5efadf3 100644 --- a/aliyun-python-sdk-core/aliyunsdkcore/auth/composer/roa_signature_composer.py +++ b/aliyun-python-sdk-core/aliyunsdkcore/auth/composer/roa_signature_composer.py @@ -18,7 +18,7 @@ # coding=utf-8 from aliyunsdkcore.vendored.six import iteritems from aliyunsdkcore.vendored.six.moves.urllib.parse import urlencode -from aliyunsdkcore.auth.algorithm import sha_hmac1 as mac1 +from aliyunsdkcore.auth.algorithm import hmac_sha256 as default_signer from aliyunsdkcore.utils import parameter_helper as helper from aliyunsdkcore.http import format_type as FormatType @@ -37,7 +37,7 @@ # accessKeyId: this is aliyun_access_key_id # format: XML or JSON # input parameters is headers -def refresh_sign_parameters(parameters, format=FormatType.RAW, signer=mac1): +def refresh_sign_parameters(parameters, format=FormatType.RAW, signer=default_signer): if parameters is None or not isinstance(parameters, dict): parameters = dict() parameters["Date"] = helper.get_rfc_2616_date() @@ -53,7 +53,7 @@ def compose_string_to_sign( uri_pattern=None, headers={}, paths=None, - signer=mac1): + signer=default_signer): sign_to_string = "" sign_to_string += method sign_to_string += HEADER_SEPARATOR @@ -128,10 +128,11 @@ def get_signature( uri_pattern, paths, method, - signer=mac1): + signer=default_signer): headers = refresh_sign_parameters( parameters=headers, - format=format) + format=format, + signer=signer) sign_to_string = compose_string_to_sign( method=method, queries=queries, @@ -151,7 +152,7 @@ def get_signature_headers( uri_pattern, paths, method, - signer=mac1): + signer=default_signer): signature, sign_to_string = get_signature( queries, access_key, diff --git a/aliyun-python-sdk-core/aliyunsdkcore/auth/composer/rpc_signature_composer.py b/aliyun-python-sdk-core/aliyunsdkcore/auth/composer/rpc_signature_composer.py index aa7eaa0eba..7cf4ea13ce 100644 --- a/aliyun-python-sdk-core/aliyunsdkcore/auth/composer/rpc_signature_composer.py +++ b/aliyun-python-sdk-core/aliyunsdkcore/auth/composer/rpc_signature_composer.py @@ -20,7 +20,7 @@ from aliyunsdkcore.vendored.six.moves.urllib.parse import urlencode from aliyunsdkcore.vendored.six.moves.urllib.request import pathname2url -from aliyunsdkcore.auth.algorithm import sha_hmac1 as mac1 +from aliyunsdkcore.auth.algorithm import hmac_sha256 as default_signer from aliyunsdkcore.utils import parameter_helper as helper @@ -33,7 +33,7 @@ def __refresh_sign_parameters( parameters, access_key_id, accept_format="JSON", - signer=mac1): + signer=default_signer): if parameters is None or not isinstance(parameters, dict): parameters = dict() if 'Signature' in parameters: @@ -64,11 +64,11 @@ def __compose_string_to_sign(method, queries): return string_to_sign -def __get_signature(string_to_sign, secret, signer=mac1): +def __get_signature(string_to_sign, secret, signer=default_signer): return signer.get_sign_string(string_to_sign, secret + '&') -def get_signed_url(params, ak, secret, accept_format, method, body_params, signer=mac1): +def get_signed_url(params, ak, secret, accept_format, method, body_params, signer=default_signer): url_params = __refresh_sign_parameters(params, ak, accept_format, signer) sign_params = dict(url_params) sign_params.update(body_params) diff --git a/aliyun-python-sdk-core/aliyunsdkcore/client.py b/aliyun-python-sdk-core/aliyunsdkcore/client.py index e0577c1d60..1ec9c6e8d1 100644 --- a/aliyun-python-sdk-core/aliyunsdkcore/client.py +++ b/aliyun-python-sdk-core/aliyunsdkcore/client.py @@ -38,6 +38,7 @@ from aliyunsdkcore.request import AcsRequest from aliyunsdkcore.http import format_type from aliyunsdkcore.auth.signers.signer_factory import SignerFactory +from aliyunsdkcore.auth.algorithm import hmac_sha256, sha_hmac1 from aliyunsdkcore.request import CommonRequest from aliyunsdkcore.endpoint.resolver_endpoint_request import ResolveEndpointRequest @@ -92,7 +93,8 @@ def __init__( debug=False, verify=None, pool_size=10, - proxy=None + proxy=None, + signature_method="HMAC-SHA256" ): """ constructor for AcsClient @@ -101,6 +103,7 @@ def __init__( :param region_id: String, region id :param auto_retry: Boolean :param max_retry_time: Number + :param signature_method: String, HMAC-SHA256 (default) or HMAC-SHA1 :param pool_size: In a multithreaded environment, you should set the maxsize of the pool to a higher number, @@ -108,6 +111,7 @@ def __init__( :return: """ + self.session = None self._max_retry_num = max_retry_time self._auto_retry = auto_retry self._ak = ak @@ -119,6 +123,10 @@ def __init__( self._read_timeout = timeout self._extra_user_agent = {} self._verify = verify + if signature_method not in ("HMAC-SHA256", "HMAC-SHA1"): + raise ClientException(error_code.SDK_INVALID_PARAMS, + "Invalid signature_method, should be HMAC-SHA256 or HMAC-SHA1") + self._signature_algorithm = sha_hmac1 if signature_method == "HMAC-SHA1" else hmac_sha256 credential = { 'ak': ak, 'secret': secret, @@ -279,6 +287,7 @@ def _make_http_response(self, endpoint, request, read_timeout, connect_timeout, request.set_content_type(format_type.APPLICATION_OCTET_STREAM) method = request.get_method() + request._default_signer = self._signature_algorithm if isinstance(request, CommonRequest): request.trans_to_acs_request() diff --git a/aliyun-python-sdk-core/aliyunsdkcore/request.py b/aliyun-python-sdk-core/aliyunsdkcore/request.py index 7b8170b526..f989fa7249 100644 --- a/aliyun-python-sdk-core/aliyunsdkcore/request.py +++ b/aliyun-python-sdk-core/aliyunsdkcore/request.py @@ -29,7 +29,7 @@ from aliyunsdkcore.auth.composer import rpc_signature_composer as rpc_signer from aliyunsdkcore.auth.composer import roa_signature_composer as roa_signer from aliyunsdkcore.utils.parameter_helper import md5_sum -from aliyunsdkcore.auth.algorithm import sha_hmac1 +from aliyunsdkcore.auth.algorithm import hmac_sha256 from aliyunsdkcore.acs_exception import exceptions from aliyunsdkcore.acs_exception import error_code from aliyunsdkcore.compat import ensure_string @@ -85,6 +85,8 @@ def __init__(self, product, version=None, :param accept_format: :return: """ + self._signer = None + self._default_signer = hmac_sha256 self._version = version self._product = product self._action_name = action_name @@ -290,7 +292,7 @@ def __init__( location_endpoint_type='openAPI', format=None, protocol=None, - signer=sha_hmac1): + signer=None): AcsRequest.__init__( self, product, @@ -328,7 +330,7 @@ def get_url(self, region_id, access_key_id, access_key_secret): self.get_accept_format(), self.get_method(), self.get_body_params(), - self._signer) + self._signer or self._default_signer) self.string_to_sign = string_to_sign return url @@ -438,7 +440,8 @@ def get_signed_header(self, region_id, ak, secret): self.get_headers().copy(), self.get_uri_pattern(), self.get_path_params(), - self.get_method()) + self.get_method(), + self._signer or self._default_signer) self.string_to_sign = sign_to_string return signed_headers @@ -470,7 +473,6 @@ def __init__(self, domain=None, version=None, action_name=None, uri_pattern=None self._uri_pattern = uri_pattern self._product = product self._location_endpoint_type = location_endpoint_type - self._signer = sha_hmac1 self.add_header('x-sdk-invoke-type', 'common') self._path_params = None self._method = "GET" @@ -544,6 +546,9 @@ def get_signed_header(self, region_id, access_key_id, access_key_secret): def fill_params(self): + self.request._signer = self._signer + self.request._default_signer = self._default_signer + self.request.set_uri_pattern(self.get_uri_pattern()) self.request.set_uri_params(self.get_uri_params()) diff --git a/aliyun-python-sdk-core/tests/auth/composer/test_roa_signature_composer.py b/aliyun-python-sdk-core/tests/auth/composer/test_roa_signature_composer.py index 36963273fb..d21367e91c 100644 --- a/aliyun-python-sdk-core/tests/auth/composer/test_roa_signature_composer.py +++ b/aliyun-python-sdk-core/tests/auth/composer/test_roa_signature_composer.py @@ -66,14 +66,14 @@ def test_refresh_sign_parameters(self, mock_get_rfc_2616_date): mock_get_rfc_2616_date.assert_called_once_with() self.assertEqual(parameters.get('Date'), '2018-12-04T03:25:29Z') self.assertEqual(parameters.get('Accept'), 'application/octet-stream') - self.assertEqual(parameters.get('x-acs-signature-method'), 'HMAC-SHA1') + self.assertEqual(parameters.get('x-acs-signature-method'), 'HMAC-SHA256') self.assertEqual(parameters.get('x-acs-signature-version'), '1.0') # with none parameters = refresh_sign_parameters(None) mock_get_rfc_2616_date.assert_called_with() self.assertEqual(parameters.get('Date'), '2018-12-04T03:25:29Z') self.assertEqual(parameters.get('Accept'), 'application/octet-stream') - self.assertEqual(parameters.get('x-acs-signature-method'), 'HMAC-SHA1') + self.assertEqual(parameters.get('x-acs-signature-method'), 'HMAC-SHA256') self.assertEqual(parameters.get('x-acs-signature-version'), '1.0') @patch("aliyunsdkcore.utils.parameter_helper.get_rfc_2616_date") @@ -82,7 +82,7 @@ def test_get_signature(self, mock_get_rfc_2616_date): sign, _ = get_signature({}, 'access_key_id', 'access_key_secret', 'json', {}, '/', {}, 'GET') mock_get_rfc_2616_date.assert_called_once_with() - self.assertEqual(sign, 'HKCpm41tJg6b3EYdtGMbNuwALZU=') + self.assertEqual(sign, 'dH1gZsa/Z+mTd+VJmr+W9OCofWTRhmrPWFIprWVPR6o=') @patch("aliyunsdkcore.utils.parameter_helper.get_rfc_2616_date") def test_get_signature_headers(self, mock_get_rfc_2616_date): @@ -91,4 +91,4 @@ def test_get_signature_headers(self, mock_get_rfc_2616_date): {}, 'access_key_id', 'access_key_secret', 'json', {}, '/', {}, 'GET') mock_get_rfc_2616_date.assert_called_once_with() self.assertEqual(headers.get('Authorization'), - 'acs access_key_id:HKCpm41tJg6b3EYdtGMbNuwALZU=') + 'acs access_key_id:dH1gZsa/Z+mTd+VJmr+W9OCofWTRhmrPWFIprWVPR6o=') diff --git a/aliyun-python-sdk-core/tests/test_request.py b/aliyun-python-sdk-core/tests/test_request.py index c7fc98f5b7..e789f3fcd3 100644 --- a/aliyun-python-sdk-core/tests/test_request.py +++ b/aliyun-python-sdk-core/tests/test_request.py @@ -291,10 +291,10 @@ def test_get_signed_header(self, mock_get_rfc_2616_date): mock_get_rfc_2616_date.assert_called_once_with() self.assertDictEqual(headers, { 'Accept': 'application/octet-stream', - 'Authorization': 'acs accesskeyid:Lq1u0OzLW/uqLQswxwhne97Umlw=', + 'Authorization': 'acs accesskeyid:MF0ngFVlg34K5SuJeGYQiBb5UA/eXQr/UBOEJmRZhD4=', 'Date': '2018-12-04T03:25:29Z', 'x-acs-region-id': 'regionid', - 'x-acs-signature-method': 'HMAC-SHA1', + 'x-acs-signature-method': 'HMAC-SHA256', 'x-acs-signature-version': '1.0', 'x-acs-version': 'version' }) @@ -303,10 +303,10 @@ def test_get_signed_header(self, mock_get_rfc_2616_date): headers = r.get_signed_header("regionid", "accesskeyid", "secret") self.assertDictEqual(headers, { 'Accept': 'application/octet-stream', - 'Authorization': 'acs accesskeyid:Lq1u0OzLW/uqLQswxwhne97Umlw=', + 'Authorization': 'acs accesskeyid:MF0ngFVlg34K5SuJeGYQiBb5UA/eXQr/UBOEJmRZhD4=', 'Date': '2018-12-04T03:25:29Z', 'x-acs-region-id': 'regionid', - 'x-acs-signature-method': 'HMAC-SHA1', + 'x-acs-signature-method': 'HMAC-SHA256', 'x-acs-signature-version': '1.0', 'x-acs-version': 'version' }) @@ -315,10 +315,10 @@ def test_get_signed_header(self, mock_get_rfc_2616_date): headers = r.get_signed_header("regionid", "accesskeyid", "secret") self.assertDictEqual(headers, { 'Accept': 'application/octet-stream', - 'Authorization': 'acs accesskeyid:Lq1u0OzLW/uqLQswxwhne97Umlw=', + 'Authorization': 'acs accesskeyid:MF0ngFVlg34K5SuJeGYQiBb5UA/eXQr/UBOEJmRZhD4=', 'Date': '2018-12-04T03:25:29Z', 'x-acs-region-id': 'regionid', - 'x-acs-signature-method': 'HMAC-SHA1', + 'x-acs-signature-method': 'HMAC-SHA256', 'x-acs-signature-version': '1.0', 'x-acs-version': 'version' }) @@ -327,11 +327,11 @@ def test_get_signed_header(self, mock_get_rfc_2616_date): headers = r.get_signed_header("regionid", "accesskeyid", "secret") self.assertDictEqual(headers, { 'Accept': 'application/octet-stream', - 'Authorization': 'acs accesskeyid:u2RdkokGTtn2BhUmzUNJjVUh448=', + 'Authorization': 'acs accesskeyid:bUYa87QP/Ts93gc78qtkVxG/QCyYd9khVzMWCHKyt70=', 'Content-MD5': 'mgNkuembtIDdJeHwKEyFVQ==', 'Date': '2018-12-04T03:25:29Z', 'x-acs-region-id': 'regionid', - 'x-acs-signature-method': 'HMAC-SHA1', + 'x-acs-signature-method': 'HMAC-SHA256', 'x-acs-signature-version': '1.0', 'x-acs-version': 'version' }) @@ -503,11 +503,11 @@ def test_common_request_get_signed_header(self, mock_get_rfc_2616_date): mock_get_rfc_2616_date.assert_called_once_with() self.assertDictEqual(headers, { 'Accept': 'application/octet-stream', - 'Authorization': 'acs accesskeyid:j73ZHar0vd0+MGaKFlGK9JyBnEo=', + 'Authorization': 'acs accesskeyid:b6aEEEYifojv60v06CVkcnWKOR7pmJ92t8w3BQB0Dgc=', 'Date': '2018-12-04T03:25:29Z', 'x-acs-action': 'action_name', 'x-acs-region-id': 'regionid', - 'x-acs-signature-method': 'HMAC-SHA1', + 'x-acs-signature-method': 'HMAC-SHA256', 'x-acs-signature-version': '1.0', 'x-acs-version': 'version', 'x-sdk-invoke-type': 'common' diff --git a/aliyun-python-sdk-core/tests/test_signature_v2.py b/aliyun-python-sdk-core/tests/test_signature_v2.py new file mode 100644 index 0000000000..e4eac1854e --- /dev/null +++ b/aliyun-python-sdk-core/tests/test_signature_v2.py @@ -0,0 +1,163 @@ +# coding=utf-8 +import base64 +import hashlib +import hmac +import itertools +import json +import os +from urllib.parse import parse_qsl, quote, urlsplit + +from mock import patch +from tests import unittest + +from aliyunsdkcore.acs_exception.exceptions import ClientException +from aliyunsdkcore.auth.algorithm import hmac_sha256, sha_hmac1 +from aliyunsdkcore.auth.credentials import AccessKeyCredential, RamRoleArnCredential, StsTokenCredential +from aliyunsdkcore.auth.signers.rsa_key_pair_signer import GetSessionAkRequest +from aliyunsdkcore.client import AcsClient +from aliyunsdkcore.request import CommonRequest, RoaRequest, RpcRequest + + +class TestSignatureV2(unittest.TestCase): + def setUp(self): + environment = patch.dict(os.environ, {}, clear=True) + environment.start() + self.addCleanup(environment.stop) + + @staticmethod + def request(style, method): + if style.startswith('common'): + request = CommonRequest(product='Ecs', version='2014-05-26', action_name='DescribeRegions') + if style == 'common_roa': + request.set_uri_pattern('/regions/[region_id]/clusters') + request.add_path_param('region_id', 'cn-hangzhou') + elif style == 'rpc': + request = RpcRequest('Ecs', '2014-05-26', 'DescribeRegions') + else: + request = RoaRequest('CS', '2015-12-15', 'DescribeClustersForRegion', + uri_pattern='/regions/[region_id]/clusters', + path_params={'region_id': 'cn-hangzhou'}) + request.set_accept_format('JSON') + request.set_method(method) + request.add_query_param('Value', 'Unicode-中文 +/~*') + if method == 'POST': + request.add_body_params('BodyValue', 'body +/~*') + return request + + @staticmethod + def rpc_string(message): + parameters = dict(parse_qsl(urlsplit(message.get_url()).query, keep_blank_values=True)) + parameters.pop('Signature') + parameters.update(parse_qsl(message.get_content() or '', keep_blank_values=True)) + canonical = '&'.join(quote(key, safe='~') + '=' + quote(value, safe='~') + for key, value in sorted(parameters.items())) + return message.get_method() + '&%2F&' + quote(canonical, safe='~') + + def assert_signature(self, message, style, method, secret, token=None): + algorithm = hashlib.sha1 if method == 'HMAC-SHA1' else hashlib.sha256 + if style.endswith('rpc'): + parameters = dict(parse_qsl(urlsplit(message.get_url()).query, keep_blank_values=True)) + self.assertEqual(method, parameters['SignatureMethod']) + self.assertEqual('1.0', parameters['SignatureVersion']) + self.assertEqual(token, parameters.get('SecurityToken')) + signature = parameters['Signature'] + canonical = self.rpc_string(message) + secret += '&' + else: + headers = message.get_headers() + self.assertEqual(method, headers['x-acs-signature-method']) + self.assertEqual('1.0', headers['x-acs-signature-version']) + self.assertEqual(token, headers.get('x-acs-security-token')) + signature = headers['Authorization'].split(':', 1)[1] + canonical = message.get_method() + '\n' + for name in ('Accept', 'Content-MD5', 'Content-Type', 'Date'): + canonical += headers.get(name, '') + '\n' + canonical += ''.join(name.lower() + ':' + value + '\n' + for name, value in sorted(headers.items(), key=lambda item: item[0].lower()) + if name.lower().startswith('x-acs-')) + uri = urlsplit(message.get_url()) + canonical += uri.path + parameters = sorted(parse_qsl(uri.query, keep_blank_values=True)) + if parameters: + canonical += '?' + '&'.join(key + '=' + value for key, value in parameters) + expected = base64.b64encode(hmac.new(secret.encode('utf-8'), canonical.encode('utf-8'), + algorithm).digest()).decode('ascii') + self.assertEqual(expected, signature) + + def test_client_algorithms(self): + cases = itertools.product(('rpc', 'roa', 'common_rpc', 'common_roa'), + ('GET', 'POST'), (False, True), (None, 'HMAC-SHA1', 'HMAC-SHA256')) + for style, http_method, sts, method in cases: + with self.subTest(style=style, http_method=http_method, sts=sts, method=method): + credential = (StsTokenCredential('key', 'secret', 'token+/=') + if sts else AccessKeyCredential('key', 'secret')) + options = {} if method is None else {'signature_method': method} + client = AcsClient(credential=credential, **options) + request = self.request(style, http_method) + message = client._make_http_response('example.test', request, 10, 10) + self.assert_signature(message, style, method or 'HMAC-SHA256', 'secret', 'token+/=' if sts else None) + client.session.close() + + def test_explicit_request_signer_overrides_client_default(self): + client = AcsClient('key', 'secret') + request = RpcRequest('Ecs', '2014-05-26', 'DescribeRegions', signer=sha_hmac1) + message = client._make_http_response('example.test', request, 10, 10) + self.assert_signature(message, 'rpc', 'HMAC-SHA1', 'secret') + client.session.close() + + def test_request_can_be_reused_with_different_client_defaults(self): + for style in ('rpc', 'roa', 'common_rpc', 'common_roa'): + request = self.request(style, 'GET') + for method in ('HMAC-SHA1', 'HMAC-SHA256'): + with self.subTest(style=style, method=method): + client = AcsClient('key', 'secret', signature_method=method) + message = client._make_http_response('example.test', request, 10, 10) + self.assert_signature(message, style, method, 'secret') + client.session.close() + + def test_assume_role_inherits_client_algorithm(self): + for method in ('HMAC-SHA256', 'HMAC-SHA1'): + with self.subTest(method=method): + credential = RamRoleArnCredential('source-key', 'source-secret', 'role-arn', 'session') + client = AcsClient(credential=credential, signature_method=method) + exchange = [] + + def assume_role(request, signer): + exchange.append(client._make_http_response('example.test', request, 10, 10, signer)) + return 200, {}, json.dumps({'Credentials': { + 'AccessKeyId': 'temporary-key', 'AccessKeySecret': 'temporary-secret', + 'SecurityToken': 'temporary-token' + }}).encode('utf-8'), None + + client._signer._doAction = assume_role + message = client._make_http_response('example.test', self.request('rpc', 'GET'), 10, 10) + self.assertEqual(1, len(exchange)) + self.assert_signature(exchange[0], 'rpc', method, 'source-secret') + self.assert_signature(message, 'rpc', method, 'temporary-secret', 'temporary-token') + client.session.close() + + @patch('aliyunsdkcore.auth.algorithm.sha_hmac256.get_sign_string', return_value='rsa-signature') + def test_rsa_exchange_keeps_explicit_algorithm(self, sign): + client = AcsClient('public-key-id', 'private-key', signature_method='HMAC-SHA1') + message = client._make_http_response('example.test', GetSessionAkRequest(), 10, 10) + parameters = dict(parse_qsl(urlsplit(message.get_url()).query)) + self.assertEqual('SHA256withRSA', parameters['SignatureMethod']) + self.assertEqual('PRIVATEKEY', parameters['SignatureType']) + self.assertEqual('rsa-signature', parameters['Signature']) + sign.assert_called_once() + client.session.close() + + def test_invalid_client_algorithm(self): + for method in ('sha1', '', None, []): + with self.subTest(method=method): + with self.assertRaises(ClientException) as error: + AcsClient('key', 'secret', signature_method=method) + self.assertEqual('SDK.InvalidParams', error.exception.error_code) + + def test_hmac_sha256_vectors(self): + self.assertEqual('HMAC-SHA256', hmac_sha256.get_signer_name()) + self.assertEqual('1.0', hmac_sha256.get_signer_version()) + self.assertEqual('', hmac_sha256.get_signer_type()) + for source in ('source', '中文unicode'): + expected = base64.b64encode(hmac.new(b'secret', source.encode('utf-8'), hashlib.sha256).digest()) + self.assertEqual(expected.decode('ascii'), hmac_sha256.get_sign_string(source, 'secret')) From e25626fb72b2aabf2f58c545a5364331e305f93f Mon Sep 17 00:00:00 2001 From: PanpanZou <865287328@qq.com> Date: Fri, 30 Oct 2026 16:34:18 +0800 Subject: [PATCH 2/3] Document SHA1 fallback security and signature V2 scope --- aliyun-python-sdk-core/aliyunsdkcore/client.py | 3 ++- aliyun-python-sdk-core/aliyunsdkcore/request.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/aliyun-python-sdk-core/aliyunsdkcore/client.py b/aliyun-python-sdk-core/aliyunsdkcore/client.py index 1ec9c6e8d1..42ab7ebb53 100644 --- a/aliyun-python-sdk-core/aliyunsdkcore/client.py +++ b/aliyun-python-sdk-core/aliyunsdkcore/client.py @@ -103,7 +103,8 @@ def __init__( :param region_id: String, region id :param auto_retry: Boolean :param max_retry_time: Number - :param signature_method: String, HMAC-SHA256 (default) or HMAC-SHA1 + :param signature_method: Signature V2 only: HMAC-SHA256 (default) or HMAC-SHA1. + SHA1 is less secure; this compatibility fallback is not recommended. :param pool_size: In a multithreaded environment, you should set the maxsize of the pool to a higher number, diff --git a/aliyun-python-sdk-core/aliyunsdkcore/request.py b/aliyun-python-sdk-core/aliyunsdkcore/request.py index f989fa7249..3e79bdad42 100644 --- a/aliyun-python-sdk-core/aliyunsdkcore/request.py +++ b/aliyun-python-sdk-core/aliyunsdkcore/request.py @@ -293,6 +293,10 @@ def __init__( format=None, protocol=None, signer=None): + """ + :param signer: Explicit signer for signature V2 only. HMAC-SHA1 is a less + secure compatibility fallback and is not recommended. + """ AcsRequest.__init__( self, product, From f30747e9ae163e0959993c2e3bd5c1eb21208b8d Mon Sep 17 00:00:00 2001 From: PanpanZou <865287328@qq.com> Date: Tue, 3 Nov 2026 10:45:09 +0800 Subject: [PATCH 3/3] Remove newly added SHA1 fallback configuration --- .../aliyunsdkcore/client.py | 12 +-- .../aliyunsdkcore/request.py | 17 +--- .../tests/test_signature_v2.py | 80 +++++++------------ 3 files changed, 35 insertions(+), 74 deletions(-) diff --git a/aliyun-python-sdk-core/aliyunsdkcore/client.py b/aliyun-python-sdk-core/aliyunsdkcore/client.py index 42ab7ebb53..e0577c1d60 100644 --- a/aliyun-python-sdk-core/aliyunsdkcore/client.py +++ b/aliyun-python-sdk-core/aliyunsdkcore/client.py @@ -38,7 +38,6 @@ from aliyunsdkcore.request import AcsRequest from aliyunsdkcore.http import format_type from aliyunsdkcore.auth.signers.signer_factory import SignerFactory -from aliyunsdkcore.auth.algorithm import hmac_sha256, sha_hmac1 from aliyunsdkcore.request import CommonRequest from aliyunsdkcore.endpoint.resolver_endpoint_request import ResolveEndpointRequest @@ -93,8 +92,7 @@ def __init__( debug=False, verify=None, pool_size=10, - proxy=None, - signature_method="HMAC-SHA256" + proxy=None ): """ constructor for AcsClient @@ -103,8 +101,6 @@ def __init__( :param region_id: String, region id :param auto_retry: Boolean :param max_retry_time: Number - :param signature_method: Signature V2 only: HMAC-SHA256 (default) or HMAC-SHA1. - SHA1 is less secure; this compatibility fallback is not recommended. :param pool_size: In a multithreaded environment, you should set the maxsize of the pool to a higher number, @@ -112,7 +108,6 @@ def __init__( :return: """ - self.session = None self._max_retry_num = max_retry_time self._auto_retry = auto_retry self._ak = ak @@ -124,10 +119,6 @@ def __init__( self._read_timeout = timeout self._extra_user_agent = {} self._verify = verify - if signature_method not in ("HMAC-SHA256", "HMAC-SHA1"): - raise ClientException(error_code.SDK_INVALID_PARAMS, - "Invalid signature_method, should be HMAC-SHA256 or HMAC-SHA1") - self._signature_algorithm = sha_hmac1 if signature_method == "HMAC-SHA1" else hmac_sha256 credential = { 'ak': ak, 'secret': secret, @@ -288,7 +279,6 @@ def _make_http_response(self, endpoint, request, read_timeout, connect_timeout, request.set_content_type(format_type.APPLICATION_OCTET_STREAM) method = request.get_method() - request._default_signer = self._signature_algorithm if isinstance(request, CommonRequest): request.trans_to_acs_request() diff --git a/aliyun-python-sdk-core/aliyunsdkcore/request.py b/aliyun-python-sdk-core/aliyunsdkcore/request.py index 3e79bdad42..d68a6f9464 100644 --- a/aliyun-python-sdk-core/aliyunsdkcore/request.py +++ b/aliyun-python-sdk-core/aliyunsdkcore/request.py @@ -85,8 +85,6 @@ def __init__(self, product, version=None, :param accept_format: :return: """ - self._signer = None - self._default_signer = hmac_sha256 self._version = version self._product = product self._action_name = action_name @@ -292,11 +290,7 @@ def __init__( location_endpoint_type='openAPI', format=None, protocol=None, - signer=None): - """ - :param signer: Explicit signer for signature V2 only. HMAC-SHA1 is a less - secure compatibility fallback and is not recommended. - """ + signer=hmac_sha256): AcsRequest.__init__( self, product, @@ -334,7 +328,7 @@ def get_url(self, region_id, access_key_id, access_key_secret): self.get_accept_format(), self.get_method(), self.get_body_params(), - self._signer or self._default_signer) + self._signer) self.string_to_sign = string_to_sign return url @@ -444,8 +438,7 @@ def get_signed_header(self, region_id, ak, secret): self.get_headers().copy(), self.get_uri_pattern(), self.get_path_params(), - self.get_method(), - self._signer or self._default_signer) + self.get_method()) self.string_to_sign = sign_to_string return signed_headers @@ -477,6 +470,7 @@ def __init__(self, domain=None, version=None, action_name=None, uri_pattern=None self._uri_pattern = uri_pattern self._product = product self._location_endpoint_type = location_endpoint_type + self._signer = hmac_sha256 self.add_header('x-sdk-invoke-type', 'common') self._path_params = None self._method = "GET" @@ -550,9 +544,6 @@ def get_signed_header(self, region_id, access_key_id, access_key_secret): def fill_params(self): - self.request._signer = self._signer - self.request._default_signer = self._default_signer - self.request.set_uri_pattern(self.get_uri_pattern()) self.request.set_uri_params(self.get_uri_params()) diff --git a/aliyun-python-sdk-core/tests/test_signature_v2.py b/aliyun-python-sdk-core/tests/test_signature_v2.py index e4eac1854e..e8fd431c0d 100644 --- a/aliyun-python-sdk-core/tests/test_signature_v2.py +++ b/aliyun-python-sdk-core/tests/test_signature_v2.py @@ -10,7 +10,6 @@ from mock import patch from tests import unittest -from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.auth.algorithm import hmac_sha256, sha_hmac1 from aliyunsdkcore.auth.credentials import AccessKeyCredential, RamRoleArnCredential, StsTokenCredential from aliyunsdkcore.auth.signers.rsa_key_pair_signer import GetSessionAkRequest @@ -25,7 +24,7 @@ def setUp(self): self.addCleanup(environment.stop) @staticmethod - def request(style, method): + def request(style, method, parameters=True): if style.startswith('common'): request = CommonRequest(product='Ecs', version='2014-05-26', action_name='DescribeRegions') if style == 'common_roa': @@ -39,8 +38,9 @@ def request(style, method): path_params={'region_id': 'cn-hangzhou'}) request.set_accept_format('JSON') request.set_method(method) - request.add_query_param('Value', 'Unicode-中文 +/~*') - if method == 'POST': + if parameters: + request.add_query_param('Value', 'Unicode-中文 +/~*') + if parameters and method == 'POST': request.add_body_params('BodyValue', 'body +/~*') return request @@ -84,18 +84,17 @@ def assert_signature(self, message, style, method, secret, token=None): algorithm).digest()).decode('ascii') self.assertEqual(expected, signature) - def test_client_algorithms(self): + def test_client_defaults_to_sha256(self): cases = itertools.product(('rpc', 'roa', 'common_rpc', 'common_roa'), - ('GET', 'POST'), (False, True), (None, 'HMAC-SHA1', 'HMAC-SHA256')) - for style, http_method, sts, method in cases: - with self.subTest(style=style, http_method=http_method, sts=sts, method=method): + ('GET', 'POST'), (False, True), (False, True)) + for style, http_method, sts, parameters in cases: + with self.subTest(style=style, http_method=http_method, sts=sts, parameters=parameters): credential = (StsTokenCredential('key', 'secret', 'token+/=') if sts else AccessKeyCredential('key', 'secret')) - options = {} if method is None else {'signature_method': method} - client = AcsClient(credential=credential, **options) - request = self.request(style, http_method) + client = AcsClient(credential=credential) + request = self.request(style, http_method, parameters) message = client._make_http_response('example.test', request, 10, 10) - self.assert_signature(message, style, method or 'HMAC-SHA256', 'secret', 'token+/=' if sts else None) + self.assert_signature(message, style, 'HMAC-SHA256', 'secret', 'token+/=' if sts else None) client.session.close() def test_explicit_request_signer_overrides_client_default(self): @@ -105,40 +104,28 @@ def test_explicit_request_signer_overrides_client_default(self): self.assert_signature(message, 'rpc', 'HMAC-SHA1', 'secret') client.session.close() - def test_request_can_be_reused_with_different_client_defaults(self): - for style in ('rpc', 'roa', 'common_rpc', 'common_roa'): - request = self.request(style, 'GET') - for method in ('HMAC-SHA1', 'HMAC-SHA256'): - with self.subTest(style=style, method=method): - client = AcsClient('key', 'secret', signature_method=method) - message = client._make_http_response('example.test', request, 10, 10) - self.assert_signature(message, style, method, 'secret') - client.session.close() - - def test_assume_role_inherits_client_algorithm(self): - for method in ('HMAC-SHA256', 'HMAC-SHA1'): - with self.subTest(method=method): - credential = RamRoleArnCredential('source-key', 'source-secret', 'role-arn', 'session') - client = AcsClient(credential=credential, signature_method=method) - exchange = [] - - def assume_role(request, signer): - exchange.append(client._make_http_response('example.test', request, 10, 10, signer)) - return 200, {}, json.dumps({'Credentials': { - 'AccessKeyId': 'temporary-key', 'AccessKeySecret': 'temporary-secret', - 'SecurityToken': 'temporary-token' - }}).encode('utf-8'), None - - client._signer._doAction = assume_role - message = client._make_http_response('example.test', self.request('rpc', 'GET'), 10, 10) - self.assertEqual(1, len(exchange)) - self.assert_signature(exchange[0], 'rpc', method, 'source-secret') - self.assert_signature(message, 'rpc', method, 'temporary-secret', 'temporary-token') - client.session.close() + def test_assume_role_defaults_to_sha256(self): + credential = RamRoleArnCredential('source-key', 'source-secret', 'role-arn', 'session') + client = AcsClient(credential=credential) + exchange = [] + + def assume_role(request, signer): + exchange.append(client._make_http_response('example.test', request, 10, 10, signer)) + return 200, {}, json.dumps({'Credentials': { + 'AccessKeyId': 'temporary-key', 'AccessKeySecret': 'temporary-secret', + 'SecurityToken': 'temporary-token' + }}).encode('utf-8'), None + + client._signer._doAction = assume_role + message = client._make_http_response('example.test', self.request('rpc', 'GET'), 10, 10) + self.assertEqual(1, len(exchange)) + self.assert_signature(exchange[0], 'rpc', 'HMAC-SHA256', 'source-secret') + self.assert_signature(message, 'rpc', 'HMAC-SHA256', 'temporary-secret', 'temporary-token') + client.session.close() @patch('aliyunsdkcore.auth.algorithm.sha_hmac256.get_sign_string', return_value='rsa-signature') def test_rsa_exchange_keeps_explicit_algorithm(self, sign): - client = AcsClient('public-key-id', 'private-key', signature_method='HMAC-SHA1') + client = AcsClient('public-key-id', 'private-key') message = client._make_http_response('example.test', GetSessionAkRequest(), 10, 10) parameters = dict(parse_qsl(urlsplit(message.get_url()).query)) self.assertEqual('SHA256withRSA', parameters['SignatureMethod']) @@ -147,13 +134,6 @@ def test_rsa_exchange_keeps_explicit_algorithm(self, sign): sign.assert_called_once() client.session.close() - def test_invalid_client_algorithm(self): - for method in ('sha1', '', None, []): - with self.subTest(method=method): - with self.assertRaises(ClientException) as error: - AcsClient('key', 'secret', signature_method=method) - self.assertEqual('SDK.InvalidParams', error.exception.error_code) - def test_hmac_sha256_vectors(self): self.assertEqual('HMAC-SHA256', hmac_sha256.get_signer_name()) self.assertEqual('1.0', hmac_sha256.get_signer_version())