fix(auth): propagate error when token directory cannot be created (#542)
* fix(auth): propagate errors when token directory creation/permissions fail Previously, failures to create the token directory or set its permissions were silently ignored using 'let _ = ...'. This could lead to confusing errors later or security issues if permissions were left insecure. Now properly propagates errors from: - tokio::fs::create_dir_all() - std::fs::set_permissions() (on Unix) Also sanitizes the path in error messages to prevent terminal escape sequence injection, aligned with codebase security practices. * fix(auth): use spawn_blocking for set_permissions to avoid blocking async runtime Following the reviewer suggestion, std::fs::set_permissions is now executed via tokio::task::spawn_blocking to avoid potentially blocking the async runtime thread, which can cause performance issues or deadlocks under load. * fix(auth): use tokio::fs::set_permissions instead of spawn_blocking Simplifies the code by using Tokio's native async set_permissions, removing the need for manual thread spawning.
This commit is contained in:
committed by
GitHub
parent
e9970db26f
commit
b4d5e26424
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"gws": patch
|
||||
---
|
||||
|
||||
Fix auth error propagation: properly propagate errors when token directory creation or permission setting fails, instead of silently ignoring them
|
||||
+16
-2
@@ -82,11 +82,25 @@ 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 '{}': {}",
|
||||
sanitize_for_terminal(&parent.display().to_string()),
|
||||
e
|
||||
)
|
||||
})?;
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let _ = std::fs::set_permissions(parent, std::fs::Permissions::from_mode(0o700));
|
||||
tokio::fs::set_permissions(parent, std::fs::Permissions::from_mode(0o700))
|
||||
.await
|
||||
.map_err(|e| {
|
||||
anyhow::anyhow!(
|
||||
"Failed to set permissions on token directory '{}': {}",
|
||||
sanitize_for_terminal(&parent.display().to_string()),
|
||||
e
|
||||
)
|
||||
})?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user