Repository navigation
fix(auth): keep the sign-in in four corner cases the credential lock still missed - #44
Merged
Merged
Conversation
…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.
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.
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.
credentials.jsonopen, the write's renameover 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.
next command's refresh is refused and the sign-in is deleted.
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.
refused the grant. The command still fails with
auth.expired. Both commands that resolve a sign-in pass arefresh, so no command's behaviour changes; this closes the path for any other caller.
processes in different sandboxes (each hiding
/proc) wrote no namespace in their locks. So they trusted eachother'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.
and why it is accepted.
CONTRIBUTING.md("Credentials on disk") describes the retried read and rename and the namespacerule.
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:store.readafter about 5 s;getrefused 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 aWindows 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 testgives 1108 passed, 2 skipped on Linux. On Windows 11 it gives 1105passed, 5 skipped (the POSIX permission tests and the Linux-only filesystem tests). On Linux, it also passes with
/proc/self/ns/pidunreadable.A real held file, on Windows 11 with Defender on, as a standard user. A separate PowerShell process held
credentials.jsonopen with[System.IO.File]::Open, while the built store wrote a new record:NoneEBUSY)ReadWriteEPERM)None, then 10 sReadWriteNo temp, lock or guard file was left behind in any case. An elevated session reads through a share-
Nonehold,which is why the read's part of the failure doesn't show up when testing as an administrator.