Skip to content

fix(auth): keep the sign-in in four corner cases the credential lock still missed - #44

Merged
vigubikReply merged 1 commit into
mainfrom
dev/REPLY-55176-refresh-followups
Nov 26, 2026
Merged

vigubikReply merged 1 commit into
mainfrom
dev/REPLY-55176-refresh-followups

Conversation

@vigubikReply

Copy link
Copy Markdown
Contributor

What changed

A follow-up to #43. Four corner cases remained where a stored sign-in could still be lost, or where the code
read as safer than it is.

  • On Windows, a write waits out another program holding the credentials file, for about 5 s.
    • Before: while an antivirus scan, a backup or a sync tool had credentials.json open, the write's rename
      over it was retried for only about 0.6 s. Its read of the file wasn't retried at all. For a user without
      backup privileges (any normal, non-elevated user), a file held with no sharing fails that read at once.
    • Why it mattered: after a refresh, a failed write drops the token the server has just rotated, so the
      next command's refresh is refused and the sign-in is deleted.
    • Now: a write's read and its rename are retried on a busy refusal, from one shared budget per write:
      waits of 20 ms doubling up to 1 s, about 5 s in all. When the budget runs out, the write fails as before
      and leaves no temp file.
    • What stays the same:
      • Plain reads still fail at once: a refresh reads before it sends its request, so nothing is lost there.
      • Reads on other platforms still fail at once: there, these codes are a permission error.
      • The token request (20 s at most) plus the write's budget still fit inside the lock's 30 s stale age.
  • An expired sign-in reached by a caller that can't refresh it is kept. It used to be deleted, though nothing
    refused the grant. The command still fails with auth.expired. Both commands that resolve a sign-in pass a
    refresh, so no command's behaviour changes; this closes the path for any other caller.
  • On Linux, a process that can't read its own pid namespace trusts no other process's pid. Before, two such
    processes in different sandboxes (each hiding /proc) wrote no namespace in their locks. So they trusted each
    other's pids and could take over each other's live lock. Such a process now takes a namespace name no other
    process has, and judges other locks by their age alone. Windows and macOS are unchanged.
  • The lock's header comment says that breaking a stale guard file is not serialised, what that can lead to,
    and why it is accepted.
  • Docs: CONTRIBUTING.md ("Credentials on disk") describes the retried read and rename and the namespace
    rule.

How it was verified

Tests first. Each changed test was run against the code before the change and failed. No test was added;
three existing ones grew:

  • The store's retry test runs on fake timers, so it takes milliseconds. On win32:

    • a read refused for 1 s, then a rename refused until 2 s, ends stored;
    • a read refused for 3 s, then a rename refused for good, fails within about 5 s, so the budget is shared;
    • a read refused for good fails store.read after about 5 s;
    • a plain get refused for good fails at once.

    On linux, a refused read fails at once. In every case, no temp file is left behind.

  • The lock's namespace test loads the store as a Linux process that can't read /proc/self/ns/pid, and as a
    Windows one. It expects the Linux one to wait on a fresh lock left by a gone pid in another unknown namespace,
    and the Windows one to take it over, as before.

  • The resolve test: an expired record with a refresh token, resolved without a refresh, is left in the file
    byte for byte.

The suite: npm run build && npm test gives 1108 passed, 2 skipped on Linux. On Windows 11 it gives 1105
passed, 5 skipped (the POSIX permission tests and the Linux-only filesystem tests). On Linux, it also passes with
/proc/self/ns/pid unreadable.

A real held file, on Windows 11 with Defender on, as a standard user. A separate PowerShell process held
credentials.json open with [System.IO.File]::Open, while the built store wrote a new record:

Hold Before (v0.23.1's read and rename) After
2 s, share None fails at once on the read (EBUSY) stored in 2.3 s; the read waited
2 s, share ReadWrite fails at 0.6 s on the rename (EPERM) stored in 2.3 s; the rename waited
10 s, either share mode fails at 0.6 s or at once fails after about 5.3 s; the earlier record kept
1 s share None, then 10 s ReadWrite n/a fails after 5.4 s in all: the read and the rename drew on one budget

No temp, lock or guard file was left behind in any case. An elevated session reads through a share-None hold,
which is why the read's part of the failure doesn't show up when testing as an administrator.

…still missed

After the change that keeps a sign-in through a failed token refresh, four corner cases remained
where a sign-in could still be lost, or where the code read as safer than it is:

- An expired sign-in reached by a caller that has no way to refresh it was deleted. Nothing
  refused the grant, so it is now kept, and the command still fails with auth.expired.
- While another program has the credentials file open (an antivirus scan or a backup on
  Windows), a write's rename over it was retried for only about 0.6 s, and a write's read of it
  was not retried at all: for a user without backup privileges, a file held with no sharing fails
  that read at once, which after a refresh drops the token the server just rotated. On Windows the
  read is now retried too, and the read and the rename draw on one budget per write: about 5 s in
  all, each step at most 1 s. When it runs out, the write fails as before, and leaves no temp file.
  With the 20 s token request, a refresh still lets go of the lock before the lock counts as stale
  (30 s). Plain reads, and reads on other platforms, still fail at once.
- On Linux, a process that can't read its pid namespace (a sandbox that hides /proc) wrote no
  namespace in its lock, so two such processes in different sandboxes trusted each other's pids
  and could take over each other's live lock. Such a process now takes a namespace name no other
  process has, and judges other processes' locks by their age alone. Windows and macOS are
  unchanged.
- The lock's header comment now says that breaking a stale guard is not serialised, what that
  can lead to, and why it is accepted.

CONTRIBUTING's "Credentials on disk" describes the retried read and rename and the namespace
rule.
@vigubikReply
vigubikReply merged commit c074bef into main Nov 26, 2026
6 checks passed
@vigubikReply
vigubikReply deleted the dev/REPLY-55176-refresh-followups branch November 26, 2026 04:12
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.

1 participant