Skip to content

fix: fail closed when API key region detection is inconclusive#194

Merged
NianJiuZst merged 3 commits into
mainfrom
codex/region-detection-fail-closed
Jul 14, 2026
Merged

fix: fail closed when API key region detection is inconclusive#194
NianJiuZst merged 3 commits into
mainfrom
codex/region-detection-fail-closed

Conversation

@NianJiuZst

@NianJiuZst NianJiuZst commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • fail closed when neither API region can be confirmed
  • classify probes as authorized, unauthorized, unreachable, or inconclusive
  • never return or persist a guessed global fallback
  • honor an explicit --region global|cn during API-key login
  • reject saving only when the selected endpoint explicitly returns 401/403
  • warn and save the user's explicit region when validation is unavailable

Root cause

Region detection collapsed invalid credentials, blocked probes, and transient network failures into a successful global result. Callers then persisted that unverified result, overwriting a valid China-region configuration and routing later requests to the wrong API host.

The explicit-region recovery path still treated every validation exception as an authentication failure, so a blocked quota endpoint produced the same result as a rejected key.

Impact

Automatic detection remains fail-closed and leaves existing credentials unchanged when no region can be confirmed. Explicitly selecting a region now treats the user's choice as authoritative: confirmed 401/403 responses still reject the key, while network failures produce a warning and save the selected region so restricted-network users have a real recovery path.

Test reliability

Config command tests now use scoped spies or dry-run behavior instead of process-wide module mocks, preventing them from replacing the config loader used by login tests.

Fixes #191

Validation

  • bun test — 381 passed
  • targeted region and login tests — 15 passed
  • bun run typecheck
  • bun run build:dev
  • ESLint on all changed TypeScript files
  • git diff --check

@NianJiuZst NianJiuZst marked this pull request as ready for review July 14, 2026 10:51
@NianJiuZst NianJiuZst merged commit 06eec7a into main Jul 14, 2026
5 of 6 checks passed
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.

[Bug] mmx auth login 区域检测失败时静默回退到 global,导致国内用户 API 全部 401

1 participant