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: Resolve Global Access Roles for Tenant Owner Grants (#15986)
lia-by-librechat[bot]6d9f42796fdc
diff --git a/packages/data-schemas/src/methods/accessRole.tenant.spec.ts b/packages/data-schemas/src/methods/accessRole.tenant.spec.tsnew file mode 100644index 0000000000..c03e6a3b4a--- /dev/null+++ b/packages/data-schemas/src/methods/accessRole.tenant.spec.ts@@ -0,0 +1,344 @@+import mongoose from 'mongoose';+import { MongoMemoryServer } from 'mongodb-memory-server';+import { AccessRoleIds, ResourceType } from 'librechat-data-provider';+import { _resetStrictCache } from '../models/plugins/tenantIsolation';+import { createAccessRoleModel } from '~/models/accessRole';+import { createAccessRoleMethods } from './accessRole';+import { tenantStorage } from '~/config/tenantContext';+import { RoleBits } from '~/common';++let mongoServer: MongoMemoryServer;+let methods: ReturnType<typeof createAccessRoleMethods>;++beforeAll(async () => {+ mongoServer = await MongoMemoryServer.create();+ /** Applies the tenant-isolation plugin, which is what scopes these queries */+ createAccessRoleModel(mongoose);+ methods = createAccessRoleMethods(mongoose);+ await mongoose.connect(mongoServer.getUri());+});++afterAll(async () => {+ await mongoose.disconnect();+ await mongoServer.stop();+});++beforeEach(async () => {+ await mongoose.connection.dropDatabase();+ delete process.env.TENANT_ISOLATION_STRICT;+ _resetStrictCache();+});++/** Seeds the global default roles the way startup does: no tenant context, no `tenantId` */+const seedGlobalRoles = () => methods.seedDefaultRoles();++const createTenantRole = (tenantId: string, role: Parameters<typeof methods.createRole>[0]) =>+ tenantStorage.run({ tenantId }, () => methods.createRole(role));++/** A tenant that redefines `agent_owner` with EDITOR bits, shadowing the global OWNER row */+const shadowAgentOwnerAsEditor = (tenantId: string) =>+ createTenantRole(tenantId, {+ accessRoleId: AccessRoleIds.AGENT_OWNER,+ name: 'tenant-owner',+ resourceType: ResourceType.AGENT,+ permBits: RoleBits.EDITOR,+ });++describe('access role resolution under tenant isolation', () => {+ it.each(['global-first', 'tenant-first'])(+ 'prefers tenant overrides with %s insertion order',+ async (order) => {+ await seedGlobalRoles();+ await shadowAgentOwnerAsEditor('tenant-a');+ const stored = await mongoose.models.AccessRole.find().lean();+ const globals = stored.filter((role) => role.tenantId == null);+ const scoped = stored.filter((role) => role.tenantId === 'tenant-a');+ await mongoose.connection.dropDatabase();+ await mongoose.models.AccessRole.insertMany(+ order === 'global-first' ? [...globals, ...scoped] : [...scoped, ...globals],+ );+ const find = jest.spyOn(mongoose.models.AccessRole.collection, 'find');++ const role = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRoleByIdentifier(AccessRoleIds.AGENT_OWNER),+ );++ expect(role).toEqual(+ expect.objectContaining({ tenantId: 'tenant-a', permBits: RoleBits.EDITOR }),+ );+ expect(find).toHaveBeenCalledTimes(1);+ expect(find.mock.calls[0][0]).toEqual({ tenantId: { $in: ['tenant-a', null] } });+ },+ );++ it('resolves explicit null global roles under strict isolation', async () => {+ await seedGlobalRoles();+ await mongoose.models.AccessRole.collection.updateMany({}, { $set: { tenantId: null } });+ process.env.TENANT_ISOLATION_STRICT = 'true';+ _resetStrictCache();++ const role = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRoleByIdentifier(AccessRoleIds.AGENT_OWNER),+ );++ expect(role).toEqual(expect.objectContaining({ tenantId: null, permBits: RoleBits.OWNER }));+ });++ it('isolates tenant-only provisioning and restores the caller context', async () => {+ process.env.TENANT_ISOLATION_STRICT = 'true';+ _resetStrictCache();+ for (const tenantId of ['tenant-a', 'tenant-b']) {+ await tenantStorage.run({ tenantId }, () => methods.seedDefaultRoles());+ }++ await tenantStorage.run({ tenantId: 'tenant-a' }, async () => {+ const role = await methods.findRoleByIdentifier(AccessRoleIds.AGENT_OWNER);+ const roles = await methods.findRolesByResourceType(ResourceType.AGENT);+ expect(role?.tenantId).toBe('tenant-a');+ expect(roles).toHaveLength(3);+ expect(roles.every((entry) => entry.tenantId === 'tenant-a')).toBe(true);+ const scopedRoles = await mongoose.models.AccessRole.find().lean();+ expect(scopedRoles.every((entry) => entry.tenantId === 'tenant-a')).toBe(true);+ });+ await tenantStorage.run({ tenantId: 'tenant-c' }, async () => {+ await expect(methods.findRoleByIdentifier(AccessRoleIds.AGENT_OWNER)).resolves.toBeNull();+ await expect(methods.findRolesByResourceType(ResourceType.AGENT)).resolves.toEqual([]);+ });+ });++ describe('findRoleByIdentifier', () => {+ it('resolves a global default role from inside a tenant context', async () => {+ await seedGlobalRoles();++ const role = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRoleByIdentifier(AccessRoleIds.AGENT_OWNER),+ );++ expect(role).toEqual(+ expect.objectContaining({+ accessRoleId: AccessRoleIds.AGENT_OWNER,+ resourceType: ResourceType.AGENT,+ permBits: RoleBits.OWNER,+ }),+ );+ });++ it('resolves a global default role under strict isolation', async () => {+ await seedGlobalRoles();+ process.env.TENANT_ISOLATION_STRICT = 'true';+ _resetStrictCache();++ const role = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRoleByIdentifier(AccessRoleIds.SKILL_OWNER),+ );++ expect(role).toEqual(expect.objectContaining({ accessRoleId: AccessRoleIds.SKILL_OWNER }));+ });++ it("prefers the tenant's own copy of a role over the global one", async () => {+ await seedGlobalRoles();+ await shadowAgentOwnerAsEditor('tenant-a');++ const scoped = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRoleByIdentifier(AccessRoleIds.AGENT_OWNER),+ );+ const other = await tenantStorage.run({ tenantId: 'tenant-b' }, () =>+ methods.findRoleByIdentifier(AccessRoleIds.AGENT_OWNER),+ );++ expect(scoped).toEqual(+ expect.objectContaining({ tenantId: 'tenant-a', permBits: RoleBits.EDITOR }),+ );+ expect(other).toEqual(expect.objectContaining({ permBits: RoleBits.OWNER }));+ expect(other?.tenantId).toBeUndefined();+ });++ it("does not resolve another tenant's role", async () => {+ await createTenantRole('tenant-b', {+ accessRoleId: 'tenant_b_owner',+ name: 'tenant-b-owner',+ resourceType: ResourceType.AGENT,+ permBits: RoleBits.OWNER,+ });++ const role = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRoleByIdentifier('tenant_b_owner'),+ );++ expect(role).toBeNull();+ });++ it('resolves the global role with no tenant context, as before', async () => {+ await seedGlobalRoles();++ await expect(methods.findRoleByIdentifier(AccessRoleIds.AGENT_OWNER)).resolves.toEqual(+ expect.objectContaining({ accessRoleId: AccessRoleIds.AGENT_OWNER }),+ );+ });+ });++ describe('findRolesByResourceType', () => {+ it('returns the global roles for a tenant, with tenant copies overriding', async () => {+ await seedGlobalRoles();+ await createTenantRole('tenant-a', {+ accessRoleId: AccessRoleIds.AGENT_VIEWER,+ name: 'tenant-viewer',+ resourceType: ResourceType.AGENT,+ permBits: RoleBits.VIEWER,+ });++ const roles = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRolesByResourceType(ResourceType.AGENT),+ );++ expect(roles.map((role) => role.accessRoleId).sort()).toEqual(+ [AccessRoleIds.AGENT_EDITOR, AccessRoleIds.AGENT_OWNER, AccessRoleIds.AGENT_VIEWER].sort(),+ );+ expect(roles.find((role) => role.accessRoleId === AccessRoleIds.AGENT_VIEWER)).toEqual(+ expect.objectContaining({ name: 'tenant-viewer', tenantId: 'tenant-a' }),+ );+ });++ it('excludes roles belonging to another tenant', async () => {+ await seedGlobalRoles();+ await createTenantRole('tenant-b', {+ accessRoleId: 'tenant_b_role',+ name: 'tenant-b-role',+ resourceType: ResourceType.AGENT,+ permBits: RoleBits.VIEWER,+ });++ const roles = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRolesByResourceType(ResourceType.AGENT),+ );++ expect(roles.map((role) => role.accessRoleId)).not.toContain('tenant_b_role');+ });+ });++ describe('a tenant override that moves an identifier to another resource type', () => {+ /** The tenant reuses `agent_owner` as a skill role, withdrawing it from agents */+ const moveAgentOwnerToSkill = (tenantId: string) =>+ createTenantRole(tenantId, {+ accessRoleId: AccessRoleIds.AGENT_OWNER,+ name: 'tenant-skill-owner',+ resourceType: ResourceType.SKILL,+ permBits: RoleBits.OWNER,+ });++ it('withdraws the shadowed global role from its old resource type', async () => {+ await seedGlobalRoles();+ await moveAgentOwnerToSkill('tenant-a');++ const roles = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRolesByResourceType(ResourceType.AGENT),+ );++ expect(roles.map((role) => role.accessRoleId)).not.toContain(AccessRoleIds.AGENT_OWNER);+ });++ it('lists the override under its new resource type', async () => {+ await seedGlobalRoles();+ await moveAgentOwnerToSkill('tenant-a');++ const roles = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRolesByResourceType(ResourceType.SKILL),+ );++ expect(roles.find((role) => role.accessRoleId === AccessRoleIds.AGENT_OWNER)).toEqual(+ expect.objectContaining({ tenantId: 'tenant-a', resourceType: ResourceType.SKILL }),+ );+ });++ it('never yields the withdrawn owner bits through a permission lookup', async () => {+ await seedGlobalRoles();+ await moveAgentOwnerToSkill('tenant-a');++ const [exact, closest] = await tenantStorage.run({ tenantId: 'tenant-a' }, async () => [+ await methods.findRoleByPermissions(ResourceType.AGENT, RoleBits.OWNER),+ await methods.getRoleForPermissions(ResourceType.AGENT, RoleBits.OWNER),+ ]);++ expect(exact).toBeNull();+ /** The closest agent role still visible, not the superseded global owner */+ expect(closest?.accessRoleId).toBe(AccessRoleIds.AGENT_EDITOR);+ });++ it('agrees with findRoleByIdentifier on the identifier’s new meaning', async () => {+ await seedGlobalRoles();+ await moveAgentOwnerToSkill('tenant-a');++ const role = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRoleByIdentifier(AccessRoleIds.AGENT_OWNER),+ );++ expect(role).toEqual(+ expect.objectContaining({ tenantId: 'tenant-a', resourceType: ResourceType.SKILL }),+ );+ });+ });++ describe('findRoleByPermissions', () => {+ it('resolves a global default role from inside a tenant context', async () => {+ await seedGlobalRoles();++ const role = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRoleByPermissions(ResourceType.AGENT, RoleBits.OWNER),+ );++ expect(role).toEqual(expect.objectContaining({ accessRoleId: AccessRoleIds.AGENT_OWNER }));+ });++ it('does not match a global role whose identifier the tenant has redefined', async () => {+ await seedGlobalRoles();+ await shadowAgentOwnerAsEditor('tenant-a');++ const role = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.findRoleByPermissions(ResourceType.AGENT, RoleBits.OWNER),+ );++ /** No role visible to the tenant carries OWNER bits any more */+ expect(role).toBeNull();+ });+ });++ describe('getRoleForPermissions', () => {+ it('falls back to the global roles from inside a tenant context', async () => {+ await seedGlobalRoles();++ const role = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.getRoleForPermissions(ResourceType.AGENT, RoleBits.OWNER),+ );++ expect(role).toEqual(expect.objectContaining({ accessRoleId: AccessRoleIds.AGENT_OWNER }));+ });++ it('never grants the shadowed global bits when the tenant redefined the role', async () => {+ await seedGlobalRoles();+ await shadowAgentOwnerAsEditor('tenant-a');++ const role = await tenantStorage.run({ tenantId: 'tenant-a' }, () =>+ methods.getRoleForPermissions(ResourceType.AGENT, RoleBits.OWNER),+ );++ /** The closest visible role without exceeding OWNER, not the global OWNER row */+ expect(role?.permBits).toBe(RoleBits.EDITOR);+ });++ it('agrees with findRoleByIdentifier on what a shadowed identifier means', async () => {+ await seedGlobalRoles();+ await shadowAgentOwnerAsEditor('tenant-a');++ const [byIdentifier, byPermissions] = await tenantStorage.run(+ { tenantId: 'tenant-a' },+ async () => [+ await methods.findRoleByIdentifier(AccessRoleIds.AGENT_OWNER),+ await methods.getRoleForPermissions(ResourceType.AGENT, RoleBits.EDITOR),+ ],+ );++ expect(byIdentifier?.permBits).toBe(RoleBits.EDITOR);+ expect(byPermissions?.permBits).toBe(RoleBits.EDITOR);+ });+ });+});diff --git a/packages/data-schemas/src/methods/accessRole.ts b/packages/data-schemas/src/methods/accessRole.tsindex 0238d6c652..13d6c3d225 100644--- a/packages/data-schemas/src/methods/accessRole.ts+++ b/packages/data-schemas/src/methods/accessRole.ts@@ -1,6 +1,7 @@import { AccessRoleIds, ResourceType, PermissionBits } from 'librechat-data-provider';import type { Model, Types, DeleteResult } from 'mongoose';import type { IAccessRole } from '~/types';+import { getTenantId, runAsSystem, SYSTEM_TENANT_ID } from '~/config/tenantContext';import { RoleBits } from '~/common';export function createAccessRoleMethods(mongoose: typeof import('mongoose')): {@@ -24,6 +25,54 @@ export function createAccessRoleMethods(mongoose: typeof import('mongoose')): {) => Promise<IAccessRole | null>;findRolesByResourceType: (resourceType: string) => Promise<IAccessRole[]>;} {+ /** True inside a tenant's own context; false with no context or a system one. */+ function inTenantScope(): boolean {+ const tenantId = getTenantId();+ return tenantId != null && tenantId !== SYSTEM_TENANT_ID;+ }++ /**+ * Every access role visible inside the active tenant: the global defaults with the+ * tenant's own copies substituted by `accessRoleId`.+ *+ * The default roles are seeded once, globally, with no `tenantId`. A request+ * running inside a tenant context has its queries scoped to `{ tenantId: T }` by+ * the isolation plugin, so those seeded roles never match and every caller that+ * resolves a role by identifier — the owner grant on resource creation above all+ * — fails with `Role <id> not found`.+ *+ * Unfiltered by design: the merge happens first and every selection is applied to+ * its result. Substitution by identifier is only total that way — a tenant copy+ * that changes a field the caller selects on, `resourceType` above all, still has+ * to displace the global row it overrides, and a query that narrowed both sides+ * first would miss the copy and leave that row visible under its old type. The+ * read is bounded to one tenant's roles plus the global defaults, so this is a+ * few dozen small documents rather than a collection scan.+ *+ * The unique index on `(accessRoleId, tenantId)` makes the substitution unambiguous.+ */+ async function visibleTenantRoles(): Promise<IAccessRole[]> {+ const tenantId = getTenantId();+ const AccessRole = mongoose.models.AccessRole as Model<IAccessRole>;++ /** Capture the tenant before entering system context, and execute the bounded+ * query inside it. MongoDB's null match includes absent tenantId fields. */+ const roles = await runAsSystem(async () =>+ AccessRole.find({ tenantId: { $in: [tenantId, null] } })+ .lean<IAccessRole[]>()+ .exec(),+ );++ const rolesById = new Map<string, IAccessRole>();+ for (const role of roles) {+ const id = String(role.accessRoleId);+ if (role.tenantId === tenantId || !rolesById.has(id)) {+ rolesById.set(id, role);+ }+ }+ return [...rolesById.values()];+ }+/*** Find an access role by its ID* @param roleId - The role ID@@ -43,17 +92,33 @@ export function createAccessRoleMethods(mongoose: typeof import('mongoose')): {accessRoleId: string | Types.ObjectId,): Promise<IAccessRole | null> {const AccessRole = mongoose.models.AccessRole as Model<IAccessRole>;- return await AccessRole.findOne({ accessRoleId }).lean<IAccessRole>();+ if (!inTenantScope()) {+ return await AccessRole.findOne({ accessRoleId }).lean<IAccessRole>().exec();+ }+ const roles = await visibleTenantRoles();+ return roles.find((role) => String(role.accessRoleId) === String(accessRoleId)) ?? null;}/*** Find all access roles for a specific resource type+ *+ * The tenant's own copies are merged over the global default roles by+ * `accessRoleId`, so a tenant that overrides one role still sees the rest.+ ** @param resourceType - The type of resource ('agent', 'project', 'file')* @returns Array of role documents*/async function findRolesByResourceType(resourceType: string): Promise<IAccessRole[]> {const AccessRole = mongoose.models.AccessRole as Model<IAccessRole>;- return await AccessRole.find({ resourceType }).lean<IAccessRole[]>();+ if (!inTenantScope()) {+ return await AccessRole.find({ resourceType }).lean<IAccessRole[]>().exec();+ }+ /** Filtered after the merge, so a tenant copy that moves an identifier to another+ * resource type withdraws the global row from this list instead of leaving it+ * visible — and callers such as `bulkUpdatePermissions`, which key this result by+ * `accessRoleId`, cannot grant bits the tenant has superseded. */+ const roles = await visibleTenantRoles();+ return roles.filter((role) => role.resourceType === resourceType);}/**@@ -67,7 +132,13 @@ export function createAccessRoleMethods(mongoose: typeof import('mongoose')): {permBits: PermissionBits | RoleBits,): Promise<IAccessRole | null> {const AccessRole = mongoose.models.AccessRole as Model<IAccessRole>;- return await AccessRole.findOne({ resourceType, permBits }).lean<IAccessRole>();+ if (!inTenantScope()) {+ return await AccessRole.findOne({ resourceType, permBits }).lean<IAccessRole>().exec();+ }+ /** Selected from the visible set rather than queried directly, so a global role+ * whose identifier the tenant has redefined cannot match on its superseded bits. */+ const roles = await findRolesByResourceType(resourceType);+ return roles.find((role) => role.permBits === permBits) ?? null;}/**@@ -293,18 +364,21 @@ export function createAccessRoleMethods(mongoose: typeof import('mongoose')): {resourceType: string,permBits: PermissionBits | RoleBits,): Promise<IAccessRole | null> {- const AccessRole = mongoose.models.AccessRole as Model<IAccessRole>;- const exactMatch = await AccessRole.findOne({ resourceType, permBits }).lean<IAccessRole>();+ /** Both matches come from the one visible set, so an exact hit on a global role+ * the tenant has redefined cannot win over the tenant's own definition. */+ const roles = await findRolesByResourceType(resourceType);++ const exactMatch = roles.find((role) => role.permBits === permBits);if (exactMatch) {return exactMatch;}/** If no exact match, the closest role without exceeding permissions */- const roles = await AccessRole.find({ resourceType })- .sort({ permBits: -1 })- .lean<IAccessRole[]>();-- return roles.find((role) => (role.permBits & permBits) === role.permBits) || null;+ return (+ [...roles]+ .sort((a, b) => b.permBits - a.permBits)+ .find((role) => (role.permBits & permBits) === role.permBits) || null+ );}return {