-
-
Notifications
You must be signed in to change notification settings - Fork 384
fix(oauth): recover missing client registrations #135
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
Open
Waishnav
wants to merge
8
commits into
main
Choose a base branch
from
codex/oauth-client-registration-recovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b4851c4
feat(oauth): add client registration signing key
Waishnav 51b81a0
feat(oauth): sign recoverable client registrations
Waishnav 72cc884
fix(oauth): recover missing client registrations
Waishnav 4087178
test(oauth): cover registration key resolution
Waishnav a3d6a2a
docs(oauth): explain client registration recovery
Waishnav b315386
fix(oauth): harden registration key derivation
Waishnav 1aaf51b
fix(oauth): enforce registration recovery boundaries
Waishnav e67e3c0
docs(oauth): clarify recovery guarantees
Waishnav 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
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
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
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
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,64 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { | ||
| createRecoverableClientId, | ||
| recoverClientRegistration, | ||
| } from "./oauth-client-registration.js"; | ||
|
|
||
| const signingKey = "test-client-registration-key-that-is-long-enough"; | ||
| const client = { | ||
| redirect_uris: ["https://chatgpt.com/connector/oauth/test"], | ||
| client_name: "ChatGPT", | ||
| client_id_issued_at: 1_786_032_000, | ||
| token_endpoint_auth_method: "none" as const, | ||
| grant_types: ["authorization_code", "refresh_token"], | ||
| response_types: ["code"], | ||
| }; | ||
|
|
||
| const created = createRecoverableClientId(client, signingKey); | ||
| assert.equal(created.kind, "recoverable"); | ||
| if (created.kind !== "recoverable") throw new Error("Expected recoverable client ID"); | ||
| const clientId = created.clientId; | ||
| assert.match(clientId, /^devspace-v1\./); | ||
|
|
||
| const recovered = recoverClientRegistration(clientId, signingKey); | ||
| assert.ok(recovered); | ||
| assert.equal(recovered.client_id, clientId); | ||
| assert.equal(recovered.client_name, "ChatGPT"); | ||
| assert.deepEqual(recovered.redirect_uris, client.redirect_uris); | ||
| assert.deepEqual(recovered.grant_types, client.grant_types); | ||
|
|
||
| const parts = clientId.split("."); | ||
| assert.equal(parts.length, 3); | ||
| assert.equal( | ||
| recoverClientRegistration(`${parts[0]}.${parts[1]}x.${parts[2]}`, signingKey), | ||
| undefined, | ||
| ); | ||
| assert.equal( | ||
| recoverClientRegistration(`${parts[0]}.${parts[1]}.${parts[2]}x`, signingKey), | ||
| undefined, | ||
| ); | ||
| assert.equal(recoverClientRegistration(clientId, `${signingKey}-wrong`), undefined); | ||
| assert.equal(recoverClientRegistration(`devspace-v1.${"x".repeat(5000)}.signature`, signingKey), undefined); | ||
| assert.equal( | ||
| recoverClientRegistration("devspace-0b3f9c1e-2d4a-4f77-9c0e-1a2b3c4d5e6f", signingKey), | ||
| undefined, | ||
| ); | ||
| assert.equal(recoverClientRegistration(`${clientId}.extra`, signingKey), undefined); | ||
|
|
||
| assert.deepEqual( | ||
| createRecoverableClientId( | ||
| { | ||
| ...client, | ||
| token_endpoint_auth_method: "client_secret_post", | ||
| client_secret: "must-not-be-embedded", | ||
| }, | ||
| signingKey, | ||
| ), | ||
| { kind: "unsupported" }, | ||
| ); | ||
|
|
||
| const oversized = createRecoverableClientId( | ||
| { ...client, client_name: "x".repeat(5000) }, | ||
| signingKey, | ||
| ); | ||
| assert.equal(oversized.kind, "too_large"); |
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,103 @@ | ||
| import { createHmac, timingSafeEqual } from "node:crypto"; | ||
| import { | ||
| OAuthClientInformationFullSchema, | ||
| type OAuthClientInformationFull, | ||
| } from "@modelcontextprotocol/sdk/shared/auth.js"; | ||
|
|
||
| const CLIENT_ID_PREFIX = "devspace-v1"; | ||
| const MAX_CLIENT_ID_LENGTH = 4096; | ||
|
|
||
| type ClientRegistrationPayload = Omit<OAuthClientInformationFull, "client_id">; | ||
|
|
||
| export type RecoverableClientIdResult = | ||
| | { | ||
| kind: "recoverable"; | ||
| clientId: string; | ||
| registration: ClientRegistrationPayload; | ||
| } | ||
| | { kind: "unsupported" } | ||
| | { kind: "too_large"; length: number; maxLength: number }; | ||
|
|
||
| export function createRecoverableClientId( | ||
| client: ClientRegistrationPayload, | ||
| signingKey: string, | ||
| ): RecoverableClientIdResult { | ||
| const parsed = OAuthClientInformationFullSchema.safeParse({ | ||
| ...client, | ||
| client_id: "pending", | ||
| }); | ||
| if (!parsed.success || !isPublicClient(parsed.data)) { | ||
| return { kind: "unsupported" }; | ||
| } | ||
|
|
||
| const { client_id: _clientId, ...validatedRegistration } = parsed.data; | ||
| const payload = Buffer.from(JSON.stringify(validatedRegistration)).toString("base64url"); | ||
| const signedValue = `${CLIENT_ID_PREFIX}.${payload}`; | ||
| const signature = sign(signedValue, signingKey); | ||
| const clientId = `${signedValue}.${signature}`; | ||
| if (clientId.length > MAX_CLIENT_ID_LENGTH) { | ||
| return { | ||
| kind: "too_large", | ||
| length: clientId.length, | ||
| maxLength: MAX_CLIENT_ID_LENGTH, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| kind: "recoverable", | ||
| clientId, | ||
| registration: validatedRegistration, | ||
| }; | ||
| } | ||
|
|
||
| export function recoverClientRegistration( | ||
| clientId: string, | ||
| signingKey: string, | ||
| ): OAuthClientInformationFull | undefined { | ||
| if (clientId.length > MAX_CLIENT_ID_LENGTH) return undefined; | ||
|
|
||
| const [prefix, payload, signature, extra] = clientId.split("."); | ||
| if (prefix !== CLIENT_ID_PREFIX || !payload || !signature || extra !== undefined) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const signedValue = `${prefix}.${payload}`; | ||
| if (!safeEquals(signature, sign(signedValue, signingKey))) return undefined; | ||
|
|
||
| let decoded: unknown; | ||
| try { | ||
| decoded = JSON.parse(Buffer.from(payload, "base64url").toString("utf8")) as unknown; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
|
|
||
| const parsed = OAuthClientInformationFullSchema.safeParse({ | ||
| ...(isRecord(decoded) ? decoded : {}), | ||
| client_id: clientId, | ||
| }); | ||
| if (!parsed.success || !isPublicClient(parsed.data)) return undefined; | ||
| return parsed.data; | ||
| } | ||
|
|
||
| function isPublicClient(client: OAuthClientInformationFull): boolean { | ||
| return ( | ||
| client.token_endpoint_auth_method === "none" && | ||
| client.client_secret === undefined && | ||
| client.client_secret_expires_at === undefined | ||
| ); | ||
| } | ||
|
|
||
| function sign(value: string, signingKey: string): string { | ||
| return createHmac("sha256", signingKey).update(value).digest("base64url"); | ||
| } | ||
|
|
||
| function safeEquals(left: string, right: string): boolean { | ||
| const leftBuffer = Buffer.from(left); | ||
| const rightBuffer = Buffer.from(right); | ||
| if (leftBuffer.byteLength !== rightBuffer.byteLength) return false; | ||
| return timingSafeEqual(leftBuffer, rightBuffer); | ||
| } | ||
|
|
||
| function isRecord(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === "object" && value !== null && !Array.isArray(value); | ||
| } |
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.
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.