Skip to content

Commit

Permalink
Add key verification to sign url (#316)
Browse files Browse the repository at this point in the history
Co-authored-by: zxl01071856 <zxl01071856@alibaba-inc.com>
  • Loading branch information
2 people authored and huiguangjun committed Mar 8, 2023
1 parent 647c892 commit 20e5f23
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions oss2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ def sign_url(self, method, key, expires, headers=None, params=None, slash_safe=F
:return: 签名URL。
"""
if key is None or len(key.strip()) <= 0:
raise ClientError("The key is invalid, please check it.")
key = to_string(key)
logger.debug(
"Start to sign_url, method: {0}, bucket: {1}, key: {2}, expires: {3}, headers: {4}, params: {5}, slash_safe: {6}".format(
Expand Down
23 changes: 23 additions & 0 deletions tests/test_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,28 @@ def test_sign_v2_x_oss_date_url(self):
dest_bucket.delete_object(key)
dest_bucket.delete_bucket()

def test_sign_key_not_empty(self):
auth = oss2.Auth(OSS_ID, OSS_SECRET)
bucket_name = self.OSS_BUCKET + "-sign-v1-url-key-not-empty"
bucket = oss2.Bucket(auth, OSS_ENDPOINT, bucket_name)
bucket.create_bucket()
key = 'testexampleobject.txt'
headers = dict()
content = 'test example'
url = bucket.sign_url('PUT', key, 1650801600, headers=headers)
print(url)
put_result = bucket.put_object(key, content, headers=headers)

try:
key = None
bucket.sign_url('PUT', key, 1650801600, headers=headers)
except oss2.exceptions.ClientError as e:
self.assertEqual(e.body, 'ClientError: The key is invalid, please check it.')

try:
key = ''
bucket.sign_url('PUT', key, 1650801600, headers=headers)
except oss2.exceptions.ClientError as e:
self.assertEqual(e.body, 'ClientError: The key is invalid, please check it.')
if __name__ == '__main__':
unittest.main()

0 comments on commit 20e5f23

Please sign in to comment.