fix(security): redact integration secrets from viewer-accessible endpoints

Prevent low-privileged tenant members from reading raw API keys and
infrastructure credentials via tenant profile, KV config, model/MCP/web
search listings, and KB initialization config responses.
This commit is contained in:
wizardchen
2026-07-02 12:31:57 +08:00
committed by lyingbug
parent 8325dac738
commit 0b73080bb8
18 changed files with 649 additions and 61 deletions
+21 -5
View File
@@ -25,7 +25,7 @@ func TestMCPServiceResponse_OmitsSecrets(t *testing.T) {
CustomHeaders: map[string]string{"X-Trace": "abc"},
},
}
body, err := json.Marshal(NewMCPServiceResponse(svc))
body, err := json.Marshal(NewMCPServiceResponse(adminContext(), svc))
assert.NoError(t, err)
s := string(body)
assert.NotContains(t, s, "sk-real-api-key-do-not-leak",
@@ -63,7 +63,7 @@ func TestMCPServiceResponse_BuiltinStripsTenantConfig(t *testing.T) {
APIKey: "should-not-leak-via-builtin",
},
}
resp := NewMCPServiceResponse(svc)
resp := NewMCPServiceResponse(adminContext(), svc)
assert.Nil(t, resp.URL, "builtin must not leak per-tenant URL")
assert.Nil(t, resp.Headers, "builtin must not leak per-tenant headers")
assert.Nil(t, resp.AuthConfig, "builtin must not leak auth config")
@@ -73,7 +73,23 @@ func TestMCPServiceResponse_BuiltinStripsTenantConfig(t *testing.T) {
assert.False(t, strings.Contains(string(body), "X-Tenant-Secret"))
}
func TestMCPServiceResponse_NilSafe(t *testing.T) {
assert.Nil(t, NewMCPServiceResponse(nil))
assert.Equal(t, []*MCPServiceResponse{}, NewMCPServiceResponses(nil))
func TestMCPServiceResponse_ViewerStripsIntegrationDetail(t *testing.T) {
svc := &types.MCPService{
ID: "svc-2",
Headers: types.MCPHeaders{"Authorization": "Bearer secret"},
EnvVars: types.MCPEnvVars{"TOKEN": "secret"},
AuthConfig: &types.MCPAuthConfig{
CustomHeaders: map[string]string{"X-Auth": "secret"},
},
}
resp := NewMCPServiceResponse(viewerContext(), svc)
assert.Nil(t, resp.Headers)
assert.Nil(t, resp.EnvVars)
assert.NotNil(t, resp.AuthConfig)
assert.Nil(t, resp.AuthConfig.CustomHeaders)
}
func TestMCPServiceResponse_NilSafe(t *testing.T) {
assert.Nil(t, NewMCPServiceResponse(adminContext(), nil))
assert.Equal(t, []*MCPServiceResponse{}, NewMCPServiceResponses(adminContext(), nil))
}