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
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/12387-the-vendor-s-own-variables-anthropic-cla → main 0 commits fork
8 files
changed +39
−14
src/core/subscriptions.ts +4 −4
| @@ -42,8 +42,8 @@ export interface Subscription { | ||
| 42 | 42 | readonly kind: SubscriptionKind; |
| 43 | 43 | /** |
| 44 | 44 | * 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). | |
| 47 | 47 | */ |
| 48 | 48 | readonly apiBase: string | null; |
| 49 | 49 | /** Set while the provider's limit holds; the account is skipped until then. */ |
| @@ -209,8 +209,8 @@ export function normalizeToken(raw: string): string { | ||
| 209 | 209 | |
| 210 | 210 | /** |
| 211 | 211 | * 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 | |
| 214 | 214 | * could be a key — not empty once normalized, not absurdly long — is an API key: |
| 215 | 215 | * Anthropic's `sk-ant-api…`, the `sk-ant-usr…` keys that come with a |
| 216 | 216 | * 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" | ||
| 33 | 33 | const DROPPED_ANSWERED = ["connection", "keep-alive", "transfer-encoding", "content-encoding", "content-length"]; |
| 34 | 34 | |
| 35 | 35 | 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>`. */ | |
| 37 | 37 | readonly base: string; |
| 38 | 38 | close(): void; |
| 39 | 39 | } |
| @@ -98,7 +98,7 @@ export function openGatewayDoor(gateway: string, options: OutboundOptions = {}): | ||
| 98 | 98 | |
| 99 | 99 | /** |
| 100 | 100 | * 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 | |
| 102 | 102 | * (the default Anthropic host) is handed on as it is. Only for a member's |
| 103 | 103 | * account: the forge's own credential and a parent's lent door are the |
| 104 | 104 | * operator's, and may well be on a private address. |
| @@ -120,7 +120,7 @@ export async function throughGatewayDoor<T>( | ||
| 120 | 120 | |
| 121 | 121 | /** |
| 122 | 122 | * 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 | |
| 124 | 124 | * ends. Undefined, with `env` untouched, for an account without a gateway or a |
| 125 | 125 | * run on the forge's own credential. |
| 126 | 126 | */ |
src/crew/grant.ts +4 −4
| @@ -29,7 +29,7 @@ import { recordGrant, releaseGrant } from "../core/run_secret.ts"; | ||
| 29 | 29 | import { writeRefused } from "../core/cloud_account.ts"; |
| 30 | 30 | import { isSecretName, resolveScoped, type SecretScope } from "../core/secret_store.ts"; |
| 31 | 31 | import { holdValues, releaseValues } from "../kernel/log.ts"; |
| 32 | import { isModelCredential } from "./runner_env.ts"; | |
| 32 | import { isModelCredential, isVendorVariable } from "./runner_env.ts"; | |
| 33 | 33 | |
| 34 | 34 | export interface Declaration { |
| 35 | 35 | readonly names: readonly string[]; |
| @@ -46,11 +46,11 @@ export interface Declaration { | ||
| 46 | 46 | /** |
| 47 | 47 | * Names a secret may never take: the run's own environment, and every |
| 48 | 48 | * 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 | |
| 50 | 50 | * never point the runner at another endpoint or swap its key. |
| 51 | 51 | */ |
| 52 | 52 | 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)$/; | |
| 54 | 54 | |
| 55 | 55 | const HOST = /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+$/; |
| 56 | 56 | const MAX_DECLARED = 16; |
| @@ -72,7 +72,7 @@ export function parseDeclaration(...texts: readonly string[]): Declaration { | ||
| 72 | 72 | const item = entry.trim(); |
| 73 | 73 | if (item === "") continue; |
| 74 | 74 | 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)) { | |
| 76 | 76 | refused.push(`${name.slice(0, 40)}: not a usable name`); |
| 77 | 77 | continue; |
| 78 | 78 | } |
src/crew/runner.ts +1 −1
| @@ -1718,7 +1718,7 @@ async function work( | ||
| 1718 | 1718 | const sandbox = shellPolicy(); |
| 1719 | 1719 | // The run's own temp directory, never the machine's shared one; it goes |
| 1720 | 1720 | // 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 | |
| 1722 | 1722 | // `<TMPDIR>/claude-<uid>` writable, so TMPDIR has to say the same. |
| 1723 | 1723 | runTmp = sandbox.tmpDir; |
| 1724 | 1724 | if (runTmp !== undefined) { |
src/crew/runner_env.ts +6
| @@ -181,3 +181,9 @@ export function runAccount(picked: Pick, env: Readonly<Record<string, string>>): | ||
| 181 | 181 | const base = picked.apiBase !== null && picked.apiBase !== "" ? env[GATEWAY_VAR] : undefined; |
| 182 | 182 | return { token: picked.token, ...(base === undefined || base === "" ? {} : { base }) }; |
| 183 | 183 | } |
| 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 @@ | ||
| 22 | 22 | * allow-list only ever adds to them. |
| 23 | 23 | * |
| 24 | 24 | * 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 | |
| 26 | 26 | * machine's temp directory, which holds every other session's files and |
| 27 | 27 | * the forge's own, is denied whole, and so are the agent's other runs' |
| 28 | 28 | * 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"; | ||
| 11 | 11 | import { join } from "node:path"; |
| 12 | 12 | import { randomBytes } from "node:crypto"; |
| 13 | 13 | import { createApp } from "../app.ts"; |
| 14 | import { API_KEY_VAR } from "../crew/runner_env.ts"; | |
| 14 | 15 | import { crewHasCredential } from "../core/subscriptions.ts"; |
| 15 | 16 | import { createToken } from "../core/tokens.ts"; |
| 16 | 17 | import { createUser, getUserByName } from "../core/users.ts"; |
| @@ -64,6 +65,6 @@ export function finishWords(url: string, credentialPresent: boolean): readonly s | ||
| 64 | 65 | `Your forge is serving ${url}/`, |
| 65 | 66 | credentialPresent |
| 66 | 67 | ? "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).`, | |
| 68 | 69 | ]; |
| 69 | 70 | } |
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 | }); |
new above
offline