mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-21 15:33:45 +00:00
🧑🔬 fix: Preserve File Checks for Large Agent Tool Catalogs (#18405)
* fix: inspect agent tool catalogs in bounded file-policy units * test: align agent inspection fixtures with published contracts
This commit is contained in:
parent
2e792a3a73
commit
67d44d5edc
4 changed files with 366 additions and 13 deletions
|
|
@ -5562,6 +5562,85 @@ describe('initializeAgent — authorized run file snapshots', () => {
|
|||
}
|
||||
});
|
||||
|
||||
it('inspects a hydrated initialized agent with a large tool catalog without extra file reads', async () => {
|
||||
const { assertModelBoundContent } = jest.requireActual<
|
||||
typeof import('../../middleware/modelBoundContent')
|
||||
>('../../middleware/modelBoundContent');
|
||||
const loadedToolDefinitions = Array.from({ length: 67 }, (_, index) => ({
|
||||
name: `report_${index}`,
|
||||
description: 'Query a report',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: Object.fromEntries(
|
||||
Array.from({ length: 64 }, (_, field) => [
|
||||
`field_${field}`,
|
||||
{ type: 'string', description: 'A report dimension', enum: ['one', 'two'] },
|
||||
]),
|
||||
),
|
||||
},
|
||||
}));
|
||||
const { agent, req, loadTools, db } = createMocks({ loadedToolDefinitions });
|
||||
req.config = {
|
||||
config: {},
|
||||
fileStrategy: FileSources.local,
|
||||
imageOutputType: EImageOutputType.PNG,
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
openAI: {
|
||||
supportedMimeTypes: ['text/plain'],
|
||||
defaultLLMDeliveryPath: { fallback: 'text' },
|
||||
},
|
||||
},
|
||||
},
|
||||
filters: {
|
||||
files: {
|
||||
pii: {
|
||||
starterPatterns: [],
|
||||
fields: ['content', 'extracted_text'],
|
||||
uninspectable: 'block',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
const file = Object.freeze(
|
||||
inputFile({
|
||||
type: 'text/plain',
|
||||
filename: 'context.txt',
|
||||
text: 'safe extracted text',
|
||||
llmDeliveryPath: 'text',
|
||||
}),
|
||||
);
|
||||
const result = await initializeAgent(
|
||||
{
|
||||
req,
|
||||
agent,
|
||||
loadTools,
|
||||
authorizedRunFiles: [file],
|
||||
allowedProviders: new Set([Providers.OPENAI]),
|
||||
},
|
||||
db,
|
||||
);
|
||||
expect(result.toolDefinitions).toHaveLength(67);
|
||||
expect(result.requestAttachments).toHaveLength(1);
|
||||
expect(() =>
|
||||
assertModelBoundContent({ filters: req.config?.filters, agents: [result as never] }),
|
||||
).not.toThrow();
|
||||
expect(db.getFiles).not.toHaveBeenCalled();
|
||||
expect(db.getConvoFiles).not.toHaveBeenCalled();
|
||||
expect(db.getToolFilesByIds).not.toHaveBeenCalled();
|
||||
expect(file.filepath).toBe('/uploads/current.pdf');
|
||||
|
||||
// Responses callers can merge definitions after initialization; the final check must see them.
|
||||
result.toolDefinitions?.push({
|
||||
name: 'caller',
|
||||
description: 'data:application/pdf;base64,c2VjcmV0',
|
||||
parameters: { type: 'object' },
|
||||
});
|
||||
expect(() =>
|
||||
assertModelBoundContent({ filters: req.config?.filters, agents: [result as never] }),
|
||||
).toThrow('Submitted file content could not be inspected before processing.');
|
||||
});
|
||||
|
||||
it.each([undefined, 'Extracted document contents'])(
|
||||
'advertises a queued file before tool execution with extracted text %p',
|
||||
async (text) => {
|
||||
|
|
|
|||
222
packages/api/src/middleware/modelBoundContent.tools.spec.ts
Normal file
222
packages/api/src/middleware/modelBoundContent.tools.spec.ts
Normal file
|
|
@ -0,0 +1,222 @@
|
|||
import { z } from 'zod';
|
||||
import { tool } from '@langchain/core/tools';
|
||||
import type { FiltersConfig } from 'librechat-data-provider';
|
||||
import { assertModelBoundContent } from './modelBoundContent';
|
||||
|
||||
const opaque = 'data:application/pdf;base64,c2VjcmV0';
|
||||
const filters: FiltersConfig = {
|
||||
files: {
|
||||
pii: {
|
||||
starterPatterns: [],
|
||||
fields: ['content', 'extracted_text'],
|
||||
uninspectable: 'block',
|
||||
},
|
||||
},
|
||||
};
|
||||
const file = {
|
||||
file_id: 'file-context',
|
||||
filename: 'context.txt',
|
||||
filepath: '/uploads/context.txt',
|
||||
text: 'Safe extracted context',
|
||||
type: 'text/plain',
|
||||
llmDeliveryPath: 'text',
|
||||
};
|
||||
|
||||
function catalog() {
|
||||
return Array.from({ length: 67 }, (_, index) => ({
|
||||
name: `query_${index}`,
|
||||
description: 'Query a report',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: Object.fromEntries(
|
||||
Array.from({ length: 64 }, (_, field) => [
|
||||
`field_${field}`,
|
||||
{ type: 'string', description: 'A report dimension', enum: ['one', 'two'] },
|
||||
]),
|
||||
),
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
describe('agent tool catalogs at the file-inspection boundary', () => {
|
||||
it('does not turn audit-only schema exhaustion into a file-inspection block', () => {
|
||||
const audit = {
|
||||
action: 'audit' as const,
|
||||
starterPatterns: [],
|
||||
customPatterns: [{ id: 'private', label: 'private value', regex: 'PRIVATE-[A-Z]+' }],
|
||||
};
|
||||
expect(() =>
|
||||
assertModelBoundContent({
|
||||
filters: {
|
||||
messages: { pii: audit },
|
||||
files: { pii: { ...audit, uninspectable: 'allow' } },
|
||||
toolArguments: { pii: audit },
|
||||
agentInstructions: { pii: audit },
|
||||
},
|
||||
agents: [
|
||||
{
|
||||
toolDefinitions: [
|
||||
{ name: 'large', parameters: Array.from({ length: 4_200 }, () => ({})) },
|
||||
],
|
||||
requestAttachments: [file],
|
||||
} as never,
|
||||
],
|
||||
}),
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it('keeps blocking message and instruction policies active when opaque files are allowed', () => {
|
||||
const block = {
|
||||
starterPatterns: [],
|
||||
customPatterns: [{ id: 'private', label: 'private value', regex: 'PRIVATE-[A-Z]+' }],
|
||||
};
|
||||
for (const policy of [
|
||||
{
|
||||
filters: { messages: { pii: block } },
|
||||
submittedMessages: [{ role: 'user', content: 'PRIVATE-MESSAGE' }],
|
||||
},
|
||||
{
|
||||
filters: { agentInstructions: { pii: block } },
|
||||
agents: [{ instructions: 'PRIVATE-INSTRUCTIONS', requestAttachments: [file] }],
|
||||
},
|
||||
]) {
|
||||
expect(() => assertModelBoundContent(policy)).toThrow(
|
||||
'Submitted content contains a private value',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it.each([true, false])('inspects all tool definitions with a hydrated file: %s', (hydrated) => {
|
||||
const toolDefinitions = catalog();
|
||||
const agent = { toolDefinitions, requestAttachments: hydrated ? [file] : [] };
|
||||
expect(() => assertModelBoundContent({ filters, agents: [agent] })).not.toThrow();
|
||||
expect(agent.toolDefinitions).toBe(toolDefinitions);
|
||||
expect(agent.requestAttachments[0]).toBe(hydrated ? file : undefined);
|
||||
});
|
||||
|
||||
it('admits a real initialized SDK tool catalog, not only JSON definitions', () => {
|
||||
const tools = Array.from({ length: 67 }, (_, index) =>
|
||||
tool(async () => 'result', {
|
||||
name: `query_${index}`,
|
||||
description: 'Query a report',
|
||||
schema: z.object({ dimension: z.string(), filter: z.string().optional() }),
|
||||
}),
|
||||
);
|
||||
expect(() =>
|
||||
assertModelBoundContent({
|
||||
filters,
|
||||
agents: [{ tools, toolDefinitions: catalog(), requestAttachments: [file] } as never],
|
||||
}),
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it.each([true, false])(
|
||||
'blocks opaque content in the final caller tool (hydrated: %s)',
|
||||
(hydrated) => {
|
||||
for (const payload of [
|
||||
{ description: opaque },
|
||||
{ parameters: { nested: { payload: opaque } } },
|
||||
{ parameters: { nested: { type: 'input_file', file_id: 'unresolved' } } },
|
||||
{ extra: { payload: opaque } },
|
||||
]) {
|
||||
const toolDefinitions = catalog();
|
||||
const agent = { toolDefinitions, requestAttachments: hydrated ? [file] : [] };
|
||||
expect(() => assertModelBoundContent({ filters, agents: [agent] })).not.toThrow();
|
||||
Object.assign(toolDefinitions[66], payload);
|
||||
expect(() => assertModelBoundContent({ filters, agents: [agent] })).toThrow(
|
||||
'Submitted file content could not be inspected before processing.',
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it('preserves independent source-specific inspection of late tool descriptions', () => {
|
||||
const toolDefinitions = catalog();
|
||||
toolDefinitions[66].description = 'PRIVATE-LATE';
|
||||
expect(() =>
|
||||
assertModelBoundContent({
|
||||
filters: {
|
||||
...filters,
|
||||
agentInstructions: {
|
||||
pii: {
|
||||
fields: ['description'],
|
||||
starterPatterns: [],
|
||||
customPatterns: [{ id: 'private', label: 'private value', regex: 'PRIVATE-[A-Z]+' }],
|
||||
},
|
||||
},
|
||||
},
|
||||
agents: [{ toolDefinitions, requestAttachments: [file] } as never],
|
||||
}),
|
||||
).toThrow('Submitted content contains a private value');
|
||||
});
|
||||
|
||||
it('still fails closed when a single definition exceeds its structural budget', () => {
|
||||
const toolDefinitions = [
|
||||
{ name: 'huge', parameters: Array.from({ length: 4_200 }, () => ({})) },
|
||||
];
|
||||
expect(() =>
|
||||
assertModelBoundContent({
|
||||
filters,
|
||||
agents: [{ toolDefinitions, requestAttachments: [file] } as never],
|
||||
}),
|
||||
).toThrow('Submitted content could not be completely inspected before processing.');
|
||||
});
|
||||
|
||||
it('still bounds the number of tool entries', () => {
|
||||
const toolDefinitions = Array.from({ length: 4_200 }, () => ({ name: 'tool' }));
|
||||
expect(() => assertModelBoundContent({ filters, agents: [{ toolDefinitions }] })).toThrow(
|
||||
'Submitted content could not be completely inspected before processing.',
|
||||
);
|
||||
});
|
||||
|
||||
it('inspects enumerable non-index properties on a tool array', () => {
|
||||
const toolDefinitions = Object.assign(catalog(), { payload: opaque });
|
||||
expect(() => assertModelBoundContent({ filters, agents: [{ toolDefinitions }] })).toThrow(
|
||||
'Submitted file content could not be inspected before processing.',
|
||||
);
|
||||
});
|
||||
|
||||
it('inspects late executable-tool fields as well as toolDefinitions', () => {
|
||||
const tools = catalog();
|
||||
const agent = { tools, requestAttachments: [file] };
|
||||
expect(() => assertModelBoundContent({ filters, agents: [agent as never] })).not.toThrow();
|
||||
tools[66].description = opaque;
|
||||
expect(() => assertModelBoundContent({ filters, agents: [agent as never] })).toThrow(
|
||||
'Submitted file content could not be inspected before processing.',
|
||||
);
|
||||
});
|
||||
|
||||
it.each([true, false])('bounds sparse catalogs before inspection (hydrated: %s)', (hydrated) => {
|
||||
const toolDefinitions = new Array<{ name: string }>(1_000_000);
|
||||
expect(() =>
|
||||
assertModelBoundContent({
|
||||
filters,
|
||||
agents: [{ toolDefinitions, requestAttachments: hydrated ? [file] : [] } as never],
|
||||
}),
|
||||
).toThrow('Submitted content could not be completely inspected before processing.');
|
||||
});
|
||||
|
||||
it('fails closed on a revoked catalog proxy without exposing its reflection error', () => {
|
||||
const { proxy, revoke } = Proxy.revocable([{ name: 'query' }], {});
|
||||
revoke();
|
||||
expect(() =>
|
||||
assertModelBoundContent({ filters, agents: [{ toolDefinitions: proxy }] }),
|
||||
).toThrow('Submitted content could not be completely inspected before processing.');
|
||||
});
|
||||
|
||||
it.each(['proxy', 'getter'])('fails closed on a catalog %s', (kind) => {
|
||||
const toolDefinitions = [{ name: 'query' }];
|
||||
const catalog = kind === 'proxy' ? new Proxy(toolDefinitions, {}) : toolDefinitions;
|
||||
if (kind === 'getter') {
|
||||
Object.defineProperty(catalog, '0', {
|
||||
enumerable: true,
|
||||
get: () => {
|
||||
throw new Error('blocked');
|
||||
},
|
||||
});
|
||||
}
|
||||
expect(() =>
|
||||
assertModelBoundContent({ filters, agents: [{ toolDefinitions: catalog }] }),
|
||||
).toThrow('Submitted content could not be completely inspected before processing.');
|
||||
});
|
||||
});
|
||||
|
|
@ -66,11 +66,11 @@ import {
|
|||
getUserSubmittedMessageFieldPathState,
|
||||
getUserSubmittedPathState,
|
||||
} from '../protection/provenance';
|
||||
import { projectAgentFileInspection, visitAgentFileInspectionUnits } from '../protection/metadata';
|
||||
import { extractMessageContent, snapshotExternalMessages } from '../protection/adapters/messages';
|
||||
import { ContentTraversalLimitError } from '../protection/adapters/nested';
|
||||
import { ContentFilterError, isContentFilterError } from './contentFilter';
|
||||
import { createConfiguredContentInspector } from '../protection/runtime';
|
||||
import { projectAgentFileInspection } from '../protection/metadata';
|
||||
import { aggregateAuditFindingsSync } from '../protection/audit';
|
||||
|
||||
export type ModelBoundProviderAttribution = 'user' | 'model' | 'tool' | 'synthetic';
|
||||
|
|
@ -3881,22 +3881,24 @@ function inspectModelBoundContent(
|
|||
agentFilesById.set(resolvedFile.file_id, resolvedFile);
|
||||
}
|
||||
}
|
||||
const fileInspectionAgent =
|
||||
agentFilesById.size > 0 ||
|
||||
const inspectCatalogUnits =
|
||||
getBlockedUninspectableFileField(input.filters, [
|
||||
'content',
|
||||
'extracted_text',
|
||||
'transcript',
|
||||
]) != null
|
||||
? projectAgentFileInspection(agent)
|
||||
: agent;
|
||||
assertInspectableFileInput(
|
||||
input.filters,
|
||||
omitResolvedCanonicalFileLocators(fileInspectionAgent, agentFilesById, {
|
||||
onTraversalFailure: input.onTraversalFailure,
|
||||
messageCount: input.storedMessages?.length ?? 0,
|
||||
}),
|
||||
);
|
||||
]) != null;
|
||||
const inspectAgentUnit = (unit: unknown): void => {
|
||||
assertInspectableFileInput(
|
||||
input.filters,
|
||||
omitResolvedCanonicalFileLocators(unit, agentFilesById, {
|
||||
onTraversalFailure: input.onTraversalFailure,
|
||||
messageCount: input.storedMessages?.length ?? 0,
|
||||
}),
|
||||
);
|
||||
};
|
||||
if (inspectCatalogUnits) {
|
||||
visitAgentFileInspectionUnits(projectAgentFileInspection(agent), inspectAgentUnit);
|
||||
}
|
||||
appendExtractedContent(() => extractAgentContent(agent, inspector?.shouldInspect));
|
||||
}
|
||||
for (const assistant of input.assistants ?? []) {
|
||||
|
|
|
|||
|
|
@ -32,3 +32,53 @@ export function projectAgentFileInspection(
|
|||
}
|
||||
return Object.fromEntries(snapshot.entries.filter(([key]) => key !== 'deliveryRouting'));
|
||||
}
|
||||
|
||||
/** Tool catalogs are envelopes: inspect every entry, with the same bound as the agent body. */
|
||||
export function visitAgentFileInspectionUnits(
|
||||
agent: AgentContentInput | null | undefined,
|
||||
inspect: (unit: unknown) => void,
|
||||
): void {
|
||||
if (agent == null || isProxy(agent)) {
|
||||
inspect(agent);
|
||||
return;
|
||||
}
|
||||
const snapshot = getBoundedOwnEnumerableEntries(agent, CONTENT_TRAVERSAL_MAX_NODES);
|
||||
if (!snapshot.complete) {
|
||||
throw new ContentTraversalLimitError();
|
||||
}
|
||||
const catalogs: [string, unknown[]][] = [];
|
||||
const body: [string, unknown][] = [];
|
||||
for (const [key, value] of snapshot.entries) {
|
||||
if ((key === 'tools' || key === 'toolDefinitions') && isProxy(value)) {
|
||||
throw new ContentTraversalLimitError();
|
||||
}
|
||||
if ((key === 'tools' || key === 'toolDefinitions') && Array.isArray(value)) {
|
||||
catalogs.push([key, value]);
|
||||
} else {
|
||||
body.push([key, value]);
|
||||
}
|
||||
}
|
||||
inspect(Object.fromEntries(body));
|
||||
|
||||
let remainingEntries = CONTENT_TRAVERSAL_MAX_NODES;
|
||||
for (const [key, catalog] of catalogs) {
|
||||
const length: unknown = Object.getOwnPropertyDescriptor(catalog, 'length')?.value;
|
||||
if (
|
||||
typeof length !== 'number' ||
|
||||
!Number.isSafeInteger(length) ||
|
||||
length < 0 ||
|
||||
length > remainingEntries
|
||||
) {
|
||||
throw new ContentTraversalLimitError();
|
||||
}
|
||||
const entries = getBoundedOwnEnumerableEntries(catalog, remainingEntries);
|
||||
if (!entries.complete) {
|
||||
throw new ContentTraversalLimitError();
|
||||
}
|
||||
remainingEntries -= Math.max(length, entries.entries.length);
|
||||
for (const entry of entries.entries) {
|
||||
// Preserve even non-index properties: none of the catalog is exempt from file policy.
|
||||
inspect(Object.fromEntries([[key, Object.fromEntries([entry])]]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue