GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(securefile): reclaim stale lock files to prevent permanent DOS by euxarist · Pull Request #615 · Twigpine/zero

Conversation

@euxarist

@euxarist euxarist commented Jul 9, 2026 •
edited by coderabbitai Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Summary

Fixes a medium-severity issue where stale .lock files left behind after abrupt process crashes (e.g. SIGKILL or system panic) cause all future attempts to create/acquire secrets to fail with a permanent timeout, resulting in a denial of service (DOS).

This PR brings the stale lock reclamation logic from internal/cron/lock.go and internal/oauth/lock.go over to internal/securefile/securefile.go:

  • Write process-specific owner tokens to the .lock file.
  • Inspect the lock file modification time if O_EXCL acquisition fails.
  • Reclaim lock files older than 10 seconds (secureLockStaleAfter) using the same atomic rename-aside routine (reclaimStaleLock).

Changes

internal/securefile/securefile.go

  • Define secureLockStaleAfter and atomic counter secureLockSeq.
  • Update createSecretFile to write owner tokens, release only owned lock files, and detect and reclaim stale locks.
  • Add reclaimStaleLock helper.

internal/securefile/securefile_test.go

  • Add TestCreateSecretFileReclaimsStaleLock to verify that stale locks are correctly reclaimed.

Test plan

  • go test ./internal/securefile/... — ok

Summary by CodeRabbit

  • Bug Fixes
    • Improved secret file creation to safely detect and reclaim stale lock files.
    • Updated lock handling so cleanup only occurs when the lock content still matches the current operation.
    • Strengthened error handling around lock-file writing and lifecycle to reduce leftover locks.
  • Tests
    • Added a new test to verify stale lock reclamation during secret creation.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@euxarist
fix(securefile): reclaim stale lock files to prevent permanent DOS
5634a97
When createSecretFile fails to acquire the O_EXCL lock because it already exists, check if the file is stale (older than 10 seconds). If so, atomically reclaim it via a rename-aside check, preventing a permanent DOS after abrupt process crashes. Update the lock file to write and verify process-specific tokens, and add TestCreateSecretFileReclaimsStaleLock to cover the stale reclaim flow.
@coderabbitai

coderabbitai Bot commented Jul 9, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 80e83cbb-8bbb-4472-a796-d9899a096200

📥 Commits

Reviewing files that changed from the base of the PR and between 5634a97 and 3bbdf84.

📒 Files selected for processing (1)
  • internal/securefile/securefile.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/securefile/securefile.go

Walkthrough

This PR updates secret lock-file handling in internal/securefile/securefile.go with atomic lock tokens, stale-lock detection, and reclamation after a timeout. It also adds a test covering recovery from a stale .lock file.

Changes

Secure Lock Reclamation

Layer / File(s) Summary
Lock token generation and conditional cleanup
internal/securefile/securefile.go
Adds sync/atomic, secureLockStaleAfter, and secureLockSeq; lock tokens now include PID, time, and an atomic sequence, and cleanup only removes the lock file when its contents still match the token.
Stale lock detection and reclamation
internal/securefile/securefile.go
createSecretFile now checks lock-file mtime against secureLockStaleAfter and calls reclaimStaleLock, which renames the lock, checks elapsed time, and restores or removes it.
Stale lock reclaim test
internal/securefile/securefile_test.go
Adds time and TestCreateSecretFileReclaimsStaleLock, which backdates a lock file, calls createSecretFile, and verifies the lock is reclaimed and removed afterward.

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

Sequence Diagram(s)

sequenceDiagram
  participant createSecretFile
  participant LockFile
  participant reclaimStaleLock

  createSecretFile->>LockFile: inspect existing .lock mtime
  LockFile-->>createSecretFile: stale or active
  alt stale
    createSecretFile->>reclaimStaleLock: reclaim stale lock
    reclaimStaleLock->>LockFile: rename to .stale.<token>
    reclaimStaleLock->>LockFile: compare elapsed time to secureLockStaleAfter
    alt still stale
      reclaimStaleLock->>LockFile: remove stale file
    else not stale
      reclaimStaleLock->>LockFile: restore original name
    end
  else active
    createSecretFile->>createSecretFile: retry acquisition
  end
  createSecretFile->>LockFile: write new token and continue
Loading

Suggested reviewers: Vasanthdev2004, kevincodex1

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: reclaiming stale securefile lock files to avoid a permanent denial of service.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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
  • X
  • Mastodon
  • Reddit
  • LinkedIn

Comment @coderabbitai help to get the list of available commands.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

coderabbitai[bot]
coderabbitai Bot approved these changes Jul 9, 2026
View reviewed changes
@euxarist
style(securefile): gofmt const block alignment
3bbdf84
anandh8x
anandh8x approved these changes Jul 9, 2026
View reviewed changes

@anandh8x anandh8x left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM.

Stale .lock after a hard crash is a real DoS on createSecretFile (AES key mint for encrypted-file creds). Owner token + mtime stale check + rename-aside reclaim matches the proven cron/oauth pattern and avoids blind Remove races. Release only deletes when the token still matches. Test pins reclaim of a backdated lock. All CI green. Ship it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@anandh8x
anandh8x requested review from gnanam1990 and kevincodex1 July 9, 2026 16:43
gnanam1990
gnanam1990 approved these changes Jul 9, 2026
View reviewed changes

@gnanam1990 gnanam1990 left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Review

Verdict: Approve with notes (independent pass; agree with @anandh8x on the securefile fix).

Correct

Stale path.lock after hard crash is a real permanent DOS on secret mint (retry budget ~1s, then fail forever). Owner token + mtime gate + rename-aside reclaim matches the cron/oauth locker pattern and avoids blind Remove races. Release only when the lock still holds our token. Secret publish remains temp+rename. TestCreateSecretFileReclaimsStaleLock pins the happy path. CI green; go test -race ./internal/securefile/ passes. Local merge with main is clean.

Notes

  1. Same DOS still lives in internal/oauth/encrypt.go createSecretFile (no token, no reclaim, blind defer os.Remove). This PR fixes the securefile/credstore path only. Follow-up: port the same pattern or share one helper.
  2. Recovery is after secureLockStaleAfter (10s), not necessarily on the first post-crash call if the lock is still “fresh” within the wait window — fine, just not instantaneous.
  3. Process: no linked issue / issue-approved.

No disagreement with shipping the securefile change.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
kevincodex1
kevincodex1 approved these changes Jul 10, 2026
View reviewed changes
@kevincodex1
kevincodex1 merged commit 8536cc8 into Twigpine:main Jul 10, 2026
7 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot mentioned this pull request Jul 10, 2026
chore(main): release 0.4.0 #625
Merged
@Vasanthdev2004 Vasanthdev2004 mentioned this pull request Jul 11, 2026
fix(windows): retry atomic file renames on Access is denied / Sharing violation #641
Closed
@BrewTestBot BrewTestBot mentioned this pull request Jul 17, 2026
zero 0.4.0 Homebrew/homebrew-core#293679
Merged
@euxarist
euxarist deleted the fix/securefile-stale-lock-dos branch July 17, 2026 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gnanam1990 gnanam1990 gnanam1990 approved these changes
@coderabbitai coderabbitai[bot] coderabbitai[bot] approved these changes
@anandh8x anandh8x anandh8x approved these changes
@kevincodex1 kevincodex1 kevincodex1 approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@euxarist @gnanam1990 @anandh8x @kevincodex1
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.