Skip to content

Commit

Permalink
Yield protocol violation for packets without frames
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Oct 23, 2023
1 parent e1e1e6e commit 99ef446
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
6 changes: 3 additions & 3 deletions quinn-proto/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2192,7 +2192,7 @@ impl Connection {
return Ok(());
}
State::Closed(_) => {
for result in frame::Iter::new(packet.payload.freeze()) {
for result in frame::Iter::new(packet.payload.freeze())? {
let frame = match result {
Ok(frame) => frame,
Err(err) => {
Expand Down Expand Up @@ -2441,7 +2441,7 @@ impl Connection {
debug_assert_ne!(packet.header.space(), SpaceId::Data);
let payload_len = packet.payload.len();
let mut ack_eliciting = false;
for result in frame::Iter::new(packet.payload.freeze()) {
for result in frame::Iter::new(packet.payload.freeze())? {
let frame = result?;
let span = match frame {
Frame::Padding => continue,
Expand Down Expand Up @@ -2499,7 +2499,7 @@ impl Connection {
let mut close = None;
let payload_len = payload.len();
let mut ack_eliciting = false;
for result in frame::Iter::new(payload) {
for result in frame::Iter::new(payload)? {
let frame = result?;
let span = match frame {
Frame::Padding => continue,
Expand Down
16 changes: 13 additions & 3 deletions quinn-proto/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,20 @@ impl From<UnexpectedEnd> for IterErr {
}

impl Iter {
pub(crate) fn new(payload: Bytes) -> Self {
Self {
pub(crate) fn new(payload: Bytes) -> Result<Self, TransportError> {
if payload.is_empty() {
// "An endpoint MUST treat receipt of a packet containing no frames as a
// connection error of type PROTOCOL_VIOLATION."
// https://www.rfc-editor.org/rfc/rfc9000.html#name-frames-and-frame-types
return Err(TransportError::PROTOCOL_VIOLATION(
"packet payload is empty",
));
}

Ok(Self {
bytes: io::Cursor::new(payload),
last_ty: None,
}
})
}

fn take_len(&mut self) -> Result<Bytes, UnexpectedEnd> {
Expand Down Expand Up @@ -923,6 +932,7 @@ mod test {

fn frames(buf: Vec<u8>) -> Vec<Frame> {
Iter::new(Bytes::from(buf))
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap()
}
Expand Down
1 change: 1 addition & 0 deletions quinn-proto/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2257,6 +2257,7 @@ fn single_ack_eliciting_packet_triggers_ack_after_delay() {
// The ACK delay is properly calculated
assert_eq!(pair.client.captured_packets.len(), 1);
let mut frames = frame::Iter::new(pair.client.captured_packets.remove(0).into())
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(frames.len(), 1);
Expand Down

0 comments on commit 99ef446

Please sign in to comment.