一次收口 2026-11-06 的九项能力(CHANGELOG 1.5.4 → 1.8.0): - 自检探针(readiness.go):check_readiness / check_answerable 两个只读探针,把 「本体里没有」「我没权限看」「模型没抽全」三种空结果原因拆开 - 读路径语义钉死(scope.go):标记与分级的可见范围统一由 Scope 产出,实例分级 为空时继承类型默认级(fail-closed),列表与详情口径一致 - 行级可改性轴(object_owner.go):归属只收窄不宽于 grants,所有对象写路径统一 走 canWriteObject,归属查询失败即拒绝 - 公理第 6 类 certification:认证位保护人工策展内容,AI 重摄取不再覆盖已认证条目 - 跨版本结构 diff(schema_diff.go):schema_snapshots 全量快照 + diff 端点,机器 事实与人工策展分组呈现 - 分区维度 zone:分区绑类型、命中任一即可见,读路径一律带 zone 条件 - 统一分页信封(pagination.go):列表回包统一 count/total/limit/offset/has_more/ truncated,total 与 items 同源 - MCP 长任务进度(mcp_progress.go):客户端带 progressToken 才走 SSE 进度帧 - 本体 schema 导入(ontology_import.go):Turtle 导入 + 四道 fail-fast 闸门 + 整事务 + (source, external_id) 幂等 + IRI 往返不变式 MCP 工具清单并入单一注册表(mcp_registry.go),对外声明与实际可调不再各说各话; 工具数 42 → 54(只读 27 / 写入 27),注册表与分发 case 的双向漂移由 AST 单测拦截。 CHANGELOG 编号整理:新「认证位 + 行级归属」条目由 1.6.0 改为 1.5.6(1.6.x 留给 分区与结构 diff),2026-09-20 的 GB/T 规划稿改标「规划稿·未发布」——该能力已并入 v1.5.1,避免与正式发布的版本号重复。 Co-Authored-By: Claude Code <[email protected]>
416 lines
15 KiB
Go
416 lines
15 KiB
Go
//go:build integration
|
||
|
||
package main
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
// zoneProbeName 本次测试的实例名(唯一 token):任何读路径的回包里出现它 = 越权泄漏。
|
||
func zoneProbeName() string {
|
||
return fmt.Sprintf("ZONEPROBE_%d", time.Now().UnixNano())
|
||
}
|
||
|
||
// newZone 造一个分区(直接写库,避开 API 的存在性校验),返回分区名。
|
||
func newZone(t *testing.T, a *app, name string, types, principals []string) {
|
||
t.Helper()
|
||
ctx := context.Background()
|
||
if _, err := a.pool.Exec(ctx,
|
||
`INSERT INTO zones(zone_name,description) VALUES($1,'zone test')
|
||
ON CONFLICT(zone_name) DO NOTHING`, name); err != nil {
|
||
t.Fatalf("建分区失败: %v", err)
|
||
}
|
||
for _, ty := range types {
|
||
if _, err := a.pool.Exec(ctx,
|
||
`INSERT INTO object_zones(object_type,zone_name) VALUES($1,$2) ON CONFLICT DO NOTHING`,
|
||
ty, name); err != nil {
|
||
t.Fatalf("写 object_zones 失败: %v", err)
|
||
}
|
||
}
|
||
for _, p := range principals {
|
||
if _, err := a.pool.Exec(ctx,
|
||
`INSERT INTO principal_zones(principal,zone_name) VALUES($1,$2) ON CONFLICT DO NOTHING`,
|
||
p, name); err != nil {
|
||
t.Fatalf("写 principal_zones 失败: %v", err)
|
||
}
|
||
}
|
||
a.InvalidateAuthzCache()
|
||
t.Cleanup(func() {
|
||
c := context.Background()
|
||
_, _ = a.pool.Exec(c, `DELETE FROM object_zones WHERE zone_name=$1`, name)
|
||
_, _ = a.pool.Exec(c, `DELETE FROM principal_zones WHERE zone_name=$1`, name)
|
||
_, _ = a.pool.Exec(c, `DELETE FROM zones WHERE zone_name=$1`, name)
|
||
a.InvalidateAuthzCache()
|
||
})
|
||
}
|
||
|
||
// zoneProbeType 造一个探针对象类型(只建类型,不建实例)。
|
||
func zoneProbeType(t *testing.T, env *testEnv, a *app, typ string) {
|
||
t.Helper()
|
||
mh := authHeaderFor(t, a, "manager")
|
||
if code, body := env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{
|
||
"api_name": typ, "display_name": "分区探针类型",
|
||
}, mh); code != 200 && code != 201 {
|
||
t.Fatalf("建类型失败: %d %v", code, body)
|
||
}
|
||
t.Cleanup(func() {
|
||
c := context.Background()
|
||
_, _ = a.pool.Exec(c, `DELETE FROM objects WHERE type_api_name=$1`, typ)
|
||
_, _ = a.pool.Exec(c, `DELETE FROM object_types WHERE api_name=$1`, typ)
|
||
})
|
||
}
|
||
|
||
// zoneProbeInstance 造一个探针实例,返回 (pk, 名称)。
|
||
func zoneProbeInstance(t *testing.T, env *testEnv, a *app, typ, name, region string) (string, string) {
|
||
t.Helper()
|
||
zoneProbeType(t, env, a, typ)
|
||
mh := authHeaderFor(t, a, "manager")
|
||
pk := "probe-" + fmt.Sprint(time.Now().UnixNano())
|
||
if code, body := env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": typ, "pk": pk,
|
||
"properties": map[string]any{"name": name, "region": region},
|
||
}, mh); code != 200 && code != 201 {
|
||
t.Fatalf("建探针实例失败: %d %v", code, body)
|
||
}
|
||
return pk, name
|
||
}
|
||
|
||
// TestIntegration_ZoneBoundary 分区语义端到端(缺一不可):
|
||
//
|
||
// 1. 类型入区后,无分区的主体看不到它的实例(列表 + 详情都看不到);
|
||
// 2. 主体拿到分区后立刻可见;
|
||
// 3. **没入任何分区的类型不受影响**(不配置分区 = 零影响,向后兼容);
|
||
// 4. 类型同时属于两个分区时,命中任一即可见。
|
||
func TestIntegration_ZoneBoundary(t *testing.T) {
|
||
env, a := setupTestEnv(t)
|
||
seedConnectorForTest(t)
|
||
ctx := context.Background()
|
||
|
||
probe := zoneProbeName()
|
||
pk, _ := zoneProbeInstance(t, env, a, env.typeKey, probe, probe+"_R")
|
||
pubType := env.typeKey + "_pub"
|
||
pubProbe := zoneProbeName()
|
||
pubPK, _ := zoneProbeInstance(t, env, a, pubType, pubProbe, pubProbe+"_R")
|
||
|
||
// 主体:只有 L0 clearance + read 授权,**不带任何分区**
|
||
pKey := newScopedPrincipal(t, a, "zone_probe", []string{"L0"})
|
||
h := map[string]string{"X-API-Key": pKey}
|
||
|
||
zone := "TestRgo_zone_" + fmt.Sprint(time.Now().UnixNano())
|
||
|
||
// ---- 3. 未配置分区:一切照旧(公共区类型可见)----
|
||
listVisible := func() bool {
|
||
t.Helper()
|
||
code, body := env.httpDo(t, "GET", "/api/v1/connector/objects/"+env.typeKey, nil, h)
|
||
if code != 200 {
|
||
t.Fatalf("列实例失败: %d %v", code, body)
|
||
}
|
||
items, _ := body["items"].([]any)
|
||
for _, it := range items {
|
||
m, _ := it.(map[string]any)
|
||
if p, _ := m["pk"].(string); p == pk {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
if !listVisible() {
|
||
t.Error("未配置分区时,主体应能看到实例(零影响)")
|
||
}
|
||
|
||
// ---- 1. 类型入区、主体无分区 → 看不到 ----
|
||
newZone(t, a, zone, []string{env.typeKey}, nil)
|
||
if listVisible() {
|
||
t.Error("类型入区后,无分区的主体不应看到它的实例")
|
||
}
|
||
code, _ := env.httpDo(t, "GET", "/api/v1/connector/object/"+env.typeKey+"/"+pk, nil, h)
|
||
if code != 404 {
|
||
t.Errorf("越区实例的详情应 404(不泄漏存在性),got %d", code)
|
||
}
|
||
// 公共区类型不受影响
|
||
code, body := env.httpDo(t, "GET", "/api/v1/connector/objects/"+pubType, nil, h)
|
||
if code != 200 {
|
||
t.Fatalf("列公共区实例失败: %d %v", code, body)
|
||
}
|
||
found := false
|
||
items, _ := body["items"].([]any)
|
||
for _, it := range items {
|
||
m, _ := it.(map[string]any)
|
||
if p, _ := m["pk"].(string); p == pubPK {
|
||
found = true
|
||
}
|
||
}
|
||
if !found {
|
||
t.Error("未入分区的类型不应受分区影响")
|
||
}
|
||
|
||
// ---- 2. 主体拿到分区 → 可见 ----
|
||
if _, err := a.pool.Exec(ctx,
|
||
`INSERT INTO principal_zones(principal,zone_name) VALUES($1,$2) ON CONFLICT DO NOTHING`,
|
||
"zone_probe", zone); err != nil {
|
||
t.Fatalf("授予分区失败: %v", err)
|
||
}
|
||
a.InvalidateAuthzCache()
|
||
if !listVisible() {
|
||
t.Error("主体拿到分区后应能看到实例")
|
||
}
|
||
code, _ = env.httpDo(t, "GET", "/api/v1/connector/object/"+env.typeKey+"/"+pk, nil, h)
|
||
if code != 200 {
|
||
t.Errorf("持区主体的详情应 200,got %d", code)
|
||
}
|
||
|
||
// ---- 4. 类型同属两个分区:命中任一即可见 ----
|
||
zone2 := zone + "_b"
|
||
newZone(t, a, zone2, []string{env.typeKey}, nil)
|
||
// 主体同时持有两个分区
|
||
if _, err := a.pool.Exec(ctx,
|
||
`INSERT INTO principal_zones(principal,zone_name) VALUES($1,$2) ON CONFLICT DO NOTHING`,
|
||
"zone_probe", zone2); err != nil {
|
||
t.Fatalf("授予第二个分区失败: %v", err)
|
||
}
|
||
a.InvalidateAuthzCache()
|
||
if !listVisible() {
|
||
t.Error("主体持有两个分区时应看到实例")
|
||
}
|
||
// 主体被剥夺其中一个分区(只剩另一个):仍可见(命中任一即可)
|
||
if _, err := a.pool.Exec(ctx, `DELETE FROM principal_zones WHERE principal=$1 AND zone_name=$2`,
|
||
"zone_probe", zone); err != nil {
|
||
t.Fatalf("撤销分区失败: %v", err)
|
||
}
|
||
a.InvalidateAuthzCache()
|
||
if !listVisible() {
|
||
t.Error("主体改持另一个分区后,类型同属两个分区时应仍可见")
|
||
}
|
||
// 两个分区都不持有 → 不可见
|
||
if _, err := a.pool.Exec(ctx, `DELETE FROM principal_zones WHERE principal=$1`,
|
||
"zone_probe"); err != nil {
|
||
t.Fatalf("撤销全部分区失败: %v", err)
|
||
}
|
||
a.InvalidateAuthzCache()
|
||
if listVisible() {
|
||
t.Error("主体不持有任何分区时不应看到实例")
|
||
}
|
||
|
||
// ---- 类型移出所有分区 → 回到公共区 ----
|
||
if _, err := a.pool.Exec(ctx, `DELETE FROM object_zones WHERE object_type=$1`, env.typeKey); err != nil {
|
||
t.Fatalf("移出分区失败: %v", err)
|
||
}
|
||
a.InvalidateAuthzCache()
|
||
if !listVisible() {
|
||
t.Error("类型移出所有分区后应回到公共区(全体可见)")
|
||
}
|
||
}
|
||
|
||
// TestIntegration_ZoneEveryReadPathCarriesScope CI 断言:
|
||
//
|
||
// **每个读路径都必须带分区 scope**——照 benchmark §3.4 的要求
|
||
// (NVIDIA 的 zone 被全局关闭,正是因为没有这条断言守着)。
|
||
//
|
||
// 实现方式是「行为断言」而不是「代码扫描」:造一个只在受限分区里的探针实例,
|
||
// 逐条请求所有读接口,回包里出现探针 token 即判定为越权泄漏。
|
||
func TestIntegration_ZoneEveryReadPathCarriesScope(t *testing.T) {
|
||
env, a := setupTestEnv(t)
|
||
seedConnectorForTest(t)
|
||
|
||
probe := zoneProbeName()
|
||
region := probe + "_R"
|
||
pk, _ := zoneProbeInstance(t, env, a, env.typeKey, probe, region)
|
||
|
||
pKey := newScopedPrincipal(t, a, "zone_scope_probe", []string{"L0"})
|
||
h := map[string]string{"X-API-Key": pKey}
|
||
|
||
zone := "TestRgo_scope_" + fmt.Sprint(time.Now().UnixNano())
|
||
newZone(t, a, zone, []string{env.typeKey}, nil)
|
||
|
||
// 读路径清单:每条都是「会返回实例内容」的入口。
|
||
type readPath struct {
|
||
name string
|
||
do func() string
|
||
}
|
||
paths := []readPath{
|
||
{"connector/objects", func() string {
|
||
code, body := env.httpDo(t, "GET", "/api/v1/connector/objects/"+env.typeKey, nil, h)
|
||
return fmt.Sprintf("%d %v", code, body)
|
||
}},
|
||
{"connector/object", func() string {
|
||
code, body := env.httpDo(t, "GET", "/api/v1/connector/object/"+env.typeKey+"/"+pk, nil, h)
|
||
return fmt.Sprintf("%d %v", code, body)
|
||
}},
|
||
{"connector/search", func() string {
|
||
code, body := env.httpDo(t, "GET", "/api/v1/connector/search?q="+probe, nil, h)
|
||
return fmt.Sprintf("%d %v", code, body)
|
||
}},
|
||
{"connector/query", func() string {
|
||
code, body := env.httpDo(t, "POST", "/api/v1/connector/query",
|
||
map[string]any{"type": env.typeKey}, h)
|
||
return fmt.Sprintf("%d %v", code, body)
|
||
}},
|
||
{"connector/graph", func() string {
|
||
code, body := env.httpDo(t, "GET", "/api/v1/connector/graph", nil, h)
|
||
return fmt.Sprintf("%d %v", code, body)
|
||
}},
|
||
{"connector/facets", func() string {
|
||
code, body := env.httpDo(t, "GET", "/api/v1/connector/facets?type="+env.typeKey, nil, h)
|
||
return fmt.Sprintf("%d %v", code, body)
|
||
}},
|
||
{"mcp/get_objects_by_type", func() string {
|
||
text, _, rpcErr := mcpCallRaw(t, env, h, "get_objects_by_type", map[string]any{"type": env.typeKey})
|
||
return fmt.Sprintf("%s %s", rpcErr, text)
|
||
}},
|
||
{"mcp/get_object", func() string {
|
||
text, _, rpcErr := mcpCallRaw(t, env, h, "get_object", map[string]any{"type": env.typeKey, "pk": pk})
|
||
return fmt.Sprintf("%s %s", rpcErr, text)
|
||
}},
|
||
{"mcp/search_ontology", func() string {
|
||
text, _, rpcErr := mcpCallRaw(t, env, h, "search_ontology", map[string]any{"q": probe})
|
||
return fmt.Sprintf("%s %s", rpcErr, text)
|
||
}},
|
||
{"mcp/query_related", func() string {
|
||
text, _, rpcErr := mcpCallRaw(t, env, h, "query_related", map[string]any{"type": env.typeKey})
|
||
return fmt.Sprintf("%s %s", rpcErr, text)
|
||
}},
|
||
{"mcp/get_facets", func() string {
|
||
text, _, rpcErr := mcpCallRaw(t, env, h, "get_facets", map[string]any{"type": env.typeKey})
|
||
return fmt.Sprintf("%s %s", rpcErr, text)
|
||
}},
|
||
}
|
||
|
||
for _, p := range paths {
|
||
out := p.do()
|
||
// 断言用「实例主键」和「维度取值」两个 token:
|
||
// 实例名会被检索接口原样回显(query 参数),拿它当判据会误报。
|
||
if strings.Contains(out, pk) || strings.Contains(out, region) {
|
||
t.Errorf("读路径 %s 泄漏了分区外的实例内容(探针 token 出现在回包里)", p.name)
|
||
}
|
||
}
|
||
|
||
// 持区主体在同一条路径上应当看得到(证明上面的「看不到」不是因为别的条件挡的)
|
||
if _, err := a.pool.Exec(context.Background(),
|
||
`INSERT INTO principal_zones(principal,zone_name) VALUES($1,$2) ON CONFLICT DO NOTHING`,
|
||
"zone_scope_probe", zone); err != nil {
|
||
t.Fatalf("授予分区失败: %v", err)
|
||
}
|
||
a.InvalidateAuthzCache()
|
||
code, body := env.httpDo(t, "GET", "/api/v1/connector/objects/"+env.typeKey, nil, h)
|
||
if code != 200 {
|
||
t.Fatalf("列实例失败: %d %v", code, body)
|
||
}
|
||
items, _ := body["items"].([]any)
|
||
if len(items) == 0 {
|
||
t.Error("持区主体应能看到实例(否则上面的断言可能被别的原因挡掉)")
|
||
}
|
||
}
|
||
|
||
// TestIntegration_ZoneMcpTools MCP 侧的分区读写工具。
|
||
func TestIntegration_ZoneMcpTools(t *testing.T) {
|
||
env, a := setupTestEnv(t)
|
||
seedConnectorForTest(t)
|
||
mh := authHeaderFor(t, a, "manager")
|
||
zoneProbeType(t, env, a, env.typeKey)
|
||
|
||
zone := "TestRgo_mcp_" + fmt.Sprint(time.Now().UnixNano())
|
||
t.Cleanup(func() {
|
||
c := context.Background()
|
||
_, _ = a.pool.Exec(c, `DELETE FROM object_zones WHERE zone_name=$1`, zone)
|
||
_, _ = a.pool.Exec(c, `DELETE FROM principal_zones WHERE zone_name=$1`, zone)
|
||
_, _ = a.pool.Exec(c, `DELETE FROM zones WHERE zone_name=$1`, zone)
|
||
a.InvalidateAuthzCache()
|
||
})
|
||
|
||
// 建分区:类型入区,不给任何主体
|
||
text, isErr, rpcErr := mcpCallRaw(t, env, mh, "set_zone", map[string]any{
|
||
"name": zone, "description": "MCP 建的分区",
|
||
"types": []string{env.typeKey}, "principals": []string{},
|
||
})
|
||
if rpcErr != "" || isErr {
|
||
t.Fatalf("set_zone 失败: %s / %v / %s", text, isErr, rpcErr)
|
||
}
|
||
if !strings.Contains(text, zone) {
|
||
t.Errorf("set_zone 回包应含分区名:%s", text)
|
||
}
|
||
|
||
// list_zones 能看到它
|
||
text, isErr, rpcErr = mcpCallRaw(t, env, mh, "list_zones", nil)
|
||
if rpcErr != "" || isErr {
|
||
t.Fatalf("list_zones 失败: %s / %v / %s", text, isErr, rpcErr)
|
||
}
|
||
if !strings.Contains(text, zone) {
|
||
t.Errorf("list_zones 应列出该分区:%s", text)
|
||
}
|
||
|
||
// delete_zone 删掉它
|
||
text, isErr, rpcErr = mcpCallRaw(t, env, mh, "delete_zone", map[string]any{"name": zone})
|
||
if rpcErr != "" || isErr {
|
||
t.Fatalf("delete_zone 失败: %s / %v / %s", text, isErr, rpcErr)
|
||
}
|
||
text, _, _ = mcpCallRaw(t, env, mh, "list_zones", nil)
|
||
if strings.Contains(text, zone) {
|
||
t.Errorf("delete_zone 后 list_zones 不应再列出该分区:%s", text)
|
||
}
|
||
}
|
||
|
||
// TestIntegration_ZoneAPI 管理接口的校验与删除语义。
|
||
func TestIntegration_ZoneAPI(t *testing.T) {
|
||
env, a := setupTestEnv(t)
|
||
seedConnectorForTest(t)
|
||
mh := authHeaderFor(t, a, "manager")
|
||
zoneProbeType(t, env, a, env.typeKey)
|
||
zone := "TestRgo_api_" + fmt.Sprint(time.Now().UnixNano())
|
||
t.Cleanup(func() {
|
||
c := context.Background()
|
||
_, _ = a.pool.Exec(c, `DELETE FROM object_zones WHERE zone_name=$1`, zone)
|
||
_, _ = a.pool.Exec(c, `DELETE FROM principal_zones WHERE zone_name=$1`, zone)
|
||
_, _ = a.pool.Exec(c, `DELETE FROM zones WHERE zone_name=$1`, zone)
|
||
a.InvalidateAuthzCache()
|
||
})
|
||
|
||
// 引用不存在的类型 → 400(配错名字时静默失效是最难查的一类问题)
|
||
code, body := env.httpDo(t, "POST", "/api/v1/access/zones", map[string]any{
|
||
"name": zone, "types": []string{"NoSuchType_xxx"}, "principals": []string{},
|
||
}, mh)
|
||
if code != 400 {
|
||
t.Errorf("引用不存在的类型应 400,got %d %v", code, body)
|
||
}
|
||
|
||
// 引用不存在的主体 → 400
|
||
code, body = env.httpDo(t, "POST", "/api/v1/access/zones", map[string]any{
|
||
"name": zone, "types": []string{env.typeKey}, "principals": []string{"no_such_principal_xxx"},
|
||
}, mh)
|
||
if code != 400 {
|
||
t.Errorf("引用不存在的主体应 400,got %d %v", code, body)
|
||
}
|
||
|
||
// 正常创建 + 列表
|
||
code, body = env.httpDo(t, "POST", "/api/v1/access/zones", map[string]any{
|
||
"name": zone, "description": "分区说明",
|
||
"types": []string{env.typeKey}, "principals": []string{"connector"},
|
||
}, mh)
|
||
if code != 200 && code != 201 {
|
||
t.Fatalf("建分区失败: %d %v", code, body)
|
||
}
|
||
code, body = env.httpDo(t, "GET", "/api/v1/access/zones", nil, mh)
|
||
if code != 200 {
|
||
t.Fatalf("列分区失败: %d %v", code, body)
|
||
}
|
||
raw, _ := json.Marshal(body)
|
||
if !strings.Contains(string(raw), zone) {
|
||
t.Errorf("分区列表应含 %s:%s", zone, raw)
|
||
}
|
||
|
||
// 删除
|
||
code, body = env.httpDo(t, "DELETE", "/api/v1/access/zones/"+zone, nil, mh)
|
||
if code != 200 && code != 204 {
|
||
t.Fatalf("删分区失败: %d %v", code, body)
|
||
}
|
||
code, body = env.httpDo(t, "GET", "/api/v1/access/zones", nil, mh)
|
||
raw, _ = json.Marshal(body)
|
||
if strings.Contains(string(raw), zone) {
|
||
t.Errorf("删除后列表不应再含 %s:%s", zone, raw)
|
||
}
|
||
}
|