Skip to content

Commit 13d6c83

Browse files
author
kiwigitops
committed
fix: reduce encryption context serialization complexity
1 parent 58332ad commit 13d6c83

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

‎src/aws_encryption_sdk/internal/formatting/encryption_context.py‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
_LOGGER = logging.getLogger(__name__)
1717

1818

19+
def _validate_encryption_context_entry(key, value):
20+
max_entry_length = aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE
21+
if len(key) > max_entry_length:
22+
raise SerializationError("The encryption context contains a key that is too large.")
23+
if len(value) > max_entry_length:
24+
raise SerializationError("The encryption context contains a value that is too large.")
25+
26+
1927
def assemble_content_aad(message_id, aad_content_string, seq_num, length):
2028
"""Assembles the Body AAD string for a message body structure.
2129
@@ -71,12 +79,8 @@ def serialize_encryption_context(encryption_context):
7179
"Cannot encode dictionary key or value using {}.".format(aws_encryption_sdk.internal.defaults.ENCODING)
7280
)
7381

74-
max_value_length = aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE
7582
for key, value in sorted(encryption_context_list, key=lambda x: x[0]):
76-
if len(key) > max_value_length:
77-
raise SerializationError("The encryption context contains a key that is too large.")
78-
if len(value) > max_value_length:
79-
raise SerializationError("The encryption context contains a value that is too large.")
83+
_validate_encryption_context_entry(key, value)
8084
serialized_context.extend(
8185
struct.pack(
8286
">H{key_size}sH{value_size}s".format(key_size=len(key), value_size=len(value)),

0 commit comments

Comments
 (0)