mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 14:08:21 +00:00
📬 feat: Persist Channel Events and Delivery Receipts (#17595)
* 📬 feat: Persist Channel Events and Delivery Receipts
* fix: enforce channel run identity and fenced recovery
* fix: fence and erase channel state during account deletion
* refactor: share channel owner admission with private persistence
* refactor: name channel account-erasure boundary explicitly
* fix: prove channel identity and delivery authority at storage boundaries
* refactor: enforce channel delivery protocol and terminal lifecycle
* fix: enforce channel tenant and operation boundaries
* fix: initialize channel config from its composition owner
* test: verify config installs channel storage reader
* test: complete config initializer contract in model mocks
* test: include channel config reader in migration fixture
* test: await SSE subscription before advancing keepalive time
* test: Observe and Drain Concurrent Fixture Work
This commit is contained in:
parent
1458e841e4
commit
7dd4fafdf3
48 changed files with 3816 additions and 54 deletions
35
packages/api/src/channels/config.spec.ts
Normal file
35
packages/api/src/channels/config.spec.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import type { AppConfig } from '@librechat/data-schemas';
|
||||
import { createChannelStorageConfigReader } from './config';
|
||||
|
||||
it('fails closed until the configuration service installs its reader', async () => {
|
||||
await expect(createChannelStorageConfigReader().getConfig()).rejects.toThrow(
|
||||
'not been initialized',
|
||||
);
|
||||
});
|
||||
|
||||
it('reads live base deployment bounds at each operation boundary', async () => {
|
||||
const config = { endpoints: { agents: { channels: { erasureBatchSize: 2 } } } };
|
||||
const read = jest.fn(async () => config as AppConfig);
|
||||
const effective = createChannelStorageConfigReader();
|
||||
effective.initialize(read);
|
||||
expect(await effective.getConfig()).toMatchObject({ erasureBatchSize: 2, claimBatchSize: 25 });
|
||||
config.endpoints.agents.channels.erasureBatchSize = 3;
|
||||
expect((await effective.getConfig()).erasureBatchSize).toBe(3);
|
||||
expect(read).toHaveBeenCalledTimes(2);
|
||||
expect(read).toHaveBeenLastCalledWith({ baseOnly: true });
|
||||
effective.initialize(async () => ({}) as AppConfig);
|
||||
expect((await effective.getConfig()).erasureBatchSize).toBe(250);
|
||||
});
|
||||
|
||||
it('rejects invalid bounds and propagates config outages', async () => {
|
||||
const effective = createChannelStorageConfigReader();
|
||||
effective.initialize(
|
||||
async () => ({ endpoints: { agents: { channels: { erasureBatchSize: 0 } } } }) as AppConfig,
|
||||
);
|
||||
await expect(effective.getConfig()).rejects.toThrow();
|
||||
const failure = new Error('config unavailable');
|
||||
effective.initialize(async () => {
|
||||
throw failure;
|
||||
});
|
||||
await expect(effective.getConfig()).rejects.toBe(failure);
|
||||
});
|
||||
22
packages/api/src/channels/config.ts
Normal file
22
packages/api/src/channels/config.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { channelStorageConfigSchema } from 'librechat-data-provider';
|
||||
import type { ChannelStorageConfig } from 'librechat-data-provider';
|
||||
import type { AppConfig } from '@librechat/data-schemas';
|
||||
import type { GetAppConfigOptions } from '~/app/service';
|
||||
|
||||
/** Config installs its reader during composition, without a models-to-config import cycle. */
|
||||
export function createChannelStorageConfigReader(): {
|
||||
initialize: (reader: (options?: GetAppConfigOptions) => Promise<AppConfig>) => void;
|
||||
getConfig: () => Promise<ChannelStorageConfig>;
|
||||
} {
|
||||
let readConfig: ((options?: GetAppConfigOptions) => Promise<AppConfig>) | undefined;
|
||||
return {
|
||||
initialize(reader) {
|
||||
readConfig = reader;
|
||||
},
|
||||
async getConfig() {
|
||||
if (!readConfig) throw new Error('Channel storage config reader has not been initialized');
|
||||
const config = await readConfig({ baseOnly: true });
|
||||
return channelStorageConfigSchema.parse(config.endpoints?.agents?.channels ?? {});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
@ -155,3 +155,5 @@ export type * from './types';
|
|||
export * from './tools/approval';
|
||||
|
||||
export * from './mcp/approval';
|
||||
|
||||
export { createChannelStorageConfigReader } from './channels/config';
|
||||
|
|
|
|||
|
|
@ -574,3 +574,22 @@ describe('requirePreAuthTenantContext', () => {
|
|||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('reserved tenant partition', () => {
|
||||
it('rejects a reserved tenant restored from the authenticated user', () => {
|
||||
const req = mockReq({ tenantId: '_', role: 'user' });
|
||||
const res = mockRes();
|
||||
const next = jest.fn();
|
||||
tenantContextMiddleware(req, res, next);
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
it('rejects the reserved tenant when a request resumes after an async boundary', async () => {
|
||||
const req = mockReq({ tenantId: '_', role: 'user' });
|
||||
const res = mockRes();
|
||||
const next = jest.fn();
|
||||
await restoreTenantContextFromReq(req, res, next);
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -184,3 +184,25 @@ describe('preAuthTenantMiddleware', () => {
|
|||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('reserved tenant header boundary', () => {
|
||||
it('rejects the tenantless persistence sentinel before invoking unauthenticated handlers', () => {
|
||||
const previous = process.env.TRUST_TENANT_HEADER;
|
||||
process.env.TRUST_TENANT_HEADER = 'true';
|
||||
const response = { status: jest.fn().mockReturnThis(), json: jest.fn() };
|
||||
const next = jest.fn();
|
||||
try {
|
||||
preAuthTenantMiddleware(
|
||||
{ headers: { 'x-tenant-id': '_' } } as unknown as Request,
|
||||
response as unknown as Response,
|
||||
next,
|
||||
);
|
||||
expect(response.status).toHaveBeenCalledWith(400);
|
||||
expect(response.json).toHaveBeenCalledWith({ error: 'Reserved tenant identifier' });
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
if (previous === undefined) delete process.env.TRUST_TENANT_HEADER;
|
||||
else process.env.TRUST_TENANT_HEADER = previous;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -50,6 +50,12 @@ export function preAuthTenantMiddleware(req: Request, res: Response, next: NextF
|
|||
}
|
||||
|
||||
const rejection = rejectTenantHeader(tenantId);
|
||||
if (rejection === 'reserved') {
|
||||
runWithTenantContext(requestContext, () =>
|
||||
res.status(400).json({ error: 'Reserved tenant identifier' }),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (rejection === 'system') {
|
||||
runWithTenantContext(requestContext, () => {
|
||||
logger.warn('[preAuthTenant] Rejected __SYSTEM__ sentinel in X-Tenant-Id header', {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,13 @@
|
|||
import { randomUUID } from 'crypto';
|
||||
import { unlink } from 'fs/promises';
|
||||
import { isMainThread } from 'worker_threads';
|
||||
import { tenantStorage, getTenantId, logger, SYSTEM_TENANT_ID } from '@librechat/data-schemas';
|
||||
import {
|
||||
tenantStorage,
|
||||
getTenantId,
|
||||
logger,
|
||||
SYSTEM_TENANT_ID,
|
||||
isReservedTenantId,
|
||||
} from '@librechat/data-schemas';
|
||||
import type { Response, NextFunction, RequestHandler } from 'express';
|
||||
import type { TenantContext } from '@librechat/data-schemas';
|
||||
import type { ServerRequest } from '~/types/http';
|
||||
|
|
@ -171,11 +177,15 @@ export function tenantContextMiddleware(
|
|||
const context = buildTenantContext(req);
|
||||
const { tenantId } = context;
|
||||
|
||||
if (tenantId === SYSTEM_TENANT_ID) {
|
||||
if (tenantId === SYSTEM_TENANT_ID || isReservedTenantId(tenantId)) {
|
||||
logger.warn('[tenantContextMiddleware] Rejected system tenant for request route', {
|
||||
path: req.path,
|
||||
});
|
||||
res.status(403).json({ error: SYSTEM_TENANT_REJECTION_MESSAGE });
|
||||
res.status(403).json({
|
||||
error: isReservedTenantId(tenantId)
|
||||
? 'Reserved tenant identifier'
|
||||
: SYSTEM_TENANT_REJECTION_MESSAGE,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -305,11 +315,17 @@ export function restoreTenantContextFromReq(
|
|||
return;
|
||||
}
|
||||
|
||||
if (resolvedTenantId === SYSTEM_TENANT_ID) {
|
||||
if (resolvedTenantId === SYSTEM_TENANT_ID || isReservedTenantId(resolvedTenantId)) {
|
||||
logger.warn('[restoreTenantContextFromReq] Rejected system tenant for request route', {
|
||||
path: req.path,
|
||||
});
|
||||
return rejectRequestWithUploadCleanup(req, res, SYSTEM_TENANT_REJECTION_MESSAGE);
|
||||
return rejectRequestWithUploadCleanup(
|
||||
req,
|
||||
res,
|
||||
isReservedTenantId(resolvedTenantId)
|
||||
? 'Reserved tenant identifier'
|
||||
: SYSTEM_TENANT_REJECTION_MESSAGE,
|
||||
);
|
||||
}
|
||||
|
||||
const currentContext = tenantStorage.getStore();
|
||||
|
|
|
|||
7
packages/api/src/middleware/tenantHeader.spec.ts
Normal file
7
packages/api/src/middleware/tenantHeader.spec.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { rejectTenantHeader } from './tenantHeader';
|
||||
|
||||
it('reserves the channel tenantless identifier without banning underscores inside real tenant IDs', () => {
|
||||
expect(rejectTenantHeader('_')).toBe('reserved');
|
||||
expect(rejectTenantHeader('org_1')).toBeUndefined();
|
||||
expect(rejectTenantHeader('__SYSTEM__')).toBe('system');
|
||||
});
|
||||
|
|
@ -1,16 +1,19 @@
|
|||
import { SYSTEM_TENANT_ID } from '@librechat/data-schemas';
|
||||
import { SYSTEM_TENANT_ID, isReservedTenantId } from '@librechat/data-schemas';
|
||||
|
||||
const MAX_TENANT_ID_LENGTH = 128;
|
||||
const VALID_TENANT_ID = /^[-a-zA-Z0-9_.]+$/;
|
||||
|
||||
/**
|
||||
* Why a trusted `X-Tenant-Id` value names no tenant: the system sentinel, or a value that is
|
||||
* Why a trusted `X-Tenant-Id` value names no tenant: a reserved persistence/system sentinel, or a value that is
|
||||
* too long or carries characters outside the tenant id alphabet. `undefined` accepts it.
|
||||
*/
|
||||
export function rejectTenantHeader(tenantId: string): 'system' | 'malformed' | undefined {
|
||||
export function rejectTenantHeader(
|
||||
tenantId: string,
|
||||
): 'system' | 'reserved' | 'malformed' | undefined {
|
||||
if (tenantId === SYSTEM_TENANT_ID) {
|
||||
return 'system';
|
||||
}
|
||||
if (isReservedTenantId(tenantId)) return 'reserved';
|
||||
if (tenantId.length > MAX_TENANT_ID_LENGTH || !VALID_TENANT_ID.test(tenantId)) {
|
||||
return 'malformed';
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue