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:
wizardchen
2026-07-06 10:58:22 +08:00
committed by lyingbug
parent 94d18ea15a
commit c65ffa3d9c
4 changed files with 44 additions and 8 deletions
+5 -2
View File
@@ -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) {
+5 -5
View File
@@ -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",