From abcecc870a7850d33b4a0d71d3845d0f3c4a1ae5 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Mon, 13 Jul 2026 10:54:00 +0800 Subject: [PATCH] fix(router): allow scoped API keys to poll FAQ import progress Register the FAQ import progress route through apiKeyRoute with retrieve or ingest capability so scoped keys that start imports can poll task status without hitting APIKeyGate fail-closed 403. --- internal/router/router.go | 13 ++++++++----- .../router_api_key_capabilities_test.go | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/internal/router/router.go b/internal/router/router.go index 22b0883f..5a3fe2d2 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -387,11 +387,14 @@ func RegisterFAQRoutes(r *gin.RouterGroup, handler *handler.FAQHandler, g *rbacG // FAQ import result display status faq.PUT("/import/last-result/display", g.OwnedKBOrAdmin(), g.KBAccessWrite("id"), handler.UpdateLastImportResultDisplayStatus) } - // FAQ import progress route (outside of knowledge-base scope) — Viewer+ - faqImport := r.Group("/faq/import") - { - faqImport.GET("/progress/:task_id", g.Viewer(), handler.GetImportProgress) - } + // FAQ import progress route (outside of knowledge-base scope) — Viewer+. + // Scoped API keys that can ingest (they start the import) or retrieve may + // poll their own import/dry-run progress. The task is tenant-scoped by + // requireTaskProgressTenant, so a key only ever sees its own tenant's + // tasks. Declared through apiKeyRoute so the APIKeyGate doesn't fail-closed + // and 403 the poller with "scope does not allow this operation". + g.apiKeyRoute(r, http.MethodGet, "/faq/import/progress/:task_id", + apiKeyRetrieve(apiKeyIngest(apiKeyFullAccess())), g.Viewer(), handler.GetImportProgress) } // RegisterKnowledgeBaseRoutes 注册知识库相关的路由 diff --git a/internal/router/router_api_key_capabilities_test.go b/internal/router/router_api_key_capabilities_test.go index 3b4df336..46b0c989 100644 --- a/internal/router/router_api_key_capabilities_test.go +++ b/internal/router/router_api_key_capabilities_test.go @@ -401,6 +401,25 @@ func TestChunkerPreviewRouteRequiresRetrieveOrIngestCapability(t *testing.T) { } } +func TestFAQImportProgressRouteRequiresRetrieveOrIngestCapability(t *testing.T) { + gin.SetMode(gin.TestMode) + g := &rbacGuards{} + v1 := gin.New().Group("/api/v1") + + RegisterFAQRoutes(v1, &handler.FAQHandler{}, g) + + policy := mustLookupAPIKeyPolicy(t, g, http.MethodGet, "/api/v1/faq/import/progress/:task_id") + if !policy.RequireFullAccess { + t.Fatal("policy should require full access without a matching capability") + } + if !policyHasCapability(policy, types.APIKeyCapabilityRetrieve) { + t.Fatalf("policy capabilities = %#v, want retrieve", policy.Capabilities) + } + if !policyHasCapability(policy, types.APIKeyCapabilityIngest) { + t.Fatalf("policy capabilities = %#v, want ingest", policy.Capabilities) + } +} + func mustLookupAPIKeyPolicy( t *testing.T, g *rbacGuards,