mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-09-21 15:45:22 +00:00
* feat: introduce optional content protection seam * feat: enforce source-aware content filters * feat: complete source-aware content enforcement * test: activate skill file-text fail-close fixtures * fix: harden source-aware content filters * fix: harden model-bound content filtering * fix: preserve legacy filters and generated files * fix: inspect shared scalar metadata * test: align mocks with current dev dependencies * feat: add persisted content filter safeguards * feat: complete source-aware content filter enforcement * fix: move resume content preflight into TypeScript * fix: close content inspection edge cases * fix: harden content protection boundaries * fix: complete content protection safeguards * test: align persisted memory filter coverage * fix: reconcile content protection with current dev * fix: reconcile content protection with latest dev * fix: close content protection review gaps * fix: enforce source-aware provider boundaries * fix: preserve legacy PII preflight semantics * test: stabilize stored branch preflight fixture * fix: defer agent writes until protected model admission * perf: harden source-aware model-bound filtering * fix: canonicalize provider lineage before validation * fix: satisfy model-bound callback type checks * perf: Bound content protection filtering work * fix: Bound submission array traversal * fix: Stabilize bounded content snapshots * fix: Scope model-bound traversal overflows * fix: Preserve scoped content inspection * fix: Accumulate aggregate traversal scopes * fix: centralize content policy boundaries * test: align deferred tool policy context * test: align controller policy mocks * style: normalize content protection imports * fix: close content policy review gaps * fix: narrow active skill policy config * fix: address content protection review boundaries * fix: retain exact provenance overflow sentinel * fix: preserve literal and scoped provenance updates * fix: narrow persisted edit provenance * fix: isolate exact overflow attribution * fix: centralize stored prompt protection * fix: fail closed on incomplete transcript evidence * fix: align canonical transcript routing * refactor: centralize content policy preflights * fix: isolate upload policy error typing * style: sort policy preflight imports * refactor: centralize content policy boundaries
217 lines
6.6 KiB
JavaScript
217 lines
6.6 KiB
JavaScript
const express = require('express');
|
|
const request = require('supertest');
|
|
|
|
const mockGetAllUserMemories = jest.fn();
|
|
const mockProjectStoredMemories = jest.fn((memories) => memories);
|
|
const mockSetMemoryById = jest.fn();
|
|
const mockDeleteMemoryById = jest.fn();
|
|
const mockOpaqueUpdateById = jest.fn((_req, res) => res.status(202).json({ delegated: 'update' }));
|
|
const mockOpaqueDeleteById = jest.fn((_req, res) => res.status(202).json({ delegated: 'delete' }));
|
|
let mockFilters;
|
|
|
|
jest.mock('@librechat/api', () => ({
|
|
Tokenizer: { getTokenCount: jest.fn(() => 1) },
|
|
generateCheckAccess: jest.fn(() => (_req, _res, next) => next()),
|
|
inspectContent: jest.fn(() => null),
|
|
extractMemoryContent: jest.fn(() => []),
|
|
projectStoredMemories: (...args) => mockProjectStoredMemories(...args),
|
|
createMemoryManagementHandlers: jest.fn(() => ({
|
|
updateById: (...args) => mockOpaqueUpdateById(...args),
|
|
deleteById: (...args) => mockOpaqueDeleteById(...args),
|
|
})),
|
|
contentFilterBlockResponse: jest.fn(),
|
|
}));
|
|
|
|
jest.mock('librechat-data-provider', () => ({
|
|
PermissionTypes: { MEMORIES: 'MEMORIES' },
|
|
Permissions: {
|
|
USE: 'USE',
|
|
READ: 'READ',
|
|
CREATE: 'CREATE',
|
|
UPDATE: 'UPDATE',
|
|
OPT_OUT: 'OPT_OUT',
|
|
},
|
|
ResourceType: { AGENT: 'agent' },
|
|
PermissionBits: { VIEW: 1 },
|
|
}));
|
|
|
|
jest.mock('~/server/services/PermissionService', () => ({
|
|
findAccessibleResources: jest.fn().mockResolvedValue([]),
|
|
}));
|
|
|
|
jest.mock('~/models', () => ({
|
|
getAllUserMemories: (...args) => mockGetAllUserMemories(...args),
|
|
getUserMemories: jest.fn(),
|
|
toggleUserMemories: jest.fn(),
|
|
getRoleByName: jest.fn(),
|
|
createMemory: jest.fn(),
|
|
deleteMemory: jest.fn(),
|
|
setMemory: jest.fn(),
|
|
setMemoryById: (...args) => mockSetMemoryById(...args),
|
|
deleteMemoryById: (...args) => mockDeleteMemoryById(...args),
|
|
getAgents: jest.fn(),
|
|
}));
|
|
|
|
jest.mock('~/server/middleware', () => ({
|
|
requireJwtAuth: (req, _res, next) => {
|
|
req.user = { id: 'user-1', role: 'USER' };
|
|
next();
|
|
},
|
|
configMiddleware: (req, _res, next) => {
|
|
req.config = {
|
|
filters: mockFilters,
|
|
memory: { tokenLimit: 100, charLimit: 10000 },
|
|
};
|
|
next();
|
|
},
|
|
}));
|
|
|
|
const memoriesRouter = require('./memories');
|
|
|
|
const buildApp = () => {
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use('/api/memories', memoriesRouter);
|
|
return app;
|
|
};
|
|
|
|
describe('GET /api/memories content policy', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
mockFilters = {
|
|
memories: {
|
|
pii: {
|
|
starterPatterns: [],
|
|
customPatterns: [{ id: 'private', label: 'private value', regex: 'PRIVATE' }],
|
|
},
|
|
},
|
|
};
|
|
mockProjectStoredMemories.mockImplementation((memories) => memories);
|
|
});
|
|
|
|
it('returns a marked raw-free projection when current policy blocks a stored memory', async () => {
|
|
const secret = 'PRIVATE-STORED';
|
|
const stored = [
|
|
{
|
|
_id: 'memory-1',
|
|
userId: 'user-1',
|
|
key: 'safe key',
|
|
value: secret,
|
|
tokenCount: 7,
|
|
updated_at: '2026-08-01T00:00:00.000Z',
|
|
},
|
|
];
|
|
mockGetAllUserMemories.mockResolvedValue(stored);
|
|
mockProjectStoredMemories.mockReturnValue([
|
|
{
|
|
...stored[0],
|
|
value: '',
|
|
contentFilterBlocked: true,
|
|
},
|
|
]);
|
|
|
|
const response = await request(buildApp()).get('/api/memories');
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(mockProjectStoredMemories).toHaveBeenCalledWith(stored, mockFilters);
|
|
expect(response.body.memories).toEqual([
|
|
expect.objectContaining({
|
|
_id: 'memory-1',
|
|
key: 'safe key',
|
|
value: '',
|
|
contentFilterBlocked: true,
|
|
}),
|
|
]);
|
|
expect(response.body.totalTokens).toBe(7);
|
|
expect(JSON.stringify(response.body)).not.toContain(secret);
|
|
});
|
|
|
|
it('preserves safe stored memories and their existing response metadata', async () => {
|
|
const stored = [
|
|
{
|
|
_id: 'memory-safe',
|
|
userId: 'user-1',
|
|
key: 'timezone',
|
|
value: 'UTC',
|
|
tokenCount: 3,
|
|
updated_at: '2026-08-02T00:00:00.000Z',
|
|
},
|
|
];
|
|
mockGetAllUserMemories.mockResolvedValue(stored);
|
|
|
|
const response = await request(buildApp()).get('/api/memories');
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body.memories).toEqual(stored);
|
|
expect(response.body).toMatchObject({
|
|
totalTokens: 3,
|
|
tokenLimit: 100,
|
|
charLimit: 10000,
|
|
usagePercentage: 3,
|
|
});
|
|
});
|
|
|
|
it('does not expose stored content through projection errors', async () => {
|
|
const secret = 'PRIVATE-ERROR-CONTEXT';
|
|
mockGetAllUserMemories.mockResolvedValue([{ key: 'safe key', value: secret }]);
|
|
mockProjectStoredMemories.mockImplementationOnce(() => {
|
|
throw new Error(`projection failed for ${secret}`);
|
|
});
|
|
|
|
const response = await request(buildApp()).get('/api/memories');
|
|
|
|
expect(response.status).toBe(500);
|
|
expect(response.body).toEqual({ error: 'Failed to retrieve memories.' });
|
|
expect(JSON.stringify(response.body)).not.toContain(secret);
|
|
});
|
|
});
|
|
|
|
describe('opaque memory management routes', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
mockFilters = {
|
|
memories: {
|
|
pii: {
|
|
starterPatterns: [],
|
|
customPatterns: [{ id: 'private', label: 'private value', regex: 'PRIVATE' }],
|
|
},
|
|
},
|
|
};
|
|
});
|
|
|
|
it('delegates id updates to the typed handler after request middleware', async () => {
|
|
const response = await request(buildApp())
|
|
.patch('/api/memories/id/507f1f77bcf86cd799439011?agentId=agent-1')
|
|
.send({ value: 'safe replacement' });
|
|
|
|
expect(response.status).toBe(202);
|
|
expect(response.body).toEqual({ delegated: 'update' });
|
|
expect(mockOpaqueUpdateById).toHaveBeenCalledTimes(1);
|
|
expect(mockOpaqueUpdateById.mock.calls[0][0]).toEqual(
|
|
expect.objectContaining({
|
|
params: { id: '507f1f77bcf86cd799439011' },
|
|
query: { agentId: 'agent-1' },
|
|
body: { value: 'safe replacement' },
|
|
user: { id: 'user-1', role: 'USER' },
|
|
config: expect.objectContaining({ filters: mockFilters }),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('delegates id deletes to the typed handler', async () => {
|
|
const response = await request(buildApp()).delete(
|
|
'/api/memories/id/507f1f77bcf86cd799439011?agentId=agent-1',
|
|
);
|
|
|
|
expect(response.status).toBe(202);
|
|
expect(response.body).toEqual({ delegated: 'delete' });
|
|
expect(mockOpaqueDeleteById).toHaveBeenCalledTimes(1);
|
|
expect(mockOpaqueDeleteById.mock.calls[0][0]).toEqual(
|
|
expect.objectContaining({
|
|
params: { id: '507f1f77bcf86cd799439011' },
|
|
query: { agentId: 'agent-1' },
|
|
user: { id: 'user-1', role: 'USER' },
|
|
}),
|
|
);
|
|
});
|
|
});
|