-
-
Notifications
You must be signed in to change notification settings - Fork 345
feat: add MCP client access policy #84
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
Zhenyu98
wants to merge
1
commit into
Waishnav:main
Choose a base branch
from
Zhenyu98:security/chatgpt-client-access-policy
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { once } from "node:events"; | ||
| import type { AddressInfo } from "node:net"; | ||
| import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js"; | ||
| import express from "express"; | ||
| import type { ClientAccessConfig } from "./client-access.js"; | ||
| import { handleClientAccessInitialize } from "./server.js"; | ||
|
|
||
| const config: ClientAccessConfig = { | ||
| mode: "enforce", | ||
| deniedClients: ["codex"], | ||
| }; | ||
|
|
||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| let continuedRequests = 0; | ||
| app.post("/mcp", (req, res) => { | ||
| assert.equal(isInitializeRequest(req.body), true); | ||
| const { clientAccess } = handleClientAccessInitialize(res, req.body, config); | ||
| if (!clientAccess.allowed) return; | ||
|
|
||
| continuedRequests += 1; | ||
| res.sendStatus(204); | ||
| }); | ||
|
|
||
| const server = app.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
|
|
||
| try { | ||
| const address = server.address() as AddressInfo; | ||
| const endpoint = `http://127.0.0.1:${address.port}/mcp`; | ||
|
|
||
| const deniedResponse = await fetch(endpoint, { | ||
| method: "POST", | ||
| headers: { "content-type": "application/json", connection: "close" }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: "2.0", | ||
| id: 0, | ||
| method: "initialize", | ||
| params: { | ||
| protocolVersion: "2025-03-26", | ||
| capabilities: {}, | ||
| clientInfo: { name: "codex-mcp-client", version: "0.145.0" }, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| assert.equal(deniedResponse.status, 403); | ||
| assert.deepEqual(await deniedResponse.json(), { | ||
| jsonrpc: "2.0", | ||
| error: { code: -32003, message: "MCP client not allowed" }, | ||
| id: 0, | ||
| }); | ||
| assert.equal(continuedRequests, 0); | ||
|
|
||
| const allowedResponse = await fetch(endpoint, { | ||
| method: "POST", | ||
| headers: { "content-type": "application/json", connection: "close" }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: "2.0", | ||
| id: 1, | ||
| method: "initialize", | ||
| params: { | ||
| protocolVersion: "2025-03-26", | ||
| capabilities: {}, | ||
| clientInfo: { name: "", version: "" }, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| assert.equal(allowedResponse.status, 204); | ||
| assert.equal(continuedRequests, 1); | ||
| } finally { | ||
| await new Promise<void>((resolve, reject) => { | ||
| server.close((error) => { | ||
| if (error) reject(error); | ||
| else resolve(); | ||
| }); | ||
| }); | ||
| } |
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,60 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { | ||
| evaluateClientAccess, | ||
| extractDeclaredClient, | ||
| type ClientAccessConfig, | ||
| } from "./client-access.js"; | ||
|
|
||
| const enforceCodexDenylist: ClientAccessConfig = { | ||
| mode: "enforce", | ||
| deniedClients: ["codex"], | ||
| }; | ||
|
|
||
| assert.deepEqual( | ||
| extractDeclaredClient({ | ||
| jsonrpc: "2.0", | ||
| id: 0, | ||
| method: "initialize", | ||
| params: { | ||
| clientInfo: { | ||
| name: "codex-mcp-client", | ||
| title: "Codex", | ||
| version: "0.108.0-alpha.12", | ||
| }, | ||
| }, | ||
| }), | ||
| { | ||
| name: "codex-mcp-client", | ||
| title: "Codex", | ||
| version: "0.108.0-alpha.12", | ||
| identities: ["codex-mcp-client", "codex"], | ||
| }, | ||
| ); | ||
|
|
||
| assert.deepEqual( | ||
| evaluateClientAccess(enforceCodexDenylist, { | ||
| name: "codex-mcp-client", | ||
| title: "Codex", | ||
| version: "0.145.0", | ||
| identities: ["codex-mcp-client", "codex"], | ||
| }), | ||
| { | ||
| allowed: false, | ||
| reason: "denied_client", | ||
| matchedClient: "codex", | ||
| }, | ||
| ); | ||
|
|
||
| assert.deepEqual( | ||
| evaluateClientAccess(enforceCodexDenylist, undefined), | ||
| { | ||
| allowed: true, | ||
| reason: "allowed", | ||
| }, | ||
| ); | ||
|
|
||
| assert.equal(extractDeclaredClient({ method: "tools/list", params: {} }), undefined); | ||
| assert.equal( | ||
| extractDeclaredClient({ method: "initialize", params: { clientInfo: { name: "" } } }), | ||
| undefined, | ||
| ); |
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,109 @@ | ||
| export type ClientAccessMode = "off" | "enforce"; | ||
|
|
||
| export interface ClientAccessConfig { | ||
| mode: ClientAccessMode; | ||
| deniedClients: string[]; | ||
| } | ||
|
|
||
| export interface DeclaredClient { | ||
| name: string; | ||
| title?: string; | ||
| version?: string; | ||
| identities: string[]; | ||
| } | ||
|
|
||
| export interface ClientAccessDecision { | ||
| allowed: boolean; | ||
| reason: "disabled" | "allowed" | "denied_client"; | ||
| matchedClient?: string; | ||
| } | ||
|
|
||
| export type JsonRpcId = string | number | null; | ||
|
|
||
| export function extractDeclaredClient(body: unknown): DeclaredClient | undefined { | ||
| if (!isRecord(body) || body.method !== "initialize") return undefined; | ||
| const params = body.params; | ||
| if (!isRecord(params)) return undefined; | ||
| const clientInfo = params.clientInfo; | ||
| if (!isRecord(clientInfo)) return undefined; | ||
|
|
||
| const name = sanitizeText(clientInfo.name); | ||
| if (!name) return undefined; | ||
| const title = sanitizeText(clientInfo.title); | ||
| const version = sanitizeText(clientInfo.version); | ||
| const identities = clientIdentities(name, title); | ||
|
|
||
| return { | ||
| name, | ||
| ...(title ? { title } : {}), | ||
| ...(version ? { version } : {}), | ||
| identities, | ||
| }; | ||
| } | ||
|
|
||
| export function extractJsonRpcId(body: unknown): JsonRpcId { | ||
| if (!isRecord(body)) return null; | ||
| const id = body.id; | ||
| return typeof id === "string" || typeof id === "number" ? id : null; | ||
| } | ||
|
|
||
| export function evaluateClientAccess( | ||
| config: ClientAccessConfig, | ||
| client: DeclaredClient | undefined, | ||
| ): ClientAccessDecision { | ||
| if (config.mode === "off") { | ||
| return { allowed: true, reason: "disabled" }; | ||
| } | ||
|
|
||
| const identities = new Set(client?.identities ?? []); | ||
| const deniedClient = normalizedPolicyEntries(config.deniedClients).find((entry) => | ||
| identities.has(entry), | ||
| ); | ||
| if (deniedClient) { | ||
| return { | ||
| allowed: false, | ||
| reason: "denied_client", | ||
| matchedClient: deniedClient, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| allowed: true, | ||
| reason: "allowed", | ||
| }; | ||
| } | ||
|
|
||
| function clientIdentities(name: string, title?: string): string[] { | ||
| const identities = new Set<string>(); | ||
| const normalizedName = normalizeClientName(name); | ||
| if (normalizedName) identities.add(normalizedName); | ||
|
|
||
| const combined = `${name} ${title ?? ""}`.toLowerCase(); | ||
| if (/\bcodex\b/.test(combined) || normalizedName.includes("codex")) identities.add("codex"); | ||
| if (combined.includes("chatgpt") || normalizedName.includes("chatgpt")) identities.add("chatgpt"); | ||
|
|
||
| return [...identities]; | ||
| } | ||
|
|
||
| function normalizedPolicyEntries(entries: string[]): string[] { | ||
| return [...new Set(entries.map(normalizeClientName).filter(Boolean))]; | ||
| } | ||
|
|
||
| function normalizeClientName(value: string): string { | ||
| return value | ||
| .trim() | ||
| .toLowerCase() | ||
| .replace(/[^a-z0-9]+/g, "-") | ||
| .replace(/^-+|-+$/g, ""); | ||
| } | ||
|
|
||
| function sanitizeText(value: unknown): string | undefined { | ||
| if (typeof value !== "string") return undefined; | ||
| const normalized = value.replace(/[\r\n\t]+/g, " ").trim(); | ||
| if (!normalized) return undefined; | ||
| return normalized.slice(0, 160); | ||
| } | ||
|
|
||
| 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
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.