Skip to content

Commit

Permalink
fix(storage): use OrderedDict while encoding POST policy (#95)
Browse files Browse the repository at this point in the history
* use OrderedDict() while encoding POST policy

* fix(storage): use OrderedDict() while encoding POST policy
  • Loading branch information
Gurov Ilya committed Apr 1, 2020
1 parent b451e2d commit df560e1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 9 additions & 1 deletion google/cloud/storage/client.py
Expand Up @@ -16,6 +16,7 @@

import base64
import binascii
import collections
import datetime
import functools
import json
Expand Down Expand Up @@ -972,7 +973,14 @@ def generate_signed_post_policy_v4(

# encode policy for signing
policy = json.dumps(
{"conditions": conditions, "expiration": policy_expires.isoformat() + "Z"},
collections.OrderedDict(
sorted(
{
"conditions": conditions,
"expiration": policy_expires.isoformat() + "Z",
}.items()
)
),
separators=(",", ":"),
)
str_to_sign = base64.b64encode(policy.encode("utf-8"))
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/test_client.py
Expand Up @@ -1762,6 +1762,10 @@ def test_conformance_post_policy(test_data):
scheme=in_data.get("scheme"),
)
fields = policy["fields"]
out_data = test_data["policyOutput"]

decoded_policy = base64.b64decode(fields["policy"]).decode("unicode_escape")
assert decoded_policy == out_data["expectedDecodedPolicy"]

for field in (
"x-goog-algorithm",
Expand All @@ -1771,9 +1775,6 @@ def test_conformance_post_policy(test_data):
):
assert fields[field] == test_data["policyOutput"]["fields"][field]

out_data = test_data["policyOutput"]
decoded_policy = base64.b64decode(fields["policy"]).decode("unicode_escape")
assert decoded_policy == out_data["expectedDecodedPolicy"]
assert policy["url"] == out_data["url"]


Expand Down

0 comments on commit df560e1

Please sign in to comment.