Skip to content

Commit 553ef3e

Browse files
fix(installer): strip ANTHROPIC_API_KEY on legacy fallback path
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>
1 parent c270be8 commit 553ef3e

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

src/lib/agent-interface.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,23 @@ describe('initializeAgent sdkEnv auth', () => {
471471
expect(result.sdkEnv.ANTHROPIC_API_KEY).toBeUndefined();
472472
});
473473

474+
it('strips ANTHROPIC_API_KEY on legacy fallback path (no refresh token)', async () => {
475+
vi.mocked(hasCredentials).mockReturnValue(true);
476+
vi.mocked(getCredentials).mockReturnValue({
477+
accessToken: 'real-workos-token',
478+
refreshToken: null,
479+
expiresAt: Date.now() + 60_000,
480+
});
481+
482+
const result = await initializeAgent(makeAgentConfigForInit(), makeOptions({ skipAuth: false, local: false }));
483+
484+
// Legacy path sends the real WorkOS access token as the bearer; the
485+
// user's personal Anthropic key must not tag along as an x-api-key
486+
// header to the WorkOS gateway.
487+
expect(result.sdkEnv.ANTHROPIC_AUTH_TOKEN).toBe('real-workos-token');
488+
expect(result.sdkEnv.ANTHROPIC_API_KEY).toBeUndefined();
489+
});
490+
474491
it('preserves ANTHROPIC_API_KEY in direct mode', async () => {
475492
const result = await initializeAgent(
476493
makeAgentConfigForInit(),

src/lib/agent-interface.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,26 +455,31 @@ export async function initializeAgent(config: AgentConfig, options: InstallerOpt
455455
}
456456

457457
sdkEnv.ANTHROPIC_BASE_URL = gatewayUrl;
458+
// Prevent the user's personal Anthropic key (if any) from being
459+
// forwarded to the WorkOS gateway as an x-api-key header alongside
460+
// the WorkOS access token we set below.
461+
delete sdkEnv.ANTHROPIC_API_KEY;
458462
sdkEnv.ANTHROPIC_AUTH_TOKEN = creds.accessToken;
459463
authMode = options.local ? `local-gateway:${gatewayUrl}` : `workos-gateway:${gatewayUrl}`;
460464
logInfo('Sending access token to gateway (legacy mode)');
461465
}
462466
} else if (options.skipAuth) {
463-
// Skip auth mode - direct to gateway without auth. Still seed a
464-
// placeholder token so the SDK's local auth-source check passes; the
465-
// gateway itself is expected to accept unauthenticated requests here.
467+
// Skip auth mode - direct to gateway without a real token. The SDK's
468+
// local auth-source check would otherwise fail with "Not logged in",
469+
// so seed a placeholder bearer; the gateway is expected to accept
470+
// unauthenticated requests here and ignore the placeholder value.
466471
sdkEnv.ANTHROPIC_BASE_URL = gatewayUrl;
467472
delete sdkEnv.ANTHROPIC_API_KEY;
468473
sdkEnv.ANTHROPIC_AUTH_TOKEN = PROXY_PLACEHOLDER_TOKEN;
469474
authMode = `skip-auth:${gatewayUrl}`;
470-
logInfo('Skipping auth - no token sent to gateway');
475+
logInfo('Skipping auth - placeholder bearer sent to gateway');
471476
} else {
472477
// Local mode without auth - same rationale as skip-auth above.
473478
sdkEnv.ANTHROPIC_BASE_URL = gatewayUrl;
474479
delete sdkEnv.ANTHROPIC_API_KEY;
475480
sdkEnv.ANTHROPIC_AUTH_TOKEN = PROXY_PLACEHOLDER_TOKEN;
476481
authMode = `local-gateway:${gatewayUrl}`;
477-
logInfo('Local mode - no token sent to gateway');
482+
logInfo('Local mode - placeholder bearer sent to gateway');
478483
}
479484

480485
logInfo('Configured LLM gateway:', sdkEnv.ANTHROPIC_BASE_URL);

0 commit comments

Comments
 (0)