Skip to content

Commit 9eb2150

Browse files
authored
feat: add limits on list and map size (#190)
* feat: add limits on list and map size
1 parent 6f9b4a3 commit 9eb2150

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

DynamoDbEncryption/dafny/DynamoDbEncryption/src/DynamoToStruct.dfy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,9 @@ module DynamoToStruct {
521521
// See "The Parent Trick" for details: <https://leino.science/papers/krml283.html>.
522522
function method MapAttrToBytes(ghost parent: AttributeValue, m: MapAttributeValue, depth : nat): (ret: Result<seq<uint8>, string>)
523523
requires forall kv <- m.Items :: kv.1 < parent
524+
ensures MAX_MAP_SIZE < |m| ==> ret.Failure?
524525
{
526+
:- Need(|m| <= MAX_MAP_SIZE, "Map exceeds limit of " + MAX_MAP_SIZE_STR + " entries.");
525527
//= specification/dynamodb-encryption-client/ddb-attribute-serialization.md#value-type
526528
//# Value Type MUST be the [Type ID](#type-id) of the type of [Map Value](#map-value).
527529

@@ -543,7 +545,9 @@ module DynamoToStruct {
543545
}
544546

545547
function method ListAttrToBytes(l: ListAttributeValue, depth : nat): (ret: Result<seq<uint8>, string>)
548+
ensures MAX_LIST_LENGTH < |l| ==> ret.Failure?
546549
{
550+
:- Need(|l| <= MAX_LIST_LENGTH, "List exceeds limit of " + MAX_LIST_LENGTH_STR + " entries.");
547551
var count :- U32ToBigEndian(|l|);
548552
var body :- CollectList(l, depth);
549553
Success(count + body)
@@ -890,6 +894,7 @@ module DynamoToStruct {
890894
resultList : AttrValueAndLength)
891895
: (ret : Result<AttrValueAndLength, string>)
892896
requires resultList.val.L?
897+
requires remainingCount <= MAX_LIST_LENGTH
893898
ensures ret.Success? ==> ret.value.val.L?
894899
requires |serialized| + resultList.len == origSerializedSize
895900
ensures ret.Success? ==> ret.value.len <= origSerializedSize
@@ -922,6 +927,7 @@ module DynamoToStruct {
922927
resultMap : AttrValueAndLength)
923928
: (ret : Result<AttrValueAndLength, string>)
924929
requires resultMap.val.M?
930+
requires remainingCount <= MAX_MAP_SIZE
925931
ensures ret.Success? ==> ret.value.val.M?
926932
requires |serialized| + resultMap.len == origSerializedSize
927933
ensures ret.Success? ==> ret.value.len <= origSerializedSize
@@ -1056,6 +1062,7 @@ module DynamoToStruct {
10561062
Failure("List Structured Data has less than 4 bytes")
10571063
else
10581064
var len :- BigEndianToU32(value);
1065+
:- Need(len <= MAX_MAP_SIZE, "Map exceeds limit of " + MAX_MAP_SIZE_STR + " entries.");
10591066
var value := value[LENGTH_LEN..];
10601067
DeserializeMap(value, len, |value| + LENGTH_LEN + lengthBytes, depth, AttrValueAndLength(AttributeValue.M(map[]), LENGTH_LEN + lengthBytes))
10611068

@@ -1064,6 +1071,7 @@ module DynamoToStruct {
10641071
Failure("List Structured Data has less than 4 bytes")
10651072
else
10661073
var len :- BigEndianToU32(value);
1074+
:- Need(len <= MAX_LIST_LENGTH, "List exceeds limit of " + MAX_LIST_LENGTH_STR + " entries.");
10671075
var value := value[LENGTH_LEN..];
10681076
DeserializeList(value, len, |value| + LENGTH_LEN + lengthBytes, depth, AttrValueAndLength(AttributeValue.L([]), LENGTH_LEN + lengthBytes))
10691077

DynamoDbEncryption/dafny/DynamoDbEncryption/src/Util.dfy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ module DynamoDbEncryptionUtil {
1616

1717
const MAX_STRUCTURE_DEPTH := 32
1818
const MAX_STRUCTURE_DEPTH_STR := "32"
19+
const MAX_LIST_LENGTH := 100
20+
const MAX_LIST_LENGTH_STR := "100"
21+
const MAX_MAP_SIZE := 100
22+
const MAX_MAP_SIZE_STR := "100"
1923

2024
type HmacKeyMap = map<string, Bytes>
2125

0 commit comments

Comments
 (0)