fix: prevent SDK "Not logged in" error before reaching gateway#127
fix: prevent SDK "Not logged in" error before reaching gateway#127
Conversation
The Claude Agent SDK's CLI subprocess runs a local auth-source check at startup and immediately emits a result message with is_error: true and result 'Not logged in · Please run /login' — never reaching the gateway — when none of ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN, CLAUDE_CODE_OAUTH_TOKEN, apiKeyHelper, or a '/login'-managed keychain entry is present in its environment. All four proxy / gateway paths in agent-interface.ts were explicitly deleting ANTHROPIC_AUTH_TOKEN on the assumption that the credential proxy handling Authorization upstream was sufficient. For any user without Claude Code set up locally, that assumption left the SDK with source 'none' and the installer failed with 'Not logged in · Please run /login' before a single byte hit the WorkOS gateway. Seed ANTHROPIC_AUTH_TOKEN with a placeholder on the proxy paths so the SDK's local check passes (the credential proxy rewrites Authorization with the real WorkOS token before forwarding), and strip the user's personal ANTHROPIC_API_KEY from sdkEnv so it cannot leak upstream when the proxy is in use. Direct mode is unchanged. Fixes #124
Original prompt from nick.nisi@workos.com
|
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
/coderabbit review |
Greptile SummaryThis PR fixes the Claude Agent SDK emitting "Not logged in" before reaching the WorkOS gateway by seeding a Confidence Score: 5/5Safe to merge — the prior P1 credential-leak gap on the legacy fallback path is fixed and all paths are now tested. All previously identified P1 findings have been addressed. The only remaining finding is a P2 style suggestion about exporting the placeholder constant. No security regressions introduced. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[initializeAgent] --> B{direct?}
B -- Yes --> C[User key preserved\nNo base URL override\nTalks to upstream directly]
B -- No --> D{Unclaimed environment?}
D -- Yes --> E[Claim-token proxy started\nSDK pointed at proxy\nUser key deleted\nPlaceholder bearer set]
D -- No --> F{Normal gateway mode?}
F -- No --> G{Skip-auth flag?}
G -- Yes --> H[Pointed at gateway\nUser key deleted\nPlaceholder bearer set]
G -- No --> I[Local dev mode\nPointed at gateway\nUser key deleted\nPlaceholder bearer set]
F -- Yes --> J{Has credentials?}
J -- No --> K[Throw: Not authenticated]
J -- Yes --> L{Refresh token and proxy enabled?}
L -- Yes --> M[Credential proxy started\nSDK pointed at proxy\nUser key deleted\nPlaceholder bearer set]
L -- No --> N[Legacy fallback\nToken refreshed\nSDK pointed at gateway\nUser key deleted\nReal access token set]
Reviews (3): Last reviewed commit: "test(installer): use typed AgentConfig h..." | Re-trigger Greptile |
Addresses Greptile P1: the legacy fallback path (no refresh token or INSTALLER_DISABLE_PROXY=1) still leaked the user's personal ANTHROPIC_API_KEY to the WorkOS gateway as an x-api-key header alongside the WorkOS access token. Every other non-direct path already deletes it; this brings the legacy branch in line. Also clarifies the skip-auth/local log messages to reflect that a placeholder bearer is now forwarded to the gateway (the SDK's local auth-source check would otherwise fail with 'Not logged in'). Co-Authored-By: nick.nisi@workos.com <nick.nisi@workos.com>
📝 WalkthroughWalkthroughThe changes update authentication handling in the agent interface to prevent user-provided Anthropic API keys from being forwarded to the WorkOS gateway, instead using a placeholder token. The implementation covers multiple auth modes including credential proxy, claim-token proxy, legacy token-forwarding, and local auth scenarios, with corresponding test coverage. Changes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
|
Addressed the legacy-fallback P1 in 553ef3e: For the Full test suite (1573) + typecheck + oxlint clean. |
Spec files are excluded from pnpm typecheck, which masked a latent type mismatch in makeAgentConfigForInit (returned installDir instead of the required workOSApiKey / workOSApiHost fields). Ann otate the helper return type and align fields with AgentConfig so it would catch regressions under a stricter typecheck config. Co-Authored-By: nick.nisi@workos.com <nick.nisi@workos.com>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/lib/agent-interface.spec.ts`:
- Around line 409-414: The helper makeAgentConfigForInit currently returns the
wrong shape; update it to return a real AgentConfig object matching
initializeAgent's expectations by including workOSApiKey and workOSApiHost (as
test strings) along with workingDirectory and installDir so the returned value
is type-compatible with initializeAgent; locate makeAgentConfigForInit in the
spec and replace the returned object with an AgentConfig-shaped object
containing workOSApiKey, workOSApiHost, workingDirectory, and installDir.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 7d4c3314-02d2-4ea4-80c1-d8dc2b2d9747
📒 Files selected for processing (2)
src/lib/agent-interface.spec.tssrc/lib/agent-interface.ts
Summary
Fixes #124.
The Claude Agent SDK's bundled CLI runs a local auth-source check at startup. If none of
ANTHROPIC_API_KEY,ANTHROPIC_AUTH_TOKEN,CLAUDE_CODE_OAUTH_TOKEN,apiKeyHelper, or a/login-managed keychain entry is present in its environment, it immediately emits aresultmessage withis_error: trueandresult: "Not logged in · Please run /login"— never contacting the gateway at all (duration_api_ms: 0).All four proxy / gateway paths in
agent-interface.tsexplicitlydelete sdkEnv.ANTHROPIC_AUTH_TOKENon the assumption that the credential / claim-token proxy handlesAuthorizationupstream. For any user who doesn't happen to have Claude Code configured locally, that assumption leaves the SDK withapiKeySource: "none"and the installer fails withNot logged in · Please run /loginbefore a single byte hits the WorkOS gateway.This PR seeds a placeholder bearer token (
workos-cli-proxy-placeholder) on the proxy / gateway paths so the SDK's local check passes. The credential proxy still rewrites theAuthorizationheader with the real WorkOS token before forwarding upstream, so the placeholder never reaches the gateway. The user's personalANTHROPIC_API_KEYis also stripped fromsdkEnvon these paths so it can't leak into WorkOS-gateway requests.--directmode is unchanged.Reproduced directly against the bundled SDK CLI with no auth env:
With
ANTHROPIC_AUTH_TOKENset (to anything), the SDK proceeds past the local check and actually talks to the configured base URL — which in production is the credential proxy.Review & Testing Checklist for Human
ANTHROPIC_API_KEY,ANTHROPIC_AUTH_TOKEN, or~/.claudeconfig, using a normal logged-in WorkOS session — confirm the agent actually starts and the install proceeds (previously failed immediately with "Not logged in · Please run /login").ANTHROPIC_API_KEYalready present in the shell env — confirm it's NOT forwarded to the WorkOS gateway (the credential proxy should see onlyAuthorization: Bearer <workos-token>, nox-api-key).workos install --directstill works and the user'sANTHROPIC_API_KEYis preserved on that path.Notes
agent-interface.tschanges; the proxy itself is unchanged (it already overwritesAuthorizationon the credential-proxy path).delete sdkEnv.ANTHROPIC_AUTH_TOKENin--directmode — there we talk toapi.anthropic.comdirectly and the user's realANTHROPIC_API_KEYbecomes the valid auth source, so no local-check failure.Link to Devin session: https://app.devin.ai/sessions/e17e4acdc96e43c19c7dbde249b826ba
Summary by CodeRabbit
Release Notes