LibreChat/api/server/routes/admin/config.js
Danny Avila 080e5f500c
Some checks failed
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Vote Gate / Gate (push) Waiting to run
Codegraph E2E Votes / vote (shard 1/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 10/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 2/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 3/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 4/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 5/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 6/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 7/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 8/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 9/10) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/chat (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Has been cancelled
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Has been cancelled
🏘️ fix: Resolve Tenant Profiles in Their Own Partition and Keep Role Operations in the Caller's (#17417)
- GET /resolved resolves a group or user profile in the partition its document belongs to, as
  writes judge it: for an admin without a tenant reading a tenant's profile, that tenant's
  librechat.yaml and __base__. GET /catalog resolves such a scope there too, instead of
  answering 404, and GET /effective finds a user by id the way profiles are addressed and
  resolves them in their own tenant: their role, groups and documents there.
- findPartitionUser becomes findPrincipalUser, which also returns the user's tenant.
- Admin role operations address the role of the caller's tenant partition: the role lookup
  (getPartitionRole), listing, creation's name check, the members a rename migrates or a deletion
  reassigns, the members listed and counted, and the ACL entries (deletePrincipalAclEntries) and
  grants a deletion removes. A member added or removed by id must be a user of that partition.
- getRoleByName, which every request's permission checks read, reads the role of the caller's
  partition too: outside a tenant and under the system context the platform-level one, never an
  older tenant role of the same name. updateRoleByName without an id and updateAccessPermissions
  for a role read without its _id write that role, so the legacy permission route changes the
  platform role. The grants route checks a role exists with getPartitionRole.
- runInPartition moves a read into another partition only from outside a tenant, and rejects
  leaving an active one.
- deleteGrantsForPrincipal and findGrantsForPrincipal without a tenantId reach only
  platform-level grants, as moveGrantsToPrincipal does.
- updateRoleByName takes { id } to change one role document; renames, their rollback,
  description updates and updateAccessPermissions write the document they read.
- The exported keyed rename helpers (applyConfigChange rename, renameConfigEntryTombstones)
  refuse a new name no keyed path can address, as the admin routes do.
2026-10-24 18:33:12 +02:00

110 lines
3.9 KiB
JavaScript

const express = require('express');
const {
rejectUnsafeConfigKeys,
createAdminConfigHandlers,
allowBeforeTermsAcceptance,
createAdminConfigReadHandlers,
createAdminConfigCatalogHandlers,
} = require('@librechat/api');
const { SystemCapabilities } = require('@librechat/data-schemas');
const {
hasCapability,
requireCapability,
hasConfigCapability,
hasAnyConfigReadAccess,
getReadableConfigSections,
} = require('~/server/middleware/roles/capabilities');
const {
getAppConfig,
getCachedTools,
overrideCacheTtl,
loadConfigModels,
loadDefaultModels,
rederiveEndpoints,
getEndpointsConfig,
reloadConfigCaches,
resolveConfigLayers,
invalidateConfigCaches,
} = require('~/server/services/Config');
const { availableTools, toolkits } = require('~/app/clients/tools');
const { requireJwtAuth } = require('~/server/middleware');
const db = require('~/models');
const router = express.Router();
const requireAdminAccess = requireCapability(SystemCapabilities.ACCESS_ADMIN);
const handlers = createAdminConfigHandlers({
listAllConfigs: db.listAllConfigs,
findConfigByPrincipal: db.findConfigByPrincipal,
writeConfigDocument: db.writeConfigDocument,
unsetConfigField: db.unsetConfigField,
deleteConfig: db.deleteConfig,
toggleConfigActiveTracked: db.toggleConfigActiveTracked,
setConfigPriority: db.setConfigPriority,
principalExists: db.principalExists,
hasAnyConfigReadAccess,
getReadableConfigSections,
hasConfigCapability,
hasCapability,
getAppConfig,
invalidateConfigCaches,
reloadConfigCaches,
recordAuditEntry: db.recordAuditEntry,
auditFailClosed: process.env.AUDIT_LOG_FAIL_CLOSED === 'true',
overrideCacheTtlMs: overrideCacheTtl,
overrideEnvAllowlist: process.env.CONFIG_OVERRIDE_ENV_ALLOWLIST,
rederiveEndpoints,
});
const readHandlers = createAdminConfigReadHandlers({
findConfigByPrincipal: db.findConfigByPrincipal,
hasAnyConfigReadAccess,
getReadableConfigSections,
hasCapability,
resolveConfigLayers,
principalExists: db.principalExists,
findPrincipalUser: db.findPrincipalUser,
getUserPrincipals: db.getUserPrincipals,
getApplicableConfigs: db.getApplicableConfigs,
});
const catalogHandlers = createAdminConfigCatalogHandlers({
hasAnyConfigReadAccess,
getReadableConfigSections,
hasConfigCapability,
getAppConfig,
getEndpointsConfig,
loadDefaultModels,
loadConfigModels,
getCachedTools,
toolManifest: availableTools,
toolkits,
principalExists: db.principalExists,
findConfigByPrincipal: db.findConfigByPrincipal,
listAllConfigs: db.listAllConfigs,
overrideEnvAllowlist: process.env.CONFIG_OVERRIDE_ENV_ALLOWLIST,
});
/** Config reads and writes stay open to an admin who has not accepted the terms. */
router.use(allowBeforeTermsAcceptance, requireJwtAuth, requireAdminAccess, rejectUnsafeConfigKeys);
router.get('/', handlers.listConfigs);
router.get('/meta', handlers.getConfigMeta);
router.get('/base', handlers.getBaseConfig);
router.get('/catalog', catalogHandlers.getConfigCatalog);
router.get('/env-status', catalogHandlers.getEnvStatus);
router.get('/effective', readHandlers.getEffectiveConfig);
router.get('/resolved/:principalType/:principalId', readHandlers.getResolvedConfig);
router.post('/reload', handlers.reloadConfig);
router.get('/:principalType/:principalId', handlers.getConfig);
router.put('/:principalType/:principalId', handlers.upsertConfigOverrides);
router.patch('/:principalType/:principalId/fields', handlers.patchConfigField);
router.post('/:principalType/:principalId/fields/tombstone', handlers.tombstoneConfigField);
router.delete('/:principalType/:principalId/fields', handlers.deleteConfigField);
router.post('/:principalType/:principalId/changes', handlers.applyConfigChanges);
router.delete('/:principalType/:principalId', handlers.deleteConfigOverrides);
router.patch('/:principalType/:principalId/active', handlers.toggleConfig);
router.patch('/:principalType/:principalId/priority', handlers.updateConfigPriority);
module.exports = router;