Skip to content

fix(auth): stop following planted links and tearing the token file - #104

Merged
AetherAI3 merged 2 commits into
mainfrom
fix/token-store-hardening
Aug 23, 2026
Merged

AetherAI3 merged 2 commits into
mainfrom
fix/token-store-hardening

Conversation

@AetherAI3

Copy link
Copy Markdown
Owner

User problem

FileTokenStore guarded every read and write of ~/.config/aether/.token with
fsConstants.O_NOFOLLOW ?? 0. libuv does not define O_NOFOLLOW on Windows —
fs.constants.O_NOFOLLOW is undefined there (verified on win32 / Node 24.19), so the
guard collapsed to 0 and the flag did nothing. A symlink, junction or reparse point
planted at the token path by another local account was followed on both get() and
set(): the session token was handed over on read, and the next token was captured on
write. A directory junction needs no privilege at all to create on Windows, and lstat
reports one as a symlink — so this was reachable by an unprivileged local account.

Separately, set() was an in-place O_TRUNC-then-write. Between the truncate and the
write the file is empty on disk, so a crash left an empty/partial .token and a
concurrent 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 — the FileTokenStore class and three new module-private helpers only.
Plus its tests in test/auth_store.test.ts.

  • get() / set() lstat the token path before opening and refuse a symlink / reparse
    point on every platform: get() treats it as "no token" (never an error), set()
    fails loudly rather than writing a credential through it. O_NOFOLLOW is kept where the
    platform defines it, as defense-in-depth.
  • set() now writes a sibling temp file (.token.<pid>.<random>.tmp, O_EXCL|O_CREAT,
    0600 at creation), fsyncs it, and renames over the target. rename() also never
    follows a link at the destination, which is a second layer under the lstat guard. The
    temp file is unlinked on any failure, so no partial credential is ever left behind.
  • Windows rename cannot replace a destination another process currently has open and
    fails 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 and unlinks, so it removes a planted link and never the file or
    directory 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 not
    group/world-writable.

Non-goals

  • loginWithPassword, isApiKeyToken, tokenStoreFromEnv, tokenStoreForInjected,
    EnvOverrideTokenStore, StaticTokenStore — untouched.
  • The keychain migration in this file's standing TODO. Still the file store.
  • Windows ACL inspection (see assumptions).
  • No changes to main.ts, cli_registry.ts, login.ts, transport.ts, opener.ts,
    package.json, or any release file.

Security assumptions and residual risk

  • Residual Windows TOCTOU. Between the lstat and the open in get() the entry can
    in principle be swapped. On POSIX the open's O_NOFOLLOW closes that window entirely. On
    Windows there is no such flag, so an attacker who can both win a microsecond-wide race
    and create symlinks (needs SeCreateSymbolicLinkPrivilege or Developer Mode) could
    still 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, so
    even a won race cannot redirect the write.
  • Windows ownership is not checked. process.getuid does not exist on win32 and
    Windows 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.
  • Group/world-readable config dirs (e.g. an older 0755) are still accepted on
    purpose; only writable is refused, since a read bit does not let another account swap
    the token file. Refusing 0755 would break existing installs for no security gain.
  • Behaviour change worth knowing: a config dir that is itself a symlink is now
    refused. A POSIX user who deliberately symlinks ~/.config/aether into a dotfiles repo
    will 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), same
TokenStore interface, same 0700 dir / 0600 file modes, same "empty token reads as
null" behaviour. No migration: an existing .token is read exactly as before. The only
new artifact is a short-lived .tmp sibling during a write.

Rollback

git revert this PR. It is a self-contained change to one class; nothing else depends on
the new helpers.

Evidence

Base SHA: 426b12464c2a19549f421adb43348f83d028628e (origin/main)
Head SHA: 887dde95a092a34ac087b8f8d179ce64125e22f1

Files changed: src/core/auth.ts, test/auth_store.test.ts

Test commands and counts (Windows 11, Node 24.19)

gate result
npm ci 4 packages, 0 vulnerabilities
npm run typecheck clean
npm test 1169 tests, 1166 pass, 0 fail, 3 skipped
npm run smoke 4 pass, 1 skip, 2 fail — both pre-existing and environmental

The two smoke failures are not from this change: local turn needs
ollama pull qwen2.5-coder:7b (model not pulled on this box) and cloud turn is an
expired session token (HTTP 401). Note that smoke's auth step passes — it reads the
real on-disk ~/.config/aether/.token through the new get() path, so the rewritten read
is exercised against a real token file, not only fixtures.

The 3 skips are the POSIX-only assertions (0600/0700 modes, uid ownership,
group/world-writable dir), which run on the ubuntu-latest leg of CI.

Windows rename-over-existing: empirical result

  • renameSync over an existing regular file succeeds and replaces its content
    (MoveFileEx REPLACE_EXISTING). Asserted by a test, not assumed.
  • renameSync over a junction fails EPERM — so a planted link cannot be replaced
    by a write anyway; the lstat guard just makes the refusal explicit and legible.
  • renameSync over a file another process currently has open fails EPERM. This is
    the 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_NOFOLLOW is undefined; symlink(dir, path, "junction") succeeds with
    no privilege while symlink(file, path, "file") fails EPERM; lstat on a junction
    reports isSymbolicLink() === true; unlink on a junction removes the link and leaves
    the target's contents intact.

Mutation evidence

Each guard was reverted individually against the full test file, then restored.

# mutation detected?
M1 remove the lstat guard in get() not detected on win32 — see below
M2 remove the lstat guard in set() detected — set() refuses a linked token path fails (EPERM after the retry budget instead of the explicit refusal)
M3 revert the temp+rename write to in-place O_TRUNC detected — a concurrent writer never lets a reader observe an empty or partial token fails within 300ms with a run of <null> reads
M4 remove the config-dir link check detected — set() refuses a config dir that is itself a link fails: the token is written through the junction into the attacker-controlled directory and login still reports success

M1 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 readSync fails EISDIR, which
get()'s catch turns into null — the same answer, so the test cannot tell the two apart.
On Linux the O_NOFOLLOW that does exist there yields ELOOP, also null. The mutation
would only be observable via a Windows file symlink, which cannot be created on this
box without SeCreateSymbolicLinkPrivilege (EPERM, verified). The get() guard is
therefore 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 readSync catch or Developer Mode is enabled. M4 is the mutation that
proves the Windows attack surface is real and closed.

Not tested

  • Windows file symlinks (privilege unavailable on this box) — junctions cover the
    reachable case.
  • POSIX uid-mismatch (st.uid !== getuid()) — needs a second account; the sibling
    group/world-writable and symlinked-dir assertions cover the same code path's other exits.
  • Real crash-during-write (power loss). The concurrency test covers the observable
    consequence; fsync before rename covers the durability half by construction.

Dependency

None — independent leaf. It must be rebased if PR #103 (fix/windows-url-opener) or the
ship 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)

AetherAI3 and others added 2 commits August 22, 2026 19:47
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]>
@AetherAI3
AetherAI3 merged commit 4ed7b98 into main Aug 23, 2026
5 checks passed
@AetherAI3
AetherAI3 deleted the fix/token-store-hardening branch August 23, 2026 01:11
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