fix(gateway): persist pairing tokens with atomic replace - #71
Draft
cursor[bot] wants to merge 1 commit into
Draft
Conversation
auth_pair truncated auth_tokens.json in place and ignored write errors, so ENOSPC, a crash after O_TRUNC, or fdopen failure could wipe every device token and still return success with a token that never landed on disk. Write to a sibling temp file, fsync, then rename. Keep the pairing code until the store is committed so a failed pair can be retried. Co-authored-by: esadrianno <[email protected]>
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.
Bug and impact
auth_pairopenedauth_tokens.jsonwithO_TRUNCand ignoredfprintf/fcloseerrors. A disk-full write, crash after truncate, orfdopenfailure afteropen()emptied (or partially wrote) the token store and still returned success with a bearer token that never landed on disk.Concrete trigger: pairing is in progress, tokens already exist on disk (second device in the append window, or a prior successful pair), then the write hits
ENOSPC/EFBIG. Existing devices stop authenticating. If the leftover file is non-empty invalid JSON,auth_get_or_create_pairing_codewill not issue a new code, so the gateway stays locked until the file is deleted by hand.Root cause
In-place truncate-then-write with unchecked stdio, plus clearing the pairing code after a write that may have failed.
Fix
Write JSON to
auth_tokens.json.tmp,fsync, thenrenameover the live file (same pattern as dashboard config save). Checkgenerate_random_hex. Keep the pending pairing code until the store commit succeeds so a failed pair can be retried.Validation
CI=true make test_auth(includes-Werror). New regressiontest_auth_pair_write_failure_preserves_existing_tokensusesRLIMIT_FSIZEto fail the write, asserts the existing token still validates, then retries successfully after the limit is restored.