forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(server): a revoked Claude token no longer reads as authenticated #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
29b4d7c
fix(server): a revoked Claude token no longer reads as authenticated
yordis a9f945a
docs(fork): link the ledger entry to its pull request
yordis 792d618
fix(server): a value that is not a credential no longer reads as a re…
yordis 218eb1b
fix(server): a placeholder wearing the token prefix is not a token
yordis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
apps/server/src/provider/Drivers/ClaudeCredential.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import { assert, describe, it } from "@effect/vitest"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Layer from "effect/Layer"; | ||
| import { | ||
| HttpClient, | ||
| HttpClientError, | ||
| HttpClientRequest, | ||
| HttpClientResponse, | ||
| } from "effect/unstable/http"; | ||
|
|
||
| import { claudeOAuthTokenFromEnvironment, verifyClaudeOAuthToken } from "./ClaudeCredential.ts"; | ||
|
|
||
| function httpClientLayer(respond: (request: HttpClientRequest.HttpClientRequest) => Response) { | ||
| return Layer.succeed( | ||
| HttpClient.HttpClient, | ||
| HttpClient.make((request) => | ||
| Effect.succeed(HttpClientResponse.fromWeb(request, respond(request))), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| const transportFailureLayer = Layer.succeed( | ||
| HttpClient.HttpClient, | ||
| HttpClient.make((request) => | ||
| Effect.fail( | ||
| new HttpClientError.HttpClientError({ | ||
| reason: new HttpClientError.TransportError({ request, cause: new Error("offline") }), | ||
| }), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| describe("claudeOAuthTokenFromEnvironment", () => { | ||
| it("returns the configured token", () => { | ||
| assert.strictEqual( | ||
| claudeOAuthTokenFromEnvironment({ CLAUDE_CODE_OAUTH_TOKEN: "sk-ant-oat01-test" }), | ||
| "sk-ant-oat01-test", | ||
| ); | ||
| }); | ||
|
|
||
| it("treats blank and missing values alike", () => { | ||
| assert.strictEqual(claudeOAuthTokenFromEnvironment({}), undefined); | ||
| assert.strictEqual( | ||
| claudeOAuthTokenFromEnvironment({ CLAUDE_CODE_OAUTH_TOKEN: " " }), | ||
| undefined, | ||
| ); | ||
| }); | ||
|
|
||
| it("trims surrounding whitespace so a copy-pasted token still matches", () => { | ||
| assert.strictEqual( | ||
| claudeOAuthTokenFromEnvironment({ CLAUDE_CODE_OAUTH_TOKEN: " sk-ant-oat01-test\n" }), | ||
| "sk-ant-oat01-test", | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("verifyClaudeOAuthToken", () => { | ||
| it.effect("reports a live token and sends bearer auth", () => | ||
| Effect.gen(function* () { | ||
| let seen: HttpClientRequest.HttpClientRequest | undefined; | ||
| const verdict = yield* verifyClaudeOAuthToken("sk-ant-oat01-test").pipe( | ||
| Effect.provide( | ||
| httpClientLayer((request) => { | ||
| seen = request; | ||
| return Response.json({ data: [] }); | ||
| }), | ||
| ), | ||
| ); | ||
| assert.strictEqual(verdict, "live"); | ||
| assert.strictEqual(seen?.headers["authorization"], "Bearer sk-ant-oat01-test"); | ||
| assert.strictEqual(seen?.headers["anthropic-version"], "2023-06-01"); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("never asks Anthropic about an unresolved secret reference", () => | ||
| Effect.gen(function* () { | ||
| // Nothing resolved this into a credential, so a 401 would be Anthropic | ||
| // answering a question about a string that was never a token. | ||
| let requests = 0; | ||
| const verdict = yield* verifyClaudeOAuthToken("op://Vault/item/credential").pipe( | ||
| Effect.provide( | ||
| httpClientLayer(() => { | ||
| requests += 1; | ||
| return new Response("no", { status: 401 }); | ||
| }), | ||
| ), | ||
| ); | ||
| assert.strictEqual(verdict, "unknown"); | ||
| assert.strictEqual(requests, 0); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("never asks Anthropic about a value that is not a credential", () => | ||
| Effect.gen(function* () { | ||
| let requests = 0; | ||
| const verdict = yield* verifyClaudeOAuthToken("${MY_TOKEN}").pipe( | ||
| Effect.provide( | ||
| httpClientLayer(() => { | ||
| requests += 1; | ||
| return new Response("no", { status: 401 }); | ||
| }), | ||
| ), | ||
| ); | ||
| assert.strictEqual(verdict, "unknown"); | ||
| assert.strictEqual(requests, 0); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("never asks Anthropic about a placeholder wearing the credential prefix", () => | ||
| Effect.gen(function* () { | ||
| let requests = 0; | ||
| const verdict = yield* verifyClaudeOAuthToken("sk-ant-oat01-${MY_TOKEN}").pipe( | ||
| Effect.provide( | ||
| httpClientLayer(() => { | ||
| requests += 1; | ||
| return new Response("no", { status: 401 }); | ||
| }), | ||
| ), | ||
| ); | ||
| assert.strictEqual(verdict, "unknown"); | ||
| assert.strictEqual(requests, 0); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("reports a rejected token on 401", () => | ||
| Effect.gen(function* () { | ||
| const verdict = yield* verifyClaudeOAuthToken("sk-ant-oat01-stale").pipe( | ||
| Effect.provide(httpClientLayer(() => new Response("unauthorized", { status: 401 }))), | ||
| ); | ||
| assert.strictEqual(verdict, "rejected"); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("stays unknown when the account merely lacks scope", () => | ||
| Effect.gen(function* () { | ||
| const verdict = yield* verifyClaudeOAuthToken("sk-ant-oat01-scoped-out").pipe( | ||
| Effect.provide(httpClientLayer(() => new Response("forbidden", { status: 403 }))), | ||
| ); | ||
| assert.strictEqual(verdict, "unknown"); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("stays unknown when Anthropic is down", () => | ||
| Effect.gen(function* () { | ||
| const verdict = yield* verifyClaudeOAuthToken("sk-ant-oat01-fine").pipe( | ||
| Effect.provide(httpClientLayer(() => new Response("boom", { status: 503 }))), | ||
| ); | ||
| assert.strictEqual(verdict, "unknown"); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("stays unknown when the request never leaves the machine", () => | ||
| Effect.gen(function* () { | ||
| const verdict = yield* verifyClaudeOAuthToken("sk-ant-oat01-fine").pipe( | ||
| Effect.provide(transportFailureLayer), | ||
| ); | ||
| assert.strictEqual(verdict, "unknown"); | ||
| }), | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /** | ||
| * ClaudeCredential: liveness check for a configured Claude OAuth token. | ||
| * | ||
| * The capability probe reports which credential the CLI *found*, never whether | ||
| * Anthropic still honours it. An expired or revoked setup token still reads as | ||
| * `tokenSource: "CLAUDE_CODE_OAUTH_TOKEN"`, so Settings would keep claiming the | ||
| * provider is authenticated right up until a turn fails. This module closes | ||
| * that gap with one cheap authenticated GET. | ||
| * | ||
| * `GET /v1/models` is used because it is the only first-party endpoint that | ||
| * accepts the token, costs no message quota, and answers the only question | ||
| * worth asking: does Anthropic still accept this credential. | ||
| * | ||
| * Only a value that looks like an Anthropic credential is ever sent. Anything | ||
| * else is a question for someone other than Anthropic, and a `401` earned by an | ||
| * unresolved secret reference or a placeholder would blame the token for a | ||
| * problem that is not the token's. | ||
| * | ||
| * @module provider/Drivers/ClaudeCredential | ||
| */ | ||
| import * as Duration from "effect/Duration"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Option from "effect/Option"; | ||
| import { HttpClient, HttpClientRequest } from "effect/unstable/http"; | ||
|
|
||
| /** | ||
| * Result of asking Anthropic about a token. | ||
| * | ||
| * `unknown` is the deliberate catch-all: a proxy, an outage, or a captive | ||
| * network must never sign a working install out of Settings, so only an | ||
| * explicit rejection is treated as one. | ||
| */ | ||
| export type ClaudeCredentialVerdict = "live" | "rejected" | "unknown"; | ||
|
|
||
| const ANTHROPIC_MODELS_URL = "https://api.anthropic.com/v1/models?limit=1"; | ||
| /** | ||
| * `sk-ant-` is shared by Anthropic setup tokens (`sk-ant-oat01-`) and API keys | ||
| * (`sk-ant-api03-`). The tail is spelled out because the prefix alone accepts | ||
| * `sk-ant-oat01-${MY_TOKEN}`, and a placeholder that happens to carry the | ||
| * prefix is still a placeholder. Anything outside the character set key | ||
| * material is written with is treated as not a credential, which costs at most | ||
| * an unknown verdict and never a wrong one. | ||
| */ | ||
| const ANTHROPIC_CREDENTIAL = /^sk-ant-[A-Za-z0-9._~+/=-]+$/; | ||
| const ANTHROPIC_VERSION_HEADER = "2023-06-01"; | ||
| const VERIFY_TIMEOUT = Duration.seconds(10); | ||
|
|
||
| /** | ||
| * The OAuth token a Claude instance was configured with, if any. | ||
| * | ||
| * Reads the same variable the CLI itself reads, so an instance whose | ||
| * environment carries an `op://` reference is checked with whatever that | ||
| * reference resolved to. | ||
| */ | ||
| export function claudeOAuthTokenFromEnvironment( | ||
| environment: NodeJS.ProcessEnv, | ||
| ): string | undefined { | ||
| const token = environment["CLAUDE_CODE_OAUTH_TOKEN"]?.trim(); | ||
| return token ? token : undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Ask Anthropic whether it still accepts `token`. | ||
| * | ||
| * Never fails: a value that is not an Anthropic credential, transport errors, | ||
| * timeouts, and every non-401 status all collapse to `unknown` so the caller | ||
| * can leave the existing status alone. | ||
| */ | ||
| export const verifyClaudeOAuthToken = Effect.fn("verifyClaudeOAuthToken")(function* ( | ||
| token: string, | ||
| ): Effect.fn.Return<ClaudeCredentialVerdict, never, HttpClient.HttpClient> { | ||
| if (!ANTHROPIC_CREDENTIAL.test(token)) { | ||
| return "unknown"; | ||
| } | ||
| const client = yield* HttpClient.HttpClient; | ||
| const request = HttpClientRequest.get(ANTHROPIC_MODELS_URL).pipe( | ||
| HttpClientRequest.setHeader("authorization", `Bearer ${token}`), | ||
| HttpClientRequest.setHeader("anthropic-version", ANTHROPIC_VERSION_HEADER), | ||
| HttpClientRequest.setHeader("accept", "application/json"), | ||
| ); | ||
| const response = yield* client.execute(request).pipe( | ||
| Effect.timeoutOption(VERIFY_TIMEOUT), | ||
| Effect.orElseSucceed(() => Option.none()), | ||
| ); | ||
| if (Option.isNone(response)) { | ||
| return "unknown"; | ||
| } | ||
| const { status } = response.value; | ||
| if (status === 401) { | ||
| return "rejected"; | ||
| } | ||
| return status >= 200 && status < 300 ? "live" : "unknown"; | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.