Skip to content

fix(data): harden Windows WAL/SHM sidecars before SQLite writes them (#1559) - #1611

Merged
ffroliva merged 6 commits into
developfrom
bugfix/1559-wal-shm-takeover
Nov 30, 2026
Merged

ffroliva merged 6 commits into
developfrom
bugfix/1559-wal-shm-takeover

Conversation

@ffroliva

@ffroliva ffroliva commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Takes over #1566 by @deepslit, whose commit is kept with its authorship.

Problem

On Windows, SQLite creates gflow.db-wal and gflow.db-shm during connect and the first writes. Until the second hardening pass in DataStore.open, those files carry the parent directory's inherited ACL. With GFLOW_CLI_DB_PATH in a shared directory, another account can open a handle in that window, and Windows keeps honouring a handle opened before the ACL is tightened (#1559).

Fix

  • Before sqlite3.connect, absent -wal and -shm files beside the database are created empty with an exclusive create (no dangling-symlink follow) and hardened owner-only; SQLite reuses existing sidecar files (measured: same file identity through connect, journal_mode = WAL and the first write, fresh and reopened).
  • The pre-create and harden step runs again after PRAGMA journal_mode = WAL and before migrations, so a sidecar removed by another connection's close is recreated restricted before the first write. The pass after migrations is kept.
  • A failed open no longer deletes an "empty" sidecar: a size check cannot be atomic with the delete, and the file may belong to another writer. A leftover empty file is owner-only and reused.
  • POSIX behaviour is unchanged.

Tests

  • test_open_hardens_windows_sidecars_before_sqlite_connects (red without the fix: gflow.db-wal missing).
  • test_open_restricts_sidecars_a_concurrent_close_removed_before_first_write[fresh|reopen] (red before the second pass: file missing / inherited Everyone:(I)(R)).
  • test_failed_open_never_deletes_a_sidecar.
  • tests/data + tests/auth/test_windows_acl.py: 237 passed, 13 skipped (Windows). Twelve local gates green.

No Flow surface: local SQLite files only; the Windows ACL tests are the evidence.

Closes #1559

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Security
    • New Windows database files and SQLite WAL/SHM sidecars are restricted to the current user before SQLite writes to them. Existing files are not overwritten, and sidecars are retained if opening the database fails.
    • Protection is reapplied when sidecars may have been recreated. Files created by SQLite during a brief timing window may temporarily inherit directory permissions; another account’s already-open handle can remain usable after permissions change.
  • Documentation
    • Updated security guidance to explain these protections, limitations, and the recommendation to keep database files in a private directory.

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Explain Change →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 11 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0410d9f5-c9e8-49a5-aa67-4c0716ffa6cd
Commits

Reviewing files that changed from the base of the PR and between e4c5ca8 and cbef85a.

Files selected for processing: 10

  • CHANGELOG.md
  • docs/SECURITY.md
  • docs/superpowers/plans/2026-11-25-db-owner-only/SCENARIO.md
  • sonar-project.properties
  • src/gflow_cli/_win_owner_only.py
  • src/gflow_cli/data/store.py
  • src/gflow_cli/winsec.py
  • tests/auth/test_windows_acl.py
  • tests/data/test_store.py
  • website/docs/SECURITY.md

No actionable comments were generated in the recent review.

Walkthrough

Windows store handling now pre-creates absent database and WAL/SHM files with owner-only ACLs before SQLite connects. It restricts sidecars again after WAL initialization and before migrations. Tests and documentation cover creation, recreation, failure cases, and remaining exposure windows.

Changes: Windows Sidecar ACLs

Layer / File(s) Summary
Owner-only file creation
src/gflow_cli/winsec.py
A Windows-only function creates an empty file with a protected DACL for the current user. It rejects existing paths and reports other creation failures as OSError.
Store opening and sidecar restriction
src/gflow_cli/data/store.py, tests/data/test_store.py, CHANGELOG.md, docs/SECURITY.md, docs/superpowers/plans/2026-11-25-db-owner-only/SCENARIO.md, website/docs/SECURITY.md
Store opening pre-creates absent database and sidecar files, hardens candidate paths, and repeats sidecar restriction after WAL initialization. Tests cover ACLs, races, symlinks, and failure cases. Documentation describes the flow and remaining exposure cases.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant DataStore
  participant create_owner_only_file
  participant SQLite
  DataStore->>create_owner_only_file: Create absent database and sidecar files with owner-only ACLs
  DataStore->>SQLite: Connect and initialize WAL mode
  DataStore->>create_owner_only_file: Recreate absent sidecars before migrations
  DataStore->>SQLite: Apply migrations
Loading

Possibly related PRs

  • ffroliva/gflow-cli#1566: This PR addresses the same sidecar pre-creation behavior and issue.
  • ffroliva/gflow-cli#1543: This PR introduced post-migration sidecar restriction, which this change supplements with pre-connect handling.
  • ffroliva/gflow-cli#1493: This PR hardens the database file through the same store-opening and Windows security flow.

Suggested reviewers: deepslit

Merge Risk: Minimal · previous review e4c5c

This change creates Windows SQLite database and WAL/SHM files with owner-only permissions before SQLite writes them, which narrows the window in which another account could open them through inherited permissions. No concrete defect was identified, and POSIX behavior is unchanged. It looks ready to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: hardening Windows WAL/SHM sidecar files before SQLite writes them. The changeset pre-creates owner-only sidecars before connect and repeats hardening before migrations…
Linked Issues check Passed [#1559] The only directly linked issue is open and active, so its coding requirements apply. The requirement is to create absent -wal and -shm files with O_EXCL before connect, harden them, an…
Out of Scope Changes check Passed All listed changes relate to the Windows inherited-ACL window that #1559 describes. winsec.py (create_owner_only_file) and the store.py pre-create and second-pass hardening implement the sidecar…
Full details: Title check

Explanation

The title names the main change: hardening Windows WAL/SHM sidecar files before SQLite writes them. The changeset pre-creates owner-only sidecars before connect and repeats hardening before migrations, which matches the title. The title includes an issue reference (#1559), which is acceptable but adds no meaning. The title is 76 characters, within a typical length limit.

Full details: Linked Issues check

Explanation

[#1559] The only directly linked issue is open and active, so its coding requirements apply. The requirement is to create absent -wal and -shm files with O_EXCL before connect, harden them, and add a test that checks restriction before the first write. The change set covers each part. src/gflow_cli/winsec.py adds create_owner_only_file(path), which fails when the path exists (including symlinks) and sets a protected DACL for the current user. src/gflow_cli/data/store.py calls this before sqlite3.connect, skips existing paths, and hardens the sidecars again after PRAGMA journal_mode = WAL and before migrations. tests/data/test_store.py adds test_open_hardens_windows_sidecars_before_sqlite_connects, the concurrent-close tests for fresh and reopened databases, and test_failed_open_never_deletes_a_sidecar. The issue's exposure scope (shared GFLOW_CLI_DB_PATH directories) matches the code path. This assessment relies on the change summary and the PR description. I did not run the Windows tests, and the reported results are not independently confirmed.

Full details: Out of Scope Changes check

Explanation

All listed changes relate to the Windows inherited-ACL window that #1559 describes. winsec.py (create_owner_only_file) and the store.py pre-create and second-pass hardening implement the sidecar requirement. The store.py change that creates the main database owner-only on Windows (with logged fallback to the existing 0600 path) addresses the same window for gflow.db, so it supports the issue goal even though #1559 names only sidecars. The Windows API type declarations support winsec.py. The dangling-symlink refusal and its test cover the new pre-create path. The changes to CHANGELOG.md, docs/SECURITY.md, website/docs/SECURITY.md, and the superpowers scenario document this behavior. No unrelated functional change appears in the summary. POSIX behavior is reported as unchanged. The database-file creation change is the broadest item. It is in scope because it applies the same rationale, but a reviewer should confirm it in the diff.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 376b31a3-4ba2-499c-be8a-0827085235e9
Commits

Reviewing files that changed from the base of the PR and between dfd0bdb and e4c5ca8.

Files selected for processing: 7

  • CHANGELOG.md
  • docs/SECURITY.md
  • docs/superpowers/plans/2026-11-25-db-owner-only/SCENARIO.md
  • src/gflow_cli/data/store.py
  • src/gflow_cli/winsec.py
  • tests/data/test_store.py
  • website/docs/SECURITY.md

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 0 remain after this review.

deepslit and others added 6 commits November 30, 2026 00:08
…ect (#1559)

Signed-off-by: deepslit <731369471@qq.com>
…ver delete them (#1559)

Run the pre-create and harden pass for -wal/-shm a second time after
PRAGMA journal_mode = WAL and before apply_migrations, so sidecars that
another connection's last close removed after the first pass are recreated
empty and owner-only before SQLite writes to them. On a fresh database the
first migration write is what creates the sidecars, so the pass pre-creates
them rather than only hardening existing files.

Drop the empty-sidecar cleanup on a failed open: a -wal can hold another
connection's committed frames, and a size check cannot be made atomic with
the unlink. An empty owner-only sidecar is reused by SQLite on the next open.
…ation (#1559)

Creating an empty file and restricting it with icacls afterwards left a
window in which the file carried its directory's inherited ACL; a handle
another account opened then keeps working after the ACL changes and reads
what SQLite later writes. winsec.create_owner_only_file creates the file
with CreateFileW, CREATE_NEW and a protected DACL granting only the current
user, the same ACL harden_files_once applies, without following a symlink.
A new gflow.db and the pre-created -wal/-shm use it; harden_files_once
stays as the second line. A failed sidecar create now logs
data_store_sidecar_create_failed instead of file_acl_failed.
…back window (#1559)

Declare argtypes and restype for CloseHandle and
ConvertStringSecurityDescriptorToSecurityDescriptorW so a handle value
above 2**31 is passed as a HANDLE instead of raising ctypes.ArgumentError.
Add a test that create_owner_only_file refuses a dangling symlink at the
target name without creating the link target, and state in SECURITY.md
that the plain-create fallback for a new gflow.db reopens the
inherited-ACL window.
Move the ctypes CreateFileW body into gflow_cli._win_owner_only, which
winsec.create_owner_only_file delegates to after resolving the user SID,
and exclude that native glue from the coverage metric: the Linux runner
that measures coverage cannot reach it, while the Windows CI job runs the
real-ACL tests against it.

Cover the store's Windows branches with tests that pretend to be Windows
and stub the owner-only create and the ACL pass: the call order around
connect and the WAL pragma, the plain-create fallback and its log event,
the existing-file refusal, and a sidecar lost to a concurrent create.
Cover the winsec delegate the same way.
@ffroliva
ffroliva force-pushed the bugfix/1559-wal-shm-takeover branch from e4c5ca8 to cbef85a Compare November 30, 2026 00:09
@ffroliva

Copy link
Copy Markdown
Owner Author

Evidence. No Flow surface (local SQLite files). Windows ACL tests: tests/data + tests/auth/test_windows_acl.py 247 passed locally. The two dangling-symlink tests skip on the development machine (no symlink privilege) but ran on CI's ProfileLease (windows-latest) job: that job collected 51 database tests and skipped 12, which are exactly the 12 POSIX-mode tests; locally the same file skips those 12 plus the 2 symlink tests.

Reviews. /code-review xhigh asked for the sidecars to be owner-only at creation (a handle opened between create and icacls would survive the ACL change); fixed with CreateFileW + a protected DACL (_win_owner_only.py), then a focused security review of that delta (MERGE; handle argtypes and the database-fallback limit folded in). ponytail-review xhigh MERGE. SonarCloud: the native glue module is excluded from coverage like the other unreachable-on-Linux transports; the Windows branches of store.py are covered by platform-faked tests that run on Linux. Gate green.

CodeRabbit: rate limited; the two earlier CodeRabbit findings on #1566 are addressed (second hardening pass; no deletion of sidecars).

@ffroliva
ffroliva merged commit c473f70 into develop Nov 30, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

data: harden Windows WAL/SHM sidecars before SQLite first writes them

2 participants