Skip to content

fix(server): fail-closed auth gate + per-instance token (closes #132) - #137

Merged
dahai80 merged 1 commit into
mainfrom
fix/auth-fail-closed-132
Aug 25, 2026
Merged

dahai80 merged 1 commit into
mainfrom
fix/auth-fail-closed-132

Conversation

@dahai80

@dahai80 dahai80 commented Aug 25, 2026

Copy link
Copy Markdown
Owner

What

Closes #132. The project API server auth gate 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, /ws/chat stream) was reachable unauthenticated.

Root cause

src/server/projectApiServer.ts:

// Auth check — FAIL-OPEN
if (config.authToken) {
    const auth = req.headers.get("Authorization");
    if (auth !== `Bearer ${config.authToken}`) {
        return errorResponse("Unauthorized", 401);
    }
}

config.authToken defaulted to "" (falsy), so the whole block was skipped. The same pattern existed in the /ws/chat WebSocket upgrade handler. start.sh set AUTH="${FUSION_API_KEY:-}" → empty when no API key → --auth="" → auth disabled.

Fix — fail-closed

Empty authToken no longer disables auth. New resolveEffectiveAuthToken(config) resolves the effective token:

  1. Explicit --auth / FUSION_CODE_AUTH_TOKEN / FUSION_API_KEY → used verbatim (precedence: FUSION_CODE_AUTH_TOKEN > FUSION_API_KEY).
  2. --no-auth / FUSION_CODE_NO_AUTH=1 → auth explicitly disabled (dev only, loud logForDebugging warning). Only opt-out path.
  3. Otherwise → generate a per-instance random token (32 random bytes, hex) and persist it to ~/.fusion-code/server.token (mode 0600) 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/chat WebSocket upgrade gate use the effective token and are fail-closed. ServerConfig gains optional authDisabled / effectiveAuthToken / tokenFile fields. resolveEffectiveAuthToken is exported for testability.

Files

  • src/server/projectApiServer.ts — resolveEffectiveAuthToken() helper; both gates rewritten fail-closed; wired into startProjectApiServer.
  • src/server/types.ts — ServerConfig adds authDisabled / effectiveAuthToken / tokenFile.
  • src/entrypoints/cli.tsx — --serve path honors FUSION_CODE_AUTH_TOKEN, --no-auth, FUSION_CODE_NO_AUTH.
  • start.sh — forwards FUSION_CODE_AUTH_TOKEN; passes --no-auth when FUSION_CODE_NO_AUTH set.
  • src/__tests__/server/auth.test.ts — 8 new tests.

Tests

bun test src/__tests__/server/auth.test.ts — 8 pass:

  • explicit authToken passes through verbatim
  • empty authToken generates a token (fail-closed, NOT null)
  • generated token is 32 random bytes hex (64 chars)
  • FUSION_CODE_NO_AUTH=1 disables auth (explicit opt-out)
  • config.authDisabled flag disables auth even with empty env
  • opt-out precedence pinned
  • generated token persists to server.token (0600)
  • two empty-config calls generate distinct per-instance tokens

Gates

  • bun run typecheck — 0 errors
  • bun test src/__tests__/ — 444 pass, 0 fail
  • bun run build:dev — pass
  • CI lint scope (.github/workflows/ci.yml) excludes src/server/ + src/__tests__/server/ (pre-existing dirs); edits match each file's existing style.

Backward compatibility

  • FUSION_API_KEY still works (unchanged precedence, now second to FUSION_CODE_AUTH_TOKEN).
  • Explicit --auth=... still works verbatim.
  • The only behavior change is the previously-unauthenticated case (empty token): it now requires a generated token discoverable at ~/.fusion-code/server.token. Clients that relied on the unauthenticated server must either set FUSION_CODE_NO_AUTH=1 (dev) or read the token file.

Co-Authored-By: Claude Fable 5 [email protected]

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]>
@dahai80
dahai80 merged commit 0c1f6ee into main Aug 25, 2026
1 check passed
@dahai80
dahai80 deleted the fix/auth-fail-closed-132 branch August 25, 2026 10:01
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.

安全: projectApiServer (11441) 默认 authToken="" 导致鉴权完全跳过 — fusion-studio 发送固定 Bearer fg-admin-key 被忽略

1 participant