Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changeset/fix-issue-493-token-dir-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---\n"@googleworkspace/cli": patch\n---\n\nfix(auth): propagate error when token directory cannot be created
8 changes: 7 additions & 1 deletion src/token_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ impl EncryptedTokenStorage {
let encrypted = crate::credential_store::encrypt(json.as_bytes())?;

if let Some(parent) = self.file_path.parent() {
let _ = tokio::fs::create_dir_all(parent).await;
tokio::fs::create_dir_all(parent).await.map_err(|e| {
anyhow::anyhow!(
"Failed to create token directory '{}': {}",
parent.display(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The path parent.display() is included in the error message without sanitization. If a path contains terminal escape sequences, this could lead to escape sequence injection when the error is printed to a terminal. It's safer to sanitize external inputs like file paths before including them in messages that will be displayed.

Suggested change
parent.display(),
sanitize_for_terminal(&parent.display().to_string()),
References
  1. Sanitize error strings printed to the terminal to prevent escape sequence injection.

e
)
})?;
Comment on lines +85 to +91
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While this change correctly propagates errors from create_dir_all, a similar issue exists a few lines below that should also be addressed for robust error handling.

The result of std::fs::set_permissions on line 95 is currently ignored. If setting the directory permissions fails, it could leave the token storage directory with incorrect, possibly insecure, permissions. This error should also be propagated to make the error handling in this block complete.

std::fs::set_permissions(parent, std::fs::Permissions::from_mode(0o700))
    .map_err(|e| anyhow::anyhow!("Failed to set permissions on token directory '{}': {}", parent.display(), e))?;

#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
Expand Down
Loading