mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 14:08:21 +00:00
* 🪵 fix: Aggregate Audit Findings and Rate Limit Shared Link Reads Audit-mode content filter rules wrote one `logger.info` per matching fragment, and inspection deliberately continues past every finding. A shared conversation full of distinct matching strings therefore produced one log write per fragment, regenerated on every retrieval of `GET /api/share/:shareId` — a public, unauthenticated route with no limiter. The 4,096-entry inspection dedupe set is a memory bound, not an event budget: once full, further values are inspected and logged again. Audit metadata carries no inspected text — only rule, source, field and provenance — so thousands of those writes were byte-identical lines. Findings are now counted inside an aggregation scope and reported once per distinct key with an `occurrences` count when the scope ends: - `assertModelBoundContent` opens a scope when audit rules are configured, bounding one inspection pass (a single message's fragments). - `assertConversationImportContentAllowed` opens the outer scope for import and shared-link snapshots, so per-message inspections nest into one report instead of one per message. - `createShareContentPreflight` spans the shared-file metadata pass too. Nested scopes reuse the outermost aggregation, and outside any scope the previous immediate write is kept. `GET /api/share/:shareId` re-inspects the whole snapshot on every request, so it now carries IP and user limiters like the neighbouring fork route (`SHARE_IP_MAX`/`SHARE_IP_WINDOW`, `SHARE_USER_MAX`/`SHARE_USER_WINDOW`, `SHARE_VIOLATION_SCORE`); the user limiter skips anonymous viewers. * 🔒 fix: Address Codex Findings on Share Audit Aggregation - The aggregation key joined rule fields with a space, but the filter schema allows spaces in custom pattern ids and labels, so `{id: 'a', label: 'b c'}` and `{id: 'a b', label: 'c'}` collided on the same source/field/provenance and merged two rules' counts under one identity. Serialize the tuple instead. - `SHARE_VIOLATION_SCORE` left unset reached `logViolation` as `undefined`, which selects that function's default score of 1 rather than the documented zero. With default ban settings a viewer refreshing a rate-limited shared link would be banned after 20 rejections. Default the score to 0 in code, not only in `.env.example`. - `removePorts` returns a full IPv6 address, so an attacker holding a prefix could rotate the host portion for a fresh bucket on every request — and for anonymous viewers the IP limiter is the only bound. Derive the key through `ipKeyGenerator` so IPv6 clients group by /56.
127 lines
3.7 KiB
JavaScript
127 lines
3.7 KiB
JavaScript
const express = require('express');
|
|
const request = require('supertest');
|
|
|
|
const mockLogViolation = jest.fn();
|
|
|
|
jest.mock('@librechat/api', () => ({
|
|
limiterCache: jest.fn(() => undefined),
|
|
removePorts: (req) => req.ip,
|
|
}));
|
|
jest.mock(
|
|
'~/cache/logViolation',
|
|
() =>
|
|
(...args) =>
|
|
mockLogViolation(...args),
|
|
);
|
|
|
|
describe('shared link limiters', () => {
|
|
let originalEnv;
|
|
|
|
beforeEach(() => {
|
|
originalEnv = { ...process.env };
|
|
jest.resetModules();
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.env = originalEnv;
|
|
});
|
|
|
|
const buildApp = (user, { trustProxy = false } = {}) => {
|
|
const { createShareLimiters } = require('./shareLimiters');
|
|
const { shareIpLimiter, shareUserLimiter } = createShareLimiters();
|
|
const app = express();
|
|
if (trustProxy) {
|
|
app.set('trust proxy', true);
|
|
}
|
|
app.get(
|
|
'/api/share/:shareId',
|
|
(req, _res, next) => {
|
|
req.user = user;
|
|
next();
|
|
},
|
|
shareIpLimiter,
|
|
shareUserLimiter,
|
|
(_req, res) => res.status(200).json({ ok: true }),
|
|
);
|
|
return app;
|
|
};
|
|
|
|
it('rejects an anonymous retrieval flood by IP', async () => {
|
|
process.env.SHARE_IP_MAX = '2';
|
|
process.env.SHARE_IP_WINDOW = '1';
|
|
const app = buildApp(undefined);
|
|
|
|
await request(app).get('/api/share/share-1').expect(200);
|
|
await request(app).get('/api/share/share-1').expect(200);
|
|
const response = await request(app).get('/api/share/share-1').expect(429);
|
|
|
|
expect(response.body).toEqual({ message: 'Too many shared link requests. Try again later' });
|
|
expect(mockLogViolation).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
expect.anything(),
|
|
'share_limit',
|
|
expect.objectContaining({ limiter: 'ip', max: 2, windowInMinutes: 1 }),
|
|
0,
|
|
);
|
|
});
|
|
|
|
it('keeps an unset violation score from banning shared link viewers', async () => {
|
|
process.env.SHARE_IP_MAX = '1';
|
|
delete process.env.SHARE_VIOLATION_SCORE;
|
|
const app = buildApp({ id: 'user-1' });
|
|
|
|
await request(app).get('/api/share/share-1').expect(200);
|
|
await request(app).get('/api/share/share-1').expect(429);
|
|
|
|
expect(mockLogViolation).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
expect.anything(),
|
|
'share_limit',
|
|
expect.anything(),
|
|
0,
|
|
);
|
|
});
|
|
|
|
it('buckets IPv6 viewers by subnet so address rotation cannot mint a bucket', async () => {
|
|
process.env.SHARE_IP_MAX = '1';
|
|
const app = buildApp(undefined, { trustProxy: true });
|
|
|
|
const retrieve = (address) =>
|
|
request(app).get('/api/share/share-1').set('X-Forwarded-For', address);
|
|
|
|
expect((await retrieve('2001:db8:1234:5600::1')).status).toBe(200);
|
|
expect((await retrieve('2001:db8:1234:5622::9')).status).toBe(429);
|
|
expect((await retrieve('2001:db8:1234:5700::1')).status).toBe(200);
|
|
});
|
|
|
|
it('rejects an authenticated retrieval flood by user', async () => {
|
|
process.env.SHARE_IP_MAX = '100';
|
|
process.env.SHARE_USER_MAX = '1';
|
|
process.env.SHARE_USER_WINDOW = '1';
|
|
process.env.SHARE_VIOLATION_SCORE = '3';
|
|
const app = buildApp({ id: 'user-1' });
|
|
|
|
await request(app).get('/api/share/share-1').expect(200);
|
|
await request(app).get('/api/share/share-1').expect(429);
|
|
|
|
expect(mockLogViolation).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
expect.anything(),
|
|
'share_limit',
|
|
expect.objectContaining({ limiter: 'user', max: 1 }),
|
|
'3',
|
|
);
|
|
});
|
|
|
|
it('leaves the per-user bucket untouched for anonymous viewers', async () => {
|
|
process.env.SHARE_IP_MAX = '100';
|
|
process.env.SHARE_USER_MAX = '1';
|
|
const app = buildApp(undefined);
|
|
|
|
await request(app).get('/api/share/share-1').expect(200);
|
|
await request(app).get('/api/share/share-1').expect(200);
|
|
|
|
expect(mockLogViolation).not.toHaveBeenCalled();
|
|
});
|
|
});
|