Repository navigation
fix(auth): stop following planted links and tearing the token file - #104
Merged
Merged
Conversation
FileTokenStore guarded its reads and writes with `O_NOFOLLOW ?? 0`. libuv does not define O_NOFOLLOW on Windows, so `fsConstants.O_NOFOLLOW` is `undefined` there and the guard collapsed to 0: a symlink or junction planted at ~/.config/aether/.token by another local account was followed on BOTH get() and set(), handing over the session token or capturing the next one. Verified on win32/Node 24, where a directory junction needs no privilege to create and lstat reports it as a symlink. - get()/set() lstat the token path first and refuse a symlink/reparse point on every platform (get treats it as "no token"; set fails loudly rather than writing a credential through it). O_NOFOLLOW stays where it exists. - set() writes a 0600 O_EXCL sibling temp file, fsyncs, and renames over the target instead of the old O_TRUNC-then-write. In place, a crash between truncate and write left an empty .token and a concurrent reader saw a truncated credential. rename() also never follows a link at the destination. - Windows rename cannot replace a destination another process has open and fails EPERM; a bounded ~350ms retry covers it, and gives up loudly. - clear() lstats and unlinks, so it removes a planted link and never the file or directory it points at, and a missing token is not an error. - set() validates the config dir: real directory, not a link, owned by us and not group/world-writable (uid/mode POSIX-only; Windows ACLs are not exposed by Node). Co-Authored-By: Claude Fable 5 <[email protected]>
The symlinked-config-dir test skipped on win32, which is where it matters most: a directory junction needs no privilege there, so redirecting the config dir is the most reachable form of this attack — and the mutation run confirms that without assertSafeConfigDir's link check, set() writes the token straight through the junction into an attacker-controlled directory and still reports success. Junction on win32, symlink on POSIX; both assert the same property. Co-Authored-By: Claude Fable 5 <[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.
User problem
FileTokenStoreguarded every read and write of~/.config/aether/.tokenwithfsConstants.O_NOFOLLOW ?? 0. libuv does not defineO_NOFOLLOWon Windows —fs.constants.O_NOFOLLOWisundefinedthere (verified on win32 / Node 24.19), so theguard collapsed to
0and the flag did nothing. A symlink, junction or reparse pointplanted at the token path by another local account was followed on both
get()andset(): the session token was handed over on read, and the next token was captured onwrite. A directory junction needs no privilege at all to create on Windows, and
lstatreports one as a symlink — so this was reachable by an unprivileged local account.
Separately,
set()was an in-placeO_TRUNC-then-write. Between the truncate and thewrite the file is empty on disk, so a crash left an empty/partial
.tokenand aconcurrent reader in another process saw a truncated credential and reported "not logged
in". This is reproduced empirically below, not argued from theory.
Scope
src/core/auth.ts— theFileTokenStoreclass and three new module-private helpers only.Plus its tests in
test/auth_store.test.ts.get()/set()lstatthe token path before opening and refuse a symlink / reparsepoint on every platform:
get()treats it as "no token" (never an error),set()fails loudly rather than writing a credential through it.
O_NOFOLLOWis kept where theplatform defines it, as defense-in-depth.
set()now writes a sibling temp file (.token.<pid>.<random>.tmp,O_EXCL|O_CREAT,0600at creation),fsyncs it, andrenames over the target.rename()also neverfollows a link at the destination, which is a second layer under the
lstatguard. Thetemp file is unlinked on any failure, so no partial credential is ever left behind.
renamecannot replace a destination another process currently has open andfails
EPERM. A bounded (~350ms) win32-only retry covers it and then gives up loudly —a login that cannot store its token must not report success. This was found by the new
concurrency test failing, not predicted.
clear()lstats andunlinks, so it removes a planted link and never the file ordirectory it points at, never recurses into a junction, and a missing token is not an
error.
set()validates the config dir: a real directory, not a link, owned by us and notgroup/world-writable.
Non-goals
loginWithPassword,isApiKeyToken,tokenStoreFromEnv,tokenStoreForInjected,EnvOverrideTokenStore,StaticTokenStore— untouched.main.ts,cli_registry.ts,login.ts,transport.ts,opener.ts,package.json, or any release file.Security assumptions and residual risk
lstatand theopeninget()the entry canin principle be swapped. On POSIX the open's
O_NOFOLLOWcloses that window entirely. OnWindows there is no such flag, so an attacker who can both win a microsecond-wide race
and create symlinks (needs
SeCreateSymbolicLinkPrivilegeor Developer Mode) couldstill get through. Closing it needs a native handle API Node does not expose. The write
path does not share this residual:
rename()never follows a destination link, soeven a won race cannot redirect the write.
process.getuiddoes not exist on win32 andWindows models this with ACLs, which Node does not expose. The uid/mode half of the
config-dir check is POSIX-only and deliberately skipped rather than faked. The
link half of that check does run on Windows, and the test below proves it.
0755) are still accepted onpurpose; only writable is refused, since a read bit does not let another account swap
the token file. Refusing
0755would break existing installs for no security gain.refused. A POSIX user who deliberately symlinks
~/.config/aetherinto a dotfiles repowill now get a clear error on login instead of a silent write. This is intentional, and
the error names the path and the reason.
Compatibility
Same file path (
<configDir>/.token), same format (raw token text), sameTokenStoreinterface, same0700dir /0600file modes, same "empty token reads asnull" behaviour. No migration: an existing
.tokenis read exactly as before. The onlynew artifact is a short-lived
.tmpsibling during a write.Rollback
git revertthis PR. It is a self-contained change to one class; nothing else depends onthe new helpers.
Evidence
Base SHA:
426b12464c2a19549f421adb43348f83d028628e(origin/main)Head SHA:
887dde95a092a34ac087b8f8d179ce64125e22f1Files changed:
src/core/auth.ts,test/auth_store.test.tsTest commands and counts (Windows 11, Node 24.19)
npm cinpm run typechecknpm testnpm run smokeThe two smoke failures are not from this change:
local turnneedsollama pull qwen2.5-coder:7b(model not pulled on this box) andcloud turnis anexpired session token (HTTP 401). Note that smoke's
authstep passes — it reads thereal on-disk
~/.config/aether/.tokenthrough the newget()path, so the rewritten readis exercised against a real token file, not only fixtures.
The 3 skips are the POSIX-only assertions (
0600/0700modes, uid ownership,group/world-writable dir), which run on the
ubuntu-latestleg of CI.Windows
rename-over-existing: empirical resultrenameSyncover an existing regular file succeeds and replaces its content(MoveFileEx
REPLACE_EXISTING). Asserted by a test, not assumed.renameSyncover a junction failsEPERM— so a planted link cannot be replacedby a write anyway; the
lstatguard just makes the refusal explicit and legible.renameSyncover a file another process currently has open failsEPERM. This isthe finding that produced the bounded retry: the 200-iteration concurrency test failed
on the first run with
EPERM: operation not permitted, rename '...\.token.<pid>.<rand>.tmp' -> '...\.token'.fs.constants.O_NOFOLLOWisundefined;symlink(dir, path, "junction")succeeds withno privilege while
symlink(file, path, "file")failsEPERM;lstaton a junctionreports
isSymbolicLink() === true;unlinkon a junction removes the link and leavesthe target's contents intact.
Mutation evidence
Each guard was reverted individually against the full test file, then restored.
lstatguard inget()lstatguard inset()set() refuses a linked token pathfails (EPERMafter the retry budget instead of the explicit refusal)O_TRUNCa concurrent writer never lets a reader observe an empty or partial tokenfails within 300ms with a run of<null>readsset() refuses a config dir that is itself a linkfails: the token is written through the junction into the attacker-controlled directory and login still reports successM1 is reported honestly as not detected on this box. With the guard removed on Windows,
a junction at the token path opens as a directory and
readSyncfailsEISDIR, whichget()'s catch turns intonull— the same answer, so the test cannot tell the two apart.On Linux the
O_NOFOLLOWthat does exist there yieldsELOOP, alsonull. The mutationwould only be observable via a Windows file symlink, which cannot be created on this
box without
SeCreateSymbolicLinkPrivilege(EPERM, verified). Theget()guard istherefore correct and defensible but, on the platforms testable here, redundant with the
existing failure modes — it is kept because it is the only thing that holds if a future
refactor removes the
readSynccatch or Developer Mode is enabled. M4 is the mutation thatproves the Windows attack surface is real and closed.
Not tested
reachable case.
st.uid !== getuid()) — needs a second account; the siblinggroup/world-writable and symlinked-dir assertions cover the same code path's other exits.
consequence;
fsyncbefore rename covers the durability half by construction.Dependency
None — independent leaf. It must be rebased if PR #103 (
fix/windows-url-opener) or theship stack lands first, but neither touches
src/core/auth.ts, so no conflict is expected.Author model: claude-opus (requested gpt56_terra — ROUTING_DRIFT: hosted fleet unreachable, aek_ 401)