LibreChat/packages/api/src/admin/lease.spec.ts
Marco Beretta 76192bdfd0
🛂 feat: Audit Resource Sharing Changes and Fail Closed on Reversible Audit Writes (#17053)
* feat: Audit Resource ACL Changes and Fail Closed by Default

* fix: Keep Non-Reversible Admin Audit Handlers Fail-Open

* docs: Narrow AUDIT_LOG_FAIL_CLOSED Description

* fix: Audit and Restore ACL and Insights Changes in One Pass

* fix: Break Audit and Insights Permission Type Cycle

* test: Cover Resource ACL Audit Scenarios

* refactor: Audit Resource Permission Changes Before Writing

* test: Align Concurrent Revoke Audit Expectation

* fix: Skip Deleting ACL Entries That Were Not Audited

* fix: Read ACL Snapshots Through the Write Session

* fix: Never Block Revocations on Audit Outages and Normalize Principal Keys

* fix: Let an Explicit Public Disable Win Over a Public Update

* fix: Audit Duplicate ACL Rows as Combined Permissions

* fix: Audit Permission Changes Under One Target and Dedupe Revokes

* fix: Audit Capability Grants Before Writing Them

* feat: Serialize Resource Permission Updates With a Per-Resource Lease

* fix: Fence the Resource Lease Before Writes and Always Audit Grant Assignments

* fix: Renew the Resource Lease Before Each ACL Write Phase

* fix: Serialize System Grant Assignment and Revocation With a Per-Grant Lease

* fix: Keep Leases Alive During Audits, Bound Lock Index Wait, Canonicalize Audit Target IDs

* fix: Retry Deferred Revoke Audits Under the Lease and Audit Canonical Principal and Role IDs

* test: Expect Role IDs in Permission Audit Metadata

* fix: Read Mutation Snapshots From the Primary Outside Transactions

* fix: Flush Deferred Audits Before Cache Invalidation, Keep Renewals Monotonic, Keep Role Case in Grant Lease Keys

* refactor: Map the Busy Permission Error to Its Code in the API Package

* style: Sort Lease Imports and Type the Renewal Spec Read

* fix: Keep Success Audits After Commit, Report Downgrades as Revocations, Code Audit Outages

* refactor: Move the Audit Outage Code to Its Own Module

* fix: Translate Lock Failures, Lease Non-Transactional Sessions, and Code Grant Audit Outages

* fix: Sanitize Lease Renewal Failures at the Permission Boundary

* fix: Annotate the Unavailable Error Code for Isolated Declarations

* fix: Keep the Admin Route Fail-Closed Opt-In and Document Lease Clock Skew Safety

* fix: Translate Grant Lease Store Failures to a Retryable 503

* fix: Log Failed Grant Audit Writes With Safe Metadata Only

* test: Detect Raw Error Arguments in the Grant Audit Log Spec

* feat: Make Permission Lease Limits Configurable and Localize Retryable Sharing Failures

* fix: Bound Lock Index Readiness by the Lease Wait and Carry the Resource ID in Every Audit Entry

* test: Cover Lease Config Reaching the Leases and the Sharing Dialog Failure Messages

* fix: Load App Config on the Sharing and Grant Routes and Log Failed Lease Releases

* perf: Load App Config Only on the Grant Mutation Routes
2026-10-15 00:30:16 +02:00

104 lines
3.8 KiB
TypeScript

import { inspect } from 'util';
import { logger } from '@librechat/data-schemas';
import { grantLeaseKey, withLease } from './lease';
jest.mock('@librechat/data-schemas', () => ({
...jest.requireActual('@librechat/data-schemas'),
logger: { error: jest.fn(), warn: jest.fn(), info: jest.fn(), debug: jest.fn() },
}));
describe('withLease', () => {
afterEach(() => jest.useRealTimers());
it('renews the lease while the callback is still running', async () => {
jest.useFakeTimers();
const lease = {
acquireResourceLock: jest.fn().mockResolvedValue(true),
renewResourceLock: jest.fn().mockResolvedValue(true),
releaseResourceLock: jest.fn().mockResolvedValue(true),
};
const run = withLease(
lease,
'k',
() => new Promise<string>((resolve) => setTimeout(() => resolve('done'), 25_000)),
);
await jest.advanceTimersByTimeAsync(25_000);
await expect(run).resolves.toBe('done');
expect(lease.renewResourceLock.mock.calls.length).toBeGreaterThanOrEqual(2);
expect(lease.releaseResourceLock).toHaveBeenCalledTimes(1);
});
it('does not lose the lease on a transient renewal error', async () => {
const lease = {
acquireResourceLock: jest.fn().mockResolvedValue(true),
renewResourceLock: jest.fn().mockRejectedValueOnce(new Error('blip')).mockResolvedValue(true),
releaseResourceLock: jest.fn().mockResolvedValue(true),
};
await expect(
withLease(lease, 'k', async (fence) => {
await expect(fence()).rejects.toMatchObject({ code: 'RESOURCE_PERMISSIONS_UNAVAILABLE' });
await fence();
return 'ok';
}),
).resolves.toBe('ok');
});
it('turns a lock-store failure on acquire into the unavailable error', async () => {
const lease = {
acquireResourceLock: jest.fn().mockRejectedValue(new Error('mongo secret detail')),
renewResourceLock: jest.fn(),
releaseResourceLock: jest.fn(),
};
await expect(withLease(lease, 'k', async () => 'x')).rejects.toMatchObject({
statusCode: 503,
message: expect.not.stringContaining('secret'),
});
});
it('honors configured wait and TTL limits', async () => {
const lease = {
acquireResourceLock: jest.fn().mockResolvedValue(false),
renewResourceLock: jest.fn().mockResolvedValue(true),
releaseResourceLock: jest.fn().mockResolvedValue(true),
};
const started = Date.now();
await expect(withLease(lease, 'k', async () => 'x', { waitMs: 0 })).rejects.toMatchObject({
code: 'RESOURCE_PERMISSIONS_BUSY',
});
expect(Date.now() - started).toBeLessThan(1000);
lease.acquireResourceLock.mockResolvedValue(true);
await withLease(lease, 'k', async () => 'x', { ttlMs: 12_345 });
expect(lease.acquireResourceLock).toHaveBeenLastCalledWith(
expect.objectContaining({ ttlMs: 12_345 }),
);
});
it('logs a failed release with safe metadata and still returns the result', async () => {
const lease = {
acquireResourceLock: jest.fn().mockResolvedValue(true),
renewResourceLock: jest.fn().mockResolvedValue(true),
releaseResourceLock: jest.fn().mockRejectedValue(new Error('mongo secret detail')),
};
(logger.error as jest.Mock).mockClear();
await expect(withLease(lease, 'k', async () => 'ok')).resolves.toBe('ok');
expect(logger.error).toHaveBeenCalledTimes(1);
expect(inspect((logger.error as jest.Mock).mock.calls)).not.toContain('secret');
});
it('keys one grant the same for a user ObjectId in either case', () => {
const hex = 'abcdef0123456789abcdef01';
expect(grantLeaseKey('user', hex, 'cap')).toBe(grantLeaseKey('USER', hex.toUpperCase(), 'cap'));
});
it('keeps role names case-sensitive', () => {
const hex = 'abcdef0123456789abcdef01';
expect(grantLeaseKey('role', hex, 'cap')).not.toBe(
grantLeaseKey('role', hex.toUpperCase(), 'cap'),
);
});
});