fix(gpclient): honor the requested username when reusing cookies

This commit is contained in:
Kevin Yue committed 2026-11-30 13:58:56 +08:00
1 parent 89f6364a7b
commit 88e0b5b9e4
3 files changed
+65 -8

No files matched your search

+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 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());
}
}