Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(api-keys): enforce scope from request context after attach
wizardchenc65ffa3d9cde
diff --git a/internal/handler/dto/auth_test.go b/internal/handler/dto/auth_test.goindex bc2f1df81..bff1c2222 100644--- a/internal/handler/dto/auth_test.go+++ b/internal/handler/dto/auth_test.go@@ -26,7 +26,7 @@ func TestAuthLoginResponse_ViewerOmitsActiveTenantSecrets(t *testing.T) {assert.NotContains(t, s, "parser-secret-123")}-func TestAuthLoginResponse_OwnerGetsAPIKey(t *testing.T) {+func TestAuthLoginResponse_OwnerOmitsLegacyTenantAPIKey(t *testing.T) {tenant := sampleSecretTenant()resp := NewAuthLoginResponse(&types.LoginResponse{Success: true,@@ -38,7 +38,10 @@ func TestAuthLoginResponse_OwnerGetsAPIKey(t *testing.T) {})body, err := json.Marshal(resp)require.NoError(t, err)- assert.Contains(t, string(body), "tenant-api-key-123")+ s := string(body)+ assert.NotContains(t, s, `"api_key"`)+ assert.NotContains(t, s, "legacy-search-secret-999")+ assert.Contains(t, s, "web_search_config")}func TestAuthOIDCCallbackResponse_ViewerOmitsTenantSecrets(t *testing.T) {diff --git a/internal/handler/dto/tenant_test.go b/internal/handler/dto/tenant_test.goindex b7e98a48c..157bb6912 100644--- a/internal/handler/dto/tenant_test.go+++ b/internal/handler/dto/tenant_test.go@@ -25,14 +25,15 @@ func TestTenantResponse_ViewerOmitsSecrets(t *testing.T) {assert.NotContains(t, s, "credentials")}-func TestTenantResponse_OwnerGetsAPIKeyOnly(t *testing.T) {+func TestTenantResponse_OwnerOmitsLegacyTenantAPIKey(t *testing.T) {tenant := sampleSecretTenant()body, err := json.Marshal(NewTenantResponse(ownerContext(), tenant))require.NoError(t, err)s := string(body)- assert.Contains(t, s, "tenant-api-key-123")+ assert.NotContains(t, s, `"api_key"`)assert.NotContains(t, s, "legacy-search-secret-999")assert.NotContains(t, s, "parser-secret-123")+ assert.Contains(t, s, "web_search_config")}func TestTenantResponse_AdminGetsRedactedIntegrationConfigs(t *testing.T) {@@ -58,9 +59,8 @@ func TestTenantResponsesCrossTenant_RedactsEvenForOwnerContext(t *testing.T) {func sampleSecretTenant() *types.Tenant {return &types.Tenant{- ID: 42,- Name: "tenant",- APIKey: "tenant-api-key-123",+ ID: 42,+ Name: "tenant",WebSearchConfig: &types.WebSearchConfig{APIKey: "legacy-search-secret-999",ProxyURL: "http://proxy.internal:8080",diff --git a/internal/middleware/auth.go b/internal/middleware/auth.goindex 3e4ee926f..f89deab44 100644--- a/internal/middleware/auth.go+++ b/internal/middleware/auth.go@@ -260,7 +260,7 @@ func authenticateAPIKeyRequest(if c.IsAborted() {return false}- if err := authorizeTenantAPIKeyAccess(ctx, c.Request.Method, c.Request.URL.Path); err != nil {+ if err := authorizeTenantAPIKeyAccess(c.Request.Context(), c.Request.Method, c.Request.URL.Path); err != nil {c.JSON(http.StatusForbidden, gin.H{"error": "Forbidden: API key scope does not allow this operation"})c.Abort()return falsediff --git a/internal/middleware/auth_api_key_scope_test.go b/internal/middleware/auth_api_key_scope_test.gonew file mode 100644index 000000000..ad896b7f9--- /dev/null+++ b/internal/middleware/auth_api_key_scope_test.go@@ -0,0 +1,33 @@+package middleware++import (+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/Tencent/WeKnora/internal/types"+ "github.com/gin-gonic/gin"+)++// TestAuthenticateAPIKeyScopeUsesRequestContextAfterAttach verifies the+// authorizeTenantAPIKeyAccess call in authenticateAPIKeyRequest reads scope+// from c.Request.Context() after attachAPIKeyAuthContext, not the pre-attach ctx.+func TestAuthenticateAPIKeyScopeUsesRequestContextAfterAttach(t *testing.T) {+ gin.SetMode(gin.TestMode)++ c, _ := gin.CreateTestContext(httptest.NewRecorder())+ c.Request = httptest.NewRequest(http.MethodDelete, "/api/v1/knowledge-bases/kb-1", nil)++ preAttach := c.Request.Context()+ c.Request = c.Request.WithContext(types.WithTenantAPIKeyScope(preAttach, types.TenantAPIKeyScope{+ KeyID: 1,+ Scopes: types.StringArray{types.TenantAPIKeyScopeRead},+ }))++ if err := authorizeTenantAPIKeyAccess(preAttach, c.Request.Method, c.Request.URL.Path); err != nil {+ t.Fatalf("pre-attach ctx should not carry scope and must not block: %v", err)+ }+ if err := authorizeTenantAPIKeyAccess(c.Request.Context(), c.Request.Method, c.Request.URL.Path); err == nil {+ t.Fatal("post-attach request context must enforce read scope on DELETE")+ }+}