mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-03-24 16:46:33 +01:00
🧯 fix: Remove Revoked Agents from User Favorites (#12296)
* 🧯 fix: Remove revoked agents from user favorites When agent access is revoked, the agent remained in the user's favorites causing repeated 403 errors on page load. Backend now cleans up favorites on permission revocation; frontend treats 403 like 404 and auto-removes stale agent references. * 🧪 fix: Address review findings for stale agent favorites cleanup - Guard cleanup effect with ref to prevent infinite loop on mutation failure (Finding 1) - Use validated results.revoked instead of raw request payload for revokedUserIds (Finding 3) - Stabilize staleAgentIds memo with string key to avoid spurious re-evaluation during drag-drop (Finding 5) - Add JSDoc with param types to removeRevokedAgentFromFavorites (Finding 7) - Return promise from removeRevokedAgentFromFavorites for testability - Add 7 backend tests covering revocation cleanup paths - Add 3 frontend tests for 403 handling and stale cleanup persistence
This commit is contained in:
parent
b189972381
commit
93952f06b4
4 changed files with 409 additions and 3 deletions
|
|
@ -24,7 +24,7 @@ const {
|
|||
entraIdPrincipalFeatureEnabled,
|
||||
searchEntraIdPrincipals,
|
||||
} = require('~/server/services/GraphApiService');
|
||||
const { AclEntry, AccessRole } = require('~/db/models');
|
||||
const { Agent, AclEntry, AccessRole, User } = require('~/db/models');
|
||||
|
||||
/**
|
||||
* Generic controller for resource permission endpoints
|
||||
|
|
@ -43,6 +43,28 @@ const validateResourceType = (resourceType) => {
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes an agent from the favorites of specified users (fire-and-forget).
|
||||
* Both AGENT and REMOTE_AGENT resource types share the Agent collection.
|
||||
* @param {string} resourceId - The agent's MongoDB ObjectId hex string
|
||||
* @param {string[]} userIds - User ObjectId strings whose favorites should be cleaned
|
||||
*/
|
||||
const removeRevokedAgentFromFavorites = (resourceId, userIds) =>
|
||||
Agent.findOne({ _id: resourceId }, { id: 1 })
|
||||
.lean()
|
||||
.then((agent) => {
|
||||
if (!agent) {
|
||||
return;
|
||||
}
|
||||
return User.updateMany(
|
||||
{ _id: { $in: userIds }, 'favorites.agentId': agent.id },
|
||||
{ $pull: { favorites: { agentId: agent.id } } },
|
||||
);
|
||||
})
|
||||
.catch((err) => {
|
||||
logger.error('[removeRevokedAgentFromFavorites] Error cleaning up favorites', err);
|
||||
});
|
||||
|
||||
/**
|
||||
* Bulk update permissions for a resource (grant, update, remove)
|
||||
* @route PUT /api/{resourceType}/{resourceId}/permissions
|
||||
|
|
@ -155,6 +177,16 @@ const updateResourcePermissions = async (req, res) => {
|
|||
grantedBy: userId,
|
||||
});
|
||||
|
||||
const isAgentResource =
|
||||
resourceType === ResourceType.AGENT || resourceType === ResourceType.REMOTE_AGENT;
|
||||
const revokedUserIds = results.revoked
|
||||
.filter((p) => p.type === PrincipalType.USER && p.id)
|
||||
.map((p) => p.id);
|
||||
|
||||
if (isAgentResource && revokedUserIds.length > 0) {
|
||||
removeRevokedAgentFromFavorites(resourceId, revokedUserIds);
|
||||
}
|
||||
|
||||
/** @type {TUpdateResourcePermissionsResponse} */
|
||||
const response = {
|
||||
message: 'Permissions updated successfully',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue