-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(security): create atomic-write temp files with restrictive permissions #531
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 all commits
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@googleworkspace/cli": patch | ||
| --- | ||
|
|
||
| Create atomic-write temp files with restrictive permissions before rename. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| //! File-system utilities. | ||
|
|
||
| use std::io; | ||
| #[cfg(unix)] | ||
| use std::os::unix::fs::OpenOptionsExt; | ||
| use std::path::Path; | ||
|
|
||
| /// Write `data` to `path` atomically. | ||
|
|
@@ -40,7 +42,22 @@ pub fn atomic_write(path: &Path, data: &[u8]) -> io::Result<()> { | |
| .map(|p| p.join(&tmp_name)) | ||
| .unwrap_or_else(|| std::path::PathBuf::from(&tmp_name)); | ||
|
|
||
| std::fs::write(&tmp_path, data)?; | ||
| { | ||
| use std::fs::OpenOptions; | ||
| use std::io::Write; | ||
|
|
||
| let mut opts = OpenOptions::new(); | ||
| opts.write(true).create(true).truncate(true); | ||
| #[cfg(unix)] | ||
| { | ||
| opts.mode(0o600); | ||
| } | ||
|
|
||
| let mut file = opts.open(&tmp_path)?; | ||
| file.write_all(data)?; | ||
| file.sync_all()?; | ||
| } | ||
|
|
||
| std::fs::rename(&tmp_path, path)?; | ||
| Ok(()) | ||
| } | ||
|
|
@@ -56,7 +73,22 @@ pub async fn atomic_write_async(path: &Path, data: &[u8]) -> io::Result<()> { | |
| .map(|p| p.join(&tmp_name)) | ||
| .unwrap_or_else(|| std::path::PathBuf::from(&tmp_name)); | ||
|
|
||
| tokio::fs::write(&tmp_path, data).await?; | ||
| { | ||
| use tokio::fs::OpenOptions; | ||
| use tokio::io::AsyncWriteExt; | ||
|
|
||
| let mut opts = OpenOptions::new(); | ||
| opts.write(true).create(true).truncate(true); | ||
| #[cfg(unix)] | ||
| { | ||
| opts.mode(0o600); | ||
| } | ||
|
Comment on lines
+82
to
+85
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. Similar to the synchronous version, this fix only applies restrictive permissions on Unix systems. The vulnerability of a readable temporary file remains on Windows. To address this cross-platform, you could consider using a crate like |
||
|
|
||
| let mut file = opts.open(&tmp_path).await?; | ||
| file.write_all(data).await?; | ||
| file.sync_all().await?; | ||
| } | ||
|
|
||
| tokio::fs::rename(&tmp_path, path).await?; | ||
| Ok(()) | ||
| } | ||
|
|
@@ -99,4 +131,24 @@ mod tests { | |
| atomic_write_async(&path, b"async hello").await.unwrap(); | ||
| assert_eq!(fs::read(&path).unwrap(), b"async hello"); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_atomic_write_permissions() { | ||
| #[cfg(unix)] | ||
| { | ||
| use std::os::unix::fs::PermissionsExt; | ||
|
|
||
| let dir = tempfile::tempdir().unwrap(); | ||
|
|
||
| let sync_path = dir.path().join("sync.txt"); | ||
| atomic_write(&sync_path, b"sync").unwrap(); | ||
| let sync_mode = fs::metadata(&sync_path).unwrap().permissions().mode() & 0o777; | ||
| assert_eq!(sync_mode, 0o600); | ||
|
|
||
| let async_path = dir.path().join("async.txt"); | ||
| atomic_write_async(&async_path, b"async").await.unwrap(); | ||
| let async_mode = fs::metadata(&async_path).unwrap().permissions().mode() & 0o777; | ||
| assert_eq!(async_mode, 0o600); | ||
| } | ||
| } | ||
| } | ||
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.
This correctly applies restrictive permissions on Unix. However, this security fix is platform-specific and does not apply to Windows. On Windows, the temporary file for sensitive data will still be created with default permissions, which could allow other users on the system to read it. This leaves the security vulnerability open on Windows systems.
For a robust, cross-platform solution, I recommend using the
tempfilecrate (which is already a dev-dependency). It handles secure temporary file creation on major platforms by default, which would fully resolve this security issue.