|
| 1 | +export type ClientAccessMode = "off" | "audit" | "enforce"; |
| 2 | + |
| 3 | +export interface ClientAccessConfig { |
| 4 | + mode: ClientAccessMode; |
| 5 | + allowedClients: string[]; |
| 6 | + deniedClients: string[]; |
| 7 | +} |
| 8 | + |
| 9 | +export interface DeclaredClient { |
| 10 | + name: string; |
| 11 | + title?: string; |
| 12 | + version?: string; |
| 13 | + identities: string[]; |
| 14 | +} |
| 15 | + |
| 16 | +export interface ClientAccessDecision { |
| 17 | + allowed: boolean; |
| 18 | + enforced: boolean; |
| 19 | + reason: "disabled" | "allowed" | "denied_client" | "client_not_allowed"; |
| 20 | + matchedClient?: string; |
| 21 | +} |
| 22 | + |
| 23 | +export function extractDeclaredClient(body: unknown): DeclaredClient | undefined { |
| 24 | + if (!isRecord(body) || body.method !== "initialize") return undefined; |
| 25 | + const params = body.params; |
| 26 | + if (!isRecord(params)) return undefined; |
| 27 | + const clientInfo = params.clientInfo; |
| 28 | + if (!isRecord(clientInfo)) return undefined; |
| 29 | + |
| 30 | + const name = sanitizeText(clientInfo.name); |
| 31 | + if (!name) return undefined; |
| 32 | + const title = sanitizeText(clientInfo.title); |
| 33 | + const version = sanitizeText(clientInfo.version); |
| 34 | + const identities = clientIdentities(name, title); |
| 35 | + |
| 36 | + return { |
| 37 | + name, |
| 38 | + ...(title ? { title } : {}), |
| 39 | + ...(version ? { version } : {}), |
| 40 | + identities, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +export function evaluateClientAccess( |
| 45 | + config: ClientAccessConfig, |
| 46 | + client: DeclaredClient | undefined, |
| 47 | +): ClientAccessDecision { |
| 48 | + if (config.mode === "off") { |
| 49 | + return { allowed: true, enforced: false, reason: "disabled" }; |
| 50 | + } |
| 51 | + |
| 52 | + const identities = new Set(client?.identities ?? []); |
| 53 | + const deniedClient = normalizedPolicyEntries(config.deniedClients).find((entry) => |
| 54 | + identities.has(entry), |
| 55 | + ); |
| 56 | + if (deniedClient) { |
| 57 | + return { |
| 58 | + allowed: config.mode !== "enforce", |
| 59 | + enforced: config.mode === "enforce", |
| 60 | + reason: "denied_client", |
| 61 | + matchedClient: deniedClient, |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + const allowedClients = normalizedPolicyEntries(config.allowedClients); |
| 66 | + if (allowedClients.length > 0 && !allowedClients.some((entry) => identities.has(entry))) { |
| 67 | + return { |
| 68 | + allowed: config.mode !== "enforce", |
| 69 | + enforced: config.mode === "enforce", |
| 70 | + reason: "client_not_allowed", |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + return { |
| 75 | + allowed: true, |
| 76 | + enforced: config.mode === "enforce", |
| 77 | + reason: "allowed", |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +function clientIdentities(name: string, title?: string): string[] { |
| 82 | + const identities = new Set<string>(); |
| 83 | + const normalizedName = normalizeClientName(name); |
| 84 | + if (normalizedName) identities.add(normalizedName); |
| 85 | + |
| 86 | + const combined = `${name} ${title ?? ""}`.toLowerCase(); |
| 87 | + if (/\bcodex\b/.test(combined) || normalizedName.includes("codex")) identities.add("codex"); |
| 88 | + if (combined.includes("chatgpt") || normalizedName.includes("chatgpt")) identities.add("chatgpt"); |
| 89 | + |
| 90 | + return [...identities]; |
| 91 | +} |
| 92 | + |
| 93 | +function normalizedPolicyEntries(entries: string[]): string[] { |
| 94 | + return [...new Set(entries.map(normalizeClientName).filter(Boolean))]; |
| 95 | +} |
| 96 | + |
| 97 | +function normalizeClientName(value: string): string { |
| 98 | + return value |
| 99 | + .trim() |
| 100 | + .toLowerCase() |
| 101 | + .replace(/[^a-z0-9]+/g, "-") |
| 102 | + .replace(/^-+|-+$/g, ""); |
| 103 | +} |
| 104 | + |
| 105 | +function sanitizeText(value: unknown): string | undefined { |
| 106 | + if (typeof value !== "string") return undefined; |
| 107 | + const normalized = value.replace(/[\r\n\t]+/g, " ").trim(); |
| 108 | + if (!normalized) return undefined; |
| 109 | + return normalized.slice(0, 160); |
| 110 | +} |
| 111 | + |
| 112 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 113 | + return typeof value === "object" && value !== null && !Array.isArray(value); |
| 114 | +} |
0 commit comments