Files
cc-switch/tests/utils/providerConfigUtils.bedrock.test.ts
Jason 2185498e88 fix: stop shared config and live files from leaking provider settings
Common config snippet (Claude Code):
- Treat custom request headers (`*_CUSTOM_HEADERS`, `HEADERS`) and any
  key containing COOKIE or AUTHORIZATION as credentials. Header names in
  general stay shareable (CLAUDE_CODE_ATTRIBUTION_HEADER,
  OTEL_EXPORTER_OTLP_HEADERS).
- Never share key fields: all ANTHROPIC_* settings, the protocol
  selectors (CLAUDE_CODE_USE_BEDROCK/VERTEX/...), AWS_* and Vertex
  regions, and top-level model/apiKeyHelper/fallbackModel. Switching
  from Bedrock to the official provider no longer keeps Claude Code on
  Bedrock, and a /model choice no longer follows every switch.
- Entries an existing snippet already shared are stripped from the
  current provider's row on its next switch-away, so they cannot get
  stuck in the wrong provider. A provider carrying AWS or Google
  credentials keeps that family's selectors and regions.

Live files holding keys are now written owner-only (0600): Codex
auth.json and config.toml, Claude Code settings.json and Grok Build
config.toml. New files used to follow the umask (0644).

Presets:
- E-FlowCode and PIPELLM no longer ship personal preferences; Codex
  E-FlowCode drops `personality`.
- AWS Bedrock (API Key) stores the key in env.AWS_BEARER_TOKEN_BEDROCK,
  which Claude Code reads. The key field and the proxy read it too;
  existing rows with a top-level apiKey keep working and are edited in
  place.
- A preset scan keeps preference keys out of Claude and Codex presets.

Remove dead code only reached from tests: ProxyService::start_with_takeover
and its helpers, and the ConfigService sync_current_providers_to_live
family.

Behavior changes: ANTHROPIC_* settings such as ANTHROPIC_LOG are no
longer shared; a Bedrock or Vertex provider whose selectors only lived in
the shared snippet may need them re-entered once.
2026-09-26 23:03:42 +08:00

50 lines
1.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
getApiKeyFromConfig,
hasApiKeyField,
setApiKeyInConfig,
} from "@/utils/providerConfigUtils";
const bedrockConfig = (env: Record<string, string>, extra = {}) =>
JSON.stringify({
...extra,
env: { CLAUDE_CODE_USE_BEDROCK: "1", AWS_REGION: "us-west-2", ...env },
});
describe("Bedrock API Key field", () => {
it("reads and writes env.AWS_BEARER_TOKEN_BEDROCK", () => {
const config = bedrockConfig({ AWS_BEARER_TOKEN_BEDROCK: "" });
expect(hasApiKeyField(config, "claude")).toBe(true);
const updated = setApiKeyInConfig(config, "bedrock-key", {
appType: "claude",
});
const parsed = JSON.parse(updated);
expect(parsed.env.AWS_BEARER_TOKEN_BEDROCK).toBe("bedrock-key");
expect(parsed).not.toHaveProperty("apiKey");
expect(parsed.env).not.toHaveProperty("ANTHROPIC_AUTH_TOKEN");
expect(getApiKeyFromConfig(updated, "claude")).toBe("bedrock-key");
});
// 旧版预设把 Key 写在顶层 apiKey:存量行照常显示,编辑时写回原处,不改写行结构
it("keeps legacy rows with a top-level apiKey working", () => {
const legacy = bedrockConfig({}, { apiKey: "legacy-key" });
expect(hasApiKeyField(legacy, "claude")).toBe(true);
expect(getApiKeyFromConfig(legacy, "claude")).toBe("legacy-key");
const parsed = JSON.parse(
setApiKeyInConfig(legacy, "rotated-key", { appType: "claude" }),
);
expect(parsed.apiKey).toBe("rotated-key");
expect(parsed.env).not.toHaveProperty("AWS_BEARER_TOKEN_BEDROCK");
});
it("prefers the env token over a leftover top-level apiKey", () => {
const both = bedrockConfig(
{ AWS_BEARER_TOKEN_BEDROCK: "env-key" },
{ apiKey: "stale-key" },
);
expect(getApiKeyFromConfig(both, "claude")).toBe("env-key");
});
});