|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import type { RouteUserContext } from './ai-routes.js'; |
| 4 | + |
| 5 | +/** |
| 6 | + * The subset of an agent definition relevant to "who can chat with it". |
| 7 | + * Kept structural (not the full Agent type) so the check is decoupled from the |
| 8 | + * runtime's loaded-agent shape and trivially unit-testable. |
| 9 | + */ |
| 10 | +export interface AgentAccessSpec { |
| 11 | + /** Allow-list of user IDs or role names permitted to chat. */ |
| 12 | + access?: string[]; |
| 13 | + /** Permissions or roles the caller must ALL hold to use the agent. */ |
| 14 | + permissions?: string[]; |
| 15 | + /** Declared scope. Only `private` is gate-able at the route layer today. */ |
| 16 | + visibility?: 'global' | 'organization' | 'private'; |
| 17 | +} |
| 18 | + |
| 19 | +export interface AgentAccessDecision { |
| 20 | + allowed: boolean; |
| 21 | + /** Human-readable reason when denied (safe to surface in a 403). */ |
| 22 | + reason?: string; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Enforce per-agent access control (ADR-0049, #1884). |
| 27 | + * |
| 28 | + * Before this check the chat route only enforced the coarse, static route-level |
| 29 | + * permissions (`['ai:chat','ai:agents']`) — every agent's `access`/`permissions` |
| 30 | + * were a no-op, so "who can chat with this agent" enforced nothing. This closes |
| 31 | + * that gap for the two fields that concretely express it: |
| 32 | + * |
| 33 | + * - `permissions` (required): the caller must hold EVERY listed entry. An |
| 34 | + * entry is satisfied if it appears in the caller's `permissions` OR `roles` |
| 35 | + * (the spec field is "Required permissions or roles"). Empty → no extra |
| 36 | + * requirement beyond the route-level gate. |
| 37 | + * - `access` (allow-list): when present and non-empty, the caller must match |
| 38 | + * at least one entry, by `userId` or by membership in `roles`. Empty/absent |
| 39 | + * → not restricted by allow-list. |
| 40 | + * |
| 41 | + * `visibility` (`organization`/`private`) is intentionally NOT enforced here: |
| 42 | + * the request context carries no tenant id or agent-owner, so a correct |
| 43 | + * organization/private gate needs auth-middleware changes (tracked separately). |
| 44 | + * Enforcing a partial/guessed version would risk both lock-out and false |
| 45 | + * security, so we enforce only what the context can decide. |
| 46 | + * |
| 47 | + * Fails CLOSED on a malformed user (no userId) — an unauthenticated caller that |
| 48 | + * slipped past the route gate is denied rather than defaulted-open. |
| 49 | + */ |
| 50 | +export function evaluateAgentAccess( |
| 51 | + agent: AgentAccessSpec, |
| 52 | + user: RouteUserContext | undefined, |
| 53 | +): AgentAccessDecision { |
| 54 | + const required = agent.permissions ?? []; |
| 55 | + const allowList = agent.access ?? []; |
| 56 | + |
| 57 | + // No per-agent restriction declared → allowed (route-level gate already passed). |
| 58 | + if (required.length === 0 && allowList.length === 0) { |
| 59 | + return { allowed: true }; |
| 60 | + } |
| 61 | + |
| 62 | + if (!user || !user.userId) { |
| 63 | + return { allowed: false, reason: 'authentication required to chat with this agent' }; |
| 64 | + } |
| 65 | + |
| 66 | + const held = new Set<string>([...(user.permissions ?? []), ...(user.roles ?? [])]); |
| 67 | + |
| 68 | + // Required permissions: caller must hold ALL. |
| 69 | + const missing = required.filter((p) => !held.has(p)); |
| 70 | + if (missing.length > 0) { |
| 71 | + return { |
| 72 | + allowed: false, |
| 73 | + reason: `missing required permission(s): ${missing.join(', ')}`, |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + // Allow-list: caller must match by userId or role. |
| 78 | + if (allowList.length > 0) { |
| 79 | + const roles = new Set<string>(user.roles ?? []); |
| 80 | + const matched = allowList.some((entry) => entry === user.userId || roles.has(entry)); |
| 81 | + if (!matched) { |
| 82 | + return { allowed: false, reason: 'not in this agent’s access list' }; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return { allowed: true }; |
| 87 | +} |
0 commit comments