mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 08:12:00 +02:00

refactor: organize Sharing/Agent components, improve type safety for resource types and access role ids, rename enums to PascalCase refactor: organize Sharing/Agent components, improve type safety for resource types and access role ids chore: move sharing related components to dedicated "Sharing" directory chore: remove PublicSharingToggle component and update index exports chore: move non-sidepanel agent components to `~/components/Agents` chore: move AgentCategoryDisplay component with tests chore: remove commented out code refactor: change PERMISSION_BITS from const to enum for better type safety refactor: reorganize imports in GenericGrantAccessDialog and update index exports for hooks refactor: update type definitions to use ACCESS_ROLE_IDS for improved type safety refactor: remove unused canAccessPromptResource middleware and related code refactor: remove unused prompt access roles from createAccessRoleMethods refactor: update resourceType in AclEntry type definition to remove unused 'prompt' value refactor: introduce ResourceType enum and update resourceType usage across data provider files for improved type safety refactor: update resourceType usage to ResourceType enum across sharing and permissions components for improved type safety refactor: standardize resourceType usage to ResourceType enum across agent and prompt models, permissions controller, and middleware for enhanced type safety refactor: update resourceType references from PROMPT_GROUP to PROMPTGROUP for consistency across models, middleware, and components refactor: standardize access role IDs and resource type usage across agent, file, and prompt models for improved type safety and consistency chore: add typedefs for TUpdateResourcePermissionsRequest and TUpdateResourcePermissionsResponse to enhance type definitions chore: move SearchPicker to PeoplePicker dir refactor: implement debouncing for query changes in SearchPicker for improved performance chore: fix typing, import order for agent admin settings fix: agent admin settings, prevent agent form submission refactor: rename `ACCESS_ROLE_IDS` to `AccessRoleIds` refactor: replace PermissionBits with PERMISSION_BITS refactor: replace PERMISSION_BITS with PermissionBits
97 lines
3.2 KiB
JavaScript
97 lines
3.2 KiB
JavaScript
const { logger } = require('@librechat/data-schemas');
|
|
const { Constants, isAgentsEndpoint, ResourceType } = require('librechat-data-provider');
|
|
const { canAccessResource } = require('./canAccessResource');
|
|
const { getAgent } = require('~/models/Agent');
|
|
|
|
/**
|
|
* Agent ID resolver function for agent_id from request body
|
|
* Resolves custom agent ID (e.g., "agent_abc123") to MongoDB ObjectId
|
|
* This is used specifically for chat routes where agent_id comes from request body
|
|
*
|
|
* @param {string} agentCustomId - Custom agent ID from request body
|
|
* @returns {Promise<Object|null>} Agent document with _id field, or null if not found
|
|
*/
|
|
const resolveAgentIdFromBody = async (agentCustomId) => {
|
|
// Handle ephemeral agents - they don't need permission checks
|
|
if (agentCustomId === Constants.EPHEMERAL_AGENT_ID) {
|
|
return null; // No permission check needed for ephemeral agents
|
|
}
|
|
|
|
return await getAgent({ id: agentCustomId });
|
|
};
|
|
|
|
/**
|
|
* Middleware factory that creates middleware to check agent access permissions from request body.
|
|
* This middleware is specifically designed for chat routes where the agent_id comes from req.body
|
|
* instead of route parameters.
|
|
*
|
|
* @param {Object} options - Configuration options
|
|
* @param {number} options.requiredPermission - The permission bit required (1=view, 2=edit, 4=delete, 8=share)
|
|
* @returns {Function} Express middleware function
|
|
*
|
|
* @example
|
|
* // Basic usage for agent chat (requires VIEW permission)
|
|
* router.post('/chat',
|
|
* canAccessAgentFromBody({ requiredPermission: PermissionBits.VIEW }),
|
|
* buildEndpointOption,
|
|
* chatController
|
|
* );
|
|
*/
|
|
const canAccessAgentFromBody = (options) => {
|
|
const { requiredPermission } = options;
|
|
|
|
// Validate required options
|
|
if (!requiredPermission || typeof requiredPermission !== 'number') {
|
|
throw new Error('canAccessAgentFromBody: requiredPermission is required and must be a number');
|
|
}
|
|
|
|
return async (req, res, next) => {
|
|
try {
|
|
const { endpoint, agent_id } = req.body;
|
|
let agentId = agent_id;
|
|
|
|
if (!isAgentsEndpoint(endpoint)) {
|
|
agentId = Constants.EPHEMERAL_AGENT_ID;
|
|
}
|
|
|
|
if (!agentId) {
|
|
return res.status(400).json({
|
|
error: 'Bad Request',
|
|
message: 'agent_id is required in request body',
|
|
});
|
|
}
|
|
|
|
// Skip permission checks for ephemeral agents
|
|
if (agentId === Constants.EPHEMERAL_AGENT_ID) {
|
|
return next();
|
|
}
|
|
|
|
const agentAccessMiddleware = canAccessResource({
|
|
resourceType: ResourceType.AGENT,
|
|
requiredPermission,
|
|
resourceIdParam: 'agent_id', // This will be ignored since we use custom resolver
|
|
idResolver: () => resolveAgentIdFromBody(agentId),
|
|
});
|
|
|
|
const tempReq = {
|
|
...req,
|
|
params: {
|
|
...req.params,
|
|
agent_id: agentId,
|
|
},
|
|
};
|
|
|
|
return agentAccessMiddleware(tempReq, res, next);
|
|
} catch (error) {
|
|
logger.error('Failed to validate agent access permissions', error);
|
|
return res.status(500).json({
|
|
error: 'Internal Server Error',
|
|
message: 'Failed to validate agent access permissions',
|
|
});
|
|
}
|
|
};
|
|
};
|
|
|
|
module.exports = {
|
|
canAccessAgentFromBody,
|
|
};
|