fix(engine): refuse restore_to_epoch onto an existing path - #363
Open
teipsum wants to merge 1 commit into
Open
Conversation
do_restore_to_epoch copied the full backup to output_path with an unconditional std::fs::copy and further down deleted any existing <output_path>.wal sidecar directory. Pointing a restore at a live database therefore clobbered the .grafeo file and silently destroyed its sidecar WAL -- the very data an operator may be trying to recover. Add a refuse-if-exists guard at the top of do_restore_to_epoch that returns Error::InvalidValue when output_path or its sidecar already exists, before any filesystem mutation. Restores must target a fresh path. Covered by two unit tests asserting the refusal and that the pre-existing artifacts are left untouched.
Contributor
There was a problem hiding this comment.
2 issues found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="crates/grafeo-engine/src/database/backup.rs">
<violation number="1" location="crates/grafeo-engine/src/database/backup.rs:517">
P2: Sidecar path construction uses `display()` string formatting instead of path-native `OsString`/`PathBuf` operations, which can miss existing sidecar directories for non-UTF8 paths and bypass the data-loss guard.</violation>
<violation number="2" location="crates/grafeo-engine/src/database/backup.rs:518">
P2: Refuse-if-exists guard is non-atomic (TOCTOU): a concurrent process can create the output file or sidecar after the `exists()` checks but before `std::fs::copy` or `remove_dir_all`+`rename`, allowing silent clobbering.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| // be trying to recover. Restores must target a fresh path; callers that | ||
| // intend to replace a database should move or remove it first. | ||
| let sidecar_dir = format!("{}.wal", output_path.display()); | ||
| if output_path.exists() || Path::new(&sidecar_dir).exists() { |
Contributor
There was a problem hiding this comment.
P2: Refuse-if-exists guard is non-atomic (TOCTOU): a concurrent process can create the output file or sidecar after the exists() checks but before std::fs::copy or remove_dir_all+rename, allowing silent clobbering.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At crates/grafeo-engine/src/database/backup.rs, line 518:
<comment>Refuse-if-exists guard is non-atomic (TOCTOU): a concurrent process can create the output file or sidecar after the `exists()` checks but before `std::fs::copy` or `remove_dir_all`+`rename`, allowing silent clobbering.</comment>
<file context>
@@ -507,6 +507,22 @@ pub(super) fn do_restore_to_epoch(
+ // be trying to recover. Restores must target a fresh path; callers that
+ // intend to replace a database should move or remove it first.
+ let sidecar_dir = format!("{}.wal", output_path.display());
+ if output_path.exists() || Path::new(&sidecar_dir).exists() {
+ return Err(Error::InvalidValue(format!(
+ "restore output path already exists: {} (restore must target a fresh path; \
</file context>
| // and silently destroy its sidecar WAL -- exactly the data an operator may | ||
| // be trying to recover. Restores must target a fresh path; callers that | ||
| // intend to replace a database should move or remove it first. | ||
| let sidecar_dir = format!("{}.wal", output_path.display()); |
Contributor
There was a problem hiding this comment.
P2: Sidecar path construction uses display() string formatting instead of path-native OsString/PathBuf operations, which can miss existing sidecar directories for non-UTF8 paths and bypass the data-loss guard.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At crates/grafeo-engine/src/database/backup.rs, line 517:
<comment>Sidecar path construction uses `display()` string formatting instead of path-native `OsString`/`PathBuf` operations, which can miss existing sidecar directories for non-UTF8 paths and bypass the data-loss guard.</comment>
<file context>
@@ -507,6 +507,22 @@ pub(super) fn do_restore_to_epoch(
+ // and silently destroy its sidecar WAL -- exactly the data an operator may
+ // be trying to recover. Restores must target a fresh path; callers that
+ // intend to replace a database should move or remove it first.
+ let sidecar_dir = format!("{}.wal", output_path.display());
+ if output_path.exists() || Path::new(&sidecar_dir).exists() {
+ return Err(Error::InvalidValue(format!(
</file context>
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
do_restore_to_epochcopied the full backup tooutput_pathwith an unconditionalstd::fs::copy, and further down deleted any existing<output_path>.walsidecar directory. Pointing a restore at a live database therefore clobbered the.grafeofile and silently destroyed its sidecar WAL — the very data an operator may be trying to recover.Fix
Add a refuse-if-exists guard at the top of
do_restore_to_epochthat returnsError::InvalidValuewhenoutput_pathor its sidecar already exists, before any filesystem mutation. Restores must target a fresh path.Tests
Two new unit tests assert the refusal and that the pre-existing artifacts are left untouched. Full
grafeo-enginebackup suite is green:Single commit, based on
main@4ebae02.Summary by cubic
Prevents accidental data loss by refusing to restore onto an existing database path or its
.walsidecar ingrafeo-engine. Restores must now target a fresh path.Bug Fixes
do_restore_to_epochthat returnsError::InvalidValueifoutput_pathor<output_path>.walexists.Migration
.grafeofile and<path>.wal/directory, then run restore.Written for commit 69ef608. Summary will update on new commits.