Skip to content

Commit d6ea609

Browse files
committed
fix: validate encryption context entry lengths
1 parent 04b001e commit d6ea609

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/aws_encryption_sdk/internal/formatting/encryption_context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ def serialize_encryption_context(encryption_context):
7171
"Cannot encode dictionary key or value using {}.".format(aws_encryption_sdk.internal.defaults.ENCODING)
7272
)
7373

74+
max_value_length = aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE
7475
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.")
7580
serialized_context.extend(
7681
struct.pack(
7782
">H{key_size}sH{value_size}s".format(key_size=len(key), value_size=len(value)),

test/unit/test_encryption_context.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ def test_serialize_encryption_context_too_large(self):
6464
)
6565
excinfo.match("The serialized context is too large")
6666

67+
def test_serialize_encryption_context_key_too_large(self):
68+
oversized_key = "a" * (aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE + 1)
69+
with pytest.raises(SerializationError) as excinfo:
70+
aws_encryption_sdk.internal.formatting.encryption_context.serialize_encryption_context(
71+
{oversized_key: "value"}
72+
)
73+
excinfo.match("The encryption context contains a key that is too large.")
74+
75+
def test_serialize_encryption_context_value_too_large(self):
76+
oversized_value = "a" * (aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE + 1)
77+
with pytest.raises(SerializationError) as excinfo:
78+
aws_encryption_sdk.internal.formatting.encryption_context.serialize_encryption_context(
79+
{"key": oversized_value}
80+
)
81+
excinfo.match("The encryption context contains a value that is too large.")
82+
6783
def test_serialize_encryption_context_unencodable(self):
6884
"""Validate that the serialize_encryption_context
6985
function behaves as expected when presented

0 commit comments

Comments
 (0)