Skip to content

Commit 6d36e45

Browse files
committed
fix: validate BytesValue length in KeyValuePair encoding
Fixes #128 Per draft-ietf-moq-transport-15 section 1.4.2, the maximum length of a BytesValue is 2^16-1 bytes. Previously, the code would accept BytesValues exceeding this limit during encoding, which could cause protocol violations when the peer attempts to decode the message. Now checks if the BytesValue length exceeds u16::MAX before encoding and returns EncodeError::InvalidValue if the limit is exceeded, matching the existing validation in the decode path.
1 parent f1b1d8b commit 6d36e45

File tree

1 file changed

+14
-0
lines changed
  • moq-transport/src/coding

1 file changed

+14
-0
lines changed

moq-transport/src/coding/kvp.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ impl Encode for KeyValuePair {
9898
if self.key.is_multiple_of(2) {
9999
return Err(EncodeError::InvalidValue);
100100
}
101+
// size of value must be less then 2^16-1 bytes
102+
if v.len() > u16::MAX as usize {
103+
return Err(EncodeError::InvalidValue);
104+
}
101105
self.key.encode(w)?;
102106
v.len().encode(w)?;
103107
Self::encode_remaining(w, v.len())?;
@@ -271,4 +275,14 @@ mod tests {
271275
let decoded = KeyValuePairs::decode(&mut buf).unwrap();
272276
assert_eq!(decoded, kvps);
273277
}
278+
279+
#[test]
280+
fn encode_bytes_length_exceeded() {
281+
let mut buf = BytesMut::new();
282+
283+
let huge_bytes = vec![0u8; u16::MAX as usize + 1];
284+
let kvp = KeyValuePair::new_bytes(1, huge_bytes);
285+
let result = kvp.encode(&mut buf);
286+
assert!(result.is_err(), "must rejected because bytes > 65535");
287+
}
274288
}

0 commit comments

Comments
 (0)