Skip to content

Commit c1b31a6

Browse files
committed
feat(ciborium-ll): fail decoding invalid two-byte encodings of simple values
1 parent 7c6ba83 commit c1b31a6

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

ciborium-ll/src/hdr.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ impl TryFrom<Title> for Header {
107107

108108
Title(Major::Other, Minor::More) => Self::Break,
109109
Title(Major::Other, Minor::This(x)) => Self::Simple(x),
110-
Title(Major::Other, Minor::Next1(x)) => Self::Simple(x[0]),
110+
// Two-byte sequences for simple values below 32 are not considered well-formed
111+
Title(Major::Other, Minor::Next1([0..=31])) => Err(InvalidError(()))?,
112+
Title(Major::Other, Minor::Next1([x])) => Self::Simple(x),
111113
Title(Major::Other, Minor::Next2(x)) => Self::Float(f16::from_be_bytes(x).into()),
112114
Title(Major::Other, Minor::Next4(x)) => Self::Float(f32::from_be_bytes(x).into()),
113115
Title(Major::Other, Minor::Next8(x)) => Self::Float(f64::from_be_bytes(x)),
@@ -140,6 +142,11 @@ impl From<Header> for Title {
140142

141143
Header::Simple(x) => match x {
142144
x @ 0..=23 => Title(Major::Other, Minor::This(x)),
145+
// Simple(24) is irrepresentable
146+
// Simple(25) to Simple(27) are floats
147+
// Simple(28) to Simple(30) are reserved
148+
// Should this panic or error (i.e. use TryFrom instead of From) ?
149+
// See: https://www.rfc-editor.org/rfc/rfc8949.html#section-3.3
143150
x => Title(Major::Other, Minor::Next1([x])),
144151
},
145152

ciborium-ll/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,19 @@ mod tests {
273273
(Header::Float(INFINITY), "fb7ff0000000000000", false),
274274
(Header::Float(NAN), "fb7ff8000000000000", false),
275275
(Header::Float(-INFINITY), "fbfff0000000000000", false),
276+
(Header::Simple(0), "e0", true),
277+
(Header::Simple(1), "e1", true),
278+
(Header::Simple(16), "f0", true),
279+
(Header::Simple(17), "f1", true),
280+
(Header::Simple(18), "f2", true),
281+
(Header::Simple(19), "f3", true),
276282
(Header::Simple(simple::FALSE), "f4", true),
277283
(Header::Simple(simple::TRUE), "f5", true),
278284
(Header::Simple(simple::NULL), "f6", true),
279285
(Header::Simple(simple::UNDEFINED), "f7", true),
280-
(Header::Simple(16), "f0", true),
281-
(Header::Simple(24), "f818", true),
286+
(Header::Break, "ff", true),
287+
(Header::Simple(32), "f820", true),
288+
(Header::Simple(64), "f840", true),
282289
(Header::Simple(255), "f8ff", true),
283290
(Header::Tag(0), "c0", true),
284291
(Header::Tag(1), "c1", true),

ciborium/tests/error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ fn eof() -> Error<std::io::Error> {
115115
case("fe", Error::Syntax(0)),
116116
117117
// Reserved two-byte encodings of simple values:
118-
case("f800", Error::Semantic(None, "invalid type: simple, expected known simple value".into())),
119-
case("f801", Error::Semantic(None, "invalid type: simple, expected known simple value".into())),
120-
case("f818", Error::Semantic(None, "invalid type: simple, expected known simple value".into())),
121-
case("f81f", Error::Semantic(None, "invalid type: simple, expected known simple value".into())),
118+
case("f800", Error::Syntax(0)),
119+
case("f801", Error::Syntax(0)),
120+
case("f818", Error::Syntax(0)),
121+
case("f81f", Error::Syntax(0)),
122122
123123
// Indefinite-length string chunks not of the correct type:
124124
case("5f00ff", Error::Syntax(1)),

0 commit comments

Comments
 (0)