fix(agent): enhance user authorization and concurrency handling in tool approval

- Introduced a new test to ensure that empty user IDs are rejected when a waiter has a user, preventing unauthorized approvals.
- Added RequestNonce to resolve messages to uniquely identify Resolve calls, ensuring concurrent requests do not interfere with each other.
- Updated waiter structure to use atomic operations for the resolved state, improving thread safety.
- Enhanced error handling in the Resolve method to properly manage user mismatches and ensure accurate acknowledgment delivery across instances.
- Updated MCP service handler to reject unauthenticated requests early, providing clearer feedback on authorization requirements.
This commit is contained in:
wizardchen
2026-05-10 22:41:45 +08:00
committed by lyingbug
parent 6d0f09d75f
commit 7c0964bb95
3 changed files with 62 additions and 13 deletions
+7
View File
@@ -566,6 +566,13 @@ func (h *MCPServiceHandler) ResolveToolApproval(c *gin.Context) {
}
userID, _ := c.Get(types.UserIDContextKey.String())
userIDStr, _ := userID.(string)
// Reject calls without an authenticated user up front. The gate's
// per-user authorization is fail-close, but surfacing 401 here gives
// a clearer signal that auth middleware did not populate the context.
if strings.TrimSpace(userIDStr) == "" {
c.Error(errors.NewUnauthorizedError("authenticated user required to resolve tool approval"))
return
}
if err := h.toolApprovalGate.Resolve(tenantID, userIDStr, pendingID, dec); err != nil {
switch {
case stderrors.Is(err, approval.ErrPendingNotFound):