Skip to content

BE-930: Add Workspace::snapshot - #10002

Open
thehabbos007 wants to merge 1 commit into
asa/hash-integrations/04-engine-workspacefrom
asa/hash-integrations/04a-engine-snapshots
Open

thehabbos007 wants to merge 1 commit into
asa/hash-integrations/04-engine-workspacefrom
asa/hash-integrations/04a-engine-snapshots

Conversation

@thehabbos007

Copy link
Copy Markdown
Contributor

🌟 Purpose

This PR adds Workspace::snapshot(target), which writes a copy of the database that DuckDB can open on its own.

🔍 Changes

  • snapshot runs CHECKPOINT, copies the database file to a new <target>.partial, syncs it and renames it to target. target only ever holds a complete copy.
  • An existing <target>.partial is deleted first, because copying onto a link to the database file would truncate the database.
  • A target that resolves to a workspace file, which is the database, its log, the temporary directory or the lock file, is refused. A failed copy deletes <target>.partial.

@thehabbos007 thehabbos007 self-assigned this Oct 12, 2026
@github-actions github-actions Bot added area/libs Relates to first-party libraries/crates/packages (area) type/eng > backend Owned by the @backend team area/tests New or updated tests area/apps > hash-integrations labels Oct 12, 2026
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
File::open(directory)?.sync_all()

@semgrep-code-hashintel semgrep-code-hashintel Bot Oct 12, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High severity issue identified in your code:

The application builds a file path from potentially untrusted data, which can lead to a path traversal vulnerability. An attacker can manipulate the path which the application uses to access files. If the application does not validate user input and sanitize file paths, sensitive files such as configuration or user data can be accessed, potentially creating or overwriting files. To prevent this vulnerability, validate and sanitize any input that is used to create references to file paths. Also, enforce strict file access controls. For example, choose privileges allowing public-facing applications to access only the required files.

🧹 Fixed in commit 4341978 🧹

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/fp Workspace::snapshot is a Rust filesystem API whose caller selects a trusted destination. Writing to a caller-chosen path outside the database directory is supported behavior. This library has no HTTP handler or untrusted request-to-path dataflow. Restricting the destination would change the API contract.

@thehabbos007
thehabbos007 added this pull request to stack #9985 October 12, 2026 14:49
@vercel

vercel Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
hash Ready Ready Preview Oct 19, 2026 12:50pm UTC
petrinaut Ready Ready Preview Oct 19, 2026 12:50pm UTC
petrinaut-docs Ready Ready Preview Oct 19, 2026 12:50pm UTC
1 Skipped Deployment
Project Deployment Actions Updated
hashdotdesign-tokens Ignored Ignored Preview Oct 19, 2026 12:50pm UTC

Request Review

@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.33333% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.29%. Comparing base (ae6e70b) to head (a561bc2).

Files with missing lines Patch % Lines
...al/integrations/engine/src/workspace/connection.rs 75.00% 7 Missing and 4 partials ⚠️
.../@local/integrations/engine/src/workspace/files.rs 88.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@                            Coverage Diff                             @@
##           asa/hash-integrations/04-engine-workspace   #10002   +/-   ##
==========================================================================
  Coverage                                      68.29%   68.29%           
==========================================================================
  Files                                           2003     2003           
  Lines                                         205743   205815   +72     
  Branches                                        8734     8739    +5     
==========================================================================
+ Hits                                          140513   140568   +55     
- Misses                                         63554    63565   +11     
- Partials                                        1676     1682    +6     
Flag Coverage Δ
hash-integrations-engine 89.54% <81.33%> (-1.82%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@thehabbos007
thehabbos007 force-pushed the asa/hash-integrations/04a-engine-snapshots branch from 4341978 to 231b26a Compare October 13, 2026 07:45
@thehabbos007
thehabbos007 force-pushed the asa/hash-integrations/04a-engine-snapshots branch from 231b26a to 760587e Compare October 13, 2026 09:05
@thehabbos007
thehabbos007 force-pushed the asa/hash-integrations/04a-engine-snapshots branch from 760587e to 13a1b5f Compare October 13, 2026 10:15
@thehabbos007
thehabbos007 force-pushed the asa/hash-integrations/04a-engine-snapshots branch from 13a1b5f to 5d48946 Compare October 13, 2026 19:53
@cursor

cursor Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
New filesystem and checkpoint path touches the live workspace database; safeguards (partial rename, workspace-file refusal, link handling) limit corruption risk but callers must use safe target paths.

Overview
Adds Workspace::snapshot(target) so integrations can export a standalone DuckDB file that reflects state before any later workspace requests.

The workspace thread CHECKPOINTs, then copies the live database via a <target>.partial file (remove any existing partial first, sync, rename) so targets only see complete files and hard links to the DB cannot be truncated. SnapshotError covers refused targets (resolved paths matching the DB, WAL, temp dir, or lock), checkpoint/write failures, and thread stop; failed writes clean up the partial file. WorkspaceFiles now tracks those paths and drives the safety checks.

Integration tests cover snapshot timing vs later create_table, blocked workspace paths, partial cleanup on failure, and replacing a hard link at the partial path.

Reviewed by Cursor Bugbot for commit a561bc2. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

[&self.database, &self.log, &self.temp, &self.lock]
.into_iter()
.any(|file| Self::resolve(file).is_some_and(|file| file == path))
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Snapshot can overwrite live database

High Severity

WorkspaceFiles::contains only canonicalizes parent directories and then compares the original filename, so it does not treat a file symlink and its target as the same path. A workspace opened through a symlink can therefore accept a snapshot whose target or target.partial is the real database file. write_copy then unlinks that live file with rename (or remove_file on the partial path) while DuckDB still holds the original inode, which can drop later writes and leave WAL and data files out of sync.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit ae04656. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a561bc2. Configure here.

.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
File::open(directory)?.sync_all()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Snapshot replace leaves DuckDB sidecars

Medium Severity

snapshot replaces only the main file at target. A leftover <target>.wal (or .tmp) from a previous database at that path is left in place. DuckDB then replays that log against the new copy and can refuse to open it with a WAL mismatch, so an in-place snapshot is not always a database DuckDB can open on its own.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a561bc2. Configure here.

This branch had an error being deployed

1 failed (outdated) and 3 active deployments
Preview – petrinaut-docs — a561bc2c Deployed Oct 19, 2026 by vercel[bot]
Preview – hash — a561bc2c Deployed Oct 19, 2026 by vercel[bot]
Preview – petrinaut — a561bc2c Deployed Oct 19, 2026 by vercel[bot]
Preview – hashdotdesign-tokens — 4341978a Deployed Oct 12, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/apps > hash-integrations area/libs Relates to first-party libraries/crates/packages (area) area/tests New or updated tests type/eng > backend Owned by the @backend team

Development

Successfully merging this pull request may close these issues.

2 participants