Files
ssw-ontology/server/classification_config_test.go
T
adminandClaude Code 25bbd15fd6 fix(authz): 封堵 X-Principal 头越权洞(裸头即可取得 manager)
principalFromRequest 曾把客户端可任意设置的 X-Principal 请求头列为最高
优先级、且完全不校验地决定主体。任何能发 HTTP 请求的一方带一个
X-Principal: manager 即取得平台最高权限;managerAuth /
ingestOrManagerAuth / mcpAuth / connectorAuth 读路径过滤全部沦陷。
该头排在真实凭据之前,合法的低权限 key 配该头即提权。

修复:外部入口一律不认该头,主体只来自真实凭据。
- 新增 server/principal_ctx.go:主体挂在 *http.Request 的 context 上。
  context 由代码构造、网络请求携带不了,外部伪造不命中。
- callHandler / mcpCall 两处进程内自调用改走 context 注入。
  callConn 不动(v2 已透传调用方真实 api_key / session cookie)。
- review-kit 代理 mcp-review-proxy.js 的 CORS 允许头去掉 X-Principal:
  该代理是 Access-Control-Allow-Origin: * 且原本显式放行该头,
  任意网页 JS 可借它伪造身份(main.go 的 corsMiddleware 管不到这条路)。

不采用降优先级 / 回环 IP 白名单 / 内部签名头——三者都仍留下一条外部
可构造的提权路径。

测试:新增 server/xprincipal_authz_test.go 覆盖匿名伪造(managerAuth 401 /
ingestOrManagerAuth 403 / mcpAuth 401)、该头不覆盖已解析的真实主体、
真实 manager key 仍放行;集成测试 60 处 X-Principal: manager 测试捷径
改为查库取真实 api_key。变异检查确认 MCP 进程内路径确实依赖新注入。

顺带转绿既有的 TestIntegration_ClassificationFilter:共享测试库 5 个主体
均已配 api_key,connectorAuth 要求 X-API-Key 而该测试只带 X-Principal,
迁移后自然修复。

验证:go test ./server/... 与 go test -tags=integration ./server/... 全绿;
./build.sh 通过(server/reviewkit 是 //go:embed 的,需重新编译进二进制)。
server/ai 的 TestHeuristicA5 仍红,是仓库外硬编码 fixture 路径不存在,
与本次无关。

Co-Authored-By: Claude Code <[email protected]>
2026-10-17 12:14:47 +08:00

203 lines
6.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//go:build integration
package main
import (
"strconv"
"strings"
"testing"
)
// TestIntegration_SetObjectTypeClassification 验证「设置对象类型分级配置」端点
// 端到端能力:写入 → invalidateClassCache → GET config 立刻反映 → 不存在的 property 校验。
//
// 真实场景背景(v1.3.0 之前):
// - GET /classification/config 已存在很久(v1.2.0),但 PUT 端点缺失
// - 前端编辑「对象类型分级」保存按钮看似无反应(POST 到不存在的路由 → 404)
// - 后果:587 个打了 data_level 标签的实例是「裸属性」无法被分级引擎接管
func TestIntegration_SetObjectTypeClassification(t *testing.T) {
env, a := setupTestEnv(t)
h := authHeaderFor(t, a, "manager")
// 用一个已有「真属性行」的类型,避免 setupTestEnv 新建的测试类型 properties 行为不同
// DataDemand 在本工作区有 64 个属性行(详见 docs/db/0910 schema)。
const fixedType = "DataDemand"
t.Cleanup(func() {
// 测试结束清理 DataDemand 的分级配置(避免污染其他测试)
env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+fixedType+"/classification",
map[string]any{
"classification_property": "",
"classification_levels": []string{},
"classification_default": "",
}, h)
})
// 1) PUT 设置分级属性(DataDemand 有 approval_status 字段)
code, body := env.httpDo(t, "PUT",
"/api/v1/ontology/object-types/"+fixedType+"/classification",
map[string]any{
"classification_property": "approval_status",
"classification_levels": []string{"1", "3-replaced"},
"classification_default": "1",
}, h)
if code != 200 {
t.Fatalf("PUT 应 200, got %d body=%v", code, body)
}
if body["enabled"] != true {
t.Errorf("enabled 应 true, got %v", body["enabled"])
}
// 2) GET config 立刻反映
_, getConfig := env.httpDo(t, "GET", "/api/v1/ontology/classification/config", nil, h)
var found map[string]any
for _, it := range getConfig["items"].([]any) {
m := it.(map[string]any)
if m["api_name"] == fixedType {
found = m
}
}
if found == nil {
t.Fatalf("GET config 应包含 %s, got %v", fixedType, getConfig)
}
if found["enabled"] != true || found["classification_property"] != "approval_status" {
t.Errorf("config 不匹配 PUT, got %+v", found)
}
// 3) 校验:levels 空但 property 已设 → 400(property=""+levels=[] 是关闭分级入口,合法)
code, _ = env.httpDo(t, "PUT",
"/api/v1/ontology/object-types/"+fixedType+"/classification",
map[string]any{
"classification_property": "approval_status",
"classification_levels": []string{},
}, h)
if code != 400 {
t.Errorf("property 已设但 levels=空 应 400, got %d", code)
}
// 4) 校验:default 不在 levels → 400
code, _ = env.httpDo(t, "PUT",
"/api/v1/ontology/object-types/"+fixedType+"/classification",
map[string]any{
"classification_property": "approval_status",
"classification_levels": []string{"1", "3-replaced"},
"classification_default": "GAMMA-OUT-OF-RANGE",
}, h)
if code != 400 {
t.Errorf("default 不在 levels 应 400, got %d", code)
}
// 5) 校验:property 指向不存在的属性 → 400
code, _ = env.httpDo(t, "PUT",
"/api/v1/ontology/object-types/"+fixedType+"/classification",
map[string]any{
"classification_property": "this_property_does_not_exist",
"classification_levels": []string{"x"},
}, h)
if code != 400 {
t.Errorf("property 不存在应 400, got %d", code)
}
// 6) PUT 关闭该类型分级(property=空)
code, body = env.httpDo(t, "PUT",
"/api/v1/ontology/object-types/"+fixedType+"/classification",
map[string]any{
"classification_property": "",
"classification_levels": []string{},
"classification_default": "",
}, h)
if code != 200 {
t.Fatalf("关闭分级应 200, got %d", code)
}
if body["enabled"] != false {
t.Errorf("property=空 时 enabled 应 false, got %v", body["enabled"])
}
// 7) GET config 立刻反映关闭
_, getConfig2 := env.httpDo(t, "GET", "/api/v1/ontology/classification/config", nil, h)
var found2 map[string]any
for _, it := range getConfig2["items"].([]any) {
m := it.(map[string]any)
if m["api_name"] == fixedType {
found2 = m
}
}
if found2["enabled"] != false {
t.Errorf("关闭后 GET 应 enabled=false, got %+v", found2)
}
}
// TestIntegration_SetClassificationThroughMCP 验证 MCP 工具 set_object_type_classification
// 是否正确代理到刚补的 REST PUT。需要 manager 主体。
func TestIntegration_SetClassificationThroughMCP(t *testing.T) {
env, a := setupTestEnv(t)
h := authHeaderFor(t, a, "manager")
const fixedType = "DataDemand"
t.Cleanup(func() {
env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+fixedType+"/classification",
map[string]any{
"classification_property": "",
"classification_levels": []string{},
"classification_default": "",
}, h)
})
// 用 MCP 调用 set_object_type_classification
code, body := env.httpDo(t, "POST", "/api/v1/mcp",
map[string]any{
"jsonrpc": "2.0", "id": "1", "method": "tools/call",
"params": map[string]any{
"name": "set_object_type_classification",
"arguments": map[string]any{
"api_name": fixedType,
"classification_property": "approval_status",
"classification_levels": []string{"1", "3-replaced"},
"classification_default": "1",
},
},
}, h)
if code != 200 {
t.Fatalf("MCP 调用应 200, got %d body=%v", code, body)
}
// 响应 result.content[0].text 是 JSON 字符串
result, _ := body["result"].(map[string]any)
if result == nil {
t.Fatalf("MCP 响应缺 result, got %v", body)
}
content, _ := result["content"].([]any)
if len(content) == 0 {
t.Fatalf("MCP 响应缺 content, got %v", result)
}
first, _ := content[0].(map[string]any)
text, _ := first["text"].(string)
if !strings.Contains(text, `"enabled":true`) {
t.Errorf("MCP 响应应 enabled:true, got %s", text)
}
// MCP 缺 levels 应报错(参数错误,levels 不能为空)
code, body = env.httpDo(t, "POST", "/api/v1/mcp",
map[string]any{
"jsonrpc": "2.0", "id": "2", "method": "tools/call",
"params": map[string]any{
"name": "set_object_type_classification",
"arguments": map[string]any{
"api_name": fixedType,
},
},
}, h)
if code != 200 {
t.Fatalf("MCP 缺 levels 应 200 含错误信息, got %d", code)
}
r2, _ := body["result"].(map[string]any)
c2, _ := r2["content"].([]any)
if len(c2) == 0 {
t.Fatalf("MCP 响应缺 content")
}
t2, _ := c2[0].(map[string]any)["text"].(string)
if !strings.Contains(t2, "classification_levels") {
t.Errorf("MCP 缺 levels 应报错, got %s", t2)
}
_ = strconv.Itoa // 占位避免 unused
}