From fe3125ec61820b126deaf76eff2791efcd143edf Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Mon, 30 Nov 2026 20:05:21 +0800 Subject: [PATCH] fix(gpclient): honor the requested username when reusing cookies (#732) --- apps/gpclient/src/connect/flow_tests.rs | 43 +++++++++++++++++++++++++ apps/gpclient/src/connect/gateway.rs | 4 +-- crates/gpapi/src/cookie_store.rs | 26 +++++++++++---- 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/apps/gpclient/src/connect/flow_tests.rs b/apps/gpclient/src/connect/flow_tests.rs index b3253f3..9e31f4d 100644 --- a/apps/gpclient/src/connect/flow_tests.rs +++ b/apps/gpclient/src/connect/flow_tests.rs @@ -129,6 +129,49 @@ async fn cached_tunnel_authentication_does_not_require_portal_discovery() { ); } +#[tokio::test] +async fn explicit_user_skips_another_users_cached_gateway_and_portal_credentials() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let origin = format!("http://{}", listener.local_addr().unwrap()); + let directory = tempfile::tempdir().unwrap(); + let cache = directory.path().join("cookie.json"); + let args = TestArgs::parse_from([ + "test", + &origin, + "--user", + "bob", + "--cookie-cache", + cache.to_str().unwrap(), + ]) + .connect; + let lock = directory.path().join("client.pid"); + let verbose = gpapi::clap::InfoLevelVerbosity::new(0, 0); + let shared = SharedArgs { + ignore_tls_errors: true, + lock_file: &lock, + verbose: &verbose, + log_format: LogFormat::Text, + }; + let handler = ConnectHandler::new(&args, &shared); + cookie_store::save( + &cache, + &StoredCookie::new( + args.server.clone(), + "alice".into(), + handler.os_profile.borrow().host_identity().host_id().into(), + origin, + AuthCookieCredential::new("alice", "portal-cookie", ""), + ), + ) + .unwrap(); + + assert!(handler.cached_portal_credential(&args.server).is_none()); + tokio::select! { + result = handler.try_cached_cookie(&args.server) => assert!(result.is_none()), + _ = listener.accept() => panic!("a mismatched cache entry must be skipped before contacting the gateway"), + } +} + #[tokio::test] async fn unreachable_gateway_keeps_cached_cookie() { // Nothing listens here: the cached gateway login gets no answer at all. diff --git a/apps/gpclient/src/connect/gateway.rs b/apps/gpclient/src/connect/gateway.rs index 82922cb..4a06d2a 100644 --- a/apps/gpclient/src/connect/gateway.rs +++ b/apps/gpclient/src/connect/gateway.rs @@ -83,7 +83,7 @@ impl ConnectHandler<'_> { pub(super) async fn try_cached_cookie(&self, server: &str) -> Option> { let path = cookie_cache_path(self.args)?; let host_id = self.os_profile.borrow().host_identity().host_id().to_string(); - let stored = cookie_store::load(&path, server, &host_id)?; + let stored = cookie_store::load(&path, server, &host_id, self.args.user.as_deref())?; if !stored.auth_cookie.can_authenticate_gateway() { cookie_store::clear(&path); return None; @@ -145,7 +145,7 @@ impl ConnectHandler<'_> { } let path = cookie_cache_path(self.args)?; let host_id = self.os_profile.borrow().host_identity().host_id().to_string(); - let stored = cookie_store::load(&path, server, &host_id)?; + let stored = cookie_store::load(&path, server, &host_id, self.args.user.as_deref())?; if !stored.auth_cookie.can_authenticate_gateway() { cookie_store::clear(&path); return None; diff --git a/crates/gpapi/src/cookie_store.rs b/crates/gpapi/src/cookie_store.rs index e6848f5..d8da9ef 100644 --- a/crates/gpapi/src/cookie_store.rs +++ b/crates/gpapi/src/cookie_store.rs @@ -59,12 +59,15 @@ pub fn cookie_path(custom: Option<&str>) -> Option { } } -pub fn load(path: &Path, server: &str, host_id: &str) -> Option { +pub fn load(path: &Path, server: &str, host_id: &str, requested_user: Option<&str>) -> Option { let bytes = zeroize::Zeroizing::new(platform::decode(fs::read(path).ok()?).ok()?); let stored: StoredCookie = serde_json::from_slice(&bytes).ok()?; if stored.version != STORE_VERSION || stored.server != server || stored.host_id != host_id { return None; } + if requested_user.is_some_and(|user| user != stored.auth_cookie.username()) { + return None; + } Some(stored) } @@ -116,7 +119,7 @@ mod tests { .windows(b"prelogon-auth".len()) .any(|part| part == b"prelogon-auth") ); - assert!(load(&path, "vpn.example.com", "host-1").is_some()); + assert!(load(&path, "vpn.example.com", "host-1", None).is_some()); } #[test] @@ -132,24 +135,35 @@ mod tests { assert_eq!(fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600); } - let loaded = load(&path, "vpn.example.com", "host-1").unwrap(); + let loaded = load(&path, "vpn.example.com", "host-1", None).unwrap(); assert_eq!(loaded.username, "alice"); assert_eq!(loaded.host_id, "host-1"); assert_eq!(loaded.last_gateway, "gw1.example.com"); assert_eq!(loaded.auth_cookie.user_auth_cookie(), "user-auth"); assert!( - load(&path, "other.example.com", "host-1").is_none(), + load(&path, "other.example.com", "host-1", None).is_none(), "must reject mismatched server" ); assert!( - load(&path, "vpn.example.com", "host-2").is_none(), + load(&path, "vpn.example.com", "host-2", None).is_none(), "must reject mismatched host id" ); clear(&path); assert!(!path.exists()); } + #[test] + fn matches_explicit_user_against_cached_credential() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("cookie.json"); + let stored = sample(); + save(&path, &stored).unwrap(); + + assert!(load(&path, &stored.server, &stored.host_id, Some("bob")).is_none()); + assert!(load(&path, &stored.server, &stored.host_id, Some("alice")).is_some()); + } + #[test] fn rejects_version_mismatch() { let dir = tempfile::tempdir().unwrap(); @@ -157,6 +171,6 @@ mod tests { let mut s = sample(); s.version = STORE_VERSION + 1; save(&path, &s).unwrap(); - assert!(load(&path, "vpn.example.com", "host-1").is_none()); + assert!(load(&path, "vpn.example.com", "host-1", None).is_none()); } }