fix(api-keys): enforce scope from request context after attach
Use c.Request.Context() in authenticateAPIKeyRequest so scoped keys are actually restricted, update dto tests for removed tenant.api_key, and add a regression test for the attach-context contract.
This commit is contained in:
@@ -26,7 +26,7 @@ func TestAuthLoginResponse_ViewerOmitsActiveTenantSecrets(t *testing.T) {
|
|||||||
assert.NotContains(t, s, "parser-secret-123")
|
assert.NotContains(t, s, "parser-secret-123")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAuthLoginResponse_OwnerGetsAPIKey(t *testing.T) {
|
func TestAuthLoginResponse_OwnerOmitsLegacyTenantAPIKey(t *testing.T) {
|
||||||
tenant := sampleSecretTenant()
|
tenant := sampleSecretTenant()
|
||||||
resp := NewAuthLoginResponse(&types.LoginResponse{
|
resp := NewAuthLoginResponse(&types.LoginResponse{
|
||||||
Success: true,
|
Success: true,
|
||||||
@@ -38,7 +38,10 @@ func TestAuthLoginResponse_OwnerGetsAPIKey(t *testing.T) {
|
|||||||
})
|
})
|
||||||
body, err := json.Marshal(resp)
|
body, err := json.Marshal(resp)
|
||||||
require.NoError(t, err)
|
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) {
|
func TestAuthOIDCCallbackResponse_ViewerOmitsTenantSecrets(t *testing.T) {
|
||||||
|
|||||||
@@ -25,14 +25,15 @@ func TestTenantResponse_ViewerOmitsSecrets(t *testing.T) {
|
|||||||
assert.NotContains(t, s, "credentials")
|
assert.NotContains(t, s, "credentials")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTenantResponse_OwnerGetsAPIKeyOnly(t *testing.T) {
|
func TestTenantResponse_OwnerOmitsLegacyTenantAPIKey(t *testing.T) {
|
||||||
tenant := sampleSecretTenant()
|
tenant := sampleSecretTenant()
|
||||||
body, err := json.Marshal(NewTenantResponse(ownerContext(), tenant))
|
body, err := json.Marshal(NewTenantResponse(ownerContext(), tenant))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
s := string(body)
|
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, "legacy-search-secret-999")
|
||||||
assert.NotContains(t, s, "parser-secret-123")
|
assert.NotContains(t, s, "parser-secret-123")
|
||||||
|
assert.Contains(t, s, "web_search_config")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTenantResponse_AdminGetsRedactedIntegrationConfigs(t *testing.T) {
|
func TestTenantResponse_AdminGetsRedactedIntegrationConfigs(t *testing.T) {
|
||||||
@@ -58,9 +59,8 @@ func TestTenantResponsesCrossTenant_RedactsEvenForOwnerContext(t *testing.T) {
|
|||||||
|
|
||||||
func sampleSecretTenant() *types.Tenant {
|
func sampleSecretTenant() *types.Tenant {
|
||||||
return &types.Tenant{
|
return &types.Tenant{
|
||||||
ID: 42,
|
ID: 42,
|
||||||
Name: "tenant",
|
Name: "tenant",
|
||||||
APIKey: "tenant-api-key-123",
|
|
||||||
WebSearchConfig: &types.WebSearchConfig{
|
WebSearchConfig: &types.WebSearchConfig{
|
||||||
APIKey: "legacy-search-secret-999",
|
APIKey: "legacy-search-secret-999",
|
||||||
ProxyURL: "http://proxy.internal:8080",
|
ProxyURL: "http://proxy.internal:8080",
|
||||||
|
|||||||
@@ -260,7 +260,7 @@ func authenticateAPIKeyRequest(
|
|||||||
if c.IsAborted() {
|
if c.IsAborted() {
|
||||||
return false
|
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.JSON(http.StatusForbidden, gin.H{"error": "Forbidden: API key scope does not allow this operation"})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user