-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(auth): invalidate token caches after login #796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
7461d5b
05a6ee2
b6c1e85
a94efd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -345,6 +345,25 @@ fn token_cache_path() -> PathBuf { | |
| config_dir().join("token_cache.json") | ||
| } | ||
|
|
||
| fn service_account_token_cache_path() -> PathBuf { | ||
| config_dir().join("sa_token_cache.json") | ||
| } | ||
|
|
||
| fn invalidate_token_caches() -> Result<Vec<String>, GwsError> { | ||
| let mut removed = Vec::new(); | ||
|
|
||
| for path in [token_cache_path(), service_account_token_cache_path()] { | ||
| if path.exists() { | ||
| std::fs::remove_file(&path).map_err(|e| { | ||
| GwsError::Validation(format!("Failed to remove {}: {e}", path.display())) | ||
| })?; | ||
| removed.push(path.display().to_string()); | ||
| } | ||
| } | ||
|
|
||
| Ok(removed) | ||
| } | ||
|
|
||
| /// Which scope set to use for login. | ||
| enum ScopeMode { | ||
| /// Use the default scopes (MINIMAL_SCOPES). | ||
|
|
@@ -644,13 +663,22 @@ async fn handle_login_inner( | |
| let enc_path = credential_store::save_encrypted(&creds_str) | ||
| .map_err(|e| GwsError::Auth(format!("Failed to encrypt credentials: {e}")))?; | ||
|
|
||
| // A successful login may change the active account or granted scopes. | ||
| // Remove cached access tokens so the next API call mints a token from the | ||
| // newly saved refresh token instead of reusing stale credentials. | ||
| let invalidated_token_caches = invalidate_token_caches()?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since let invalidated_token_caches = invalidate_token_caches(); |
||
|
|
||
| // Invalidate cached account timezone (may belong to the previous account). | ||
| crate::timezone::invalidate_cache(); | ||
|
|
||
| let output = json!({ | ||
| "status": "success", | ||
| "message": "Authentication successful. Encrypted credentials saved.", | ||
| "account": actual_email.as_deref().unwrap_or("(unknown)"), | ||
| "credentials_file": enc_path.display().to_string(), | ||
| "encryption": "AES-256-GCM (key in OS keyring or local `.encryption_key`; set GOOGLE_WORKSPACE_CLI_KEYRING_BACKEND=file for headless)", | ||
| "scopes": scopes, | ||
| "invalidated_token_caches": invalidated_token_caches, | ||
| }); | ||
| println!( | ||
| "{}", | ||
|
|
@@ -1457,7 +1485,7 @@ fn handle_logout() -> Result<(), GwsError> { | |
| let plain_path = plain_credentials_path(); | ||
| let enc_path = credential_store::encrypted_credentials_path(); | ||
| let token_cache = token_cache_path(); | ||
| let sa_token_cache = config_dir().join("sa_token_cache.json"); | ||
| let sa_token_cache = service_account_token_cache_path(); | ||
|
|
||
| let mut removed = Vec::new(); | ||
|
|
||
|
|
@@ -1900,6 +1928,30 @@ mod tests { | |
| assert!(path.starts_with(config_dir())); | ||
| } | ||
|
|
||
| #[test] | ||
| #[serial_test::serial] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of References
|
||
| fn invalidate_token_caches_removes_user_and_service_account_caches() { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| unsafe { | ||
| std::env::set_var("GOOGLE_WORKSPACE_CLI_CONFIG_DIR", dir.path()); | ||
| } | ||
|
|
||
| let token_cache = token_cache_path(); | ||
| let sa_token_cache = service_account_token_cache_path(); | ||
| std::fs::write(&token_cache, "{}").unwrap(); | ||
| std::fs::write(&sa_token_cache, "{}").unwrap(); | ||
|
|
||
| let removed = invalidate_token_caches().unwrap(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| assert_eq!(removed.len(), 2); | ||
| assert!(!token_cache.exists()); | ||
| assert!(!sa_token_cache.exists()); | ||
|
|
||
| unsafe { | ||
| std::env::remove_var("GOOGLE_WORKSPACE_CLI_CONFIG_DIR"); | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn handle_auth_command_empty_args_prints_usage() { | ||
| let args: Vec<String> = vec![]; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message should be sanitized before being wrapped in
GwsError::Validationto prevent escape sequence injection in the terminal. Since the path or the error string itself could contain malicious sequences, it is important to ensure they are safe for terminal output. When implementing sanitization, note that Rust'schar::is_control()only covers theCccategory, sois_dangerous_unicodeis necessary to check forCf(Format) characters. Additionally, acknowledge potential TOCTOU race conditions as a known limitation for this file operation.References
char::is_control()only covers theCc(Control) Unicode category, notCf(Format). Therefore,is_dangerous_unicodeis not redundant and is needed to check for dangerous Unicode characters.openat(O_NOFOLLOW)) is considered out of scope.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. I added a shared
remove_file_if_existshelper that sanitizes the formatted path/error before wrapping it inGwsError::Validation. It also removes files directly and treatsNotFoundas a no-op, avoiding the previous existence-check TOCTOU window for these cache cleanup paths.