Skip to content

transport: Client does not discard packets with a different SCID (RFC 9000 7.2) #4367

Open

Description

@larseggert

Related: #3814, #3785
See also: #4372

A neqo client keeps processing Initial and Handshake packets from any Source Connection ID after it has accepted the server's first Initial, although RFC 9000 Section 7.2 says it MUST discard them. This is a protocol violation with no real security impact. The packets still have to pass packet protection, and the connection ID the client sends to and checks against the authenticated initial_source_connection_id transport parameter does not change. The only new capability is narrow. Someone who saw the client's first packet, but not the server's reply, can derive the Initial keys and get a forged Initial (for example one carrying CONNECTION_CLOSE) processed after the server's Initial has arrived, which aborts that one handshake. This is possible only until the client sends its first Handshake packet. A conforming client would drop that packet, but it would equally accept the same packet if it arrived before the server's Initial.

The SCID of the first server Initial is stored in remote_initial_source_cid by start_handshake(), but no incoming packet is ever compared with it. Connection::preprocess_packet() in neqo-transport/src/connection/mod.rs checks only the Destination Connection ID for Initial and Handshake packets before decrypting them. The attached reproducer is a neqo-transport integration test. It gives a default client, after the server's first Initial, a correctly protected Initial with a PING from SCID bbbbbbbbbbbbbbbb, and the PING is counted as processed rather than dropped. Handshake packets from a changed SCID are accepted the same way until HANDSHAKE_DONE, but only the real server can produce those. The server side has the matching gap for later client Initials. The missing check appears to date from the original client handshake code (5aaa374, February 2019) and is present in every release since v0.1.0. Two alternative patches are attached: a client-only check in preprocess_packet(), and a version that also covers the server.

Details

A client does not discard long header packets whose Source Connection ID (SCID) differs from the one in
the server's first Initial packet. RFC 9000, Section 7.2:

Once a client has received a valid Initial packet from the server, it MUST discard any subsequent packet
it receives on that connection with a different Source Connection ID.

A default client that has processed the first Initial packet of a server (SCID 08000000002376b7 in one
run) is given a second, correctly protected and padded Initial packet with a PING frame and the SCID
bbbbbbbbbbbbbbbb. Stats::frame_rx.ping goes up by one and Stats::dropped_rx does not change, exactly
as for the same packet sent with the server's own SCID.

The SCID of the first Initial packet is recorded when that packet has been processed.
postprocess_packet() calls start_handshake() only while the state is WaitInitial:

// neqo-transport/src/connection/mod.rs:1738
if self.state == State::WaitInitial {
    self.start_handshake(path, packet, now);
}
// neqo-transport/src/connection/mod.rs:2107
self.remote_initial_source_cid = Some(ConnectionId::from(packet.scid()));
…
// neqo-transport/src/connection/mod.rs:2120 (client)
path.borrow_mut().set_remote_cid(packet.scid());

After that, remote_initial_source_cid is read in four places: validate_cids() (mod.rs:3185) compares
it with the initial_source_connection_id transport parameter, setup_handshake_path() (mod.rs:2027)
uses it as the remote connection ID of the handshake path, and process_tps() (mod.rs:3143) and the
NEW_CONNECTION_ID handler (mod.rs:3502) test whether it is empty. No incoming packet is compared with
it. The comparison has never existed: the gap dates back to the client handshake itself, 5aaa3741fd87
("Handshake completed", 2019-02-22), and is in every release since v0.1.0.

Every packet of a datagram goes through preprocess_packet() (mod.rs:1555, called from input_path()
at mod.rs:1834) before packet.decrypt() at mod.rs:1843. For a client, the arms of its first match
that look at packet.scid() concern Retry and Version Negotiation only; an Initial or Handshake packet
falls through to the second match, which checks the Destination Connection ID and nothing else:

// neqo-transport/src/connection/mod.rs:1668
State::WaitVersion | State::Handshaking | State::Connected | State::Confirmed => {
    if self.cid_manager.is_valid(packet.dcid()) {
        …
        PreprocessResult::Continue

The SCID of a decrypted packet is read in start_handshake() and, to fill in the same field after a
processing error, in ensure_error_path() (mod.rs:2095); neither compares it. The packet in the example
therefore reaches decrypt(), opens with the Initial keys, and its frames are handled by
process_packet().

The window is the lifetime of the keys: Initial packets are accepted this way until the client sends its
first Handshake packet (discard_keys(PacketNumberSpace::Initial, …) at mod.rs:2961), Handshake packets
until HANDSHAKE_DONE arrives (mod.rs:3603). The Handshake case was confirmed with a second test that is
not part of the reproducer because it needs the server's handshake secret from the NSS key log: a
handshake between a default client and a fixture server was relayed, and every long header packet the
server sent after its first Initial packet (an Initial packet with the end of the ServerHello and two
Handshake packets in the observed run) was re-protected with the bitwise complement of the server's SCID.
The client reached State::Confirmed. The reverse order shows the same: after a forged first Initial
from bbbbbbbbbbbbbbbb, the real server's flight from its own SCID, Handshake packet included, is
processed too (the handshake then fails in validate_cids()).

The server side has the same gap for later client Initial packets: a server sets
remote_initial_source_cid in start_handshake() or set_retry_cids() (mod.rs:593) and never
compares it either, although RFC 9000 Section 7.2 also requires it to discard Initial packets with a
different SCID.

Impact

A protocol violation; no security property is lost. A packet from another SCID still has to pass packet
protection. Handshake packets need the TLS handshake secrets, so only the server the client is talking to
can send them, and changing its own SCID gains it nothing: the client keeps sending to the connection ID
of the first Initial packet, and that is still the value validate_cids() checks against the authenticated
initial_source_connection_id transport parameter. Initial keys are derived from the Destination
Connection ID of the client's first packet, so someone who saw only that packet, and not the server's
reply with its SCID, can have an Initial packet processed after the server's Initial has arrived, where a
conforming client would drop it. Initial packets are not authenticated against such a sender in any case:
the same packet, sent so that it arrives before the server's Initial, is processed by a conforming client
as well. At worst the extra packet carries a CONNECTION_CLOSE and aborts that one handshake, exactly as
it would if it had arrived before the server's first Initial; what the missing check adds is a window
of a few milliseconds, until the client sends its first Handshake packet, in which someone who saw the
client's first packet but not the server's reply can still do this.

Suggested fix

Attached patch 1 drops long header packets whose SCID is not the recorded one in preprocess_packet(),
before protection is removed, in a client-only arm next to the existing "Client received an Initial with
a token" discard (neqo-transport/src/connection/mod.rs):

(
    packet::Type::Initial | packet::Type::ZeroRtt | packet::Type::Handshake,
    _,
    Role::Client,
) if self
    .remote_initial_source_cid
    .as_ref()
    .is_some_and(|cid| *cid != packet.scid()) =>
{
    self.stats
        .borrow_mut()
        .pkt_dropped(format!("Invalid SCID {:?}", packet.scid()));
    return Ok(PreprocessResult::Next);
}

On a client remote_initial_source_cid is None until the first server Initial has been processed, so
Retry, Version Negotiation and the first Initial are unaffected, and short header packets never reach
the arm. The patch adds client_initial_with_changed_scid to connection/tests/handshake.rs: a second
Initial from the same SCID is still processed, one from another SCID is dropped, and so is a Handshake
packet from another SCID instead of being saved for later. For that it gives the forged_server_initial
test helper a variant, forged_server_initial_from, that takes the SCID; the existing callers are
unchanged.

Attached patch 2 is the same arm without the Role::Client restriction, so it also closes the server-side
gap described above, and carries a second test, server_initial_with_changed_scid, that forges client
Initial packets with the same and with a different SCID. For a server it also drops 0-RTT and Handshake
packets from a changed SCID, which is stricter than RFC 9000 requires (it asks this of a server only for
Initial packets) but cannot affect a conforming client. Take patch 1 for the minimal, client-only change,
patch 2 to cover the server as well.

With either patch, a server that switches its SCID between Initial packets or between Initial and
Handshake (which RFC 9000 does not allow) is ignored instead of tolerated, and its packets count in
Stats::dropped_rx. If a forged Initial arrives before the server's, the client now ignores the real
server and waits for its timers, where today it fails at once with a protocol violation; both leave the
connection dead. With either patch applied to a clean checkout the reproducer prints OK, and the
workspace tests, cargo +nightly fmt --check and cargo clippy --locked --all-targets -- -D warnings are
clean.

Proposed patches

patches/1.patch
# neqo-transport/src/connection/mod.rs
# fix: client discards long header packets whose Source Connection ID differs from the server's first Initial
#   (RFC 9000, Section 7.2), in preprocess_packet()
#
# The client records the Source Connection ID of the server's first processed Initial in
#   `remote_initial_source_cid` (start_handshake) but never compared later packets with it, so Initial and
#   Handshake packets from any SCID reached decrypt() and frame processing. The patch adds a client-only match
#   arm in Connection::preprocess_packet(), next to the existing "Initial with a token" discard and before
#   protection is removed, that drops Initial, 0-RTT and Handshake packets whose SCID differs once that field
#   is set; it is None until the first server Initial is processed, so Retry, Version Negotiation and the
#   first Initial are unaffected, and short header packets (no SCID) never reach the arm. A unit test in
#   connection/tests/handshake.rs (same style as the tests of #3785/#3814) checks that a second Initial from
#   the same SCID is still processed, that one from another SCID is dropped, and that a Handshake packet from
#   another SCID is dropped instead of being saved for later; it needs a small SCID parameter on the existing
#   forged_server_initial test helper, added as a wrapper so the seven current callers are untouched. The full
#   workspace suite (1958 tests), cargo +nightly fmt --check and cargo clippy --locked --all-targets -- -D
#   warnings are clean. Behaviour change to note: a server that switches SCID between its Initial packets or
#   between Initial and Handshake (not allowed by RFC 9000) is now ignored rather than tolerated, and such
#   packets count in Stats::dropped_rx; server-side behaviour is unchanged (see candidate 2).
# reviewer: good — One client-only match arm in preprocess_packet(), before decryption, keyed on the
#   already-recorded ID; leaves Retry, version negotiation and the first Initial alone; its test fails without
#   the fix; full suite (1958), fmt and clippy clean.
# our check: FIXED · confidence high
#
diff --git a/neqo-transport/src/connection/mod.rs b/neqo-transport/src/connection/mod.rs
index c7261652..d79994ac 100644
--- a/neqo-transport/src/connection/mod.rs
+++ b/neqo-transport/src/connection/mod.rs
@@ -1577,6 +1577,23 @@ impl Connection {
                     .pkt_dropped("Client received an Initial with a token");
                 return Ok(PreprocessResult::Next);
             }
+            // RFC 9000, Section 7.2: once a client has received a valid Initial from the
+            // server, it MUST discard any packet with a different Source Connection ID.
+            // `remote_initial_source_cid` is set when that first Initial is processed.
+            (
+                packet::Type::Initial | packet::Type::ZeroRtt | packet::Type::Handshake,
+                _,
+                Role::Client,
+            ) if self
+                .remote_initial_source_cid
+                .as_ref()
+                .is_some_and(|cid| *cid != packet.scid()) =>
+            {
+                self.stats
+                    .borrow_mut()
+                    .pkt_dropped(format!("Invalid SCID {:?}", packet.scid()));
+                return Ok(PreprocessResult::Next);
+            }
             (packet::Type::Initial, State::Init, Role::Server) => {
                 let version = packet.version().ok_or(Error::ProtocolViolation)?;
                 if !packet.is_valid_initial()
diff --git a/neqo-transport/src/connection/tests/handshake.rs b/neqo-transport/src/connection/tests/handshake.rs
index 00d106d7..ad81da21 100644
--- a/neqo-transport/src/connection/tests/handshake.rs
+++ b/neqo-transport/src/connection/tests/handshake.rs
@@ -2277,6 +2277,49 @@ fn client_initial_with_token() {
     assert_eq!(client.stats().dropped_rx, dropped + 1);
 }
 
+/// RFC 9000, Section 7.2: once a client has received a valid Initial from the server,
+/// it MUST discard any packet with a different Source Connection ID. Packets with the
+/// Source Connection ID of that first Initial are still processed.
+#[cfg(not(feature = "disable-encryption"))]
+#[test]
+fn client_initial_with_changed_scid() {
+    use super::forged_server_initial_from;
+
+    // A server Initial from `scid` that carries a PING.
+    fn server_initial(ci: &[u8], scid: &[u8], pn: packet::Number) -> Datagram {
+        let ping = [FrameType::Ping.into()];
+        let initial = forged_server_initial_from(ci, scid, Version::default(), pn, &[], &ping);
+        datagram(initial)
+    }
+
+    // Disable MLKEM so that a server answers with an Initial and a Handshake packet
+    // in a single datagram.
+    let mut client = new_client(ConnectionParameters::default().mlkem(false));
+    let ci = client_initial(&mut client);
+
+    // The first Initial sets the Source Connection ID that the server uses.
+    client.process_input(server_initial(&ci, b"server", 0), now());
+    assert_ne!(*client.state(), State::WaitInitial);
+    client.process_input(server_initial(&ci, b"server", 1), now());
+    assert_eq!(client.stats().frame_rx.ping, 2);
+    assert_eq!(client.stats().dropped_rx, 0);
+
+    // An Initial with any other Source Connection ID is discarded.
+    client.process_input(server_initial(&ci, b"other", 2), now());
+    assert_eq!(client.stats().frame_rx.ping, 2);
+    assert_eq!(client.stats().dropped_rx, 1);
+
+    // So is a Handshake packet, here from a server that chose its own connection ID,
+    // which would otherwise be saved until the Handshake keys are available.
+    let mut server = default_server();
+    let s_init = server.process(Some(datagram(ci)), now()).dgram().unwrap();
+    let s_hs = split_datagram(&s_init).1.unwrap();
+    assert_handshake(&s_hs);
+    client.process_input(s_hs, now());
+    assert_eq!(client.stats().saved_datagrams, 0);
+    assert_eq!(client.stats().dropped_rx, 2);
+}
+
 #[test]
 fn received_packet_reports_its_own_cids() {
     let mut client = default_client();
diff --git a/neqo-transport/src/connection/tests/mod.rs b/neqo-transport/src/connection/tests/mod.rs
index 9a19136a..48867a41 100644
--- a/neqo-transport/src/connection/tests/mod.rs
+++ b/neqo-transport/src/connection/tests/mod.rs
@@ -393,6 +393,19 @@ fn forged_server_initial(
     pn: packet::Number,
     token: &[u8],
     frames: &[u64],
+) -> Vec<u8> {
+    forged_server_initial_from(client_initial, b"fakescid", version, pn, token, frames)
+}
+
+/// Like [`forged_server_initial`], with a choice of Source Connection ID.
+#[cfg(not(feature = "disable-encryption"))]
+fn forged_server_initial_from(
+    client_initial: &[u8],
+    scid: &[u8],
+    version: Version,
+    pn: packet::Number,
+    token: &[u8],
+    frames: &[u64],
 ) -> Vec<u8> {
     use neqo_common::Encoder;
 
@@ -412,7 +425,7 @@ fn forged_server_initial(
         packet::Type::Initial,
         version,
         Some(dcid),
-        Some(b"fakescid"),
+        Some(scid),
         packet::LIMIT,
     );
     builder.initial_token(token);
patches/2.patch
# neqo-transport/src/connection/mod.rs
# fix: both roles discard long header packets whose Source Connection ID differs from the peer's first Initial
#   (RFC 9000, Section 7.2), in preprocess_packet()
#
# Same check and placement as candidate 1, but without the Role::Client restriction: RFC 9000 Section 7.2 also
#   says that a server sets the client's connection ID from the first Initial and that subsequent Initial
#   packets with a different Source Connection ID MUST be discarded, and the server side of neqo has the same
#   gap (it sets `remote_initial_source_cid` in start_handshake() or set_retry_cids() and never compares). The
#   arm is simpler (no role in the pattern) and for a server it also drops 0-RTT and Handshake packets from a
#   changed SCID, which is stricter than the RFC requires but cannot affect a conforming client, whose SCID is
#   fixed for the handshake (and must not change after a Retry). It carries candidate 1's client test plus a
#   server test that forges client Initial packets with the same and with a different SCID. The full workspace
#   suite, including the Retry and server integration tests (1959 tests), fmt and clippy are clean. Offered
#   separately because it changes the experimental server's behaviour, which the finding did not cover;
#   maintainers who want the minimal client-only change should take candidate 1.
# reviewer: good — Same arm for both roles, which also closes the server-side gap the RFC names; full suite
#   (1959), fmt and clippy clean; note it makes the experimental server drop Handshake and 0-RTT packets from
#   a changed ID too, which is stricter than the RFC asks (it only requires this for Initial), so take patch 1
#   if you want the minimal change.
# our check: FIXED · confidence medium
#
diff --git a/neqo-transport/src/connection/mod.rs b/neqo-transport/src/connection/mod.rs
index c7261652..8bb849e1 100644
--- a/neqo-transport/src/connection/mod.rs
+++ b/neqo-transport/src/connection/mod.rs
@@ -1577,6 +1577,21 @@ impl Connection {
                     .pkt_dropped("Client received an Initial with a token");
                 return Ok(PreprocessResult::Next);
             }
+            // RFC 9000, Section 7.2: the Source Connection ID of the first Initial from
+            // the peer is the one it uses for the rest of the handshake; an endpoint MUST
+            // discard later packets with a different one (a client any packet, a server
+            // at least Initial packets). `remote_initial_source_cid` holds that value.
+            (packet::Type::Initial | packet::Type::ZeroRtt | packet::Type::Handshake, ..)
+                if self
+                    .remote_initial_source_cid
+                    .as_ref()
+                    .is_some_and(|cid| *cid != packet.scid()) =>
+            {
+                self.stats
+                    .borrow_mut()
+                    .pkt_dropped(format!("Invalid SCID {:?}", packet.scid()));
+                return Ok(PreprocessResult::Next);
+            }
             (packet::Type::Initial, State::Init, Role::Server) => {
                 let version = packet.version().ok_or(Error::ProtocolViolation)?;
                 if !packet.is_valid_initial()
diff --git a/neqo-transport/src/connection/tests/handshake.rs b/neqo-transport/src/connection/tests/handshake.rs
index 00d106d7..2c224162 100644
--- a/neqo-transport/src/connection/tests/handshake.rs
+++ b/neqo-transport/src/connection/tests/handshake.rs
@@ -2277,6 +2277,102 @@ fn client_initial_with_token() {
     assert_eq!(client.stats().dropped_rx, dropped + 1);
 }
 
+/// RFC 9000, Section 7.2: once a client has received a valid Initial from the server,
+/// it MUST discard any packet with a different Source Connection ID. Packets with the
+/// Source Connection ID of that first Initial are still processed.
+#[cfg(not(feature = "disable-encryption"))]
+#[test]
+fn client_initial_with_changed_scid() {
+    use super::forged_server_initial_from;
+
+    // A server Initial from `scid` that carries a PING.
+    fn server_initial(ci: &[u8], scid: &[u8], pn: packet::Number) -> Datagram {
+        let ping = [FrameType::Ping.into()];
+        let initial = forged_server_initial_from(ci, scid, Version::default(), pn, &[], &ping);
+        datagram(initial)
+    }
+
+    // Disable MLKEM so that a server answers with an Initial and a Handshake packet
+    // in a single datagram.
+    let mut client = new_client(ConnectionParameters::default().mlkem(false));
+    let ci = client_initial(&mut client);
+
+    // The first Initial sets the Source Connection ID that the server uses.
+    client.process_input(server_initial(&ci, b"server", 0), now());
+    assert_ne!(*client.state(), State::WaitInitial);
+    client.process_input(server_initial(&ci, b"server", 1), now());
+    assert_eq!(client.stats().frame_rx.ping, 2);
+    assert_eq!(client.stats().dropped_rx, 0);
+
+    // An Initial with any other Source Connection ID is discarded.
+    client.process_input(server_initial(&ci, b"other", 2), now());
+    assert_eq!(client.stats().frame_rx.ping, 2);
+    assert_eq!(client.stats().dropped_rx, 1);
+
+    // So is a Handshake packet, here from a server that chose its own connection ID,
+    // which would otherwise be saved until the Handshake keys are available.
+    let mut server = default_server();
+    let s_init = server.process(Some(datagram(ci)), now()).dgram().unwrap();
+    let s_hs = split_datagram(&s_init).1.unwrap();
+    assert_handshake(&s_hs);
+    client.process_input(s_hs, now());
+    assert_eq!(client.stats().saved_datagrams, 0);
+    assert_eq!(client.stats().dropped_rx, 2);
+}
+
+/// RFC 9000, Section 7.2: a server takes the connection ID of the client from the
+/// first Initial; later Initial packets with a different Source Connection ID MUST be
+/// discarded. Those with the same Source Connection ID are still processed.
+#[cfg(not(feature = "disable-encryption"))]
+#[test]
+fn server_initial_with_changed_scid() {
+    use neqo_common::Encoder;
+
+    use crate::crypto::{CryptoDxDirection, CryptoDxState};
+
+    // A client Initial from `scid` for the connection that `dcid` started, with a PING.
+    fn client_initial_from(dcid: &[u8], scid: &[u8], pn: packet::Number) -> Datagram {
+        let version = Version::default();
+        let mut write =
+            CryptoDxState::new_initial(version, CryptoDxDirection::Write, "client in", dcid, pn)
+                .unwrap();
+        let mut builder = packet::Builder::long(
+            Encoder::default(),
+            packet::Type::Initial,
+            version,
+            Some(dcid),
+            Some(scid),
+            packet::LIMIT,
+        );
+        builder.initial_token(&[]);
+        builder.pn(pn, 1);
+        assert!(builder.write_varint_frame(&[FrameType::Ping.into()]));
+        builder.encode([0; 20]); // PADDING, for the header protection sample.
+        datagram(builder.build(&mut write).unwrap().into())
+    }
+
+    // A single Initial packet with packet number 0 carries the whole `ClientHello`.
+    let mut client = new_client(
+        ConnectionParameters::default()
+            .mlkem(false)
+            .randomize_first_pn(false),
+    );
+    let mut server = default_server();
+    let ci = client_initial(&mut client);
+    let (dcid, scid) = initial_cids(&ci);
+    server.process_input(datagram(ci), now());
+    assert_ne!(*server.state(), State::WaitInitial);
+    let dropped = server.stats().dropped_rx;
+
+    server.process_input(client_initial_from(&dcid, &scid, 1), now());
+    assert_eq!(server.stats().frame_rx.ping, 1);
+    assert_eq!(server.stats().dropped_rx, dropped);
+
+    server.process_input(client_initial_from(&dcid, b"other", 2), now());
+    assert_eq!(server.stats().frame_rx.ping, 1);
+    assert_eq!(server.stats().dropped_rx, dropped + 1);
+}
+
 #[test]
 fn received_packet_reports_its_own_cids() {
     let mut client = default_client();
diff --git a/neqo-transport/src/connection/tests/mod.rs b/neqo-transport/src/connection/tests/mod.rs
index 9a19136a..48867a41 100644
--- a/neqo-transport/src/connection/tests/mod.rs
+++ b/neqo-transport/src/connection/tests/mod.rs
@@ -393,6 +393,19 @@ fn forged_server_initial(
     pn: packet::Number,
     token: &[u8],
     frames: &[u64],
+) -> Vec<u8> {
+    forged_server_initial_from(client_initial, b"fakescid", version, pn, token, frames)
+}
+
+/// Like [`forged_server_initial`], with a choice of Source Connection ID.
+#[cfg(not(feature = "disable-encryption"))]
+fn forged_server_initial_from(
+    client_initial: &[u8],
+    scid: &[u8],
+    version: Version,
+    pn: packet::Number,
+    token: &[u8],
+    frames: &[u64],
 ) -> Vec<u8> {
     use neqo_common::Encoder;
 
@@ -412,7 +425,7 @@ fn forged_server_initial(
         packet::Type::Initial,
         version,
         Some(dcid),
-        Some(b"fakescid"),
+        Some(scid),
         packet::LIMIT,
     );
     builder.initial_token(token);

When fixing this, please carry this line into the commit message:

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-TASSTEYA.

Activity

  1. added
    server-side
    client-side
    on Nov 12, 2026
  2. self-assigned this
    on Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

client-sidep3server-side

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions