Files
PaperClipAI/server/src/routes/connection-management-authz.ts
T
e2cee049f9 fix: show instruction saves in history, credit the real writer, and speed up the agent tool-rule list (#19515)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Operators and agents change agent instructions (AGENTS.md), and they
must see who changed them and when
> - Agent files replaced instruction revisions. Saves no longer write
revision rows, so instruction history is empty
> - The current revision is stamped with the reader and the date
1970-01-01, so it gives false audit data
> - Separately, an agent that lists tool rules gets a timeout when many
rules name the same resources
> - This pull request reads history from the save records that Paperclip
already writes, credits the real writer, and checks each tool-rule
resource one time
> - The benefit is a correct audit trail for instruction changes and a
tool-rule list that agents can read

## Linked Issues or Issue Description

No public issue exists. I searched open and closed issues and PRs for
these problems and found none.

**What happened?**
1. `get_agent_instruction_history` (and the history API) returns 0
revisions for an agent whose AGENTS.md was saved many times. The
activity log shows each save as `agent.files_updated` with the path,
content hash, actor and run.
2. `read_agent_instructions` returns a current revision with `createdAt`
1970-01-01. Its actor is the agent that reads it, and its `sourceRunId`
is the run that reads it.
3. `GET /api/companies/{companyId}/tools/policies` from an agent fails
with a transport timeout most of the time when the company has many tool
rules. A board user gets the list without a problem.

**Expected behavior**
1. History lists each save, newest first, with its writer, run, source
and time.
2. The current revision names the agent or user that wrote those bytes,
and the time of that save. If no save is recorded, it names no writer.
3. The agent gets the tool-rule list in normal time, with the same rules
hidden as before.

**Steps to reproduce**
1. Save an agent's instruction entry file (board editor,
`update_agent_instructions`, or a run that edits the file). Read the
history. It is empty.
2. As a different agent, call `read_agent_instructions` on that agent.
The revision shows the reader and 1970-01-01.
3. Create about 40 tool rules that name the same connection and agent.
As an agent, list `/tools/policies`. Each rule does a new set of access
checks for each resource, one after another.

**Paperclip version or commit**
`master` at `819420a60a`.

**Deployment mode**
Hosted. The new tests reproduce all 3 problems on `master`.

## What Changed

- `agent-instruction-revisions.ts`: history merges the legacy revision
rows with the `agent.files_updated` save records for the entry file. The
order is `(created_at, id)` descending, and cursors keep PostgreSQL
timestamp precision.
- `agent-instruction-revisions.ts`: the current revision takes its
writer, run, source and time from the latest save of the same content
hash. A reader is never the writer. Bytes with no save record have no
writer.
- `agent-instruction-revisions.ts`: an identical re-save over bytes with
no record is marked `source: "adopted"`. It repairs the runtime journal
but is not listed as a save.
- `agent-instruction-revisions.ts`: a history ID can be a legacy
revision, a save record, or the current content token. A save record
keeps only metadata. If its bytes are no longer current, a read returns
404 with `INSTRUCTION_REVISION_CONTENT_NOT_RETAINED`.
- `agent-file-store.ts`: saves record `byteLength`. A run collection
that changes the entry file records its path, hash and size, so history
shows saves from runs.
- `connection-management-authz.ts`: `filterConnectionManagementPolicies`
keeps one read decision for each distinct resource in a request, and
loads each application or catalog entry one time. A rule is still hidden
when any resource that it names is hidden. Board callers do not use this
path.
- Tests: new tests for save history, paging, writer credit, adoption,
and the tool-rule list. Old assertions that expected empty history now
check the exact saves.

## Verification

- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/agent-instruction-revisions.test.ts
src/__tests__/agent-instruction-working-copies.test.ts
src/__tests__/agent-directory-working-copies.test.ts
src/__tests__/built-in-agents.test.ts
src/services/native-runtime/agent-instruction-tools.integration.test.ts
src/__tests__/tool-policy-list-authz.integration.test.ts`
- With the fix: all pass (revisions: 28 passed, 1 skipped on Linux;
other 5 files: 230 passed).
  - With the new tests on `master` code: 13 tests fail, as expected.
- `src/__tests__/connection-management.integration.test.ts`: 90 passed.
- The new tool-rule test lists 40 rules that name 1 connection and
counts the connection reads. It needs fewer than 10.
- I could not run the full server typecheck in my sandbox because of a
memory limit. CI runs it.
- CI on head `a2f0b825`: all checks pass, including `ci / verify` and
`ci / e2e`.

## Risks

- History now shows entries whose ID is an activity row ID, not a
revision row ID. Clients that read or restore an older save get a clear
404 because Paperclip no longer keeps those bytes. The latest save and
legacy revisions still read and restore as before.
- Saves recorded before this change have no `byteLength`. For those,
history shows the current size when the hash matches, and 0 otherwise.
- Stock updates of built-in agents do not record a file path, so they do
not show in history.
- No migration. No change to the stored data model.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Anthropic Claude Opus 5.5 (`claude-opus-5-5`), in Claude Code with
tool use (shell, file edits, test runs). Standard context window.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: lucktastic <13757243+lucktastic@users.noreply.github.com>
2026-11-18 16:41:53 -08:00

484 lines
37 KiB
TypeScript

import { isUuidLike, type ConnectionRequiredUserAction } from "@paperclipai/shared";
import type { Request, RequestHandler } from "express";
import { and, eq, or, sql } from "drizzle-orm";
import { type Db, toolConnections, toolApplications, toolProfiles, toolProfileEntries, toolProfileBindings,
toolMcpGateways, toolMcpGatewayTokens, chatEndpoints, chatConversations, connectionGrants, connectionGrantDelegations, agents, issues,
toolConnectionInstalls, heartbeatRuns, toolPolicies, toolRuntimeSlots, toolCatalogEntries, chatSlackManagerGrants, adapterAuthSessions, companySecrets, companies, companyMemberships } from "@paperclipai/db";
import { badRequest, forbidden, notFound, HttpError } from "../errors.js";
import { assertAuthenticated, assertCompanyAccess, hasCompanyAccess, getActorInfo } from "./authz.js";
import { connectionManagementService, responsibleConnectionUser, type ConnectionManagementContext } from "../services/connection-management.js";
import type { ConnectionManagementAction, ConnectionManagementResource } from "../services/connection-management-policy.js";
const contexts = new WeakMap<Request, ConnectionManagementContext>();
export function connectionRequestContext(req: Request) { return contexts.get(req); }
export function connectionRequestUserId(req: Request) { return responsibleConnectionUser(req.actor); }
export function connectionRequestActor(req: Request) {
return { ...getActorInfo(req), ...contexts.get(req)?.audit, responsibleUserId: connectionRequestUserId(req), managementContext: contexts.get(req) };
}
export function requiredConnectionUserAction(
req: Request,
resource: ConnectionRequiredUserAction["resource"],
setupPath: string,
reason: string,
kind: ConnectionRequiredUserAction["kind"] = "authenticate",
): { requiredUserAction?: ConnectionRequiredUserAction } {
if (req.actor.type !== "agent") return {};
const userId = connectionRequestUserId(req);
if (!userId) throw forbidden("A responsible user is required for authentication");
return { requiredUserAction: { kind, userId, resource, setupPath, reason,
continuation: "Inspect this saved setup after the addressed user completes the required step." } };
}
function assertBoundedManagementSelectors(body: Record<string, unknown>) {
if (!body || typeof body !== "object" || Array.isArray(body)) throw badRequest("Resource selectors must be objects");
const arrays = ["policyIds", "credentialRefs", "credentialSecretRefs", "installs", "bindings", "entries", "agentIds"];
const identifiers = ["connectionId", "profileId", "gatewayId", "applicationId", "catalogEntryId", "projectId", "issueId", "routineId", "agentId", "targetAgentId", "subjectAgentId", "assignedAgentId", "secretId", "targetId"];
const pending: Record<string, unknown>[] = [body];
const unique = new Set<string>();
let references = 0;
for (let index = 0; index < pending.length; index++) {
const selected = pending[index];
for (const key of identifiers) {
const value = selected[key];
if (typeof value === "string") unique.add(`${key}:${value}`);
}
const keys = new Set([...arrays, ...identifiers.map(key => `${key}s`)]);
for (const key of keys) {
const values = selected[key];
if (values === undefined || values === null) continue;
if (!Array.isArray(values)) throw badRequest(`${key} must be an array`);
references += values.length;
if (references > 2048) throw badRequest("Connection management requests support at most 2048 resource references");
for (const value of values) {
if (typeof value === "string") unique.add(`${key}:${value}`);
else if (value && typeof value === "object" && !Array.isArray(value)) pending.push(value as Record<string, unknown>);
}
}
for (const key of ["selectors", "access", "binding"]) {
const value = selected[key];
if (value && typeof value === "object" && !Array.isArray(value)) pending.push(value as Record<string, unknown>);
}
if (selected === body && body.config && typeof body.config === "object" && !Array.isArray(body.config)) {
const members = (body.config as Record<string, unknown>).members;
if (members !== undefined) {
if (!Array.isArray(members)) throw badRequest("Pool members must be an array");
references += members.length;
if (references > 2048) throw badRequest("Connection management requests support at most 2048 resource references");
for (const member of members) if (member && typeof member === "object" && !Array.isArray(member)) pending.push(member as Record<string, unknown>);
}
}
if (unique.size > 1024 || pending.length > 2049) throw badRequest("Connection management requests support at most 1024 unique resource selectors");
}
}
/** Each REST registration declares its operation; this adapter only resolves resources. */
export function connectionManagementGuard(db: Db, purpose: "tool" | "chat" | "ai") {
const management = connectionManagementService(db);
return (action: ConnectionManagementAction, operation: string, options: { personal?: boolean; slackManagerGrant?: boolean } = {}): RequestHandler => async (req, _res, next) => {
const rawBody = req.body ?? {};
const authorizeRequest = async (afterHumanAction?: boolean): Promise<ConnectionManagementContext> => {
const authorize: typeof management.authorize = (actor, requestedAction, resolved, checkOptions) =>
management.authorize(actor, requestedAction, resolved, { ...checkOptions, afterHumanAction });
assertAuthenticated(req);
if (req.actor.type === "agent" && !connectionRequestUserId(req)) throw forbidden("Connection management requires a responsible user");
const inaccessibleMessage = operation.includes("setup-token-login-sessions") ? "Setup-token login session not found." : "Connection management resource not found";
if (req.actor.type === "board" && req.params.companyId && !hasCompanyAccess(req, String(req.params.companyId))) {
if (purpose === "chat" || operation.includes("setup-token-login-sessions")) throw notFound(inaccessibleMessage);
assertCompanyAccess(req, String(req.params.companyId));
}
const body = purpose === "ai" && rawBody.aiConnection ? { ...rawBody, ...rawBody.aiConnection } : rawBody;
assertBoundedManagementSelectors(body);
const userId = connectionRequestUserId(req);
if (req.actor.type === "agent") {
for (const key of ["responsibleUserId", "onBehalfOfUserId", "createdByUserId", "createdByAgentId"]) {
if (Object.hasOwn(body, key)) throw forbidden("Management identity is derived from authentication");
}
for (const key of ["ownerUserId", "subjectUserId"]) {
if (body[key] != null && body[key] !== userId) throw forbidden("Personal credentials must belong to the responsible user");
}
}
let applicationProvider: string | null = null;
let companyId = typeof req.params.companyId === "string" ? req.params.companyId : null;
const earlyCompanyId = companyId ?? req.actor.companyId;
if (earlyCompanyId) {
assertCompanyAccess(req, earlyCompanyId);
if (req.actor.type === "agent") {
const memberships = await db.select({ principalType: companyMemberships.principalType, principalId: companyMemberships.principalId }).from(companyMemberships).where(and(
eq(companyMemberships.companyId, earlyCompanyId), eq(companyMemberships.status, "active"),
or(and(eq(companyMemberships.principalType, "agent"), eq(companyMemberships.principalId, req.actor.agentId!)),
and(eq(companyMemberships.principalType, "user"), eq(companyMemberships.principalId, userId!))),
));
if (!memberships.some(member => member.principalType === "agent") || !memberships.some(member => member.principalType === "user")) throw forbidden("Connection management requires active agent and responsible-user memberships");
}
}
const resource: ConnectionManagementResource = { type: "connection_management", companyId: companyId ?? "", purpose, operation,
readOnly: req.method === "GET" && action !== "connections:gateway" && (action !== "connections:audit" || purpose === "chat") };
const targets = new Set<string>();
const extraResources: ConnectionManagementResource[] = [];
const bindingTargets: { targetType: string; targetId: string }[] = [];
const selectors: Record<string, unknown>[] = [];
const policyAudiences: Record<string, unknown>[] = [];
if (Object.hasOwn(body, "selectors")) policyAudiences.push(body.selectors ?? {});
const selectorKeys = ["connectionId", "profileId", "gatewayId", "applicationId", "catalogEntryId", "projectId", "issueId", "routineId", "agentId", "targetAgentId", "targetType", "targetId", "agentIds", "connectionIds", "profileIds", "gatewayIds", "applicationIds", "catalogEntryIds", "projectIds", "issueIds", "routineId", "routineIds", "selectorType", "toolName", "riskLevel", "defaultAction"];
const seenSelectors = new Set<string>();
const addSelector = (selected: Record<string, unknown>) => {
assertBoundedManagementSelectors(selected);
const key = JSON.stringify(selectorKeys.map(name => selected[name]));
if (seenSelectors.has(key)) return;
if (selectors.length >= 1024) throw badRequest("Connection management request spans too many resources");
seenSelectors.add(key); selectors.push(selected);
};
const addSelectors = (selected: Record<string, unknown>[]) => selected.forEach(addSelector);
addSelectors([body, body.access && typeof body.access === "object" ? body.access : {}, body.selectors ?? {}, ...(Array.isArray(body.entries) ? body.entries : [])]);
const lookupCache = new Map<string, Promise<unknown>>();
const lookup = <T,>(key: string, read: () => PromiseLike<T>): Promise<T> => {
let pending = lookupCache.get(key);
if (!pending) { pending = Promise.resolve(read()); lookupCache.set(key, pending); }
return pending as Promise<T>;
};
const bindCompany = (row: { companyId: string } | undefined) => {
if (!row || (req.actor.type === "agent" && row.companyId !== req.actor.companyId) || (req.actor.type === "board" && !hasCompanyAccess(req, row.companyId))) throw notFound(inaccessibleMessage);
if (companyId && companyId !== row.companyId) throw forbidden("Resource is outside this company");
companyId = row.companyId;
};
const uuid = (value: unknown) => { if (typeof value !== "string" || !isUuidLike(value)) throw badRequest("Expected a resource identifier"); return value; };
const text = (value: unknown) => typeof value === "string" && value ? value : null;
let connectionId = text(req.params.connectionId) ?? text(req.query.connectionId) ?? text(body.resumeConnectionId) ?? text(body.reconnectConnectionId) ?? text(body.connectionId) ?? text(req.params.poolId) ?? (operation.includes("ai-connection-pools") ? text(body.id) : null);
let profileId = text(req.params.profileId) ?? text(req.query.profileId) ?? text(body.profileId);
let gatewayId = text(req.params.gatewayId) ?? text(req.query.gatewayId) ?? text(req.query.gateway);
for (const id of new Set(Array.isArray(body.policyIds) ? body.policyIds : [])) {
const [policy] = await db.select().from(toolPolicies).where(eq(toolPolicies.id, uuid(id)));
if (policy && companyId && policy.companyId !== companyId && req.actor.type === "board") throw new HttpError(422, "Policies must belong to this company");
bindCompany(policy); addSelector(policy.selectors); policyAudiences.push(policy.selectors);
}
if (req.params.policyId) {
const [policy] = await db.select().from(toolPolicies).where(eq(toolPolicies.id, uuid(req.params.policyId)));
bindCompany(policy); addSelector(policy.selectors); policyAudiences.push(policy.selectors);
}
if (action === "connections:runtime" && (req.params.slotId || req.params.id)) {
const [slot] = await db.select().from(toolRuntimeSlots).where(eq(toolRuntimeSlots.id, uuid(req.params.slotId ?? req.params.id)));
bindCompany(slot); connectionId = slot.connectionId; resource.issueId = slot.issueId;
}
if (purpose === "ai" && req.params.sessionId && (req.actor.type === "agent" || operation.includes("/local/attempts/"))) {
const [session] = await db.select().from(adapterAuthSessions).where(or(eq(adapterAuthSessions.publicSessionId, String(req.params.sessionId)), ...(isUuidLike(String(req.params.sessionId)) ? [eq(adapterAuthSessions.id, String(req.params.sessionId))] : [])));
if (!session || session.startedByUserId !== userId) throw notFound("Sign-in attempt not found");
bindCompany(session); resource.selfServiceUserId = session.startedByUserId;
}
for (const ref of [...(Array.isArray(body.credentialRefs) ? body.credentialRefs : []), ...(Array.isArray(body.credentialSecretRefs) ? body.credentialSecretRefs : [])]) {
if (!ref.secretId) continue;
const [secret] = await lookup(`secret:${String(ref.secretId)}`, () => db.select().from(companySecrets).where(eq(companySecrets.id, uuid(ref.secretId))));
bindCompany(secret);
if (secret.deletedAt || secret.status !== "active" || (secret.scope === "user" && secret.ownerUserId !== userId)) throw forbidden("Credential binding is unavailable to the responsible user");
}
if (options.slackManagerGrant) {
const [grant] = await db.select().from(chatSlackManagerGrants).where(eq(chatSlackManagerGrants.id, uuid(req.params.grantId ?? body.grantId)));
bindCompany(grant); resource.provider = "slack";
if (grant.userId !== userId) throw forbidden("Manager consent belongs to another user");
}
if (!options.slackManagerGrant && (req.params.grantId || body.grantId)) {
const [grant] = await db.select().from(connectionGrants).where(eq(connectionGrants.id, uuid(req.params.grantId ?? body.grantId)));
bindCompany(grant);
if (connectionId && connectionId !== grant.connectionId) throw forbidden("Grant does not belong to this connection");
connectionId = grant.connectionId;
if (grant.subjectAgentId) targets.add(grant.subjectAgentId);
if (req.actor.type === "agent" && grant.kind === "user" && grant.subjectUserId !== userId) throw forbidden("Personal account belongs to another user");
}
if (req.params.delegationId) {
const [delegation] = await db.select().from(connectionGrantDelegations).where(eq(connectionGrantDelegations.id, uuid(req.params.delegationId)));
bindCompany(delegation);
if (delegation.grantId !== req.params.grantId) throw notFound("Connection grant delegation not found");
targets.add(delegation.agentId);
}
if (req.params.issueId) {
const [issue] = await db.select().from(issues).where(eq(issues.id, uuid(req.params.issueId)));
bindCompany(issue); resource.issueId = issue.id; resource.projectId = issue.projectId;
}
if (req.params.runId) {
const [run] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, uuid(req.params.runId)));
bindCompany(run);
resource.issueId = run.issueId ?? (typeof run.contextSnapshot?.issueId === "string" ? run.contextSnapshot.issueId : null);
}
if (req.params.conversationId) {
const [conversation] = await db.select().from(chatConversations).where(eq(chatConversations.id, uuid(req.params.conversationId)));
bindCompany(conversation);
if (req.params.endpointId && conversation.endpointId !== req.params.endpointId) throw notFound("Conversation not found");
resource.issueId = conversation.issueId;
}
if (body.reassignToProfileId) extraResources.push({ ...resource, profileId: uuid(body.reassignToProfileId) });
if (req.params.entryId) {
const [entry] = await db.select().from(toolProfileEntries).where(eq(toolProfileEntries.id, uuid(req.params.entryId)));
bindCompany(entry); profileId = entry.profileId; connectionId ??= entry.connectionId;
addSelector({ ...entry, ...body });
}
if (req.params.tokenId) {
const [token] = await db.select().from(toolMcpGatewayTokens).where(eq(toolMcpGatewayTokens.id, uuid(req.params.tokenId)));
bindCompany(token); gatewayId = token.gatewayId;
}
if (gatewayId) {
const [gateway] = await db.select().from(toolMcpGateways).where(eq(toolMcpGateways.id, uuid(gatewayId)));
bindCompany(gateway); profileId ??= gateway.profileId;
resource.gatewayId = gateway.id;
resource.projectId = Object.hasOwn(body, "projectId") ? text(body.projectId) : gateway.projectId;
resource.issueId = Object.hasOwn(body, "issueId") ? text(body.issueId) : gateway.issueId;
if (gateway.agentId) targets.add(gateway.agentId);
}
if (req.params.applicationId || body.applicationId) {
const [application] = await db.select().from(toolApplications).where(eq(toolApplications.id, uuid(req.params.applicationId ?? body.applicationId)));
bindCompany(application);
applicationProvider = text(application.metadata.sourceTemplateKey);
resource.provider ??= applicationProvider;
if (req.method !== "GET") {
if (application.ownerAgentId) targets.add(application.ownerAgentId);
const connections = await db.select({ id: toolConnections.id }).from(toolConnections).where(eq(toolConnections.applicationId, application.id)).limit(1025);
connections.forEach(connection => extraResources.push({ ...resource, connectionId: connection.id }));
}
}
if (req.params.endpointId) {
const [endpoint] = await db.select().from(chatEndpoints).where(eq(chatEndpoints.id, uuid(req.params.endpointId)));
bindCompany(endpoint); connectionId = endpoint.connectionId; resource.provider = endpoint.provider;
targets.add(endpoint.assignedAgentId);
}
if (connectionId) {
const [connection] = await db.select().from(toolConnections).where((isUuidLike(connectionId) ? eq(toolConnections.id, connectionId) : eq(toolConnections.uid, connectionId)));
bindCompany(connection); resource.connectionId = connection.id;
resource.purpose = connection.connectionPurpose === "ai" ? "ai" : connection.connectionPurpose === "channel" ? "chat" : "tool";
resource.provider ??= text(connection.config.sourceTemplateKey) ?? text(connection.config.provider) ?? text((connection.config.ai as Record<string, unknown> | undefined)?.provider) ?? text(connection.transportConfig.sourceTemplateKey);
resource.selfServiceUserId = connection.createdByUserId;
if (req.method !== "GET") {
const installs = await db.select().from(toolConnectionInstalls).where(eq(toolConnectionInstalls.connectionId, connection.id));
addSelectors(installs);
if (operation === "PUT /tool-connections/:connectionId/installs" && Array.isArray(body.installs)) {
const keys = (rows: Array<{ targetType: string; targetId: string }>) => new Set(rows.filter(row => row.targetType === "company").map(row => row.targetId));
const existingCompanies = keys(installs), requestedCompanies = keys(body.installs);
resource.requestedInstallCompanyIds = [...requestedCompanies];
resource.selfServiceAgentInstalls = existingCompanies.size === requestedCompanies.size && [...existingCompanies].every(id => requestedCompanies.has(id));
}
}
if (req.actor.type === "agent" && connection.credentialPolicy === "per_user") {
const grants = await db.select().from(connectionGrants).where(and(eq(connectionGrants.connectionId, connection.id), eq(connectionGrants.kind, "user"), eq(connectionGrants.status, "active")));
if (!grants.some(grant => grant.subjectUserId === userId) && connection.createdByUserId !== userId) throw forbidden("Another user's personal account is not available for delegated management");
}
}
if (connectionId && operation === "POST /companies/:companyId/tools/apps/:connectionId/finish") {
const [profile] = await db.select({ id: toolProfiles.id }).from(toolProfiles).where(and(
eq(toolProfiles.companyId, companyId!), eq(toolProfiles.profileKey, `app:${resource.connectionId}`),
));
profileId = profile?.id ?? profileId;
}
if (profileId) {
const [profile] = await db.select().from(toolProfiles).where(eq(toolProfiles.id, uuid(profileId)));
bindCompany(profile); resource.profileId = profile.id;
if (req.method !== "GET") {
const entries = await db.select().from(toolProfileEntries).where(eq(toolProfileEntries.profileId, profile.id));
addSelectors(entries);
const bindings = await db.select().from(toolProfileBindings).where(eq(toolProfileBindings.profileId, profile.id));
for (const binding of bindings) {
if (binding.targetType === "agent") targets.add(binding.targetId);
if (binding.targetType === "company") {
const companyAgents = await db.select({ id: agents.id }).from(agents).where(eq(agents.companyId, binding.companyId));
companyAgents.forEach(agent => targets.add(agent.id));
}
}
}
}
companyId ??= text(body.companyId) ?? text(req.query.companyId) ?? req.actor.companyId ?? null;
if (!companyId) throw forbidden("A company is required for connection management");
assertCompanyAccess(req, companyId);
resource.companyId = companyId;
if (operation === "POST /companies/:companyId/tools/connections" || operation === "POST /companies/:companyId/tools/applications") resource.provider = null;
else if (operation.includes("/tool-applications/:applicationId")) resource.provider = applicationProvider;
else resource.provider ??= text(body.provider) ?? text(body.galleryKey) ?? text(req.params.galleryKey);
if (operation === "PATCH /tool-applications/:applicationId" && Object.hasOwn(body, "metadata")) resource.requestedProvider = text(body.metadata?.sourceTemplateKey);
if (operation === "PATCH /tool-connections/:connectionId" && (body.config || body.transportConfig)) {
const proposed = body.config ?? body.transportConfig;
resource.requestedProvider = text(proposed.sourceTemplateKey) ?? text(proposed.provider) ?? text(proposed.ai?.provider);
}
for (const id of [req.params.agentId, req.query.agentId, req.query.agent, body.agentId, body.assignedAgentId, body.subjectAgentId, body.ownerAgentId, body.asAgentId, ...(Array.isArray(body.agentIds) ? body.agentIds : [])]) {
if (text(id)) targets.add(String(id));
}
if (body.targetType === "agent" && text(body.targetId)) targets.add(body.targetId);
for (const install of Array.isArray(body.installs) ? body.installs : []) {
if (install.targetType === "agent" && text(install.targetId)) targets.add(install.targetId);
}
if ((operation.includes("ai-connection-pools") && req.method === "POST") || body.allAgents || body.access === "all_agents" || body.targetType === "company" || body.installs?.some?.((install: { targetType: string }) => install.targetType === "company")) {
const companyAgents = await db.select({ id: agents.id }).from(agents).where(eq(agents.companyId, companyId));
companyAgents.forEach(agent => targets.add(agent.id));
}
resource.projectId ??= text(body.projectId) ?? text(body.executionDefaults?.projectId);
resource.issueId ??= text(body.issueId);
if (body.approvalIssueId) extraResources.push({ ...resource, issueId: uuid(body.approvalIssueId) });
for (const member of Array.isArray(body.config?.members) ? body.config.members : []) addSelector(member.binding ?? {});
for (const binding of Array.isArray(body.bindings) ? body.bindings : []) addSelector(binding);
if (req.method !== "GET" && (operation.includes("/tools/policies") || operation.includes("/trust-rules/")) &&
(policyAudiences.length ? policyAudiences : [{}]).some(selected => !selected.agentId && !(Array.isArray(selected.agentIds) && selected.agentIds.length))) {
const companyAgents = await db.select({ id: agents.id }).from(agents).where(eq(agents.companyId, companyId));
companyAgents.forEach(agent => targets.add(agent.id));
}
for (let index = 0; index < selectors.length; index++) {
const selected = selectors[index];
const profileOperation = operation.includes("profile");
if (profileOperation && (selected.defaultAction === "allow" || selected.selectorType === "tool_name" && text(selected.toolName) || selected.selectorType === "risk_level" && text(selected.riskLevel))) {
const connections = await lookup(`profile-catalog:${JSON.stringify([selected.defaultAction, selected.selectorType, selected.toolName, selected.riskLevel])}`, () => db.selectDistinct({ connectionId: toolCatalogEntries.connectionId }).from(toolCatalogEntries).where(and(
eq(toolCatalogEntries.companyId, resource.companyId), eq(toolCatalogEntries.status, "active"),
selected.defaultAction === "allow" ? undefined : selected.selectorType === "tool_name" ? eq(toolCatalogEntries.toolName, String(selected.toolName)) : sql`${toolCatalogEntries.riskLevel} = ${String(selected.riskLevel)}`,
)).limit(1025));
connections.forEach(connection => addSelector(connection));
}
// Expand each advertised plural selector. A permission over one member
// cannot authorize a policy that also changes another member's access.
for (const key of ["connectionId", "profileId", "gatewayId", "applicationId", "catalogEntryId", "projectId", "issueId", "routineId"] as const) {
for (const id of Array.isArray(selected[`${key}s`]) ? selected[`${key}s`] as unknown[] : []) addSelector({ [key]: uuid(id) });
}
if (selected.applicationId) {
const [application] = await lookup(`application:${String(selected.applicationId)}`, () => db.select().from(toolApplications).where(eq(toolApplications.id, uuid(selected.applicationId))));
bindCompany(application);
const connections = await lookup(`application-connections:${application.id}`, () => db.select({ id: toolConnections.id }).from(toolConnections).where(eq(toolConnections.applicationId, application.id)).limit(1025));
connections.forEach(connection => addSelector({ connectionId: connection.id }));
}
if (text(selected.profileId) && selected.profileId !== profileId) extraResources.push({ ...resource, profileId: uuid(selected.profileId) });
if (text(selected.gatewayId) && selected.gatewayId !== gatewayId) extraResources.push({ ...resource, gatewayId: uuid(selected.gatewayId) });
if (text(selected.issueId)) extraResources.push({ ...resource, issueId: uuid(selected.issueId) });
for (const id of [selected.agentId, selected.targetAgentId, ...(Array.isArray(selected.agentIds) ? selected.agentIds : [])]) {
if (text(id)) targets.add(String(id));
}
if (selected.targetType === "agent" && text(selected.targetId)) targets.add(String(selected.targetId));
if (selected.targetType === "company") {
if (text(selected.targetId) && selected.targetId !== companyId) throw forbidden("Target company does not match");
const rows = await db.select({ id: agents.id }).from(agents).where(eq(agents.companyId, companyId));
rows.forEach(row => targets.add(row.id));
}
let selectedConnectionId = text(selected.connectionId);
if (selected.catalogEntryId) {
const [entry] = await lookup(`catalog:${String(selected.catalogEntryId)}`, () => db.select().from(toolCatalogEntries).where(eq(toolCatalogEntries.id, uuid(selected.catalogEntryId))));
bindCompany(entry); selectedConnectionId = entry.connectionId;
if (profileOperation && entry.applicationId) addSelector({ applicationId: entry.applicationId });
}
if (selectedConnectionId && selectedConnectionId !== connectionId) {
const [connection] = await lookup(`connection:${selectedConnectionId}`, () => db.select({ id: toolConnections.id, companyId: toolConnections.companyId, createdByUserId: toolConnections.createdByUserId,
sourceTemplateKey: sql<string | null>`${toolConnections.config}->>'sourceTemplateKey'`, provider: sql<string | null>`${toolConnections.config}->>'provider'`,
}).from(toolConnections).where(eq(toolConnections.id, selectedConnectionId)));
bindCompany(connection);
extraResources.push({ ...resource, connectionId: connection.id,
provider: text(connection.sourceTemplateKey) ?? text(connection.provider),
selfServiceUserId: connection.createdByUserId,
});
}
if (["project", "issue", "routine", "gateway"].includes(String(selected.targetType)) && text(selected.targetId)) bindingTargets.push({ targetType: String(selected.targetType), targetId: uuid(selected.targetId) });
if (text(selected.routineId)) extraResources.push({ ...resource, routineId: uuid(selected.routineId) });
if (selected.targetType === "project" && text(selected.targetId)) extraResources.push({ ...resource, projectId: String(selected.targetId) });
if (text(selected.projectId)) extraResources.push({ ...resource, projectId: String(selected.projectId) });
}
if (!connectionId && options.personal && (body.ownership === "personal" || body.grantKind === "user" || req.method === "GET")) resource.selfServiceUserId = userId;
for (const targetId of targets) uuid(targetId);
const context = await authorize(req.actor, action, resource, { targetAgentIds: [...targets], mutation: req.method !== "GET" });
if (action === "connections:profiles" && resource.connectionId && req.method !== "GET" && (req.actor.type === "agent" || !resource.selfServiceAgentInstalls)) {
await authorize(req.actor, "connections:configure", resource, { targetAgentIds: [...targets] });
}
// Clearing a binding or audience still changes access. Check field
// presence so null, false and empty arrays cannot bypass profile authority.
const changesGatewayBindings = action === "connections:gateway" &&
["profileId", "agentId", "projectId", "issueId"].some(key => Object.hasOwn(body, key));
if (changesGatewayBindings) {
await authorize(req.actor, "connections:profiles", resource, { targetAgentIds: [...targets], mutation: req.method !== "GET" });
}
const changesAudience = req.actor.type === "agent" && req.method !== "GET" && action === "connections:configure" && (
["allAgents", "agentIds", "assignedAgentId", "installs", "bindings", "ownership", "grantKind", "credentialPolicy", "access"].some(key => Object.hasOwn(body, key)) ||
operation.includes("ai-connection-pools"));
if (changesAudience) await authorize(req.actor, "connections:profiles", resource, { targetAgentIds: [...targets] });
const uniqueResources = new Map(extraResources.map(referenced => [JSON.stringify([
referenced.connectionId, referenced.profileId, referenced.gatewayId, referenced.projectId, referenced.issueId, referenced.routineId, referenced.provider, referenced.purpose,
]), referenced]));
if (uniqueResources.size > 1024) throw badRequest("Connection management request spans too many resources");
const authorizeReferenced = async (afterHumanAction?: boolean) => {
for (const referenced of uniqueResources.values()) {
const resolved = { ...referenced, companyId: resource.companyId };
const checkOptions = { targetAgentIds: [...targets], mutation: req.method !== "GET", afterHumanAction };
await authorize(req.actor, action, resolved, checkOptions);
if (changesGatewayBindings || changesAudience) await authorize(req.actor, "connections:profiles", resolved, checkOptions);
if (action === "connections:profiles" && resolved.connectionId && req.method !== "GET" && (req.actor.type === "agent" || !resolved.selfServiceAgentInstalls)) {
await authorize(req.actor, "connections:configure", resolved, checkOptions);
}
}
};
for (const binding of bindingTargets) await management.authorizeBindingTarget(req.actor, action, resource, binding, { targetAgentIds: [...targets], mutation: req.method !== "GET", afterHumanAction });
await authorizeReferenced();
context.reauthorize = async (continuation) => {
const current = await authorizeRequest(continuation?.afterHumanAction);
context.workContext = current.workContext;
context.resource = current.resource;
context.decision = current.decision;
context.audit = current.audit;
contexts.set(req, current);
};
return context;
};
try {
contexts.set(req, await authorizeRequest());
next();
} catch (error) { next(error); }
};
}
export async function aiLoginUserAction(db: Db, req: Request, companyId: string, sessionId: string) {
if (req.actor.type !== "agent") return {};
const [company] = await db.select({ prefix: companies.issuePrefix }).from(companies).where(eq(companies.id, companyId));
if (!company) throw notFound("Company not found");
return requiredConnectionUserAction(req, { type: "ai_login", id: sessionId, companyId },
`/${encodeURIComponent(company.prefix)}/apps/connect?aiLogin=${encodeURIComponent(sessionId)}`,
"Complete the provider sign-in for this saved AI account.");
}
/** Collection projections use the same resolved-resource policy as individual reads. */
export async function filterConnectionManagementRows<T>(db: Db, req: Request, action: ConnectionManagementAction,
rows: T[], resource: (row: T) => Partial<ConnectionManagementResource>): Promise<T[]> {
if (req.actor.type !== "agent") return rows;
const context = connectionRequestContext(req);
if (!context) throw forbidden("Connection management authority is required");
const visible: T[] = [];
for (const row of rows) {
const decision = await connectionManagementService(db).capability(req.actor, action, { ...context.resource, ...resource(row) }, { mutation: false, readContext: context });
if (decision.allowed) visible.push(row);
}
return visible;
}
/** Policies may name several private resources; omit the entire rule if any is hidden. */
export async function filterConnectionManagementPolicies<T extends { selectors: Record<string, unknown> }>(db: Db, req: Request, rows: T[]): Promise<T[]> {
if (req.actor.type !== "agent") return rows;
const context = connectionRequestContext(req);
if (!context) throw forbidden("Connection management authority is required");
// Rules often name the same resources. One read decision per resource keeps
// long rule lists within the agent API timeout.
const service = connectionManagementService(db);
const decisions = new Map<string, Promise<boolean>>();
const applicationConnections = new Map<string, Promise<{ connectionId: string }[]>>();
const catalogConnections = new Map<string, Promise<{ connectionId: string }[]>>();
// Query builders run again on every await; settle each load once.
const once = <V>(cache: Map<string, Promise<V>>, key: string, load: () => PromiseLike<V>) => {
if (!cache.has(key)) cache.set(key, Promise.resolve(load()));
return cache.get(key)!;
};
const visible: T[] = [];
for (const row of rows) {
const resources: Partial<ConnectionManagementResource>[] = [{}];
const ids = (key: string) => [row.selectors[key], ...(Array.isArray(row.selectors[`${key}s`]) ? row.selectors[`${key}s`] as unknown[] : [])].filter((id): id is string => typeof id === "string");
for (const key of ["connectionId", "profileId", "gatewayId", "projectId", "issueId", "routineId"] as const) for (const id of ids(key)) resources.push({ [key]: id });
for (const id of ids("agentId")) resources.push({ targetAgentId: id });
for (const applicationId of ids("applicationId")) {
resources.push(...await once(applicationConnections, applicationId, () => db.select({ connectionId: toolConnections.id }).from(toolConnections).where(and(eq(toolConnections.applicationId, applicationId), eq(toolConnections.companyId, context.resource.companyId)))));
}
for (const id of ids("catalogEntryId")) {
resources.push(...await once(catalogConnections, id, () => db.select({ connectionId: toolCatalogEntries.connectionId }).from(toolCatalogEntries).where(and(eq(toolCatalogEntries.id, id), eq(toolCatalogEntries.companyId, context.resource.companyId)))));
}
let allowed = true;
for (const resource of resources) {
const key = JSON.stringify(Object.entries(resource).sort(([a], [b]) => a.localeCompare(b)));
if (!(await once(decisions, key, async () => (await service.capability(req.actor, "connections:profiles", { ...context.resource, ...resource }, { mutation: false, readContext: context })).allowed))) { allowed = false; break; }
}
if (allowed) visible.push(row);
}
return visible;
}