mirror of
https://github.com/n0-computer/iroh.git
synced 2026-11-29 00:01:42 +00:00
fix(iroh-relay): prevent empty datagrams payload from disconnecting recipient (#4646)
<!-- Attention agents and contributors using AI, please see our AI policy before opening a PR: https://github.com/n0-computer/iroh/blob/main/AI_POLICY.md --> <!-- Any PRs violating this policy will be closed. --> <!-- As per our contributor guidelines, all PRs that do not link to an issue with the label `ready-for-pr` will be closed: https://github.com/n0-computer/iroh/blob/main/CONTRIBUTING.md --> ## Description Recipients now gracefully ignore empty datagrams instead of erroring, and the no longer used error variant is marked as deprecated. Resolution to https://github.com/n0-computer/iroh/security/advisories/GHSA-5c6r-mh39-9998 ## API Changes <!-- Optional, any API additions, deprecations or breaking changes, including how to migrate older code. --> ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [x] Self-review. - [ ] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [ ] All API changes documented. --------- Co-authored-by: ramfox <kasey@n0.computer> Co-authored-by: Floris Bruynooghe <flub@n0.computer>
This commit is contained in:
co-authored by
ramfox
Floris Bruynooghe
parent
325b3d3e13
commit
55181bfd33
@@ -40,6 +40,7 @@ pub enum SendError {
|
||||
StreamError { source: AnyError },
|
||||
#[error("Exceeds max packet size ({MAX_PACKET_SIZE}): {size}")]
|
||||
ExceedsMaxPacketSize { size: usize },
|
||||
/// Deprecated: no longer returned. Will be marked `#[deprecated]` in the next minor release.
|
||||
#[error("Attempted to send empty packet")]
|
||||
EmptyPacket {},
|
||||
}
|
||||
@@ -150,9 +151,6 @@ impl Sink<ClientToRelayMsg> for Conn {
|
||||
size <= MAX_PACKET_SIZE,
|
||||
SendError::ExceedsMaxPacketSize { size }
|
||||
);
|
||||
if let ClientToRelayMsg::Datagrams { datagrams, .. } = &frame {
|
||||
ensure!(!datagrams.contents.is_empty(), SendError::EmptyPacket);
|
||||
}
|
||||
|
||||
Pin::new(&mut self.conn)
|
||||
.start_send(frame.to_bytes().freeze())
|
||||
|
||||
@@ -1383,6 +1383,30 @@ mod tests {
|
||||
.connect()
|
||||
.await?;
|
||||
|
||||
// Empty datagrams must pass through without disconnecting either client.
|
||||
// The nonempty exchanges below also verify that both connections remain usable.
|
||||
let msg = Datagrams {
|
||||
ecn: None,
|
||||
segment_size: None,
|
||||
contents: bytes::Bytes::new(),
|
||||
};
|
||||
let res = try_send_recv(&mut client_a, &mut client_b, b_key, msg.clone()).await?;
|
||||
assert_eq!(
|
||||
res,
|
||||
RelayToClientMsg::Datagrams {
|
||||
remote_endpoint_id: a_key,
|
||||
datagrams: msg.clone(),
|
||||
}
|
||||
);
|
||||
let res = try_send_recv(&mut client_b, &mut client_a, a_key, msg.clone()).await?;
|
||||
assert_eq!(
|
||||
res,
|
||||
RelayToClientMsg::Datagrams {
|
||||
remote_endpoint_id: b_key,
|
||||
datagrams: msg,
|
||||
}
|
||||
);
|
||||
|
||||
info!("sending a -> b");
|
||||
|
||||
// send message from a to b
|
||||
|
||||
@@ -112,7 +112,9 @@ pub enum SendError {
|
||||
/// The size of the packet that was too large
|
||||
size: usize,
|
||||
},
|
||||
/// Attempted to send an empty packet
|
||||
/// Attempt to send an empty packet
|
||||
///
|
||||
/// Deprecated: no longer returned. Will be marked `#[deprecated]` in the next minor release.
|
||||
#[error("Attempted to send empty packet")]
|
||||
EmptyPacket {},
|
||||
}
|
||||
@@ -133,9 +135,6 @@ where
|
||||
size <= MAX_PACKET_SIZE,
|
||||
SendError::ExceedsMaxPacketSize { size }
|
||||
);
|
||||
if let RelayToClientMsg::Datagrams { datagrams, .. } = &item {
|
||||
ensure!(!datagrams.contents.is_empty(), SendError::EmptyPacket);
|
||||
}
|
||||
|
||||
Pin::new(&mut self.inner)
|
||||
.start_send(item.to_bytes().freeze())
|
||||
|
||||
@@ -125,6 +125,13 @@ impl RelayTransport {
|
||||
}
|
||||
};
|
||||
|
||||
// Empty datagrams are valid relay traffic, but cannot contain a QUIC packet.
|
||||
// Ignore them rather than treating them as oversized segments.
|
||||
if dm.datagrams.contents.is_empty() {
|
||||
self.pending_item = None;
|
||||
continue;
|
||||
}
|
||||
|
||||
// This *tries* to make the datagrams fit into our buffer by re-batching them.
|
||||
let num_segments = dm
|
||||
.datagrams
|
||||
@@ -460,7 +467,11 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn relay_recv_outputs_are_contiguous_after_drops() {
|
||||
for segment_size in [NonZeroU16::new(2000), None] {
|
||||
for (segment_size, contents) in [
|
||||
(NonZeroU16::new(2000), Bytes::from(vec![0u8; 4000])),
|
||||
(None, Bytes::from(vec![0u8; 4000])),
|
||||
(None, Bytes::new()),
|
||||
] {
|
||||
let (mut transport, sender) = test_relay_transport();
|
||||
let url = staging::default_na_east_relay().url;
|
||||
let sources = [
|
||||
@@ -483,7 +494,7 @@ mod tests {
|
||||
datagrams: Datagrams {
|
||||
ecn: None,
|
||||
segment_size,
|
||||
contents: Bytes::from(vec![0u8; 4000]),
|
||||
contents: contents.clone(),
|
||||
},
|
||||
})
|
||||
.unwrap();
|
||||
@@ -518,7 +529,11 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn relay_recv_wakes_after_exhausting_drop_budget() {
|
||||
for segment_size in [NonZeroU16::new(2000), None] {
|
||||
for (segment_size, contents) in [
|
||||
(NonZeroU16::new(2000), Bytes::from(vec![0u8; 4000])),
|
||||
(None, Bytes::from(vec![0u8; 4000])),
|
||||
(None, Bytes::new()),
|
||||
] {
|
||||
let (mut transport, sender) = test_relay_transport();
|
||||
let url = staging::default_na_east_relay().url;
|
||||
let src = SecretKey::from_bytes(&[8u8; 32]).public();
|
||||
@@ -530,7 +545,7 @@ mod tests {
|
||||
datagrams: Datagrams {
|
||||
ecn: None,
|
||||
segment_size,
|
||||
contents: Bytes::from(vec![0u8; 4000]),
|
||||
contents: contents.clone(),
|
||||
},
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
Reference in New Issue
Block a user