Repository navigation
fix(server): fail-closed auth gate + per-instance token (closes #132) - #137
Merged
Merged
Conversation
The project API server auth check was fail-open: `if (config.authToken)` skipped auth entirely when authToken was empty — the default, and what start.sh passes when FUSION_API_KEY is unset. The project API (project context, sessions, memory, chat stream) was reachable unauthenticated. Make the gate fail-closed: - Empty authToken now generates a per-instance random token (32 random bytes, hex) persisted to ~/.fusion-code/server.token (mode 0600) so a trusted local client (Fusion Studio) can discover it. The server stays authenticated; an unknown client cannot connect. - Auth is only disabled via explicit opt-out: --no-auth flag or FUSION_CODE_NO_AUTH=1 (dev only, logged as a loud warning). - Explicit --auth / FUSION_CODE_AUTH_TOKEN / FUSION_API_KEY passes through verbatim (precedence: FUSION_CODE_AUTH_TOKEN > FUSION_API_KEY). - Both the HTTP fetch gate and the /ws/chat WebSocket upgrade gate use the effective token; neither skips when authToken is empty. - start.sh forwards FUSION_CODE_AUTH_TOKEN and passes --no-auth when FUSION_CODE_NO_AUTH is set. ServerConfig gains optional authDisabled / effectiveAuthToken / tokenFile fields; resolveEffectiveAuthToken() is exported for testability. Tests (8): explicit pass-through, empty-generates-token, hex format, no-auth opt-out, config.authDisabled flag, opt-out precedence, persisted token file (0600), per-instance distinctness. Co-Authored-By: Claude Fable 5 <[email protected]>
This was referenced Aug 26, 2026
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
Closes #132. The project API server auth gate was fail-open:
if (config.authToken)skipped auth entirely whenauthTokenwas empty — the default, and whatstart.shpasses whenFUSION_API_KEYis unset. The project API (project context, sessions, memory,/ws/chatstream) was reachable unauthenticated.Root cause
src/server/projectApiServer.ts:config.authTokendefaulted to""(falsy), so the whole block was skipped. The same pattern existed in the/ws/chatWebSocket upgrade handler.start.shsetAUTH="${FUSION_API_KEY:-}"→ empty when no API key →--auth=""→ auth disabled.Fix — fail-closed
Empty
authTokenno longer disables auth. NewresolveEffectiveAuthToken(config)resolves the effective token:--auth/FUSION_CODE_AUTH_TOKEN/FUSION_API_KEY→ used verbatim (precedence:FUSION_CODE_AUTH_TOKEN>FUSION_API_KEY).--no-auth/FUSION_CODE_NO_AUTH=1→ auth explicitly disabled (dev only, loudlogForDebuggingwarning). Only opt-out path.~/.fusion-code/server.token(mode0600) so a trusted local client (Fusion Studio) can discover it. Server stays authenticated; an unknown client cannot connect.Both the HTTP fetch gate and the
/ws/chatWebSocket upgrade gate use the effective token and are fail-closed.ServerConfiggains optionalauthDisabled/effectiveAuthToken/tokenFilefields.resolveEffectiveAuthTokenis exported for testability.Files
src/server/projectApiServer.ts—resolveEffectiveAuthToken()helper; both gates rewritten fail-closed; wired intostartProjectApiServer.src/server/types.ts—ServerConfigaddsauthDisabled/effectiveAuthToken/tokenFile.src/entrypoints/cli.tsx—--servepath honorsFUSION_CODE_AUTH_TOKEN,--no-auth,FUSION_CODE_NO_AUTH.start.sh— forwardsFUSION_CODE_AUTH_TOKEN; passes--no-authwhenFUSION_CODE_NO_AUTHset.src/__tests__/server/auth.test.ts— 8 new tests.Tests
bun test src/__tests__/server/auth.test.ts— 8 pass:authTokenpasses through verbatimauthTokengenerates a token (fail-closed, NOT null)FUSION_CODE_NO_AUTH=1disables auth (explicit opt-out)config.authDisabledflag disables auth even with empty envserver.token(0600)Gates
bun run typecheck— 0 errorsbun test src/__tests__/— 444 pass, 0 failbun run build:dev— pass.github/workflows/ci.yml) excludessrc/server/+src/__tests__/server/(pre-existing dirs); edits match each file's existing style.Backward compatibility
FUSION_API_KEYstill works (unchanged precedence, now second toFUSION_CODE_AUTH_TOKEN).--auth=...still works verbatim.~/.fusion-code/server.token. Clients that relied on the unauthenticated server must either setFUSION_CODE_NO_AUTH=1(dev) or read the token file.Co-Authored-By: Claude Fable 5 [email protected]