Skip to content

Commit

Permalink
Encode special characters in presigned urls (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulineribeyre authored Aug 9, 2019
1 parent 3b765df commit 45ad662
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 44 deletions.
5 changes: 5 additions & 0 deletions cirrus/google_cloud/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import base64
import json
from urllib.parse import quote

from oauth2client.service_account import ServiceAccountCredentials

Expand Down Expand Up @@ -162,6 +163,10 @@ def get_signed_url(
str: Completed signed URL
"""
path_to_resource = path_to_resource.strip("/")

# escape special characters
path_to_resource = quote(path_to_resource)

string_to_sign = _get_string_to_sign(
path_to_resource, http_verb, expires, extension_headers, content_type, md5_value
)
Expand Down
8 changes: 1 addition & 7 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import time

# Python 2 and 3 compatible
try:
from unittest.mock import MagicMock
from unittest.mock import patch
except ImportError:
from mock import MagicMock
from mock import patch
from unittest.mock import MagicMock, patch

import pytest

Expand Down
38 changes: 8 additions & 30 deletions test/test_google_cloud_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@
import datetime
import json

# Python 2 and 3 compatible
try:
from unittest import mock
from unittest.mock import MagicMock
from unittest.mock import patch
except ImportError:
import mock
from mock import MagicMock
from mock import patch
from unittest.mock import MagicMock, patch

from googleapiclient.errors import HttpError
import pytest
Expand Down Expand Up @@ -1380,9 +1372,7 @@ def test_add_member_backoff_giveup(test_cloud_manager):
test_cloud_manager._admin_service.configure_mock(**mock_config)
warn = cirrus.google_cloud.manager.logger.warn
error = cirrus.google_cloud.manager.logger.error
with mock.patch(
"cirrus.google_cloud.manager.logger.warn"
) as logger_warn, mock.patch(
with patch("cirrus.google_cloud.manager.logger.warn") as logger_warn, patch(
"cirrus.google_cloud.manager.logger.error"
) as logger_error:
# keep the side effect to actually put logs, so you can see the format with `-s`
Expand All @@ -1408,9 +1398,7 @@ def test_authorized_session_retry(test_cloud_manager):
test_cloud_manager._authed_session.configure_mock(**mock_config)
warn = cirrus.google_cloud.manager.logger.warn
error = cirrus.google_cloud.manager.logger.error
with mock.patch(
"cirrus.google_cloud.manager.logger.warn"
) as logger_warn, mock.patch(
with patch("cirrus.google_cloud.manager.logger.warn") as logger_warn, patch(
"cirrus.google_cloud.manager.logger.error"
) as logger_error:
# keep the side effect to actually put logs, so you can see the format with `-s`
Expand All @@ -1435,9 +1423,7 @@ def test_handled_exception_no_retry(test_cloud_manager):
test_cloud_manager._admin_service.configure_mock(**mock_config)
warn = cirrus.google_cloud.manager.logger.warn
error = cirrus.google_cloud.manager.logger.error
with mock.patch(
"cirrus.google_cloud.manager.logger.warn"
) as logger_warn, mock.patch(
with patch("cirrus.google_cloud.manager.logger.warn") as logger_warn, patch(
"cirrus.google_cloud.manager.logger.error"
) as logger_error:
# keep the side effect to actually put logs, so you can see the format with `-s`
Expand Down Expand Up @@ -1469,9 +1455,7 @@ def test_handled_exception_403_no_retry(test_cloud_manager):
test_cloud_manager._authed_session.configure_mock(**mock_config)
warn = cirrus.google_cloud.manager.logger.warn
error = cirrus.google_cloud.manager.logger.error
with mock.patch(
"cirrus.google_cloud.manager.logger.warn"
) as logger_warn, mock.patch(
with patch("cirrus.google_cloud.manager.logger.warn") as logger_warn, patch(
"cirrus.google_cloud.manager.logger.error"
) as logger_error:
# keep the side effect to actually put logs, so you can see the format with `-s`
Expand Down Expand Up @@ -1502,9 +1486,7 @@ def test_unhandled_exception_403_ratelimit_retry(test_cloud_manager):
test_cloud_manager._authed_session.configure_mock(**mock_config)
warn = cirrus.google_cloud.manager.logger.warn
error = cirrus.google_cloud.manager.logger.error
with mock.patch(
"cirrus.google_cloud.manager.logger.warn"
) as logger_warn, mock.patch(
with patch("cirrus.google_cloud.manager.logger.warn") as logger_warn, patch(
"cirrus.google_cloud.manager.logger.error"
) as logger_error:
# keep the side effect to actually put logs, so you can see the format with `-s`
Expand All @@ -1529,9 +1511,7 @@ def test_unhandled_exception_retry(test_cloud_manager):
test_cloud_manager._admin_service.configure_mock(**mock_config)
warn = cirrus.google_cloud.manager.logger.warn
error = cirrus.google_cloud.manager.logger.error
with mock.patch(
"cirrus.google_cloud.manager.logger.warn"
) as logger_warn, mock.patch(
with patch("cirrus.google_cloud.manager.logger.warn") as logger_warn, patch(
"cirrus.google_cloud.manager.logger.error"
) as logger_error:
# keep the side effect to actually put logs, so you can see the format with `-s`
Expand All @@ -1557,9 +1537,7 @@ def test_authorized_session_unhandled_exception_retry(test_cloud_manager):
test_cloud_manager._authed_session.configure_mock(**mock_config)
warn = cirrus.google_cloud.manager.logger.warn
error = cirrus.google_cloud.manager.logger.error
with mock.patch(
"cirrus.google_cloud.manager.logger.warn"
) as logger_warn, mock.patch(
with patch("cirrus.google_cloud.manager.logger.warn") as logger_warn, patch(
"cirrus.google_cloud.manager.logger.error"
) as logger_error:
# keep the side effect to actually put logs, so you can see the format with `-s`
Expand Down
37 changes: 30 additions & 7 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import pytest

# Python 2 and 3 compatible
try:
from unittest.mock import MagicMock
from unittest.mock import patch
except ImportError:
from mock import MagicMock
from mock import patch
from unittest.mock import MagicMock, patch
from urllib.parse import quote

from cirrus.google_cloud.utils import (
_get_string_to_sign,
Expand Down Expand Up @@ -89,6 +84,34 @@ def test_get_string_to_sign():
)


def test_get_string_to_sign_escaped():
http_verb = "GET"
md5_hash = "rmYdCNHKFXam78uCt7xQLw=="
content_type = "text/plain"
expires = "1388534400"
ext_headers = ["x-goog-encryption-algorithm:AES256", "x-goog-meta-foo:bar,baz"]
# get_signed_url() quotes the path before calling _get_string_to_sign()
resource_path = quote("/bucket/P0001_T1/[test] ;.tar.gz")

result = _get_string_to_sign(
path_to_resource=resource_path,
http_verb=http_verb,
expires=expires,
extension_headers=ext_headers,
content_type=content_type,
md5_value=md5_hash,
)

assert result == (
"GET\n"
"rmYdCNHKFXam78uCt7xQLw==\n"
"text/plain\n"
"1388534400\n"
"x-goog-encryption-algorithm:AES256\n"
"x-goog-meta-foo:bar,baz\n" + quote("/bucket/P0001_T1/[test] ;.tar.gz")
)


def test_get_string_to_sign_no_optional_params():
http_verb = "GET"
expires = "1388534400"
Expand Down

0 comments on commit 45ad662

Please sign in to comment.