Compare commits

..
Author SHA1 Message Date
Kevin Yue 88e0b5b9e4 fix(gpclient): honor the requested username when reusing cookies 2026-11-30 13:58:56 +08:00
3 changed files with 65 additions and 8 deletions

No files matched your search

+43
View File
@@ -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] #[tokio::test]
async fn unreachable_gateway_keeps_cached_cookie() { async fn unreachable_gateway_keeps_cached_cookie() {
// Nothing listens here: the cached gateway login gets no answer at all. // Nothing listens here: the cached gateway login gets no answer at all.
+2 -2
View File
@@ -83,7 +83,7 @@ impl ConnectHandler<'_> {
pub(super) async fn try_cached_cookie(&self, server: &str) -> Option<anyhow::Result<()>> { pub(super) async fn try_cached_cookie(&self, server: &str) -> Option<anyhow::Result<()>> {
let path = cookie_cache_path(self.args)?; let path = cookie_cache_path(self.args)?;
let host_id = self.os_profile.borrow().host_identity().host_id().to_string(); 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() { if !stored.auth_cookie.can_authenticate_gateway() {
cookie_store::clear(&path); cookie_store::clear(&path);
return None; return None;
@@ -145,7 +145,7 @@ impl ConnectHandler<'_> {
} }
let path = cookie_cache_path(self.args)?; let path = cookie_cache_path(self.args)?;
let host_id = self.os_profile.borrow().host_identity().host_id().to_string(); 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() { if !stored.auth_cookie.can_authenticate_gateway() {
cookie_store::clear(&path); cookie_store::clear(&path);
return None; return None;
+20 -6
View File
@@ -59,12 +59,15 @@ pub fn cookie_path(custom: Option<&str>) -> Option<PathBuf> {
} }
} }
pub fn load(path: &Path, server: &str, host_id: &str) -> Option<StoredCookie> { pub fn load(path: &Path, server: &str, host_id: &str, requested_user: Option<&str>) -> Option<StoredCookie> {
let bytes = zeroize::Zeroizing::new(platform::decode(fs::read(path).ok()?).ok()?); let bytes = zeroize::Zeroizing::new(platform::decode(fs::read(path).ok()?).ok()?);
let stored: StoredCookie = serde_json::from_slice(&bytes).ok()?; let stored: StoredCookie = serde_json::from_slice(&bytes).ok()?;
if stored.version != STORE_VERSION || stored.server != server || stored.host_id != host_id { if stored.version != STORE_VERSION || stored.server != server || stored.host_id != host_id {
return None; return None;
} }
if requested_user.is_some_and(|user| user != stored.auth_cookie.username()) {
return None;
}
Some(stored) Some(stored)
} }
@@ -116,7 +119,7 @@ mod tests {
.windows(b"prelogon-auth".len()) .windows(b"prelogon-auth".len())
.any(|part| part == b"prelogon-auth") .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] #[test]
@@ -132,24 +135,35 @@ mod tests {
assert_eq!(fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600); 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.username, "alice");
assert_eq!(loaded.host_id, "host-1"); assert_eq!(loaded.host_id, "host-1");
assert_eq!(loaded.last_gateway, "gw1.example.com"); assert_eq!(loaded.last_gateway, "gw1.example.com");
assert_eq!(loaded.auth_cookie.user_auth_cookie(), "user-auth"); assert_eq!(loaded.auth_cookie.user_auth_cookie(), "user-auth");
assert!( assert!(
load(&path, "other.example.com", "host-1").is_none(), load(&path, "other.example.com", "host-1", None).is_none(),
"must reject mismatched server" "must reject mismatched server"
); );
assert!( 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" "must reject mismatched host id"
); );
clear(&path); clear(&path);
assert!(!path.exists()); 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] #[test]
fn rejects_version_mismatch() { fn rejects_version_mismatch() {
let dir = tempfile::tempdir().unwrap(); let dir = tempfile::tempdir().unwrap();
@@ -157,6 +171,6 @@ mod tests {
let mut s = sample(); let mut s = sample();
s.version = STORE_VERSION + 1; s.version = STORE_VERSION + 1;
save(&path, &s).unwrap(); 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());
} }
} }