Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #55 #188

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions src/aws_encryption_sdk/internal/formatting/encryption_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ def assemble_content_aad(message_id, aad_content_string, seq_num, length):
return struct.pack(fmt, message_id, aad_content_string.value, seq_num, length)


# help function to simplify code
4gatepylon marked this conversation as resolved.
Show resolved Hide resolved
# this is necessary to pass flake8
def _serialize_encryption_context_list(encryption_context_list, serialized_context):
for key, value in sorted(encryption_context_list, key=lambda x: x[0]):
try:
serialized_context.extend(
struct.pack(
">H{key_size}sH{value_size}s".format(key_size=len(key), value_size=len(value)),
len(key),
key,
len(value),
value,
)
)
# we check to make sure that we return the right type of error message for an overly long key or value
except struct.error as struct_error:
message = str(struct_error)
if message == "'H' format requires 0 <= number <= 65535":
4gatepylon marked this conversation as resolved.
Show resolved Hide resolved
# the key or value were too large
raise SerializationError("Key or Value are too large. Maximum length is 65535")
# else we can just raise the struct error as it was (unknown)
raise struct_error
4gatepylon marked this conversation as resolved.
Show resolved Hide resolved
if len(serialized_context) > aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE:
raise SerializationError("The serialized context is too large.")
return bytes(serialized_context)


def serialize_encryption_context(encryption_context):
"""Serializes the contents of a dictionary into a byte string.

Expand Down Expand Up @@ -80,20 +107,7 @@ def serialize_encryption_context(encryption_context):
raise SerializationError(
"Cannot encode dictionary key or value using {}.".format(aws_encryption_sdk.internal.defaults.ENCODING)
)

for key, value in sorted(encryption_context_list, key=lambda x: x[0]):
serialized_context.extend(
struct.pack(
">H{key_size}sH{value_size}s".format(key_size=len(key), value_size=len(value)),
len(key),
key,
len(value),
value,
)
)
if len(serialized_context) > aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE:
raise SerializationError("The serialized context is too large.")
return bytes(serialized_context)
return _serialize_encryption_context_list(encryption_context_list, serialized_context)


def read_short(source, offset):
Expand Down
9 changes: 9 additions & 0 deletions test/unit/test_encryption_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,12 @@ def test_deserialize_encryption_context_empty(self):
serialized_encryption_context=b""
)
assert test == {}

# these three tests (in one function) make sure that whether a key or value or both are too long
4gatepylon marked this conversation as resolved.
Show resolved Hide resolved
# we throw the right type of serialization error and not the struct.error
# from struct.pack()
def test_serialize_encryption_context_key_value_too_long(self):
for dictionary in [{"0" * 2 ** 16: "short"}, {"short": "0" * 2 ** 16}, {"0" * 2 ** 16: "0" * 2 ** 16}]:
with pytest.raises(SerializationError) as excinfo:
aws_encryption_sdk.internal.formatting.encryption_context.serialize_encryption_context(dictionary)
excinfo.match(r"Key or Value are too large. Maximum length is 65535")