File tree Expand file tree Collapse file tree
src/aws_encryption_sdk/internal/formatting Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )),
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments