mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-30 01:36:14 -05:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88e0b5b9e4 |
No files matched your search
@@ -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.
|
||||
|
||||
@@ -83,7 +83,7 @@ impl ConnectHandler<'_> {
|
||||
pub(super) async fn try_cached_cookie(&self, server: &str) -> Option<anyhow::Result<()>> {
|
||||
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;
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user