mirror of
https://github.com/Tencent/WeKnora.git
synced 2026-08-30 16:53:21 +08:00
0e1482bc4c
logger.CloneContext rebuilds a fresh context.Background() and only copies a hand-curated allow-list of context keys (LoggerContextKey, TenantIDContextKey, UserContextKey, etc.) before passing it to downstream handlers — the chat / agent QA pipelines all do `ctx := logger.CloneContext(c.Request.Context())` at the top of parseQARequest. TenantRoleContextKey was missing from that allow-list. Any call site that read the role via TenantRoleFromContext(reqCtx.ctx) saw the type-zero TenantRole and the helper's least-privilege fallback returned Viewer. The auth middleware had set role=Owner correctly on the parent context, but the cloned context lost it. Symptom: an Owner of their home tenant got 403 on POST /agent-chat when the agent had runnable_by_viewer=false, because the gate at session/qa.go:520 saw role=viewer despite middleware logging role=owner two ms earlier. Diagnostic log lines added in the previous commits (auth.go:145 "[auth] resolved role=...", auth.go:283 "[auth] resolveTenantRole step1 hit row_role=...") made this immediately visible — a request showed role=owner at the middleware layer and role=viewer at the handler layer, which can only mean ctx propagation lost it. Keeping the diagnostic logs in place so similar regressions surface fast. Fix is one line: add TenantRoleContextKey to the CloneContext allow-list, with a comment pinning it to the bug it fixes so a future drive-by removal has to weigh that loss.