👯 fix: Reject Duplicate Tool Approval Identities (#15933)

* fix: Reject aliased tool approval identities

* fix: Keep approval validation in TypeScript

* style: Format approval validation
This commit is contained in:
Danny Avila 2026-09-14 15:08:26 -04:00 • committed by GitHub
parent 16faadb998
commit b1f6f6df2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 251 additions and 25 deletions

View file

@ -2370,6 +2370,42 @@ describe('ResumeAgentController (POST /agents/chat/resume)', () => {
expect(mockGenerationJobManager.approvals.resolve).not.toHaveBeenCalled();
});
it('400 when a persisted approval payload aliases two calls to the same id', async () => {
const job = makeToolApprovalJob();
job.metadata.pendingAction.payload.action_requests = [
{ tool_call_id: 'tc1', name: 'write', arguments: { value: 'hidden' } },
{ tool_call_id: 'tc1', name: 'write', arguments: { value: 'visible' } },
];
job.metadata.pendingAction.payload.review_configs = [
{ tool_call_id: 'tc1', action_name: 'write', allowed_decisions: ['approve', 'reject'] },
{ tool_call_id: 'tc1', action_name: 'write', allowed_decisions: ['approve', 'reject'] },
];
mockGenerationJobManager.getJob.mockResolvedValue(job);
const res = await post(approveBody());
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/invalid tool approval payload/i);
expect(mockGenerationJobManager.approvals.resolve).not.toHaveBeenCalled();
});
it('400 when the client submits duplicate decisions for one tool-call id', async () => {
mockGenerationJobManager.getJob.mockResolvedValue(makeToolApprovalJob());
const res = await post(
approveBody({
decisions: [
{ tool_call_id: 'tc1', decision: 'approve' },
{ tool_call_id: 'tc1', decision: 'reject' },
],
}),
);
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/invalid tool approval decisions/i);
expect(mockGenerationJobManager.approvals.resolve).not.toHaveBeenCalled();
});
it('403 when a decision is not permitted by the tool policy', async () => {
const job = makeToolApprovalJob();
// Policy restricts tc1 to reject only; an `approve` must be refused.

View file

@ -13,15 +13,12 @@ const {
GenerationJobManager,
GENERATION_RECOVERY_FAILED_ERROR,
isPendingActionStale,
mapToolApprovalResolutions,
resolveToolApprovalResume,
resolveAskUserQuestionResume,
buildResolvedAskUserQuestion,
appendResolvedAskUserQuestion,
attachAskUserQuestionAnswers,
findAskUserQuestionContentIndex,
findUndecidedToolCalls,
findDisallowedDecisions,
findIncompleteDecisions,
computeAgentRequestFingerprint,
computeLegacyAgentRequestFingerprint,
captureAgentCheckpointGeneration,
@ -351,27 +348,7 @@ function resolveResumeValue(pendingAction, body) {
const payload = pendingAction.payload;
if (payload?.type === 'tool_approval') {
const resolutions = Array.isArray(body.decisions) ? body.decisions : [];
const undecided = findUndecidedToolCalls(payload, resolutions);
if (undecided.length > 0) {
return { status: 400, error: 'Every paused tool call must be decided', undecided };
}
// Enforce the policy's per-tool allowed_decisions — a crafted POST must not
// approve a tool the policy restricted to (e.g.) reject/respond.
const disallowed = findDisallowedDecisions(payload, resolutions);
if (disallowed.length > 0) {
return { status: 403, error: 'Decision not permitted for one or more tools', disallowed };
}
// `edit`/`respond` must carry their payload — otherwise toSdkDecision's defensive
// defaults ({} / '') would resume with an empty input/result the user didn't approve.
const incomplete = findIncompleteDecisions(resolutions);
if (incomplete.length > 0) {
return {
status: 400,
error: 'edit requires editedArguments and respond requires responseText',
incomplete,
};
}
return { resumeValue: mapToolApprovalResolutions(resolutions) };
return resolveToolApprovalResume(payload, resolutions);
}
if (payload?.type === 'ask_user_question') {
return resolveAskUserQuestionResume(payload, body);

View file

@ -9,6 +9,7 @@ import {
buildToolApprovalPayload,
buildAskUserQuestionPayload,
buildPendingAction,
isToolApprovalPayloadValid,
toClientPendingAction,
computeAgentRequestFingerprint,
computeLegacyAgentRequestFingerprint,
@ -310,6 +311,54 @@ describe('buildPendingAction', () => {
expect(typeof action.createdAt).toBe('number');
});
test('rejects duplicate tool-call ids before the approval is persisted', () => {
const duplicatePayload: Agents.ToolApprovalInterruptPayload = {
type: 'tool_approval',
action_requests: [
{ name: 'shell', arguments: { command: 'rm marker' }, tool_call_id: 'duplicate' },
{ name: 'shell', arguments: { command: 'ls' }, tool_call_id: 'duplicate' },
],
review_configs: [
{
action_name: 'shell',
tool_call_id: 'duplicate',
allowed_decisions: ['approve', 'reject'],
},
{
action_name: 'shell',
tool_call_id: 'duplicate',
allowed_decisions: ['approve', 'reject'],
},
],
};
expect(isToolApprovalPayloadValid(duplicatePayload)).toBe(false);
expect(() => buildPendingAction(duplicatePayload, ctx)).toThrow(
'Invalid tool approval payload',
);
});
test('rejects review policies that do not map one-to-one to the requested calls', () => {
const mismatchedPayload: Agents.ToolApprovalInterruptPayload = {
type: 'tool_approval',
action_requests: [
{ name: 'read_file', arguments: { path: 'safe.txt' }, tool_call_id: 'call-1' },
],
review_configs: [
{
action_name: 'read_file',
tool_call_id: 'call-2',
allowed_decisions: ['approve', 'reject'],
},
],
};
expect(isToolApprovalPayloadValid(mismatchedPayload)).toBe(false);
expect(() => buildPendingAction(mismatchedPayload, ctx)).toThrow(
'Invalid tool approval payload',
);
});
test('wraps an ask_user_question payload with the same envelope', () => {
const askPayload: Agents.AskUserQuestionInterruptPayload = {
type: 'ask_user_question',

View file

@ -315,6 +315,42 @@ export function buildToolApprovalPayload(
};
}
/** Require one uniquely identified review policy for every paused tool call. */
export function isToolApprovalPayloadValid(payload: Agents.ToolApprovalInterruptPayload): boolean {
if (payload.action_requests.length !== payload.review_configs.length) {
return false;
}
const requestedIds = new Set<string>();
for (const request of payload.action_requests) {
if (
typeof request.tool_call_id !== 'string' ||
request.tool_call_id.length === 0 ||
requestedIds.has(request.tool_call_id)
) {
return false;
}
requestedIds.add(request.tool_call_id);
}
const reviewedIds = new Set<string>();
for (const config of payload.review_configs) {
if (
typeof config.tool_call_id !== 'string' ||
config.tool_call_id.length === 0 ||
reviewedIds.has(config.tool_call_id)
) {
return false;
}
if (!requestedIds.has(config.tool_call_id)) {
return false;
}
reviewedIds.add(config.tool_call_id);
}
return true;
}
/** Build an ask-user-question interrupt payload. */
export function buildAskUserQuestionPayload(
question: Agents.AskUserQuestionRequest,
@ -792,6 +828,9 @@ export function buildPendingAction(
payload: Agents.HumanInterruptPayload,
ctx: PendingActionContext,
): Agents.PendingAction {
if (payload.type === 'tool_approval' && !isToolApprovalPayloadValid(payload)) {
throw new Error('Invalid tool approval payload');
}
const createdAt = Date.now();
const ttlExpiresAt = typeof ctx.ttlMs === 'number' ? createdAt + ctx.ttlMs : undefined;
let absoluteExpiresAt: number | undefined;

View file

@ -14,6 +14,8 @@ import {
serializeAskUserAnswerVariants,
resolveAskUserQuestionResume,
findUndecidedToolCalls,
hasInvalidToolApprovalResolutions,
resolveToolApprovalResume,
findDisallowedDecisions,
findIncompleteDecisions,
createContentIndexOffsetHandlers,
@ -230,6 +232,79 @@ describe('findUndecidedToolCalls', () => {
});
});
describe('hasInvalidToolApprovalResolutions', () => {
const payload: Agents.ToolApprovalInterruptPayload = {
type: 'tool_approval',
action_requests: [
{ tool_call_id: 'a', name: 'read', arguments: {} },
{ tool_call_id: 'b', name: 'write', arguments: {} },
],
review_configs: [],
};
test('rejects duplicate decisions for one tool-call id', () => {
expect(
hasInvalidToolApprovalResolutions(payload, [
{ tool_call_id: 'a', decision: 'approve' },
{ tool_call_id: 'a', decision: 'reject' },
]),
).toBe(true);
});
test('rejects decisions for a tool call outside the pending batch', () => {
expect(
hasInvalidToolApprovalResolutions(payload, [
{ tool_call_id: 'a', decision: 'approve' },
{ tool_call_id: 'foreign', decision: 'approve' },
]),
).toBe(true);
});
test('accepts one decision for each requested tool call', () => {
expect(
hasInvalidToolApprovalResolutions(payload, [
{ tool_call_id: 'a', decision: 'approve' },
{ tool_call_id: 'b', decision: 'reject' },
]),
).toBe(false);
});
});
describe('resolveToolApprovalResume', () => {
const payload: Agents.ToolApprovalInterruptPayload = {
type: 'tool_approval',
action_requests: [
{ tool_call_id: 'a', name: 'read', arguments: {} },
{ tool_call_id: 'b', name: 'write', arguments: {} },
],
review_configs: [
{ tool_call_id: 'a', action_name: 'read', allowed_decisions: ['approve', 'reject'] },
{ tool_call_id: 'b', action_name: 'write', allowed_decisions: ['approve', 'reject'] },
],
};
test('fails closed when a persisted payload contains duplicate call identities', () => {
const aliased = {
...payload,
action_requests: [payload.action_requests[0], payload.action_requests[0]],
review_configs: [payload.review_configs[0], payload.review_configs[0]],
};
expect(
resolveToolApprovalResume(aliased, [{ tool_call_id: 'a', decision: 'approve' }]),
).toEqual({ status: 400, error: 'Invalid tool approval payload' });
});
test('returns the SDK decision map only for a complete unambiguous batch', () => {
expect(
resolveToolApprovalResume(payload, [
{ tool_call_id: 'a', decision: 'approve' },
{ tool_call_id: 'b', decision: 'reject' },
]),
).toEqual({ resumeValue: { a: { type: 'approve' }, b: { type: 'reject' } } });
});
});
describe('findDisallowedDecisions', () => {
const payload: Agents.ToolApprovalInterruptPayload = {
type: 'tool_approval',

View file

@ -9,6 +9,7 @@ import type {
} from '@librechat/agents';
import type { Agents } from 'librechat-data-provider';
import { ASK_USER_QUESTION_TOOL_NAME } from './askUserQuestionTool';
import { isToolApprovalPayloadValid } from './policy';
/**
* Translate the host-facing approval wire format into the SDK's resume value.
@ -266,6 +267,22 @@ export function findUndecidedToolCalls(
return payload.action_requests.map((a) => a.tool_call_id).filter((id) => !decided.has(id));
}
/** Reject ambiguous or foreign decisions before adapting them to the SDK's ID-keyed map. */
export function hasInvalidToolApprovalResolutions(
payload: Agents.ToolApprovalInterruptPayload,
resolutions: readonly Agents.ToolApprovalResolution[],
): boolean {
const requestedIds = new Set(payload.action_requests.map((request) => request.tool_call_id));
const resolutionIds = new Set<string>();
for (const resolution of resolutions) {
if (resolutionIds.has(resolution.tool_call_id) || !requestedIds.has(resolution.tool_call_id)) {
return true;
}
resolutionIds.add(resolution.tool_call_id);
}
return false;
}
/**
* Enforce the policy's per-tool `allowed_decisions`. Returns the `tool_call_id`s
* whose submitted decision is NOT one the interrupt's `review_configs` permits for
@ -317,6 +334,39 @@ export function findIncompleteDecisions(
.map((r) => r.tool_call_id);
}
/** Validate and translate one complete tool-approval batch for the resume controller. */
export function resolveToolApprovalResume(
payload: Agents.ToolApprovalInterruptPayload,
resolutions: readonly Agents.ToolApprovalResolution[],
):
| { resumeValue: ToolApprovalDecisionMap }
| { status: 400; error: string; undecided?: string[]; incomplete?: string[] }
| { status: 403; error: string; disallowed: string[] } {
if (!isToolApprovalPayloadValid(payload)) {
return { status: 400, error: 'Invalid tool approval payload' };
}
if (hasInvalidToolApprovalResolutions(payload, resolutions)) {
return { status: 400, error: 'Invalid tool approval decisions' };
}
const undecided = findUndecidedToolCalls(payload, resolutions);
if (undecided.length > 0) {
return { status: 400, error: 'Every paused tool call must be decided', undecided };
}
const disallowed = findDisallowedDecisions(payload, resolutions);
if (disallowed.length > 0) {
return { status: 403, error: 'Decision not permitted for one or more tools', disallowed };
}
const incomplete = findIncompleteDecisions(resolutions);
if (incomplete.length > 0) {
return {
status: 400,
error: 'edit requires editedArguments and respond requires responseText',
incomplete,
};
}
return { resumeValue: mapToolApprovalResolutions(resolutions) };
}
/**
* Reconcile persisted tool-step indices with the content being seeded into a
* rebuilt aggregator.