Public: anyone can read this; the crew can change it.
P0: a real Anthropic key is refused when spawning a forge — accept any key as pasted (cherry-pick steward/model-key-accept) #7602
scout/forge:scout/7552-p0-a-real-anthropic-key-is-refused-when → main 1 commit fork
6 files
changed +35
−18
src/actions/spawn.ts +3
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | * idempotent — and a finished one is answered, not made twice. |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | import { normalizeToken } from "../core/subscriptions.ts"; | |
| 15 | 16 | import { mkdir, readdir, rename } from "node:fs/promises"; |
| 16 | 17 | import { join } from "node:path"; |
| 17 | 18 | import { createApp, type App } from "../app.ts"; |
| @@ -224,6 +225,8 @@ async function make( | ||
| 224 | 225 | plan: SpawnPlan, |
| 225 | 226 | deps: SpawnDeps, |
| 226 | 227 | ): Promise<Result<Spawned, SpawnError>> { |
| 228 | // The key as the person meant it: whitespace and a `Bearer ` prefix from the paste dropped. | |
| 229 | if (deps.modelKey !== undefined) deps = { ...deps, modelKey: normalizeToken(deps.modelKey) }; | |
| 227 | 230 | const planHash = sha256(JSON.stringify(plan)); |
| 228 | 231 | let ledger = await readLedger(app, plan.name); |
| 229 | 232 | if (ledger) { |
src/core/subscriptions.ts +16 −6
| @@ -65,7 +65,7 @@ interface Row { | ||
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | export const NAME_MAX = 40; |
| 68 | const TOKEN_MAX = 512; | |
| 68 | const TOKEN_MAX = 4096; | |
| 69 | 69 | |
| 70 | 70 | /** The forge's own token, from its environment: a row with no sealed token. */ |
| 71 | 71 | export const PLATFORM = "platform"; |
| @@ -209,9 +209,18 @@ export const API_BASE_MAX = 200; | ||
| 209 | 209 | * a gateway hands out with its base URL. The forge keeps no list of shapes; |
| 210 | 210 | * the provider answers a bad key on the first call, and the run says so. |
| 211 | 211 | */ |
| 212 | export function kindOfToken(token: string): SubscriptionKind | undefined { | |
| 213 | if (token === "" || token.length > TOKEN_MAX || /\s|\0/.test(token)) | |
| 214 | return undefined; | |
| 212 | /** | |
| 213 | * A pasted key as the owner meant it: every whitespace character (a wrapped line, a trailing newline, a | |
| 214 | * non-breaking space from a web page) and a leading `Bearer ` are dropped. Keys never contain whitespace, | |
| 215 | * and a gateway may use any shape at all, so nothing else is judged. | |
| 216 | */ | |
| 217 | export function normalizeToken(raw: string): string { | |
| 218 | return raw.replace(/^\s*bearer\s+/i, "").replace(/[\s\u200b-\u200d\ufeff]+/g, ""); | |
| 219 | } | |
| 220 | ||
| 221 | export function kindOfToken(raw: string): SubscriptionKind | undefined { | |
| 222 | const token = normalizeToken(raw); | |
| 223 | if (token === "" || token.length > TOKEN_MAX || /\0/.test(token)) return undefined; | |
| 215 | 224 | if (/^sk-ant-oat\d*-/.test(token)) return "oauth"; |
| 216 | 225 | return "api_key"; |
| 217 | 226 | } |
| @@ -312,7 +321,8 @@ export function addSubscription( | ||
| 312 | 321 | if (parsed === undefined) return err("invalid_api_base"); |
| 313 | 322 | apiBase = parsed; |
| 314 | 323 | } |
| 315 | const kind = kindOfToken(input.token); | |
| 324 | const token = normalizeToken(input.token); | |
| 325 | const kind = kindOfToken(token); | |
| 316 | 326 | if (kind === undefined) return err("invalid_token"); |
| 317 | 327 | // A Claude subscription token is Anthropic's own plan — never a gateway. |
| 318 | 328 | if (kind === "oauth" && apiBase !== null) return err("invalid_api_base"); |
| @@ -325,7 +335,7 @@ export function addSubscription( | ||
| 325 | 335 | subscriptionId, |
| 326 | 336 | name, |
| 327 | 337 | kind, |
| 328 | seal(dataDir, input.token, aad(subscriptionId)), | |
| 338 | seal(dataDir, token, aad(subscriptionId)), | |
| 329 | 339 | apiBase, |
| 330 | 340 | input.createdBy ?? null, |
| 331 | 341 | ); |
src/spawn/door.ts +1 −1
| @@ -219,7 +219,7 @@ export async function spawnDoor( | ||
| 219 | 219 | if (kind === undefined) |
| 220 | 220 | return invalid( |
| 221 | 221 | "modelKey", |
| 222 | "must be one line with no spaces: an Anthropic key (sk-ant-api…, sk-ant-usr…), a `claude setup-token` token (sk-ant-oat…), or a gateway's key with its base URL", | |
| 222 | "is empty: paste an Anthropic key, a `claude setup-token` token, or your gateway's key (with its base URL)", | |
| 223 | 223 | ); |
| 224 | 224 | if (kind === "oauth" && modelEndpoint !== undefined) |
| 225 | 225 | return invalid("modelEndpoint", "a subscription token (sk-ant-oat…) talks to Anthropic itself and takes no base URL"); |
test/new-forge.test.ts +2 −2
| @@ -229,13 +229,13 @@ describe("+ New forge", () => { | ||
| 229 | 229 | const key = await post(admin, [ |
| 230 | 230 | ["name", "kai"], |
| 231 | 231 | ["apps", "alice/bakery"], |
| 232 | ["modelKey", "sk-ant-api03 pasted with a space"], | |
| 232 | ["modelKey", "\u200b"], | |
| 233 | 233 | ["budgetUsd", "5"], |
| 234 | 234 | ["do", "plan"], |
| 235 | 235 | ]); |
| 236 | 236 | expect(key.status).toBe(422); |
| 237 | 237 | expect(await key.text()).toMatch( |
| 238 | /id="f-modelKey-err"[^>]*>[^<]*one line with no spaces/, | |
| 238 | /id="f-modelKey-err"[^>]*>[^<]*is empty/, | |
| 239 | 239 | ); |
| 240 | 240 | }); |
| 241 | 241 |
test/spawn-keys.test.ts +3 −3
| @@ -116,7 +116,7 @@ describe("a spawned child's own keys and domain", () => { | ||
| 116 | 116 | await rm(dir, { recursive: true, force: true }); |
| 117 | 117 | }); |
| 118 | 118 | |
| 119 | test("a setup-token or a subscription's credits key may go to a child under a budget; a key needs a budget; a token with a space is refused", async () => { | |
| 119 | test("a setup-token or a subscription's credits key may go to a child under a budget; a key needs a budget; an empty key is refused", async () => { | |
| 120 | 120 | const oauth = await post({ |
| 121 | 121 | name: "maria", |
| 122 | 122 | apps: ["alice/bakery"], |
| @@ -148,11 +148,11 @@ describe("a spawned child's own keys and domain", () => { | ||
| 148 | 148 | name: "maria", |
| 149 | 149 | apps: ["alice/bakery"], |
| 150 | 150 | dryRun: true, |
| 151 | modelKey: "sk-ant-api03 with a space", | |
| 151 | modelKey: " \u200b ", | |
| 152 | 152 | budgetUsd: 5, |
| 153 | 153 | }); |
| 154 | 154 | expect(spaced.status).toBe(422); |
| 155 | expect(await spaced.text()).toContain("one line with no spaces"); | |
| 155 | expect(await spaced.text()).toContain("is empty"); | |
| 156 | 156 | expect( |
| 157 | 157 | ( |
| 158 | 158 | await post({ |
test/subscriptions.test.ts +10 −6
| @@ -18,7 +18,7 @@ import { | ||
| 18 | 18 | EVERYONE, |
| 19 | 19 | getSubscription, |
| 20 | 20 | hostOfApiBase, |
| 21 | kindOfToken, | |
| 21 | kindOfToken, normalizeToken, | |
| 22 | 22 | listGrants, |
| 23 | 23 | listSubscriptions, |
| 24 | 24 | parseApiBase, |
| @@ -41,15 +41,19 @@ const OR_KEY = "sk-or-v1-openrouter-key-for-the-test-aaaaaaaa"; | ||
| 41 | 41 | const GATEWAY = "https://openrouter.ai/api"; |
| 42 | 42 | |
| 43 | 43 | describe("a token's kind", () => { |
| 44 | test("a setup-token is a plan; any other one-line secret is an API key; spaces, nothing and absurd lengths are refused", () => { | |
| 44 | test("a setup-token is a plan; any other secret is an API key; whitespace and a Bearer prefix from the paste are dropped; nothing and absurd lengths are refused", () => { | |
| 45 | 45 | expect(kindOfToken(OAT)).toBe("oauth"); |
| 46 | 46 | expect(kindOfToken(KEY)).toBe("api_key"); |
| 47 | 47 | expect(kindOfToken(OR_KEY)).toBe("api_key"); |
| 48 | 48 | expect(kindOfToken("sk-ant-usr01-credits-with-a-subscription")).toBe("api_key"); |
| 49 | 49 | expect(kindOfToken("hunter2")).toBe("api_key"); |
| 50 | 50 | expect(kindOfToken("")).toBeUndefined(); |
| 51 | expect(kindOfToken("sk-ant-oat01-with space")).toBeUndefined(); | |
| 52 | expect(kindOfToken(`sk-ant-api03-${"x".repeat(600)}`)).toBeUndefined(); | |
| 51 | expect(kindOfToken("sk-ant-oat01-wrapped\nacross lines")).toBe("oauth"); | |
| 52 | expect(kindOfToken(`${KEY}\n`)).toBe("api_key"); | |
| 53 | expect(kindOfToken("Bearer my-own-gateway.key/v2:abc")).toBe("api_key"); | |
| 54 | expect(normalizeToken(" sk-ant-api03-a\u00a0b\u200bc \n")).toBe("sk-ant-api03-abc"); | |
| 55 | expect(kindOfToken(" \n\u200b ")).toBeUndefined(); | |
| 56 | expect(kindOfToken(`sk-ant-api03-${"x".repeat(5000)}`)).toBeUndefined(); | |
| 53 | 57 | }); |
| 54 | 58 | |
| 55 | 59 | test("parses an Anthropic-compatible base URL, stripping /v1", () => { |
| @@ -96,7 +100,7 @@ describe("the pool", () => { | ||
| 96 | 100 | error: "invalid_name", |
| 97 | 101 | }); |
| 98 | 102 | expect( |
| 99 | addSubscription(app.db, dataDir, { name: "nope", token: "hunter 2" }), | |
| 103 | addSubscription(app.db, dataDir, { name: "nope", token: " \n " }), | |
| 100 | 104 | ).toEqual({ ok: false, error: "invalid_token" }); |
| 101 | 105 | expect(JSON.stringify(listSubscriptions(app.db))).not.toContain(OAT); |
| 102 | 106 | const raw = app.db |
| @@ -537,7 +541,7 @@ describe("the pages", () => { | ||
| 537 | 541 | expect(added.status).toBe(303); |
| 538 | 542 | const refused = await post("/settings/subscriptions", { |
| 539 | 543 | name: "nope", |
| 540 | token: "hunter 2", | |
| 544 | token: "\u200b", | |
| 541 | 545 | }); |
| 542 | 546 | expect(refused.status).toBe(303); |
| 543 | 547 | expect(listSubscriptions(app.db).map((s) => s.name)).toEqual(["work"]); |
new above
offline