mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-03 03:36:57 +01:00
Enforce caller authority on delegated connection tests
Recheck private task access before exposing saved results; intersect caller and target profiles, policies, credentials, and rate admissions. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
69bf11aef9
commit
70b7c973c8
4 files changed
+145
-20
No files matched your search
@@ -190,7 +190,7 @@ function sha256(value: unknown): string {
|
||||
return createHash("sha256").update(stableStringify(value)).digest("hex");
|
||||
}
|
||||
|
||||
type ConsumedRateAdmission = { policyId: string; versionHash: string };
|
||||
type ConsumedRateAdmission = { policyId: string; versionHash: string; bucketKey?: string };
|
||||
function ratePolicyVersionHash(policy: typeof toolPolicies.$inferSelect): string {
|
||||
return sha256({ ...policy, createdAt: policy.createdAt.toISOString(), updatedAt: policy.updatedAt.toISOString() });
|
||||
}
|
||||
@@ -1238,6 +1238,19 @@ export function toolAccessPolicyService(db: Db, options: { toolActionSigningSecr
|
||||
return { profiles: activeProfiles, entries };
|
||||
}
|
||||
|
||||
function profileAllowsTool(profileState: Awaited<ReturnType<typeof effectiveProfiles>>, ctx: ToolAccessContext) {
|
||||
return profileState.profiles.some(profile => {
|
||||
const entries = profileState.entries.filter(entry => entry.profileId === profile.id && profileEntryMatches(entry, ctx));
|
||||
return !entries.some(entry => entry.effect === "exclude")
|
||||
&& (profile.defaultAction === "allow" || entries.some(entry => entry.effect === "include"));
|
||||
});
|
||||
}
|
||||
|
||||
async function hasProfileAccess(input: ToolAccessDecisionInput) {
|
||||
const loaded = await loadContext(input);
|
||||
return loaded.ok && profileAllowsTool(await effectiveProfiles(loaded.ctx), loaded.ctx);
|
||||
}
|
||||
|
||||
async function explicitGrant(ctx: ToolAccessContext, cache?: ToolAccessDecisionCache): Promise<boolean> {
|
||||
const principalType = ctx.actorType === "agent" ? "agent" : ctx.actorType === "user" ? "user" : null;
|
||||
const principalId = ctx.actorType === "agent" ? ctx.agentId : ctx.actorId;
|
||||
@@ -1386,18 +1399,21 @@ export function toolAccessPolicyService(db: Db, options: { toolActionSigningSecr
|
||||
status: toolInvocations.status, policyDecision: toolInvocations.policyDecision, approvalState: toolInvocations.approvalState,
|
||||
policyExplanation: toolInvocations.policyExplanation,
|
||||
}).from(toolInvocations).where(and(eq(toolInvocations.companyId, ctx.companyId), eq(toolInvocations.id, invocationId)));
|
||||
const delegated = invocation?.policyExplanation?.delegatedConnectionTest as { agentId?: string } | undefined;
|
||||
const isDelegatedCaller = invocation?.actorType === "agent" && invocation.actorId === ctx.agentId
|
||||
&& delegated?.agentId === ctx.agentId && invocation.agentId !== ctx.agentId;
|
||||
const authorized = invocation && (
|
||||
(invocation.status === "executing" && (invocation.policyDecision === "allow" || (invocation.policyDecision === "require_approval" && invocation.approvalState === "approved")))
|
||||
|| (invocation.status === "awaiting_approval" && invocation.policyDecision === "require_approval" && ["pending", "approved"].includes(invocation.approvalState))
|
||||
)
|
||||
&& invocation.actorType === ctx.actorType && invocation.actorId === ctx.actorId && invocation.agentId === ctx.agentId
|
||||
&& invocation.actorType === ctx.actorType && invocation.actorId === ctx.actorId && (invocation.agentId === ctx.agentId || isDelegatedCaller)
|
||||
&& invocation.runId === ctx.heartbeatRunId && invocation.issueId === ctx.issueId && invocation.gatewayId === ctx.gatewayId
|
||||
&& invocation.applicationId === ctx.applicationId && invocation.connectionId === ctx.connectionId
|
||||
&& invocation.catalogEntryId === ctx.catalogEntryId && invocation.toolName === ctx.toolName
|
||||
&& invocation.providerType === ctx.providerType && invocation.applicationKey === ctx.applicationKey
|
||||
&& invocation.upstreamToolName === ctx.upstreamToolName
|
||||
&& invocation.argumentsHash === ctx.argumentsHash;
|
||||
const stored = authorized ? invocation.policyExplanation?.consumedRateAdmissions : null;
|
||||
const stored = authorized ? invocation.policyExplanation?.[isDelegatedCaller ? "delegatedCallerConsumedRateAdmissions" : "consumedRateAdmissions"] : null;
|
||||
return Array.isArray(stored) ? stored.filter((item): item is ConsumedRateAdmission => isRecord(item)
|
||||
&& typeof item.policyId === "string" && CANONICAL_UUID_RE.test(item.policyId)
|
||||
&& typeof item.versionHash === "string" && /^[a-f0-9]{64}$/.test(item.versionHash)) : [];
|
||||
@@ -1410,7 +1426,7 @@ export function toolAccessPolicyService(db: Db, options: { toolActionSigningSecr
|
||||
*/
|
||||
async function decide(
|
||||
input: ToolAccessDecisionInput,
|
||||
options: { cache?: ToolAccessDecisionCache; admittedInvocationId?: string } = {},
|
||||
options: { cache?: ToolAccessDecisionCache; admittedInvocationId?: string; sharedRateDecision?: ToolAccessDecision } = {},
|
||||
): Promise<ToolAccessDecision> {
|
||||
const consumedRateAdmissions: ConsumedRateAdmission[] = [];
|
||||
const result = await decideInternal(input, options, consumedRateAdmissions);
|
||||
@@ -1421,7 +1437,7 @@ export function toolAccessPolicyService(db: Db, options: { toolActionSigningSecr
|
||||
|
||||
async function decideInternal(
|
||||
input: ToolAccessDecisionInput,
|
||||
options: { cache?: ToolAccessDecisionCache; admittedInvocationId?: string },
|
||||
options: { cache?: ToolAccessDecisionCache; admittedInvocationId?: string; sharedRateDecision?: ToolAccessDecision },
|
||||
consumedRateAdmissions: ConsumedRateAdmission[],
|
||||
): Promise<ToolAccessDecision> {
|
||||
// A decision that consumes a rate limit can also record a trust-rule hit
|
||||
@@ -1438,11 +1454,7 @@ export function toolAccessPolicyService(db: Db, options: { toolActionSigningSecr
|
||||
? await admittedRateLimitAdmissions(ctx, options.admittedInvocationId) : [];
|
||||
const profileState = await effectiveProfiles(ctx, cache);
|
||||
const effectiveProfileIds = profileState.profiles.map((profile) => profile.id);
|
||||
const permittedByProfile = profileState.profiles.some((profile) => {
|
||||
const matchingEntries = profileState.entries.filter((entry) => entry.profileId === profile.id && profileEntryMatches(entry, ctx));
|
||||
return !matchingEntries.some((entry) => entry.effect === "exclude")
|
||||
&& (profile.defaultAction === "allow" || matchingEntries.some((entry) => entry.effect === "include"));
|
||||
});
|
||||
const permittedByProfile = profileAllowsTool(profileState, ctx);
|
||||
const policies = await cachedRead(cache, `enabled-policies:${ctx.companyId}`, async () =>
|
||||
db.select().from(toolPolicies).where(and(eq(toolPolicies.companyId, ctx.companyId), eq(toolPolicies.enabled, true))).orderBy(asc(toolPolicies.priority), asc(toolPolicies.createdAt)));
|
||||
for (const policy of policies) {
|
||||
@@ -1509,11 +1521,20 @@ export function toolAccessPolicyService(db: Db, options: { toolActionSigningSecr
|
||||
}
|
||||
const versionHash = ratePolicyVersionHash(policy);
|
||||
if (admittedRates.some(admission => admission.policyId === policy.id && admission.versionHash === versionHash)) continue;
|
||||
// Paired caller/target checks for one delegated test share a slot only
|
||||
// when the exact policy version and counter bucket are identical.
|
||||
const shared = options.sharedRateDecision?.policyExplanation?.consumedRateAdmissions;
|
||||
const bucketKey = `${policy.id}:${rateBucket(rateLimitRule(policy)!, ctx)}`;
|
||||
if (input.consumeRateLimit === true && Array.isArray(shared) && shared.some(item => isRecord(item)
|
||||
&& item.policyId === policy.id && item.versionHash === versionHash && item.bucketKey === bucketKey)) {
|
||||
consumedRateAdmissions.push({ policyId: policy.id, versionHash, bucketKey });
|
||||
continue;
|
||||
}
|
||||
const state = await enforceRateLimit(policy, ctx, input.consumeRateLimit === true);
|
||||
if (state?.limited) {
|
||||
return decision("rate_limited", "rate_limited", "Tool access rate limit exceeded.", effectiveProfileIds, [policy.id], { rateLimitState: state, redactionPlan: redaction.redactionPlan, policyExplanation });
|
||||
}
|
||||
if (state && input.consumeRateLimit === true) consumedRateAdmissions.push({ policyId: policy.id, versionHash });
|
||||
if (state && input.consumeRateLimit === true) consumedRateAdmissions.push({ policyId: policy.id, versionHash, bucketKey: state.bucketKey });
|
||||
continue;
|
||||
}
|
||||
if (policy.policyType === "trust_rule") {
|
||||
@@ -2161,6 +2182,7 @@ export function toolAccessPolicyService(db: Db, options: { toolActionSigningSecr
|
||||
|
||||
return {
|
||||
decide,
|
||||
hasProfileAccess,
|
||||
writeAudit,
|
||||
recordInvocation,
|
||||
summarizeAndRedact,
|
||||
|
||||
@@ -16,6 +16,7 @@ import { browserUseService } from "./browser-use.js";
|
||||
import { isBrowserUseConnection } from "./browser-use-client.js";
|
||||
import { COGNEE_STDIO_TEMPLATE, cogneeCloudUrl, callCogneeCloud } from "./cognee-connection.js";
|
||||
import { HttpError } from "../errors.js";
|
||||
import { authorizationService, canActorReadIssuePrivacy, type AuthorizationActor } from "./authorization.js";
|
||||
import { claimSlackRateLimitRetry } from "./connectors/slack-retry.js";
|
||||
import { resolveSlackTaskAuthority } from "./connectors/slack-authority.js";
|
||||
import { SLACK_TOOLS } from "@paperclipai/shared";
|
||||
@@ -1195,11 +1196,11 @@ export function createToolGatewayService(
|
||||
signal?.throwIfAborted();
|
||||
// Legacy/plugin sessions may not carry identity fields. Resolve them from
|
||||
// the admitted run, never from parameters. Signed approvals keep their origin.
|
||||
let identity = session;
|
||||
let identity = session.originatingAgentId ? { ...session, agentId: session.originatingAgentId } : session;
|
||||
if (session.runId && !approvedAction && !session.identityContextId) {
|
||||
const [run] = await db.select({ responsibleUserId: heartbeatRuns.responsibleUserId }).from(heartbeatRuns)
|
||||
.where(and(eq(heartbeatRuns.id, session.runId), eq(heartbeatRuns.companyId, session.companyId)));
|
||||
identity = await captureSessionIdentity({ ...session, responsibleUserId: run?.responsibleUserId ?? session.responsibleUserId });
|
||||
identity = await captureSessionIdentity({ ...identity, responsibleUserId: run?.responsibleUserId ?? session.responsibleUserId });
|
||||
}
|
||||
return injectionPrevention.classify({ companyId: identity.companyId, source: "gateway",
|
||||
actorType: identity.actorType === "user" ? "user" : identity.agentId ? "agent" : "system",
|
||||
@@ -3827,7 +3828,7 @@ export function createToolGatewayService(
|
||||
try {
|
||||
return (await resolveConnectionGrantSecret(db, connection, grant, ref, {
|
||||
consumerType: "tool_connection", consumerId: connection.id,
|
||||
actorType: "system", actorId: session.agentId,
|
||||
actorType: "system", actorId: session.originatingAgentId ?? session.agentId,
|
||||
responsibleUserId: grant.subjectUserId, issueId: session.issueId, heartbeatRunId: session.runId,
|
||||
})).value;
|
||||
} catch (error) {
|
||||
@@ -7425,6 +7426,41 @@ export function createToolGatewayService(
|
||||
);
|
||||
}
|
||||
|
||||
function delegatedTestCallerSession(session: ToolGatewaySession): ToolGatewaySession | null {
|
||||
const callerId = session.originatingAgentId;
|
||||
return callerId && callerId !== session.agentId ? { ...session, agentId: callerId } : null;
|
||||
}
|
||||
|
||||
async function assertDelegatedTestCredentialAccess(session: ToolGatewaySession, tool: ToolGatewayDescriptor) {
|
||||
const caller = delegatedTestCallerSession(session);
|
||||
if (!caller || !tool.connectionId) return;
|
||||
if (!(await policyService.hasProfileAccess(policyInputForTool({ session: caller, tool })))) {
|
||||
throw new ToolGatewayHttpError(403, "Connection is not available in the acting agent's profiles", "test_caller_connection_unavailable");
|
||||
}
|
||||
const [connection] = await db.select().from(toolConnections).where(and(
|
||||
eq(toolConnections.companyId, session.companyId), eq(toolConnections.id, tool.connectionId),
|
||||
));
|
||||
if (!connection) throw new ToolGatewayHttpError(404, "Tool connection not found", "connection_not_found");
|
||||
if (connection.authKind === "none") return;
|
||||
// Resolve consent metadata before any vault read. Testing another agent
|
||||
// cannot select that agent's dedicated credential or a different identity.
|
||||
const callerGrant = await resolveConnectionGrant(caller, connection, false);
|
||||
const targetGrant = await resolveConnectionGrant(session, connection, false);
|
||||
if (callerGrant.id !== targetGrant.id) {
|
||||
throw new ToolGatewayHttpError(403, "Tests cannot use another agent's credential identity", "test_credential_identity_mismatch");
|
||||
}
|
||||
}
|
||||
|
||||
async function assertCurrentDelegatedTestCaller(session: ToolGatewaySession, tool: ToolGatewayDescriptor, parameters: unknown, invocationId: string) {
|
||||
const caller = delegatedTestCallerSession(session);
|
||||
if (!caller) return;
|
||||
const current = await policyService.decide(policyInputForTool({ session: caller, tool, parameters }), { admittedInvocationId: invocationId });
|
||||
if (!current.allowed && !(session.approvedInvocationId && current.decision === "require_approval")) {
|
||||
throw new ToolGatewayHttpError(403, current.explanation, current.reasonCode);
|
||||
}
|
||||
await assertDelegatedTestCredentialAccess(session, tool);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute an already-authorized test-tab tool call against the connected MCP
|
||||
* server and record the result/error onto the invocation. Shared by
|
||||
@@ -7488,6 +7524,7 @@ export function createToolGatewayService(
|
||||
await args.managementContext?.reauthorize();
|
||||
if (args.managementContext?.actor.type === "agent") {
|
||||
Object.assign(args.session, args.managementContext.workContext);
|
||||
await assertCurrentDelegatedTestCaller(args.session, args.tool, args.parameters, args.invocationId);
|
||||
const current = await policyService.decide(policyInputForTool({ session: args.session, tool: args.tool, parameters: args.parameters }), { admittedInvocationId: args.invocationId });
|
||||
if (!current.allowed && !(args.session.approvedInvocationId && current.decision === "require_approval")) {
|
||||
throw new ToolGatewayHttpError(403, current.explanation, current.reasonCode);
|
||||
@@ -9564,7 +9601,23 @@ export function createToolGatewayService(
|
||||
idempotencyKey: `test-call:${randomUUID()}`,
|
||||
consumeRateLimit: true,
|
||||
});
|
||||
const accessDecision = await policyService.decide(decisionInput);
|
||||
await assertDelegatedTestCredentialAccess(session, tool);
|
||||
const callerSession = delegatedTestCallerSession(session);
|
||||
const callerDecisionInput = callerSession ? policyInputForTool({ session: callerSession, tool, parameters: requestedParameters, consumeRateLimit: true }) : null;
|
||||
const callerDecision = callerDecisionInput ? await policyService.decide(callerDecisionInput) : null;
|
||||
const targetDecision = await policyService.decide(decisionInput, { sharedRateDecision: callerDecision ?? undefined });
|
||||
// Both principals' action controls apply. Either denial wins; either
|
||||
// Ask first decision creates the ordinary human approval request.
|
||||
const chosenDecision = callerDecision && !callerDecision.allowed && callerDecision.decision !== "require_approval" ? callerDecision
|
||||
: !targetDecision.allowed ? targetDecision
|
||||
: callerDecision?.decision === "require_approval" ? callerDecision : targetDecision;
|
||||
const accessDecision = { ...chosenDecision, policyExplanation: {
|
||||
...chosenDecision.policyExplanation,
|
||||
consumedRateAdmissions: targetDecision.policyExplanation?.consumedRateAdmissions,
|
||||
delegatedCallerConsumedRateAdmissions: callerDecision?.policyExplanation?.consumedRateAdmissions,
|
||||
} };
|
||||
if (callerDecisionInput && callerDecision) await policyService.writeAudit(callerDecisionInput, callerDecision);
|
||||
if (accessDecision.allowed || accessDecision.decision === "require_approval") await assertDelegatedTestCredentialAccess(session, tool);
|
||||
const recorded = await recordToolInvocation(
|
||||
decisionInput,
|
||||
accessDecision,
|
||||
@@ -9772,6 +9825,7 @@ export function createToolGatewayService(
|
||||
companyId: string;
|
||||
connectionId: string;
|
||||
actionRequestId: string;
|
||||
actor?: AuthorizationActor;
|
||||
}): Promise<ToolConnectionTestCallStatus> {
|
||||
const [actionRequest] = await db
|
||||
.select()
|
||||
@@ -9807,6 +9861,23 @@ export function createToolGatewayService(
|
||||
"action_request_not_found",
|
||||
);
|
||||
}
|
||||
if (input.actor?.type === "agent" && invocation.actorId !== input.actor.agentId) {
|
||||
throw new ToolGatewayHttpError(403, "Test belongs to another principal", "test_principal_access_denied");
|
||||
}
|
||||
if (invocation.issueId) {
|
||||
if (!input.actor) throw new ToolGatewayHttpError(403, "Task access is required", "test_task_access_denied");
|
||||
const [issue] = await db.select().from(issues).where(and(eq(issues.companyId, input.companyId), eq(issues.id, invocation.issueId)));
|
||||
const decision = issue ? await authorizationService(db).decide({ actor: input.actor, action: "issue:read", resource: {
|
||||
type: "issue", companyId: input.companyId, issueId: issue.id, projectId: issue.projectId,
|
||||
parentIssueId: issue.parentId, assigneeAgentId: issue.assigneeAgentId, assigneeUserId: issue.assigneeUserId,
|
||||
} }) : null;
|
||||
if (!issue || !decision?.allowed || (input.actor.type === "agent" && (
|
||||
!(await canActorReadIssuePrivacy(db, input.actor, issue)) || !input.actor.onBehalfOfUserId
|
||||
|| !(await canActorReadIssuePrivacy(db, { type: "board", userId: input.actor.onBehalfOfUserId }, issue))
|
||||
))) {
|
||||
throw new ToolGatewayHttpError(403, "Task is unavailable to this principal", "test_task_access_denied");
|
||||
}
|
||||
}
|
||||
return buildTestCallStatus(actionRequest, invocation);
|
||||
},
|
||||
|
||||
|
||||
Reference in new issue
Block a user