Skip to content

Commit b693a53

Browse files
author
18adrianoh
committed
Second round of Liz's commends on issue aws#55.
1 parent c7743fd commit b693a53

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/aws_encryption_sdk/internal/formatting/encryption_context.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,13 @@ def assemble_content_aad(message_id, aad_content_string, seq_num, length):
4747
return struct.pack(fmt, message_id, aad_content_string.value, seq_num, length)
4848

4949

50-
def _key_or_value_too_large(encryption_context_list):
51-
"""Helper method to check whether a key or value are too long."""
52-
longest_key = max(encryption_context_list, key=lambda x: len(x[0]))[0]
53-
longest_value = max(encryption_context_list, key=lambda x: len(x[1]))[1]
54-
return len(longest_key) > 2 ** 16 - 1 or len(longest_value) > 2 ** 16 - 1
55-
56-
5750
def _serialize_encryption_context_list(encryption_context_list, serialized_context):
5851
"""Helper function to serialize the list of encryption context key-value pairs."""
5952
for key, value in sorted(encryption_context_list, key=lambda x: x[0]):
6053
try:
54+
if len(key) > 2 ** 16 - 1 or len(value) > 2 ** 16 - 1:
55+
raise SerializationError("Key or Value are too large. Maximum length is 65535")
56+
6157
serialized_context.extend(
6258
struct.pack(
6359
">H{key_size}sH{value_size}s".format(key_size=len(key), value_size=len(value)),
@@ -67,19 +63,19 @@ def _serialize_encryption_context_list(encryption_context_list, serialized_conte
6763
value,
6864
)
6965
)
70-
# we check to make sure that we return the right type of error message for an overly long key or value
7166
except struct.error as struct_error:
7267
message = str(struct_error)
73-
# a key or value were too long and struct gave us the expected error
7468
if message == "'H' format requires 0 <= number <= 65535":
7569
raise SerializationError("Key or Value are too large. Maximum length is 65535")
76-
# a key or value were too long and struct's error message is different (maybe an update)
77-
if _key_or_value_too_large(encryption_context_list):
78-
raise SerializationError("Key or Value are too large. Maximum length is 65535")
79-
# else we can just raise the struct error as it was (unknown)
80-
raise SerializationError("Unknown Error with struct pack. Could not serialize key or value")
70+
71+
# else unknown
72+
raise SerializationError(
73+
[SerializationError("Unknown Error with struct pack. Could not serialize key or value"), struct_error]
74+
)
75+
8176
if len(serialized_context) > aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE:
8277
raise SerializationError("The serialized context is too large.")
78+
8379
return bytes(serialized_context)
8480

8581

0 commit comments

Comments
 (0)