mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 13:00:55 +00:00
Profile writes are judged in the tenant partition the profile merges in. For a role, or a
caller inside a tenant, that is the caller's partition. For a group or user profile written by
an admin without a tenant, it is the stored document's tenant: PUT, PATCH and POST …/changes
validate on librechat.yaml as that tenant resolves it with that tenant's __base__ over it, use
the same Base for the recheck after commit and for key binding, tombstones and field deletes
judge re-entry on that tenant's librechat.yaml, and every route reports `ignored` over that
tenant's Base. Each attempt reads the document in that partition only, and a document that
moved partition while the write ran answers 409 CONFIG_VERSION_CONFLICT. The config store's
partition rules are unchanged; another tenant's Base is read in that tenant's context.
Deleting a group, role or user clears the config caches when it removes or restores the
principal's profile (`invalidatingConfigStore`), awaited before the response and broadcast with
Redis as config writes do; a call that rejects clears too, since it may have committed.
POST …/changes lets `unset` remove any part of a top-level section configSchema does not
define; the section's own `unknown_section` issue is no longer blamed on the removal, and every
other op there still answers 400 UNKNOWN_SECTION.
POST /api/admin/config/reload, for an admin without a tenant holding broad manage:configs (a
tenant admin gets 403, since the reload reaches every tenant), runs the config cache clear without
a write, loads librechat.yaml again on the answering instance, and once it loaded publishes the
clear over Redis so every other instance loads the file on its next read. It uses
ConfigCacheInvalidation.reload, which reports the clear, the load and the publish instead of only
logging a failure (writes keep `invalidate`, which leaves the load to the next read). It answers
200 `{ success: true, broadcast }`, `broadcast` false without Redis; 422 CONFIG_RELOAD_INVALID
with each schema issue's location when the loader refuses the file, keeping the last good config
and publishing nothing; 500 CONFIG_RELOAD_FAILED when a clear fails or the config cannot be built
from the file; and 503 CONFIG_RELOAD_BROADCAST_FAILED when this instance reloaded but the publish
failed, since an instance that misses the broadcast keeps its cached librechat.yaml, which never
expires. Every reload that runs is audited as `config.reloaded`, a failed one with outcome
`failure` and its code. The meta reports `features.reloadRoute`. Writes still reload the file.
The e2e content-filter helper uses the route instead of a sentinel profile.
docs/mcp-overrides.md describes the current timing: the awaited clear, the Redis broadcast, what
still waits for a TTL, that a process the clear misses keeps its old librechat.yaml, and hiding
as an access boundary at checkout.
88 lines
2.7 KiB
JavaScript
88 lines
2.7 KiB
JavaScript
const express = require('express');
|
|
const request = require('supertest');
|
|
|
|
const GROUP_ID = '507f1f77bcf86cd799439011';
|
|
|
|
const mockRequireJwtAuth = jest.fn((req, res, next) => {
|
|
if (req.headers.authorization !== 'Bearer admin') {
|
|
return res.status(401).json({ message: 'Unauthorized' });
|
|
}
|
|
req.user = { id: 'user-1', _id: 'user-1', role: 'ADMIN', tenantId: 'tenant-a' };
|
|
next();
|
|
});
|
|
const mockDb = {
|
|
findGroupById: jest.fn(),
|
|
deleteGroup: jest.fn(),
|
|
deleteConfig: jest.fn(),
|
|
restoreConfig: jest.fn(),
|
|
findConfigByPrincipal: jest.fn(),
|
|
deleteAclEntries: jest.fn(),
|
|
recordAuditEntry: jest.fn(),
|
|
};
|
|
|
|
jest.mock('~/server/middleware', () => ({ requireJwtAuth: mockRequireJwtAuth }));
|
|
|
|
jest.mock('~/server/middleware/roles/capabilities', () => ({
|
|
requireCapability: jest.fn(() => (_req, _res, next) => next()),
|
|
}));
|
|
|
|
jest.mock('~/server/services/Config', () => ({
|
|
invalidateConfigCaches: jest.fn(async () => undefined),
|
|
}));
|
|
|
|
jest.mock('~/models', () => mockDb);
|
|
|
|
function createApp() {
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use('/api/admin/groups', require('./groups'));
|
|
return app;
|
|
}
|
|
|
|
describe('admin group routes', () => {
|
|
let app;
|
|
|
|
beforeAll(() => {
|
|
app = createApp();
|
|
});
|
|
|
|
beforeEach(() => {
|
|
const group = { _id: GROUP_ID, name: 'Support', memberIds: ['member-1'] };
|
|
mockDb.findGroupById.mockResolvedValue(group);
|
|
mockDb.deleteGroup.mockResolvedValue(group);
|
|
mockDb.deleteAclEntries.mockResolvedValue({ deletedCount: 0 });
|
|
mockDb.findConfigByPrincipal.mockResolvedValue(null);
|
|
});
|
|
|
|
it("clears the config caches through the Config service when a deletion removes the group's profile", async () => {
|
|
const { invalidateConfigCaches } = require('~/server/services/Config');
|
|
mockDb.deleteConfig.mockResolvedValue({
|
|
principalType: 'group',
|
|
principalId: GROUP_ID,
|
|
overrides: { interface: { customWelcome: 'group hello' } },
|
|
});
|
|
|
|
await request(app)
|
|
.delete(`/api/admin/groups/${GROUP_ID}`)
|
|
.set('authorization', 'Bearer admin')
|
|
.expect(200);
|
|
|
|
expect(mockDb.deleteConfig).toHaveBeenCalledWith('group', GROUP_ID);
|
|
expect(invalidateConfigCaches).toHaveBeenCalledTimes(1);
|
|
expect(invalidateConfigCaches).toHaveBeenCalledWith('tenant-a');
|
|
expect(mockDb.deleteGroup).toHaveBeenCalledWith(GROUP_ID);
|
|
});
|
|
|
|
it('clears nothing when the group had no profile', async () => {
|
|
const { invalidateConfigCaches } = require('~/server/services/Config');
|
|
mockDb.deleteConfig.mockResolvedValue(null);
|
|
|
|
await request(app)
|
|
.delete(`/api/admin/groups/${GROUP_ID}`)
|
|
.set('authorization', 'Bearer admin')
|
|
.expect(200);
|
|
|
|
expect(mockDb.deleteConfig).toHaveBeenCalledTimes(1);
|
|
expect(invalidateConfigCaches).not.toHaveBeenCalled();
|
|
});
|
|
});
|