Public: anyone can read this; the crew can change it.

The vendor's own variables (ANTHROPIC_*, CLAUDE_CODE_*) are read only inside the Claude runner's module #16757

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/12387-the-vendor-s-own-variables-anthropic-cla → main 0 commits fork

  • Merged. mason/12387-the-vendor-s-own-variables-anthropic-cla was merged into main as 46e0349.

Review

Sign in to review this change.
8 files changed +39 −14
src/core/subscriptions.ts +4 −4
@@ -42,8 +42,8 @@ export interface Subscription {
4242 readonly kind: SubscriptionKind;
4343 /**
4444 * Anthropic-compatible API base URL (OpenRouter, etc.), or null for
45 * Anthropic itself. Plain text — not a secret. Claude Code reads it as
46 * `ANTHROPIC_BASE_URL` (without a trailing `/v1`; the SDK adds that).
45 * Anthropic itself. Plain text — not a secret. Claude Code reads it as the gateway variable
46 * (named in runner_env.ts; without a trailing `/v1`, the SDK adds that).
4747 */
4848 readonly apiBase: string | null;
4949 /** Set while the provider's limit holds; the account is skipped until then. */
@@ -209,8 +209,8 @@ export function normalizeToken(raw: string): string {
209209
210210 /**
211211 * What kind of token this is. A token from `claude setup-token` begins
212 * `sk-ant-oat…` and is a person's own plan: it goes under
213 * `CLAUDE_CODE_OAUTH_TOKEN` and never takes a base URL. Anything else that
212 * `sk-ant-oat…` and is a person's own plan: it goes under the OAuth variable
213 * (runner_env.ts names it) and never takes a base URL. Anything else that
214214 * could be a key — not empty once normalized, not absurdly long — is an API key:
215215 * Anthropic's `sk-ant-api…`, the `sk-ant-usr…` keys that come with a
216216 * subscription's API credits, OpenRouter's `sk-or-v1-…`, or whatever shape
src/crew/gateway_door.ts +3 −3
@@ -33,7 +33,7 @@ const DROPPED_ASKED = ["host", "connection", "keep-alive", "proxy-authorization"
3333 const DROPPED_ANSWERED = ["connection", "keep-alive", "transfer-encoding", "content-encoding", "content-length"];
3434
3535 export interface GatewayDoor {
36 /** What Claude Code is given as `ANTHROPIC_BASE_URL`: `http://127.0.0.1:<port>/<secret>`. */
36 /** What Claude Code is given as its gateway base URL: `http://127.0.0.1:<port>/<secret>`. */
3737 readonly base: string;
3838 close(): void;
3939 }
@@ -98,7 +98,7 @@ export function openGatewayDoor(gateway: string, options: OutboundOptions = {}):
9898
9999 /**
100100 * Run `fn` with a member's account env pointed at a door when it names a
101 * gateway, and close the door after. An env without `ANTHROPIC_BASE_URL`
101 * gateway, and close the door after. An env without a gateway base URL
102102 * (the default Anthropic host) is handed on as it is. Only for a member's
103103 * account: the forge's own credential and a parent's lent door are the
104104 * operator's, and may well be on a private address.
@@ -120,7 +120,7 @@ export async function throughGatewayDoor<T>(
120120
121121 /**
122122 * A run's door, when its picked account has a gateway: `env`'s
123 * `ANTHROPIC_BASE_URL` is pointed at it, and the caller closes it when the run
123 * gateway base URL is pointed at it, and the caller closes it when the run
124124 * ends. Undefined, with `env` untouched, for an account without a gateway or a
125125 * run on the forge's own credential.
126126 */
src/crew/grant.ts +4 −4
@@ -29,7 +29,7 @@ import { recordGrant, releaseGrant } from "../core/run_secret.ts";
2929 import { writeRefused } from "../core/cloud_account.ts";
3030 import { isSecretName, resolveScoped, type SecretScope } from "../core/secret_store.ts";
3131 import { holdValues, releaseValues } from "../kernel/log.ts";
32 import { isModelCredential } from "./runner_env.ts";
32 import { isModelCredential, isVendorVariable } from "./runner_env.ts";
3333
3434 export interface Declaration {
3535 readonly names: readonly string[];
@@ -46,11 +46,11 @@ export interface Declaration {
4646 /**
4747 * Names a secret may never take: the run's own environment, and every
4848 * variable a model runner reads for its credential or gateway
49 * (`isModelCredential`: ANTHROPIC_*, OPENAI_*, …), so a granted secret can
49 * (`isModelCredential`, `isVendorVariable`), so a granted secret can
5050 * never point the runner at another endpoint or swap its key.
5151 */
5252 const RESERVED =
53 /^(FORGE_|GIT_|CLAUDE_|ANTHROPIC_|OPENAI_|LD_|DYLD_|BUN_|NODE_)|^(PATH|HOME|USER|SHELL|PWD|TMPDIR|IFS)$/;
53 /^(FORGE_|GIT_|OPENAI_|LD_|DYLD_|BUN_|NODE_)|^(PATH|HOME|USER|SHELL|PWD|TMPDIR|IFS)$/;
5454
5555 const HOST = /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+$/;
5656 const MAX_DECLARED = 16;
@@ -72,7 +72,7 @@ export function parseDeclaration(...texts: readonly string[]): Declaration {
7272 const item = entry.trim();
7373 if (item === "") continue;
7474 const [name = "", host] = item.split("@") as [string, string?];
75 if (!isSecretName(name) || RESERVED.test(name) || isModelCredential(name)) {
75 if (!isSecretName(name) || RESERVED.test(name) || isModelCredential(name) || isVendorVariable(name)) {
7676 refused.push(`${name.slice(0, 40)}: not a usable name`);
7777 continue;
7878 }
src/crew/runner.ts +1 −1
@@ -1718,7 +1718,7 @@ async function work(
17181718 const sandbox = shellPolicy();
17191719 // The run's own temp directory, never the machine's shared one; it goes
17201720 // when the run does (the `finally` below). Both names: Claude Code's
1721 // sandbox reads CLAUDE_CODE_TMPDIR, but its shell still binds
1721 // sandbox reads its own temp-dir variable, but its shell still binds
17221722 // `<TMPDIR>/claude-<uid>` writable, so TMPDIR has to say the same.
17231723 runTmp = sandbox.tmpDir;
17241724 if (runTmp !== undefined) {
src/crew/runner_env.ts +6
@@ -181,3 +181,9 @@ export function runAccount(picked: Pick, env: Readonly<Record<string, string>>):
181181 const base = picked.apiBase !== null && picked.apiBase !== "" ? env[GATEWAY_VAR] : undefined;
182182 return { token: picked.token, ...(base === undefined || base === "" ? {} : { base }) };
183183 }
184
185 /** Whether `name` is one of Claude Code's own variables, of any kind. */
186 export const isVendorVariable = (name: string): boolean => /^(ANTHROPIC_|CLAUDE_)/.test(name);
187
188 /** The variable that holds the forge's own API key, for a message that tells a person to set it. */
189 export const API_KEY_VAR = "ANTHROPIC_API_KEY";
src/crew/sandbox.ts +1 −1
@@ -22,7 +22,7 @@
2222 * allow-list only ever adds to them.
2323 *
2424 * Each run has its own temp directory (`tmpDir`, handed to Claude Code as
25 * `CLAUDE_CODE_TMPDIR` and `TMPDIR`) under the agent's own `.t/`; the
25 * its temp-dir variable and `TMPDIR`) under the agent's own `.t/`; the
2626 * machine's temp directory, which holds every other session's files and
2727 * the forge's own, is denied whole, and so are the agent's other runs'
2828 * temp directories. The shell also writes bun's install cache, one per
src/packs/up.ts +2 −1
@@ -11,6 +11,7 @@ import { chmodSync, writeFileSync } from "node:fs";
1111 import { join } from "node:path";
1212 import { randomBytes } from "node:crypto";
1313 import { createApp } from "../app.ts";
14 import { API_KEY_VAR } from "../crew/runner_env.ts";
1415 import { crewHasCredential } from "../core/subscriptions.ts";
1516 import { createToken } from "../core/tokens.ts";
1617 import { createUser, getUserByName } from "../core/users.ts";
@@ -64,6 +65,6 @@ export function finishWords(url: string, credentialPresent: boolean): readonly s
6465 `Your forge is serving ${url}/`,
6566 credentialPresent
6667 ? "The crew has a model credential and will start work."
67 : `The crew is off until you add a model credential: sign in and open ${url}/settings/subscriptions (or start this command again with ANTHROPIC_API_KEY set).`,
68 : `The crew is off until you add a model credential: sign in and open ${url}/settings/subscriptions (or start this command again with ${API_KEY_VAR} set).`,
6869 ];
6970 }
test/vendor-names.test.ts added +18
@@ -0,0 +1,18 @@
1 import { expect, test } from "bun:test";
2 import { Glob } from "bun";
3
4 // Claude Code's own variable names are spelled in one module; the rest ask it.
5 const OWNER = "src/crew/runner_env.ts";
6 const NAME = /(?<![A-Z0-9_])(ANTHROPIC_|CLAUDE_CODE_)/;
7
8 test("vendor variable names appear only in the runner's env module", async () => {
9 const offenders: string[] = [];
10 for await (const file of new Glob("src/**/*.ts").scan(".")) {
11 if (file === OWNER) continue;
12 const lines = (await Bun.file(file).text()).split("\n");
13 lines.forEach((l, i) => {
14 if (NAME.test(l)) offenders.push(`${file}:${i + 1}: ${l.trim()}`);
15 });
16 }
17 expect(offenders).toEqual([]);
18 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs