一次收口 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]>
281 lines
10 KiB
Go
281 lines
10 KiB
Go
package main
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// 这些测试守护 Dynamic Security 读路径的**语义**(不触库):
|
||
// 「不施加过滤」与「过滤后为空」必须在类型层面区分开,
|
||
// 否则一次 len(x)==0 的疏忽就会让「什么都看不到」的主体「看到全部」。
|
||
|
||
// ---------- Scope 三态 ----------
|
||
|
||
func TestScopeNoneVsEmpty(t *testing.T) {
|
||
// 最关键的一条:空集合 ≠ 不施加过滤。
|
||
empty := allowScope(nil)
|
||
if empty.IsUnscoped() {
|
||
t.Fatal("空集合不得被当成 unscoped:None 与 [] 混用正是访问控制漏洞的成因")
|
||
}
|
||
if empty.IsDenyAll() {
|
||
t.Fatal("空集合是 ScopeAllow 而不是 ScopeDenyAll:两者 SQL 落法不同,不能混为一谈")
|
||
}
|
||
if empty.Allows("L0") {
|
||
t.Error("空集合不应放行任何取值")
|
||
}
|
||
|
||
// 不施加过滤:放行一切
|
||
un := unscopedScope()
|
||
if !un.IsUnscoped() || un.IsDenyAll() {
|
||
t.Error("unscoped 语义不对")
|
||
}
|
||
if !un.Allows("L0") || !un.Allows("任意值") {
|
||
t.Error("unscoped 应放行一切")
|
||
}
|
||
|
||
// 拒绝全部:什么都不放行
|
||
d := denyAllScope()
|
||
if d.IsUnscoped() {
|
||
t.Error("deny_all 不应是 unscoped")
|
||
}
|
||
if d.Allows("L0") {
|
||
t.Error("deny_all 不应放行任何取值")
|
||
}
|
||
}
|
||
|
||
func TestAllowScopeCopiesAndSorts(t *testing.T) {
|
||
src := []string{"L3", "L1"}
|
||
sc := allowScope(src)
|
||
src[0] = "L9" // 调用方事后改切片,不应影响已构造的范围
|
||
if sc.Allows("L9") {
|
||
t.Error("allowScope 应拷贝入参,否则调用方改动会导致语义漂移")
|
||
}
|
||
if sc.Values[0] != "L1" || sc.Values[1] != "L3" {
|
||
t.Errorf("allowScope 应排序,保证 SQL 参数确定性:%v", sc.Values)
|
||
}
|
||
}
|
||
|
||
// ---------- 标记维度 ----------
|
||
|
||
func TestMarkingScope(t *testing.T) {
|
||
// manager:不受限
|
||
if sc := markingScope(&Authz{Principal: "manager"}); !sc.IsUnscoped() {
|
||
t.Errorf("manager 应为 unscoped,实际 %s", sc)
|
||
}
|
||
// nil authz:按 manager 处理
|
||
if sc := markingScope(nil); !sc.IsUnscoped() {
|
||
t.Errorf("nil authz 应为 unscoped,实际 %s", sc)
|
||
}
|
||
// 有 clearance:可见集合 = clearance
|
||
sc := markingScope(&Authz{Principal: "analyst", Clearances: map[string]bool{"L0": true, "L2": true}})
|
||
if sc.IsUnscoped() || sc.IsDenyAll() {
|
||
t.Fatalf("有 clearance 的主体不应是 unscoped/deny_all:%s", sc)
|
||
}
|
||
if !sc.Allows("L2") || sc.Allows("L3") {
|
||
t.Errorf("可见集合应恰为 clearance:%s", sc)
|
||
}
|
||
// 一条 clearance 都没有 → 什么都看不到(**不是**「没有过滤条件所以全放行」)
|
||
sc = markingScope(&Authz{Principal: "nobody", Clearances: map[string]bool{}})
|
||
if !sc.IsDenyAll() {
|
||
t.Errorf("无 clearance 的主体应为 deny_all,实际 %s", sc)
|
||
}
|
||
if sc.IsUnscoped() {
|
||
t.Error("无 clearance 绝不能是 unscoped")
|
||
}
|
||
}
|
||
|
||
func TestMarkingCond(t *testing.T) {
|
||
// unscoped → TRUE
|
||
cond, args := markingCond(unscopedScope(), "o", "type_api_name", "pk", nil)
|
||
if cond != "TRUE" || len(args) != 0 {
|
||
t.Errorf("unscoped → TRUE,got %q args=%v", cond, args)
|
||
}
|
||
// deny_all → FALSE
|
||
cond, args = markingCond(denyAllScope(), "o", "type_api_name", "pk", nil)
|
||
if cond != "FALSE" || len(args) != 0 {
|
||
t.Errorf("deny_all → FALSE,got %q args=%v", cond, args)
|
||
}
|
||
// allow → NOT EXISTS 条件 + 一个 text[] 参数
|
||
cond, args = markingCond(allowScope([]string{"L0", "L1"}), "o", "type_api_name", "pk", nil)
|
||
if !strings.Contains(cond, "NOT EXISTS") || !strings.Contains(cond, "object_markings") {
|
||
t.Errorf("allow 应生成 NOT EXISTS 条件:%q", cond)
|
||
}
|
||
if !strings.Contains(cond, "$1") {
|
||
t.Errorf("占位符应为 $1:%q", cond)
|
||
}
|
||
if len(args) != 1 {
|
||
t.Fatalf("应有 1 个参数,got %d", len(args))
|
||
}
|
||
vals, ok := args[0].([]string)
|
||
if !ok || len(vals) != 2 {
|
||
t.Fatalf("参数应是可见标记集合,got %#v", args[0])
|
||
}
|
||
// 已有参数时占位符要顺延
|
||
cond, args = markingCond(allowScope([]string{"L0"}), "l", "source_type", "source_pk", []interface{}{"x"})
|
||
if !strings.Contains(cond, "$2") {
|
||
t.Errorf("占位符应顺延为 $2:%q", cond)
|
||
}
|
||
if len(args) != 2 {
|
||
t.Fatalf("参数应追加为 2 个,got %d", len(args))
|
||
}
|
||
}
|
||
|
||
func TestScopeAllowsMarkings(t *testing.T) {
|
||
sc := allowScope([]string{"L0", "L1"})
|
||
if !sc.AllowsMarkings(nil) {
|
||
t.Error("无标记 = 公开,应可见")
|
||
}
|
||
if !sc.AllowsMarkings([]string{"L0", "L1"}) {
|
||
t.Error("标记全在可见集合内,应可见")
|
||
}
|
||
if sc.AllowsMarkings([]string{"L0", "L3"}) {
|
||
t.Error("有一个标记越级,应不可见")
|
||
}
|
||
// deny_all 连无标记对象都不放行(主体没有任何 clearance)
|
||
if denyAllScope().AllowsMarkings(nil) {
|
||
t.Error("deny_all 下无标记对象也不应可见")
|
||
}
|
||
// 空标记字符串按「无标记」处理
|
||
if !sc.AllowsMarkings([]string{""}) {
|
||
t.Error("空标记应按公开处理")
|
||
}
|
||
}
|
||
|
||
// ---------- 分级维度 ----------
|
||
|
||
func TestLevelScope(t *testing.T) {
|
||
cfg := classConfig{Property: "lvl", Levels: []string{"L0", "L1", "L2"}, Default: "L1"}
|
||
|
||
// 未启用分级 → 不施加过滤
|
||
if sc := levelScope(&Authz{Principal: "analyst", Clearances: map[string]bool{"L0": true}}, classConfig{}); !sc.IsUnscoped() {
|
||
t.Errorf("未启用分级应 unscoped,实际 %s", sc)
|
||
}
|
||
// manager → 不施加过滤
|
||
if sc := levelScope(&Authz{Principal: "manager"}, cfg); !sc.IsUnscoped() {
|
||
t.Errorf("manager 应 unscoped,实际 %s", sc)
|
||
}
|
||
// 完全无交集 → 拒绝全部(fail-closed)
|
||
sc := levelScope(&Authz{Principal: "x", Clearances: map[string]bool{"ZZ": true}}, cfg)
|
||
if !sc.IsDenyAll() {
|
||
t.Errorf("无交集应 deny_all,实际 %s", sc)
|
||
}
|
||
// 覆盖全部级别 → 不施加过滤
|
||
sc = levelScope(&Authz{Principal: "x", Clearances: map[string]bool{"L0": true, "L1": true, "L2": true}}, cfg)
|
||
if !sc.IsUnscoped() {
|
||
t.Errorf("覆盖全部级别应 unscoped,实际 %s", sc)
|
||
}
|
||
// 部分覆盖 → 可见 = 覆盖到的级别
|
||
sc = levelScope(&Authz{Principal: "x", Clearances: map[string]bool{"L0": true, "L1": true}}, cfg)
|
||
if sc.IsUnscoped() || sc.IsDenyAll() {
|
||
t.Fatalf("部分覆盖应是 allow:%s", sc)
|
||
}
|
||
if !sc.Allows("L1") || sc.Allows("L2") {
|
||
t.Errorf("可见集合应恰为 L0,L1:%s", sc)
|
||
}
|
||
}
|
||
|
||
func TestDeniedLevels(t *testing.T) {
|
||
all := []string{"L0", "L1", "L2"}
|
||
if got := deniedLevels(unscopedScope(), all); got != nil {
|
||
t.Errorf("unscoped 不应屏蔽任何级别,got %v", got)
|
||
}
|
||
// deny_all:全部级别 + 未定义级别
|
||
got := deniedLevels(denyAllScope(), all)
|
||
if len(got) != 4 {
|
||
t.Errorf("deny_all 应屏蔽全部级别 + 未定义级,got %v", got)
|
||
}
|
||
foundEmpty := false
|
||
for _, g := range got {
|
||
if g == "" {
|
||
foundEmpty = true
|
||
}
|
||
}
|
||
if !foundEmpty {
|
||
t.Errorf("deny_all 应连「未定义级别」一起屏蔽,got %v", got)
|
||
}
|
||
// allow:补集
|
||
got = deniedLevels(allowScope([]string{"L0", "L1"}), all)
|
||
if len(got) != 1 || got[0] != "L2" {
|
||
t.Errorf("allow 的补集应只剩 L2,got %v", got)
|
||
}
|
||
}
|
||
|
||
func TestEffectiveLevel(t *testing.T) {
|
||
cfg := classConfig{Property: "lvl", Levels: []string{"L0", "L1"}, Default: "L1"}
|
||
if got := effectiveLevel("L0", cfg); got != "L0" {
|
||
t.Errorf("实例自身分级优先,got %q", got)
|
||
}
|
||
// 分级为空 → 继承类型默认级(**不是**「无限制」)
|
||
if got := effectiveLevel("", cfg); got != "L1" {
|
||
t.Errorf("空分级应继承类型默认级 L1,got %q", got)
|
||
}
|
||
// 类型也没设默认级 → 仍未定义
|
||
if got := effectiveLevel("", classConfig{Property: "lvl"}); got != "" {
|
||
t.Errorf("无默认级时仍应为空,got %q", got)
|
||
}
|
||
}
|
||
|
||
// ---------- 分级 SQL ----------
|
||
|
||
func TestClassCondSQL(t *testing.T) {
|
||
cfgs := map[string]classConfig{
|
||
"T1": {Property: "lvl", Levels: []string{"L0", "L1", "L2"}, Default: "L1"},
|
||
"T2": {Property: "lvl", Levels: []string{"L0", "L1", "L2"}, Default: "L0"},
|
||
}
|
||
|
||
// manager → 不施加过滤
|
||
cond, args := classCondSQL("o", cfgs, &Authz{Principal: "manager"}, nil)
|
||
if cond != "TRUE" || len(args) != 0 {
|
||
t.Errorf("manager 应为 TRUE,got %q args=%v", cond, args)
|
||
}
|
||
// 未启用任何分级 → 不施加过滤
|
||
cond, _ = classCondSQL("o", map[string]classConfig{}, &Authz{Principal: "x"}, nil)
|
||
if cond != "TRUE" {
|
||
t.Errorf("无分级配置应为 TRUE,got %q", cond)
|
||
}
|
||
|
||
// 主体只能看 L0 → 两个类型都要过滤
|
||
authz := &Authz{Principal: "x", Clearances: map[string]bool{"L0": true}}
|
||
cond, args = classCondSQL("o", cfgs, authz, nil)
|
||
if cond == "TRUE" {
|
||
t.Fatal("有分级配置时不应返回 TRUE")
|
||
}
|
||
// 生效级别必须回落到类型默认级
|
||
if !strings.Contains(cond, "COALESCE") || !strings.Contains(cond, "classification") {
|
||
t.Errorf("条件应含实例分级与默认级的 COALESCE:%q", cond)
|
||
}
|
||
// objects.classification 存空串而不是 NULL → 必须 NULLIF,否则空串不会回落到默认级
|
||
if !strings.Contains(cond, "NULLIF") {
|
||
t.Errorf("条件应把空串视作未分级(NULLIF),否则存量空分级不会继承类型默认级:%q", cond)
|
||
}
|
||
if !strings.Contains(cond, "ANY") {
|
||
t.Errorf("条件应含屏蔽表匹配:%q", cond)
|
||
}
|
||
// 参数顺序:先默认级 CASE(类型+默认级),再屏蔽表 CASE(类型+级别数组)
|
||
if len(args) != 8 {
|
||
t.Fatalf("2 类型 × (默认级 2 参数 + 屏蔽表 2 参数) = 8,got %d:%v", len(args), args)
|
||
}
|
||
if args[0] != "T1" || args[1] != "L1" || args[2] != "T2" || args[3] != "L0" {
|
||
t.Errorf("默认级参数顺序不对:%v", args)
|
||
}
|
||
if args[4] != "T1" || args[6] != "T2" {
|
||
t.Errorf("屏蔽表参数顺序不对:%v", args)
|
||
}
|
||
deny1, ok := args[5].([]string)
|
||
if !ok || len(deny1) != 2 || deny1[0] != "L1" || deny1[1] != "L2" {
|
||
t.Errorf("T1 对 L0-only 主体的屏蔽表应为 [L1 L2],got %#v", args[5])
|
||
}
|
||
// 占位符数量必须与参数个数一致(否则 SQL 直接报错)
|
||
if strings.Count(cond, "$") != len(args) {
|
||
t.Errorf("占位符数 %d ≠ 参数数 %d:%q", strings.Count(cond, "$"), len(args), cond)
|
||
}
|
||
|
||
// 与该类型级别无交集 → deny_all:屏蔽表含全部级别 + 未定义级
|
||
authz2 := &Authz{Principal: "x", Clearances: map[string]bool{"ZZ": true}}
|
||
_, args = classCondSQL("o", cfgs, authz2, nil)
|
||
deny2, ok := args[5].([]string)
|
||
if !ok || len(deny2) != 4 {
|
||
t.Errorf("无交集时屏蔽表应为 全部级别 + 未定义级,got %#v", args[5])
|
||
}
|
||
}
|