From 7c0964bb95e1c294a7c92997525f78e940a9fc8b Mon Sep 17 00:00:00 2001 From: wizardchen Date: Sun, 10 May 2026 22:41:45 +0800 Subject: [PATCH] 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. --- internal/agent/approval/gate.go | 44 ++++++++++++++++++++-------- internal/agent/approval/gate_test.go | 24 +++++++++++++++ internal/handler/mcp_service.go | 7 +++++ 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/internal/agent/approval/gate.go b/internal/agent/approval/gate.go index ae2ee931..fe150764 100644 --- a/internal/agent/approval/gate.go +++ b/internal/agent/approval/gate.go @@ -9,6 +9,7 @@ import ( "os" "strings" "sync" + "sync/atomic" "time" "github.com/Tencent/WeKnora/internal/config" @@ -43,14 +44,19 @@ type resolveMessage struct { // resolveAck so the caller can know if delivery actually happened. ReplyChannel string `json:"reply_channel,omitempty"` OriginID string `json:"origin_id,omitempty"` + // RequestNonce uniquely identifies this Resolve call. The owning + // instance echoes it back in resolveAck so concurrent Resolve callers + // for the same pendingID don't consume each other's acks. + RequestNonce string `json:"request_nonce,omitempty"` } // resolveAck is published by the owning instance back to the caller's // per-pending reply channel so HTTP semantics stay accurate cross-instance. type resolveAck struct { - PendingID string `json:"pending_id"` - Status string `json:"status"` // ok | not_found | tenant_mismatch | user_mismatch | already_resolved - OriginID string `json:"origin_id,omitempty"` + PendingID string `json:"pending_id"` + Status string `json:"status"` // ok | not_found | tenant_mismatch | user_mismatch | already_resolved + OriginID string `json:"origin_id,omitempty"` + RequestNonce string `json:"request_nonce,omitempty"` } // pubsubChannel returns the namespaced pubsub channel name. @@ -121,7 +127,9 @@ type waiter struct { tenantID uint64 userID string // empty means "skip user check" once sync.Once - resolved bool // set true once a decision was delivered (by user, timer, or ctx) + // resolved is atomic so deliverLocal can read it without holding g.mu + // (deliver writes it from timer/ctx branches that don't take g.mu). + resolved atomic.Bool } // deliver returns true when this call won the race and actually delivered the @@ -135,7 +143,7 @@ func (w *waiter) deliver(d Decision) bool { w.once.Do(func() { select { case w.ch <- d: - w.resolved = true + w.resolved.Store(true) delivered = true default: } @@ -227,9 +235,10 @@ func (g *Gate) runSubscriber() { } if status != "" { ackPayload, _ := json.Marshal(resolveAck{ - PendingID: m.PendingID, - Status: status, - OriginID: instanceID, + PendingID: m.PendingID, + Status: status, + OriginID: instanceID, + RequestNonce: m.RequestNonce, }) pubCtx, pubCancel := context.WithTimeout(ctx, 2*time.Second) if pErr := g.rdb.Publish(pubCtx, m.ReplyChannel, ackPayload).Err(); pErr != nil { @@ -352,9 +361,6 @@ func (g *Gate) RequestAndWait(ctx context.Context, req PendingRequest) (Decision defer timer.Stop() emitResolved := func(d Decision) { - if req.EventBus == nil { - return - } _ = req.EventBus.Emit(context.WithoutCancel(ctx), event.Event{ ID: pendingID + "-approval-resolved", Type: event.EventToolApprovalResolved, @@ -427,6 +433,9 @@ func (g *Gate) Resolve(tenantID uint64, userID, pendingID string, d Decision) er // the local path would return, so HTTP responses stay accurate even when the // session lives on a different replica. func (g *Gate) resolveCrossInstance(tenantID uint64, userID, pendingID string, d Decision) error { + // Per-call nonce so concurrent Resolve callers for the same pendingID + // don't consume each other's acks on the shared reply channel. + nonce := uuid.New().String() replyChannel := pubsubChannel() + ":reply:" + pendingID sub := g.rdb.Subscribe(context.Background(), replyChannel) defer func() { _ = sub.Close() }() @@ -449,6 +458,7 @@ func (g *Gate) resolveCrossInstance(tenantID uint64, userID, pendingID string, d Canceled: d.ContextCanceled, ReplyChannel: replyChannel, OriginID: instanceID, + RequestNonce: nonce, }) if mErr != nil { return fmt.Errorf("encode pubsub payload: %w", mErr) @@ -473,6 +483,10 @@ func (g *Gate) resolveCrossInstance(tenantID uint64, userID, pendingID string, d if jErr := json.Unmarshal([]byte(msg.Payload), &ack); jErr != nil { continue } + // Ignore acks intended for a different concurrent Resolve call. + if ack.RequestNonce != "" && ack.RequestNonce != nonce { + continue + } switch ack.Status { case "ok": return nil @@ -502,12 +516,16 @@ func (g *Gate) deliverLocal(tenantID uint64, userID, pendingID string, d Decisio g.mu.Unlock() return ErrTenantMismatch } - if w.userID != "" && userID != "" && w.userID != userID { + // Authorization: when the pending was registered with a userID, the + // caller MUST present the same non-empty userID. An empty caller + // userID is treated as a mismatch (fail-close) so a missing auth + // middleware cannot bypass the per-user check. + if w.userID != "" && w.userID != userID { g.mu.Unlock() return ErrUserMismatch } - already := w.resolved g.mu.Unlock() + already := w.resolved.Load() if already { return ErrAlreadyResolved diff --git a/internal/agent/approval/gate_test.go b/internal/agent/approval/gate_test.go index 61ba2330..5452344f 100644 --- a/internal/agent/approval/gate_test.go +++ b/internal/agent/approval/gate_test.go @@ -127,6 +127,30 @@ func TestGate_Resolve_UserMismatch(t *testing.T) { require.True(t, d.Approved) } +// TestGate_Resolve_EmptyUserIDRejectedWhenWaiterHasUser guards against the +// previous fail-open short-circuit where an empty caller userID skipped the +// per-user check entirely (allowing same-tenant cross-user approval). +func TestGate_Resolve_EmptyUserIDRejectedWhenWaiterHasUser(t *testing.T) { + bus := event.NewEventBus() + g := NewGate(&config.Config{Agent: &config.AgentConfig{ToolApprovalTimeoutSeconds: 2}}, &stubChecker{required: true}, nil) + req := PendingRequest{ + TenantID: 1, UserID: "alice", EventBus: bus, + SessionID: "s1", AssistantMessageID: "m1", + ServiceID: "svc", MCPToolName: "t", Args: json.RawMessage(`{}`), + } + bus.On(event.EventToolApprovalRequired, func(_ context.Context, evt event.Event) error { + data := evt.Data.(event.ToolApprovalRequiredData) + go func() { + require.ErrorIs(t, g.Resolve(1, "", data.PendingID, Decision{Approved: true}), ErrUserMismatch) + _ = g.Resolve(1, "alice", data.PendingID, Decision{Approved: false, Reason: "no"}) + }() + return nil + }) + d, err := g.RequestAndWait(context.Background(), req) + require.NoError(t, err) + require.False(t, d.Approved) +} + func TestGate_Resolve_AlreadyResolvedAfterTimeout(t *testing.T) { g := NewGate(&config.Config{Agent: &config.AgentConfig{ToolApprovalTimeoutSeconds: 1}}, &stubChecker{required: true}, nil) bus := event.NewEventBus() diff --git a/internal/handler/mcp_service.go b/internal/handler/mcp_service.go index dc2abd86..f7b0f759 100644 --- a/internal/handler/mcp_service.go +++ b/internal/handler/mcp_service.go @@ -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):