fix(security): eliminate TOCTOU race condition in atomic writes#500
fix(security): eliminate TOCTOU race condition in atomic writes#500dumko2001 wants to merge 1 commit intogoogleworkspace:mainfrom
Conversation
- Use tempfile::NamedTempFile for synchronous atomic_write to ensure 0600 permissions at creation. - Use tokio::fs::OpenOptions with mode(0o600) for asynchronous atomic_write_async. - Remove redundant set_permissions calls in oauth_config.rs and credential_store.rs. - Ensure tempfile is a regular dependency (was dev-dependency). - Add tests to verify file permissions on Unix systems. Fixes googleworkspace#401
🦋 Changeset detectedLatest commit: b32659b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the security posture of file persistence operations by addressing a critical TOCTOU race condition. By ensuring that sensitive files, such as OAuth secrets and encrypted credentials, are created with strict permissions from the outset, it mitigates the risk of local attackers exploiting a brief window of vulnerability. The changes streamline file writing logic by centralizing secure temporary file handling, improving both robustness and maintainability. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively eliminates a critical TOCTOU race condition in atomic_write and atomic_write_async. The synchronous implementation correctly leverages tempfile::NamedTempFile to ensure atomic creation with secure permissions. The asynchronous version is also correctly implemented to generate a random temporary file, create it with O_EXCL and secure permissions, and then atomically rename it. The related changes, such as removing redundant permission-setting calls and updating dependencies, are appropriate. The addition of tests to verify file permissions is a valuable improvement. The changes are well-executed and significantly improve the security of file writing operations. I have not found any issues of high or critical severity.
Description
This PR fixes a critical TOCTOU (Time-of-Check to Time-of-Use) race condition in
fs_util::atomic_writeandfs_util::atomic_write_asyncwhere temporary files were initially created with default system permissions before being tightened to0600. This could allow a local attacker to read sensitive OAuth secrets or encrypted credentials during the brief window before permissions were applied.Fixes #401
Changes:
tempfile::NamedTempFilefor synchronousatomic_write, ensuring0600permissions andO_EXCLflags are set atomically at creation time.atomic_write_asyncto usetokio::fs::OpenOptionswith.mode(0o600)for secure asynchronous writes.set_permissionscalls inoauth_config.rsandcredential_store.rsthat are now handled natively by the atomic write utilities.tempfileto a regular dependency (was previously only indev-dependencies).fs_util.rsto verify that files are created with0600permissions on Unix.Dry Run Output:
N/A (This change affects internal file persistence logic, not Discovery-based API requests).
Checklist:
AGENTS.mdguidelines.cargo fmt --allto format the code perfectly.cargo clippy -- -D warningsand resolved all warnings..changeset/fix-security-toctou.md).