From 873c930648ba1607d3b06be89f4de720fd0cd123 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Fri, 20 Nov 2026 17:14:01 +0100 Subject: [PATCH] fix: a connection-bound login is never given to another request The service proxy, a static service's `forward` and the public link router keep their connections to a service per target (#556, #559). A service that ties its login to the connection (Windows Negotiate/NTLM, Kestrel's and HTTP.sys's default) then took a second visitor's request on such a kept connection as the first visitor's authenticated session. `Pool::send` now hands the connection back as `Option`: `None` when the request carried `Authorization: Negotiate|NTLM` or the answer `WWW-Authenticate: Negotiate|NTLM` (`binds_connection`), as YARP does. The type makes every user of the pool handle it; such a connection goes with its answer. Closes #569 Bug-Class: connection-bound-state-shared Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 7 +++ crates/devstack-daemon/src/proxy.rs | 2 +- crates/devstack-hub/src/lib.rs | 3 +- crates/devstack-node/src/connpool.rs | 91 +++++++++++++++++++++++++--- 4 files changed, 93 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e8a56cb..08db0871 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,13 @@ and listed with their assets on the `install.ps1`. The setup's command for the distribution installs without a hub there (`DEVSTACK_NO_HUB_INTEGRATION=1`): the one on Windows runs the node agent (#596). +- **A Windows login of one visitor no longer reaches the next.** The + service proxy, a static service's `forward` and the public link router + keep their connections to a service (#556, #559). A service that ties its + login to the connection (Windows Negotiate or NTLM, Kestrel's and + HTTP.sys's default) took the next visitor's request on such a connection + as the first visitor's. A connection whose request or answer carries + Negotiate or NTLM is closed after its answer now, as YARP does (#569). ## [0.51.0] - 2026-11-19 diff --git a/crates/devstack-daemon/src/proxy.rs b/crates/devstack-daemon/src/proxy.rs index 7cee5237..5c4ae1c6 100644 --- a/crates/devstack-daemon/src/proxy.rs +++ b/crates/devstack-daemon/src/proxy.rs @@ -457,7 +457,7 @@ async fn handle(sh: Arc, mut req: Request) -> Result break false, } }; - if complete { + if complete && let Some(sender) = sender { give_back(&sh, port, sender).await; } }); diff --git a/crates/devstack-hub/src/lib.rs b/crates/devstack-hub/src/lib.rs index 5ed82e95..7e5cc81b 100644 --- a/crates/devstack-hub/src/lib.rs +++ b/crates/devstack-hub/src/lib.rs @@ -3709,10 +3709,11 @@ async fn daemon_send( let copy = repeatable.then(|| again(&req)); match sender.try_send_request(req).await { Ok(resp) => { + // The daemon's own API: no login bound to its connection. return Ok(devstack_node::connpool::GiveBack::wrap( daemon_pool(), port, - sender, + Some(sender), resp, )); } diff --git a/crates/devstack-node/src/connpool.rs b/crates/devstack-node/src/connpool.rs index 0bf55cc9..065e9871 100644 --- a/crates/devstack-node/src/connpool.rs +++ b/crates/devstack-node/src/connpool.rs @@ -132,23 +132,30 @@ where /// if hyper hands it back unsent, or if it can be sent twice /// ([`replay`]) and the error is that close ([`closed_unanswered`]). /// A new connection is never tried twice: its failure is the upstream's. + /// + /// The connection comes back with the answer, to give back once that is + /// read; `None` when it must serve no other request ([`binds_connection`]). pub async fn send( &self, key: &K, req: Request, connect: F, - ) -> Result<(SendRequest, Response), String> + ) -> Result<(Option>, Response), String> where B: Default, F: FnOnce() -> Fut, Fut: std::future::Future, String>>, { + let credentials = req.headers().clone(); + let reusable = |s: SendRequest, r: &Response| { + (!binds_connection(&credentials, r.headers())).then_some(s) + }; let req = match self.kept(key) { None => req, Some(mut s) => { let copy = replay(&req); match s.try_send_request(req).await { - Ok(r) => return Ok((s, r)), + Ok(r) => return Ok((reusable(s, &r), r)), Err(e) => match again(e, copy) { Ok(req) => req, Err(e) => return Err(e.to_string()), @@ -158,10 +165,34 @@ where }; let mut s = connect().await?; let r = s.send_request(req).await.map_err(|e| e.to_string())?; - Ok((s, r)) + Ok((reusable(s, &r), r)) } } +/// Whether a request (`request`, its headers) or its answer (`answer`) ties +/// a login to the connection: Windows Negotiate or NTLM, which Kestrel and +/// HTTP.sys authenticate per connection. Such a connection serves no other +/// request, whoever sends it (#569), as YARP does. +pub fn binds_connection(request: &hyper::HeaderMap, answer: &hyper::HeaderMap) -> bool { + let bound = |v: &hyper::header::HeaderValue| { + let scheme = v + .to_str() + .unwrap_or("") + .split_whitespace() + .next() + .unwrap_or(""); + scheme.eq_ignore_ascii_case("negotiate") || scheme.eq_ignore_ascii_case("ntlm") + }; + request + .get_all(hyper::header::AUTHORIZATION) + .iter() + .any(bound) + || answer + .get_all(hyper::header::WWW_AUTHENTICATE) + .iter() + .any(bound) +} + /// What to send again on a new connection after a request on a kept one /// failed with `e`: the request hyper hands back unsent, else `copy` (from /// [`replay`]) if the upstream closed the connection before it answered; @@ -243,17 +274,18 @@ where K: Eq + Hash + Clone + Send + 'static, B: Body + Send + 'static, { - /// `resp`'s body, giving `sender` back to `pool` under `key` at its end. + /// `resp`'s body, giving `sender` back to `pool` under `key` at its end + /// (none: the connection goes with the answer). pub fn wrap( pool: Arc>, key: K, - sender: SendRequest, + sender: Option>, resp: Response, ) -> Response { let (parts, body) = resp.into_parts(); let mut wrapped = Self { body, - back: Some((pool, key, sender)), + back: sender.map(|s| (pool, key, s)), }; // An empty answer (a 204, a HEAD) may never be polled. if wrapped.body.is_end_stream() { @@ -322,6 +354,9 @@ mod tests { ClosesKept, /// Closes on every request without an answer. ClosesAll, + /// Answers a request without credentials with a Negotiate challenge + /// (401), one with them with 200, and keeps the connection open. + Challenges, } /// A raw HTTP/1.1 upstream; counts the connections it accepted. @@ -351,13 +386,22 @@ mod tests { let close = match mode { Upstream::ClosesKept => served > 0, Upstream::ClosesAll => true, + Upstream::Challenges => false, }; if close { return; // dropped: closed, no answer } served += 1; - let ok = b"HTTP/1.1 200 OK\r\ncontent-length: 2\r\n\r\nok"; - if s.write_all(ok).await.is_err() { + let credentials = String::from_utf8_lossy(&head) + .to_ascii_lowercase() + .contains("\r\nauthorization:"); + let answer: &[u8] = if matches!(mode, Upstream::Challenges) && !credentials + { + b"HTTP/1.1 401 Unauthorized\r\nwww-authenticate: Negotiate\r\nwww-authenticate: NTLM\r\ncontent-length: 0\r\n\r\n" + } else { + b"HTTP/1.1 200 OK\r\ncontent-length: 2\r\n\r\nok" + }; + if s.write_all(answer).await.is_err() { return; } } @@ -463,4 +507,35 @@ mod tests { assert_eq!(through(&pool, port, request("POST", "x")).await, Ok(200)); assert_eq!(conns.load(Ordering::SeqCst), 2); } + + /// #569: a service that ties its login to the connection (Windows + /// Negotiate/NTLM, Kestrel's and HTTP.sys's default) takes the next + /// request on it as the first visitor's. Neither the challenge's + /// connection nor the one that carried the credentials goes back. + #[tokio::test] + async fn a_connection_bound_login_is_never_given_to_another_request() { + let (port, conns) = upstream(Upstream::Challenges).await; + let pool = Pool::new(IDLE_MAX); + // The challenge: `WWW-Authenticate: Negotiate`. + assert_eq!(through(&pool, port, request("GET", "")).await, Ok(401)); + // The credentials, on whatever connection. + let mut auth = request("GET", ""); + auth.headers_mut() + .insert("authorization", "NTLM TlRMTVNTUAAB".parse().unwrap()); + assert_eq!(through(&pool, port, auth).await, Ok(200)); + tokio::time::sleep(Duration::from_millis(100)).await; + assert_eq!( + pool.idle(&()), + 0, + "a connection with a bound login was kept" + ); + assert_eq!(conns.load(Ordering::SeqCst), 2); + // Any other answer keeps its connection, as before. + let mut basic = request("GET", ""); + basic + .headers_mut() + .insert("authorization", "Basic dTpw".parse().unwrap()); + assert_eq!(through(&pool, port, basic).await, Ok(200)); + kept_again(&pool).await; + } }