Skip to content

Commit 8393836

Browse files
author
Michael de Hoog
authored
Restrict CBOR element parsing to supported types (#5)
1 parent 98e49d7 commit 8393836

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/CborDecode.sol

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ library LibCborElement {
3838

3939
// Returns true if the element is null
4040
function isNull(CborElement self) internal pure returns (bool) {
41-
return cborType(self) == 0xF6;
41+
uint8 _type = cborType(self);
42+
return _type == 0xf6 || _type == 0xf7; // null or undefined
4243
}
4344

4445
// Pack 3 uint80s into a uint256
@@ -101,11 +102,15 @@ library CborDecode {
101102
uint8 _type = uint8(cbor[ix] & 0xe0);
102103
uint8 ai = uint8(cbor[ix] & 0x1f);
103104
if (_type == 0xe0) {
104-
require(!required || ai != 22, "null value for required element");
105-
// primitive type, retain the additional information
105+
// The primitive type can encode a float, bool, null, undefined, etc.
106+
// We only need support for null (and we treat undefined as null).
107+
require(ai == 22 || ai == 23, "only null primitive values are supported");
108+
require(!required, "null value for required element");
109+
// retain the additional information:
106110
return LibCborElement.toCborElement(_type | ai, ix + 1, 0);
107111
}
108112
require(_type == expectedType, "unexpected type");
113+
require(ai < 28, "unsupported type");
109114
if (ai == 24) {
110115
return LibCborElement.toCborElement(_type, ix + 2, uint8(cbor[ix + 1]));
111116
} else if (ai == 25) {

0 commit comments

Comments
 (0)