Conversation
* fix: prevent sync from overwriting existing non-symlink files * test: add regression tests for sync overwrite protection
Summary of ChangesHello @laggu, 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 addresses a potential data loss scenario in the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. 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
|
|
✅ Automated Review: Checks Passed
Ready for merge. |
There was a problem hiding this comment.
Code Review
This pull request aims to introduce a crucial safety check to prevent git volume sync from overwriting existing files or directories that are not symlinks, thereby preventing data loss. However, this implementation introduces a significant Time-of-Check to Time-of-Use (TOCTOU) race condition, which could still lead to data loss in shared environments. Furthermore, it causes a regression that breaks the idempotency of the sync command for directories in ModeCopy, impacting the tool's ability to ensure a consistent state over multiple runs.
| if _, err := os.Lstat(vol.TargetPath); err == nil { | ||
| // Target exists, check if it's a symlink (safe to remove) | ||
| // We allow overwriting symlinks because they are likely created by us (or user wants to replace them) | ||
| // But we DO NOT overwrite regular files or directories to prevent data loss. | ||
| targetInfo, err := os.Lstat(vol.TargetPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to check target %s: %w", vol.Target, err) | ||
| } | ||
|
|
||
| if targetInfo.Mode()&os.ModeSymlink == 0 { | ||
| // It is NOT a symlink (regular file or directory) | ||
| // For safety, we skip this volume and report an error. | ||
| return fmt.Errorf("target %s already exists and is not a symlink (skipping to prevent data loss)", vol.Target) | ||
| } | ||
| } |
There was a problem hiding this comment.
This section introduces a critical Time-of-Check to Time-of-Use (TOCTOU) race condition. The os.Lstat check (lines 101-115) for existing files/directories, followed by os.RemoveAll (line 124), creates a window where an attacker could replace a symlink with a regular directory or file. This could lead to os.RemoveAll recursively deleting attacker-supplied content, potentially causing significant data loss and undermining the intended overwrite protection. Additionally, consider defining a specific error type for this scenario to allow callers to handle it programmatically, rather than relying on string matching of the error message "target %s already exists and is not a symlink (skipping to prevent data loss)".
| if targetInfo.Mode()&os.ModeSymlink == 0 { | ||
| // It is NOT a symlink (regular file or directory) | ||
| // For safety, we skip this volume and report an error. | ||
| return fmt.Errorf("target %s already exists and is not a symlink (skipping to prevent data loss)", vol.Target) |
There was a problem hiding this comment.
Regression: Broken Idempotency for Directory Sync
The check targetInfo.Mode()&os.ModeSymlink == 0 prevents overwriting any existing directory that is not a symlink. However, when git-volume is used in ModeCopy for a directory, it creates a real directory at the target path.
On subsequent runs of the sync command, this check will identify the previously created directory as "not a symlink" and fail with an error. This breaks the idempotency of the sync operation, which is a core design principle of the tool (as noted in the comments on line 117). This can lead to partial sync states where some volumes are updated while others are skipped due to this error, potentially leaving the workspace in an inconsistent or insecure configuration.
Severity: Medium
Vulnerability Type: Logic Error
|
Closing: sync의 무조건 삭제/재생성(Delete-and-Recreate) 동작이 올바른 설계라는 결론.
overwrite protection은 불필요하며, 오히려 idempotency를 깨뜨리는 regression이 발생함 (ModeCopy 디렉토리 재sync 시 에러). |
Summary
Prevents
git volume syncfrom overwriting existing files or directories that are not symlinks.Description
Currently,
synccommand removes the target path blindly before linking/copying. This PR adds a check to ensure we only overwrite if the target does not exist or is a symlink. If a regular file or directory exists at the target path, we skip it and return an error to prevent data loss.Issue
Closes #27
Type of Change
Testing
internal/gitvolume/sync_test.go