Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions containers/agent/api-proxy-health-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ if [ -n "$ANTHROPIC_BASE_URL" ]; then

# Verify ANTHROPIC_AUTH_TOKEN is placeholder (if present)
if [ -n "$ANTHROPIC_AUTH_TOKEN" ]; then
if [ "$ANTHROPIC_AUTH_TOKEN" != "placeholder-token-for-credential-isolation" ]; then
if [ "$ANTHROPIC_AUTH_TOKEN" != "sk-ant-placeholder-key-for-credential-isolation" ]; then
echo "[health-check][ERROR] ANTHROPIC_AUTH_TOKEN contains non-placeholder value!"
echo "[health-check][ERROR] Token should be 'placeholder-token-for-credential-isolation'"
echo "[health-check][ERROR] Token should be 'sk-ant-placeholder-key-for-credential-isolation'"
exit 1
fi
echo "[health-check] ✓ ANTHROPIC_AUTH_TOKEN is placeholder value (correct)"
Expand Down
36 changes: 33 additions & 3 deletions containers/api-proxy/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ function shouldStripHeader(name) {
}

// Read API keys from environment (set by docker-compose)
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
const COPILOT_GITHUB_TOKEN = process.env.COPILOT_GITHUB_TOKEN;
// Trim whitespace/newlines to prevent malformed HTTP headers — env vars from
// CI secrets or docker-compose YAML may include trailing whitespace.
const OPENAI_API_KEY = (process.env.OPENAI_API_KEY || '').trim() || undefined;
const ANTHROPIC_API_KEY = (process.env.ANTHROPIC_API_KEY || '').trim() || undefined;
const COPILOT_GITHUB_TOKEN = (process.env.COPILOT_GITHUB_TOKEN || '').trim() || undefined;

// Configurable API target hosts (supports custom endpoints / internal LLM routers)
const OPENAI_API_TARGET = process.env.OPENAI_API_TARGET || 'api.openai.com';
Expand Down Expand Up @@ -332,6 +334,21 @@ function proxyRequest(req, res, targetHost, injectHeaders, provider, basePath =
headers['x-request-id'] = requestId;
Object.assign(headers, injectHeaders);

// Log auth header injection for debugging credential-isolation issues
const injectedKey = injectHeaders['x-api-key'] || injectHeaders['authorization'];
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

injectHeaders uses 'Authorization' (capital A) for OpenAI/Copilot, but this diagnostic checks only injectHeaders['authorization'] (lowercase). As a result, auth_inject logs won’t trigger for those providers. Consider normalizing header keys (e.g., lowercasing injectHeaders keys) or checking both cases so the diagnostic works consistently.

This issue also appears in the following locations of the same file:

  • line 340
  • line 343
Suggested change
const injectedKey = injectHeaders['x-api-key'] || injectHeaders['authorization'];
const injectedKey = injectHeaders['x-api-key']
|| injectHeaders['authorization']
|| injectHeaders['Authorization'];

Copilot uses AI. Check for mistakes.
if (injectedKey) {
const keyPreview = injectedKey.length > 8
? `${injectedKey.substring(0, 8)}...${injectedKey.substring(injectedKey.length - 4)}`
: '(short)';
logRequest('debug', 'auth_inject', {
request_id: requestId,
provider,
key_length: injectedKey.length,
key_preview: keyPreview,
has_anthropic_version: !!headers['anthropic-version'],
});
}

const options = {
hostname: targetHost,
port: 443,
Expand Down Expand Up @@ -391,6 +408,19 @@ function proxyRequest(req, res, targetHost, injectHeaders, provider, basePath =

// Copy response headers and add X-Request-ID
const resHeaders = { ...proxyRes.headers, 'x-request-id': requestId };

// Log upstream auth failures prominently for debugging
if (proxyRes.statusCode === 401 || proxyRes.statusCode === 403) {
logRequest('warn', 'upstream_auth_error', {
request_id: requestId,
provider,
status: proxyRes.statusCode,
upstream_host: targetHost,
path: sanitizeForLog(req.url),
message: `Upstream returned ${proxyRes.statusCode} — check that the API key is valid and has not expired`,
});
}

res.writeHead(proxyRes.statusCode, resHeaders);
proxyRes.pipe(res);
});
Expand Down
10 changes: 5 additions & 5 deletions src/docker-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2048,7 +2048,7 @@ describe('docker-manager', () => {
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.ANTHROPIC_BASE_URL).toBe('http://172.30.0.30:10001');
expect(env.ANTHROPIC_AUTH_TOKEN).toBe('placeholder-token-for-credential-isolation');
expect(env.ANTHROPIC_AUTH_TOKEN).toBe('sk-ant-placeholder-key-for-credential-isolation');
expect(env.CLAUDE_CODE_API_KEY_HELPER).toBe('/usr/local/bin/get-claude-key.sh');
});

Expand All @@ -2059,7 +2059,7 @@ describe('docker-manager', () => {
const env = agent.environment as Record<string, string>;
expect(env.OPENAI_BASE_URL).toBe('http://172.30.0.30:10000/v1');
expect(env.ANTHROPIC_BASE_URL).toBe('http://172.30.0.30:10001');
expect(env.ANTHROPIC_AUTH_TOKEN).toBe('placeholder-token-for-credential-isolation');
expect(env.ANTHROPIC_AUTH_TOKEN).toBe('sk-ant-placeholder-key-for-credential-isolation');
expect(env.CLAUDE_CODE_API_KEY_HELPER).toBe('/usr/local/bin/get-claude-key.sh');
});

Expand All @@ -2070,7 +2070,7 @@ describe('docker-manager', () => {
const env = agent.environment as Record<string, string>;
expect(env.OPENAI_BASE_URL).toBeUndefined();
expect(env.ANTHROPIC_BASE_URL).toBe('http://172.30.0.30:10001');
expect(env.ANTHROPIC_AUTH_TOKEN).toBe('placeholder-token-for-credential-isolation');
expect(env.ANTHROPIC_AUTH_TOKEN).toBe('sk-ant-placeholder-key-for-credential-isolation');
expect(env.CLAUDE_CODE_API_KEY_HELPER).toBe('/usr/local/bin/get-claude-key.sh');
});

Expand Down Expand Up @@ -2130,7 +2130,7 @@ describe('docker-manager', () => {
// Agent should have the BASE_URL to reach the sidecar instead
expect(env.ANTHROPIC_BASE_URL).toBe('http://172.30.0.30:10001');
// Agent should have placeholder token for Claude Code compatibility
expect(env.ANTHROPIC_AUTH_TOKEN).toBe('placeholder-token-for-credential-isolation');
expect(env.ANTHROPIC_AUTH_TOKEN).toBe('sk-ant-placeholder-key-for-credential-isolation');
} finally {
if (origKey !== undefined) {
process.env.ANTHROPIC_API_KEY = origKey;
Expand Down Expand Up @@ -2219,7 +2219,7 @@ describe('docker-manager', () => {
expect(env.ANTHROPIC_API_KEY).toBeUndefined();
expect(env.ANTHROPIC_BASE_URL).toBe('http://172.30.0.30:10001');
// But should have placeholder token for Claude Code compatibility
expect(env.ANTHROPIC_AUTH_TOKEN).toBe('placeholder-token-for-credential-isolation');
expect(env.ANTHROPIC_AUTH_TOKEN).toBe('sk-ant-placeholder-key-for-credential-isolation');
} finally {
if (origKey !== undefined) {
process.env.ANTHROPIC_API_KEY = origKey;
Expand Down
3 changes: 2 additions & 1 deletion src/docker-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,8 @@ export function generateDockerCompose(

// Set placeholder token for Claude Code CLI compatibility
// Real authentication happens via ANTHROPIC_BASE_URL pointing to api-proxy
environment.ANTHROPIC_AUTH_TOKEN = 'placeholder-token-for-credential-isolation';
// Use sk-ant- prefix so Claude Code's key-format validation passes
environment.ANTHROPIC_AUTH_TOKEN = 'sk-ant-placeholder-key-for-credential-isolation';
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing ANTHROPIC_AUTH_TOKEN placeholder here will cause the agent entrypoint pre-flight script (containers/agent/api-proxy-health-check.sh) to exit 1 because it still hard-codes the previous value ('placeholder-token-for-credential-isolation'). Update the health check to accept the new placeholder (or relax the check) so api-proxy-enabled runs don’t fail before the user command executes.

Suggested change
environment.ANTHROPIC_AUTH_TOKEN = 'sk-ant-placeholder-key-for-credential-isolation';
environment.ANTHROPIC_AUTH_TOKEN = 'sk-ant-placeholder-token-for-credential-isolation';

Copilot uses AI. Check for mistakes.
logger.debug('ANTHROPIC_AUTH_TOKEN set to placeholder value for credential isolation');

// Set API key helper for Claude Code CLI to use credential isolation
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/api-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('API Proxy Sidecar', () => {
);

expect(result).toSucceed();
expect(result.stdout).toContain('ANTHROPIC_AUTH_TOKEN=placeholder-token-for-credential-isolation');
expect(result.stdout).toContain('ANTHROPIC_AUTH_TOKEN=sk-ant-placeholder-key-for-credential-isolation');
}, 180000);

test('should set OPENAI_BASE_URL in agent when OpenAI key is provided', async () => {
Expand Down
Loading