Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🍴 fix: Split Combined Bit Updates in ACL Permission Writes (#16172)
lia-by-librechat[bot]fe03f124f0a8
diff --git a/packages/data-schemas/src/methods/aclEntry.spec.ts b/packages/data-schemas/src/methods/aclEntry.spec.tsindex a2831fa22c..e5c86ac495 100644--- a/packages/data-schemas/src/methods/aclEntry.spec.ts+++ b/packages/data-schemas/src/methods/aclEntry.spec.ts@@ -1682,6 +1682,113 @@ describe('AclEntry Model Tests', () => {* `permBits: { $in: permissionBitSupersets(X) }`), so it warrants direct* coverage independent of the higher-level parity and behavior specs.*/+ describe('modifyPermissionBits (atomic guarded writes, issue #16163)', () => {+ const principal = new mongoose.Types.ObjectId();+ const resource = new mongoose.Types.ObjectId();+ const INSIGHTS = PermissionBits.VIEW_INSIGHTS;++ const seed = (permBits: number) =>+ AclEntry.create({+ principalType: PrincipalType.USER,+ principalId: principal,+ principalModel: PrincipalModel.USER,+ resourceType: ResourceType.AGENT,+ resourceId: resource,+ permBits,+ grantedBy: grantedById,+ });++ const modify = (add?: number | null, remove?: number | null) =>+ methods.modifyPermissionBits(+ PrincipalType.USER,+ principal,+ ResourceType.AGENT,+ resource,+ add,+ remove,+ );++ test('adds bits, leaving independently administered bits alone', async () => {+ await seed(PermissionBits.VIEW | INSIGHTS);+ const updated = await modify(PermissionBits.EDIT, null);+ expect(updated?.permBits).toBe(PermissionBits.VIEW | PermissionBits.EDIT | INSIGHTS);+ });++ test('removes bits, leaving independently administered bits alone', async () => {+ await seed(PermissionBits.VIEW | PermissionBits.EDIT | INSIGHTS);+ const updated = await modify(null, PermissionBits.EDIT);+ expect(updated?.permBits).toBe(PermissionBits.VIEW | INSIGHTS);+ });++ test('applies an add and a remove in one stored value', async () => {+ await seed(PermissionBits.VIEW | PermissionBits.EDIT | INSIGHTS);+ const updated = await modify(PermissionBits.SHARE, PermissionBits.EDIT);+ expect(updated?.permBits).toBe(PermissionBits.VIEW | PermissionBits.SHARE | INSIGHTS);+ });++ test('changes bits in one guarded write and never emits $bit', async () => {+ await seed(PermissionBits.VIEW);+ const spy = jest.spyOn(AclEntry, 'bulkWrite');+ try {+ await modify(PermissionBits.EDIT, PermissionBits.VIEW);+ expect(spy).toHaveBeenCalledTimes(1);+ expect(spy.mock.calls[0][0]).toHaveLength(1);+ expect(spy.mock.calls[0][0][0]).toMatchObject({+ updateOne: {+ filter: { permBits: PermissionBits.VIEW },+ update: { $set: { permBits: PermissionBits.EDIT } },+ },+ });+ expect(JSON.stringify(spy.mock.calls)).not.toContain('$bit');+ } finally {+ spy.mockRestore();+ }+ });++ test('retries a concurrent Insights grant without overwriting it', async () => {+ await seed(PermissionBits.VIEW | PermissionBits.EDIT);+ const real = AclEntry.bulkWrite.bind(AclEntry);+ const race = (async (...args: Parameters<typeof real>) => {+ await AclEntry.updateMany({ principalId: principal }, { $set: { permBits: 19 } });+ return real(...args);+ }) as unknown as typeof AclEntry.bulkWrite;+ const spy = jest.spyOn(AclEntry, 'bulkWrite').mockImplementationOnce(race);+ try {+ const updated = await modify(PermissionBits.SHARE, PermissionBits.EDIT);+ expect(updated?.permBits).toBe(PermissionBits.VIEW | PermissionBits.SHARE | INSIGHTS);+ expect(spy).toHaveBeenCalledTimes(2);+ } finally {+ spy.mockRestore();+ }+ });++ test('removes a bit named in both masks, matching the prior precedence', async () => {+ await seed(PermissionBits.VIEW | INSIGHTS);+ const updated = await modify(PermissionBits.EDIT | PermissionBits.SHARE, PermissionBits.EDIT);+ expect(updated?.permBits).toBe(PermissionBits.VIEW | PermissionBits.SHARE | INSIGHTS);+ });++ test('initializes an absent permission field without inheriting anything', async () => {+ await seed(PermissionBits.VIEW);+ await mongoose.models.AclEntry.collection.updateMany(+ { principalId: principal },+ { $unset: { permBits: '' } },+ );+ const updated = await modify(PermissionBits.EDIT, PermissionBits.VIEW);+ expect(updated?.permBits).toBe(PermissionBits.EDIT);+ });++ test('returns null when no entry matches', async () => {+ expect(await modify(PermissionBits.EDIT, PermissionBits.VIEW)).toBeNull();+ });++ test('returns the entry unchanged when neither side is requested', async () => {+ await seed(PermissionBits.VIEW | INSIGHTS);+ const updated = await modify(null, null);+ expect(updated?.permBits).toBe(PermissionBits.VIEW | INSIGHTS);+ });+ });+describe('permissionBitSupersets', () => {test('requiredBits=0 matches every permBits value in [0, 31]', () => {const result = permissionBitSupersets(0);diff --git a/packages/data-schemas/src/methods/documentdb.spec.ts b/packages/data-schemas/src/methods/documentdb.spec.tsindex 4f09c8abc8..f44eb9fbd2 100644--- a/packages/data-schemas/src/methods/documentdb.spec.ts+++ b/packages/data-schemas/src/methods/documentdb.spec.ts@@ -564,6 +564,132 @@ function findMixedSelectStrings(sourceFile: ts.SourceFile): string[] {return offenses;}+/**+ * Regression guard for the combined update shape reported in #16163, not a+ * declaration that all MongoDB-compatible engines reject it. Resolve simple+ * lexical aliases and spreads only when they feed a $bit field. This is a+ * syntactic backstop, not interprocedural dataflow or vendor certification.+ */+const BIT_OPERATORS = new Set(['and', 'or', 'xor']);++function findCombinedBitOperators(sourceFile: ts.SourceFile): string[] {+ const offenses: string[] = [];+ const declarations = new Map<ts.Node, Map<string, ts.Expression | undefined>>();+ const assignments: ts.BinaryExpression[] = [];+ const isScope = (node: ts.Node) =>+ ts.isBlock(node) || ts.isSourceFile(node) || ts.isFunctionLike(node);+ const scopeOf = (node: ts.Node): ts.Node => {+ let scope = node.parent;+ while (scope.parent && !isScope(scope)) scope = scope.parent;+ return scope;+ };+ const collect = (node: ts.Node): void => {+ if ((ts.isVariableDeclaration(node) || ts.isParameter(node)) && ts.isIdentifier(node.name)) {+ const scope = scopeOf(node);+ const names = declarations.get(scope) ?? new Map();+ names.set(node.name.text, node.initializer);+ declarations.set(scope, names);+ }+ if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.EqualsToken) {+ assignments.push(node);+ }+ ts.forEachChild(node, collect);+ };+ collect(sourceFile);++ const binding = (identifier: ts.Identifier) => {+ let scope: ts.Node | undefined = identifier.parent;+ while (scope) {+ const names = declarations.get(scope);+ if (names?.has(identifier.text)) return { scope, value: names.get(identifier.text) };+ scope = scope.parent;+ }+ return undefined;+ };+ const propertyKey = (node: ts.PropertyAccessExpression | ts.ElementAccessExpression) => {+ if (ts.isPropertyAccessExpression(node)) return node.name.text;+ return ts.isStringLiteralLike(node.argumentExpression)+ ? node.argumentExpression.text+ : undefined;+ };+ const literal = (+ expression: ts.Expression,+ seen = new Set<ts.Node>(),+ ): ts.ObjectLiteralExpression | undefined => {+ const value = unwrapExpression(expression);+ if (seen.has(value)) return undefined;+ seen.add(value);+ if (ts.isObjectLiteralExpression(value)) return value;+ if (ts.isIdentifier(value)) {+ const found = binding(value)?.value;+ if (found) return literal(found, seen);+ }+ return undefined;+ };+ const operatorKeys = (expression: ts.Expression, seen = new Set<ts.Node>()): Set<string> => {+ const keys = new Set<string>();+ const value = unwrapExpression(expression);+ if (seen.has(value)) return keys;+ seen.add(value);+ if (ts.isIdentifier(value)) {+ const found = binding(value);+ if (found?.value) for (const key of operatorKeys(found.value, seen)) keys.add(key);+ for (const assignment of assignments) {+ if (+ !(+ ts.isPropertyAccessExpression(assignment.left) ||+ ts.isElementAccessExpression(assignment.left)+ )+ )+ continue;+ const target = unwrapExpression(assignment.left.expression);+ const key = propertyKey(assignment.left);+ if (+ ts.isIdentifier(target) &&+ target.text === value.text &&+ binding(target)?.scope === found?.scope &&+ key &&+ BIT_OPERATORS.has(key)+ )+ keys.add(key);+ }+ } else if (ts.isObjectLiteralExpression(value)) {+ for (const property of value.properties) {+ const key = propertyName(property);+ if (key && BIT_OPERATORS.has(key)) keys.add(key);+ if (ts.isSpreadAssignment(property)) {+ for (const spreadKey of operatorKeys(property.expression, seen)) keys.add(spreadKey);+ }+ }+ }+ return keys;+ };+ const inspectDocument = (expression: ts.Expression): void => {+ const document = literal(expression);+ if (!document) return;+ for (const field of document.properties) {+ const bag = ts.isPropertyAssignment(field) ? field.initializer : undefined;+ if (bag && operatorKeys(bag).size > 1) {+ offenses.push(offenseAt(sourceFile, field, `$bit.${propertyName(field) ?? '?'}`));+ }+ }+ };+ const visit = (node: ts.Node): void => {+ if (ts.isPropertyAssignment(node) && propertyName(node) === '$bit')+ inspectDocument(node.initializer);+ if (+ ts.isBinaryExpression(node) &&+ node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&+ (ts.isPropertyAccessExpression(node.left) || ts.isElementAccessExpression(node.left)) &&+ propertyKey(node.left) === '$bit'+ )+ inspectDocument(node.right);+ ts.forEachChild(node, visit);+ };+ visit(sourceFile);+ return offenses;+}+function propertyName(property: ts.ObjectLiteralElementLike): string | undefined {const name = property.name;if (name == null || !(ts.isIdentifier(name) || ts.isStringLiteral(name))) {@@ -715,6 +841,10 @@ describe('Amazon DocumentDB compatibility', () => {expect(parsedSources.flatMap(findMixedSelectStrings)).toEqual([]);});+ it('combines no $bit operators on one field', () => {+ expect(parsedSources.flatMap(findCombinedBitOperators)).toEqual([]);+ });+it('uses no $set or $unset pipeline stages', () => {expect(parsedSources.flatMap(findAliasStages)).toEqual([]);});@@ -801,6 +931,66 @@ describe('Amazon DocumentDB compatibility', () => {expect(findPipelineUpdates(parse('fixture.ts', source))).toEqual([]);});+ it.each([+ [+ 'literal combination',+ `Model.updateOne(filter, { $bit: { permBits: { or: 1, and: -2 } } });`,+ ],+ [+ 'bulk payload combination',+ `await Model.bulkWrite([{ updateMany: { filter, update: { $bit: { p: { or: 1, and: -2 } } } } }]);`,+ ],+ ['three operators', `Model.updateOne(filter, { $bit: { p: { or: 1, and: -2, xor: 4 } } });`],+ [+ 'bag merged by spread',+ `const first = { or: 1 }; Model.updateOne(q, { $bit: { permBits: { ...first, and: -2 } } });`,+ ],+ [+ 'combined bag reached through a variable',+ `const ops = { or: 1, and: -2 };\nModel.updateOne(filter, { $bit: { permBits: ops } });`,+ ],+ [+ 'operator added to a bag after its literal',+ `const ops = { or: 1 };\nops.and = -2;\nModel.updateOne(filter, { $bit: { permBits: ops } });`,+ ],+ [+ 'bag merged behind a cast',+ `const first = { or: 1 }; Model.updateOne(q, { $bit: { permBits: { ...(first as Record<string, unknown>), and: -2 } } });`,+ ],+ ])('flags a combined $bit update: %s', (_shape, source) => {+ expect(findCombinedBitOperators(parse('fixture.ts', source))).not.toEqual([]);+ });++ it.each([+ ['single or', `Model.updateOne(filter, { $bit: { permBits: { or: 1 } } });`],+ ['single and', `Model.findOneAndUpdate(filter, { $bit: { permBits: { and: -2 } } });`],+ [+ 'one operator per field on two fields',+ `Model.updateOne(filter, { $bit: { a: { or: 1 }, b: { and: -2 } } });`,+ ],+ ['spread without an operator key', `const update = { ...base, permBits: 3 };`],+ [+ 'single-operator bag reached through a variable',+ `const ops = { or: 1 };\nModel.updateOne(filter, { $bit: { permBits: ops } });`,+ ],+ ['unrelated and/or naming', `const flags = { and: true, or: false };`],+ ['unrelated spread bag', `const flags = { ...base, and: true };`],+ [+ 'repeated same operator',+ `const ops = { or: 1 }; ops.or = 2; Model.updateOne(q, { $bit: { p: ops } });`,+ ],+ [+ 'shadowed alias',+ `const ops = { or: 1, and: -2 }; function f() { const ops = { or: 1 }; Model.updateOne(q, { $bit: { p: ops } }); }`,+ ],+ [+ 'shadowed assignment',+ `const ops = { or: 1 }; function f() { const ops = {}; ops.and = -2; } Model.updateOne(q, { $bit: { p: ops } });`,+ ],+ ])('accepts a single-operator $bit shape: %s', (_shape, source) => {+ expect(findCombinedBitOperators(parse('fixture.ts', source))).toEqual([]);+ });+it.each([['_id with an un-hide token', `Model.find({}).select('_id +hiddenField');`],['regardless of token order', `Model.find({}).select('+hiddenField _id');`],