@furongjun1999/dsh-memory 0.9.7 → 0.9.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +150 -662
- package/codebuddy/CODEBUDDY.md +337 -204
- package/docs/README.md +21 -0
- package/docs/discipline/harnesses.yaml +11 -3
- package/docs/discipline/templates/compact.txt.tmpl +20 -18
- package/docs/discipline/templates/full.md.tmpl +63 -61
- package/docs/discipline/templates/rules.mdc.tmpl +5 -3
- package/docs/discipline/templates/skill.md.tmpl +25 -23
- package/docs/discipline/templates/spec.txt.tmpl +1 -1
- package/docs/discipline/templates/zcode-user.md.tmpl +1 -1
- package/docs//350/247/204/350/214/203/AI/347/274/226/347/250/213/351/200/232/347/224/250/350/247/204/350/214/203.md +4 -0
- package/dsh/cordis.yml.example +2 -2
- package/lib/hooks.d.ts +11 -1
- package/lib/hooks.js +59 -4
- package/lib/index.d.ts +1 -1
- package/lib/index.js +11 -5
- package/lib/init.js +28 -4
- package/lib/lib/hook_audit.d.ts +3 -0
- package/lib/lib/hook_audit.js +63 -0
- package/md_cg/autonomy.py +1 -1
- package/md_cg/config_registry.py +329 -329
- package/md_cg/config_registry_bulk.py +483 -482
- package/md_cg/config_render.py +27 -0
- package/md_cg/config_validate.py +456 -455
- package/md_cg/conflict_surface.py +127 -0
- package/md_cg/mcp_server.py +5324 -5227
- package/md_cg/mdcg.py +12 -1
- package/md_cg/mdcos.py +7159 -7108
- package/md_cg/pooling.py +487 -485
- package/md_cg/qmd_llm.py +334 -334
- package/md_cg/reach.py +10 -4
- package/md_cg/sleep.py +2 -2
- package/md_cg/sustain.py +13 -4
- package/md_cg/test_auto_defaults.py +463 -463
- package/md_cg/test_autonomy_emotion.py +328 -328
- package/md_cg/test_bm25_default.py +56 -0
- package/md_cg/test_c3_transient_read_negative.py +7 -3
- package/md_cg/test_conflict_surface.py +75 -0
- package/md_cg/test_conflict_value_untrusted.py +63 -0
- package/md_cg/test_exact_text_cache.py +194 -191
- package/md_cg/test_heartbeat_ledger.py +563 -557
- package/md_cg/test_hotcache_tool.py +121 -0
- package/md_cg/test_issue274_read_node_id_none.py +364 -341
- package/md_cg/test_issue561_pending_review.py +89 -5
- package/md_cg/test_issue578_service_info_gate.py +264 -0
- package/md_cg/test_n184_keys_concurrent_provision.py +1 -1
- package/md_cg/test_p3_legacy_closure.py +441 -438
- package/md_cg/test_p43_pooling.py +419 -412
- package/md_cg/test_reach.py +5 -1
- package/md_cg/test_reach_exits.py +54 -0
- package/md_cg/test_reach_single_char.py +74 -0
- package/md_cg/test_read_scope_b27.py +2 -2
- package/md_cg/test_search_bm25_default.py +60 -0
- package/md_cg/test_security_audit_v21.py +249 -249
- package/md_cg/test_sleep_p1.py +1032 -1032
- package/md_cg/test_sustain_bad_stamp.py +178 -0
- package/md_cg/test_untrusted_audit.py +55 -0
- package/md_cg/test_untrusted_classifier.py +42 -0
- package/md_cg/test_untrusted_enforcement.py +66 -0
- package/md_cg/untrusted.py +45 -0
- package/md_cg/untrusted_audit.py +87 -0
- package/md_cg/value_conflict.py +71 -0
- package/package.json +7 -7
- package/src/hooks.ts +986 -932
- package/src/index.ts +20 -14
- package/src/init.ts +21 -4
- package/src/lib/hook_audit.ts +334 -282
- package/zcode/AGENTS.md +337 -204
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
2
|
# 功能名:#561 第①项(#553 可见性)——读面对「有待复核新值」的节点标注 pending_review
|
|
3
3
|
# 生效条件:python -X utf8 -m md_cg.test_issue561_pending_review;被测面=mcp_server 的 cg(op=read) 单节点分支 + mdcos.pending_proposals
|
|
4
|
-
# 子功能:A
|
|
4
|
+
# 子功能:A/K 组=初始待审与反恒真;R 组=红队打回、重开实例与再次审批;T 组=reject/noop 终态;--mutate=两类错误改法恰好点红
|
|
5
5
|
# 执行:直接跑;全部 PASS 即通过(退出码 0)
|
|
6
|
-
# 验证方式:断言全过 + 退出码 0
|
|
6
|
+
# 验证方式:断言全过 + 退出码 0;--mutate 的两条定点变异须恰好命中各自红项
|
|
7
7
|
# 不适用条件:① 不判检索列表(search/recall)是否逐条标注(本项只钉单节点读面;列表面加标注要按结果数付扫描成本);
|
|
8
8
|
# ② 不判 DSH 宿主侧提示(#561 第②项);③ 不判治理面 review_list 的形状(既有守卫覆盖)。
|
|
9
9
|
"""issue #561 第①项守卫:读面的「有新值待复核」必须可见。
|
|
@@ -17,6 +17,10 @@ from __future__ import annotations
|
|
|
17
17
|
import os
|
|
18
18
|
import sys
|
|
19
19
|
import tempfile
|
|
20
|
+
import json
|
|
21
|
+
import subprocess
|
|
22
|
+
import inspect
|
|
23
|
+
import textwrap
|
|
20
24
|
|
|
21
25
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
22
26
|
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
@@ -53,9 +57,11 @@ def main():
|
|
|
53
57
|
from md_cg import crypto
|
|
54
58
|
os.environ[crypto.MASTER_ENV] = "ab" * 32
|
|
55
59
|
from md_cg import mcp_server as MS
|
|
56
|
-
from md_cg.mdcos import MdCGSecure
|
|
60
|
+
from md_cg.mdcos import MdCGSecure, MdCGOS
|
|
57
61
|
from md_cg.security import Principal
|
|
58
62
|
|
|
63
|
+
_apply_mutation(MdCGOS)
|
|
64
|
+
|
|
59
65
|
p = Principal(tenant="t", actor="designer", role="designer",
|
|
60
66
|
clearance="private", can_write=True, can_admin=True)
|
|
61
67
|
cg = MdCGSecure(os.environ["MDCG_ROOT"], principal=p)
|
|
@@ -88,7 +94,27 @@ def main():
|
|
|
88
94
|
ok(cg.pending_proposals("") == [] and cg.pending_proposals("NOPE") == [],
|
|
89
95
|
"K03 空/不存在 id ⇒ 空列表(不抛)",
|
|
90
96
|
(cg.pending_proposals(""), cg.pending_proposals("NOPE")))
|
|
91
|
-
|
|
97
|
+
print("== R 组:红队打回仍待再审批,读面不能提前关闭 ==")
|
|
98
|
+
rr = cg.review_decide(pid, "reject", redteam={
|
|
99
|
+
"verdict": "reject", "round": 1, "issues": ["合成用例:需再次审批"]})
|
|
100
|
+
ok(rr.get("state") == "needs_reapproval", "R00 红队打回进入非终态", rr)
|
|
101
|
+
queued = [r for r in cg.review_list() if r.get("pid") == pid]
|
|
102
|
+
ok(len(queued) == 1 and queued[0].get("status") == "needs_reapproval"
|
|
103
|
+
and pid not in cg._closed_pids(), "R01 治理面仍待审且未关闭", queued)
|
|
104
|
+
ra_pending = read("A")
|
|
105
|
+
ok(ra_pending.get("pending_review") is True
|
|
106
|
+
and pid in ra_pending.get("pending_proposals", []),
|
|
107
|
+
"R02 待再审批在读面仍可见", ra_pending)
|
|
108
|
+
ok("5433" in ra_pending.get("content", "")
|
|
109
|
+
and "5432" not in ra_pending.get("content", ""),
|
|
110
|
+
"R03 红队打回不写入提案新值", ra_pending.get("content"))
|
|
111
|
+
reopened = MdCGSecure(os.environ["MDCG_ROOT"], principal=p)
|
|
112
|
+
reread = MS.call_tool(reopened, "cg", {"op": "read", "node_id": "A"})
|
|
113
|
+
ok(reread.get("pending_review") is True
|
|
114
|
+
and pid in reread.get("pending_proposals", []),
|
|
115
|
+
"R04 重新打开实例仍可见", reread)
|
|
116
|
+
cg.review_decide(pid, "accept", reason="test",
|
|
117
|
+
redteam={"verdict": "pass", "round": 2})
|
|
92
118
|
ra2 = read("A")
|
|
93
119
|
ok(isinstance(ra2, dict) and "pending_review" not in ra2,
|
|
94
120
|
"K04 裁决后键随之消失(不是一次性标记)",
|
|
@@ -97,13 +123,71 @@ def main():
|
|
|
97
123
|
"K05 裁决 accept 后新值已并入(旧值仍在,聚合可读)",
|
|
98
124
|
(ra2 or {}).get("content"))
|
|
99
125
|
|
|
126
|
+
print("== T 组:真正终态不得留下待复核标记 ==")
|
|
127
|
+
for nid, action in (("C", "reject"), ("D", "noop")):
|
|
128
|
+
cg.add(nid, "终态对照旧值:" + nid, layer="contextual")
|
|
129
|
+
pv2 = cg.propose(nid, "终态对照候选:" + nid, layer="contextual", info=True)
|
|
130
|
+
pid2 = pv2.get("pid") if isinstance(pv2, dict) else pv2
|
|
131
|
+
before2 = read(nid)
|
|
132
|
+
ok(before2.get("pending_review") is True,
|
|
133
|
+
"T00 " + action + " 对照前置确实待审", before2)
|
|
134
|
+
result2 = cg.review_decide(pid2, action, reason="合成终态对照")
|
|
135
|
+
after2 = read(nid)
|
|
136
|
+
ok(result2.get("ok") is True and "pending_review" not in after2
|
|
137
|
+
and "pending_proposals" not in after2,
|
|
138
|
+
"T01 " + action + " 终态不留标记", after2)
|
|
139
|
+
|
|
100
140
|
print("")
|
|
101
141
|
print("=" * 60)
|
|
102
142
|
print("#561① 守卫:%d 通过,%d 失败" % (PASS, FAIL))
|
|
103
143
|
for f in FAILS:
|
|
104
144
|
print(" 红项:" + f)
|
|
145
|
+
print("RESULT " + json.dumps({"pass": PASS, "fail": FAIL, "failures": FAILS}, ensure_ascii=False))
|
|
105
146
|
return 0 if FAIL == 0 else 1
|
|
106
147
|
|
|
107
148
|
|
|
149
|
+
def _apply_mutation(cls):
|
|
150
|
+
"""只在显式变异子进程内改函数副本,不改磁盘源码。"""
|
|
151
|
+
flag = next((x.split("=", 1)[1] for x in sys.argv
|
|
152
|
+
if x.startswith("--mutation=")), None)
|
|
153
|
+
if flag is None:
|
|
154
|
+
return
|
|
155
|
+
fn = cls.pending_proposals
|
|
156
|
+
source = textwrap.dedent(inspect.getsource(fn))
|
|
157
|
+
needle = 'if (not pid or (decided.get(pid) or {}).get("status")\n in TERMINAL_DECISION_STATUS):'
|
|
158
|
+
replacements = {"old-decided": "if not pid or pid in decided:",
|
|
159
|
+
"keep-terminal": "if not pid:"}
|
|
160
|
+
if flag not in replacements or source.count(needle) != 1:
|
|
161
|
+
raise RuntimeError("变异锚点缺失或未知变异:" + str(flag))
|
|
162
|
+
namespace = dict(fn.__globals__)
|
|
163
|
+
exec(compile(source.replace(needle, replacements[flag]),
|
|
164
|
+
"<pending-proposals-mutation>", "exec"), namespace)
|
|
165
|
+
cls.pending_proposals = namespace["pending_proposals"]
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _run_mutations():
|
|
169
|
+
expected = {
|
|
170
|
+
"old-decided": {"R02 待再审批在读面仍可见", "R04 重新打开实例仍可见"},
|
|
171
|
+
"keep-terminal": {"K04 裁决后键随之消失(不是一次性标记)",
|
|
172
|
+
"T01 reject 终态不留标记", "T01 noop 终态不留标记"},
|
|
173
|
+
}
|
|
174
|
+
for name, red in expected.items():
|
|
175
|
+
proc = subprocess.run([sys.executable, "-X", "utf8", "-m",
|
|
176
|
+
"md_cg.test_issue561_pending_review", "--mutation=" + name],
|
|
177
|
+
cwd=_REPO, capture_output=True, text=True, encoding="utf-8", timeout=30)
|
|
178
|
+
lines = [s[len("RESULT "):] for s in proc.stdout.splitlines()
|
|
179
|
+
if s.startswith("RESULT ")]
|
|
180
|
+
result = json.loads(lines[-1]) if lines else {}
|
|
181
|
+
actual = set(result.get("failures", []))
|
|
182
|
+
if proc.returncode != 1 or actual != red:
|
|
183
|
+
print(proc.stdout)
|
|
184
|
+
print(proc.stderr)
|
|
185
|
+
print("变异自证失败:", name, "expected=", sorted(red), "actual=", sorted(actual))
|
|
186
|
+
return 1
|
|
187
|
+
print("PASS mutation", name, "恰好点红", sorted(actual))
|
|
188
|
+
print("定点变异:2 通过,0 失败")
|
|
189
|
+
return 0
|
|
190
|
+
|
|
191
|
+
|
|
108
192
|
if __name__ == "__main__":
|
|
109
|
-
sys.exit(main())
|
|
193
|
+
sys.exit(_run_mutations() if "--mutate" in sys.argv else main())
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# 功能名:mdcg_service_info 必须与 mdcg_whoami 同闸,且工具面闸表对 TOOLS 完备(issue #578 / dsh-bounty#435)
|
|
3
|
+
# 生效条件:python -X utf8 -m md_cg.test_issue578_service_info_gate [--mutate];被测面=md_cg/mcp_server.py 的 _MDCG_TOOL_OP / _MDCG_OP_REQUIRE 与 _dispatch 对该入口的处置
|
|
4
|
+
# 子功能:G01-G03 静态完备性(对 TOOLS 求双向差集 + 同载荷同档);G04-G06 行为同判(4 个 principal 逐格对照 whoami,含反向对照);G07 载荷收敛**观察项**(不判红,设计取舍未定);M 组=--mutate 定点变异(删掉那一行 ⇒ 必须转红)
|
|
5
|
+
# 执行:--mutate 时在镜像树里删掉 "mdcg_service_info": "read", 一行(回缺陷),守卫必须按指定项转红
|
|
6
|
+
# 验证方式:全部 PASS 即通过(退出码 0);--mutate 下红项集合须与预期逐项相符
|
|
7
|
+
# 不适用条件:① 不判 mdcg_whitebox 的工具面口径(#398 KNOWN_WEAK 已登记、另案);② 不判 MDCG_MCP_SURFACE 的面成员(tools/call 不受 surface 约束,另案);③ 不判载荷该不该带 token_id/session(G07 只打印观察值,不改判据);④ 不判 service_info 的触发面是否应更窄(full surface 才有 tools/list 条目,属产品取舍)。
|
|
8
|
+
"""issue #578 守卫:mdcg_service_info 不得绕过工具面闸表。
|
|
9
|
+
|
|
10
|
+
病象(修前):该入口**不在** _MDCG_TOOL_OP(31 个工具里唯一缺项)⇒ _dispatch 取
|
|
11
|
+
_MDCG_OP_REQUIRE.get(name) 得 None ⇒ 跳过 require_op;而它回吐的 principal.as_dict()
|
|
12
|
+
与**受 read 闸管着**的 mdcg_whoami 是同一份身份清单(tenant/actor/role/clearance/
|
|
13
|
+
can_write/can_admin/session/token_id/layers_allow/ops_allow)+库根绝对路径 ⇒ 只读访客
|
|
14
|
+
与「被 --ops-allow 收窄到不含 read」的令牌都能拿到它。
|
|
15
|
+
|
|
16
|
+
同时钉住上一轮修法没覆盖到的盲点:#398 的守卫
|
|
17
|
+
test_issue398_toolface_op_parity.py 的 E02 断言是
|
|
18
|
+
len(_MDCG_TOOL_OP) == len(_MDCG_OP_REQUIRE) >= 30 —— 两边同源派生,**自比自恒真**,
|
|
19
|
+
「映射缺一项」在结构上不可见。本守卫改为**对 TOOLS 求双向差集**,缺项当场点名。
|
|
20
|
+
"""
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import os
|
|
24
|
+
import shutil
|
|
25
|
+
import subprocess
|
|
26
|
+
import sys
|
|
27
|
+
import tempfile
|
|
28
|
+
|
|
29
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
30
|
+
|
|
31
|
+
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
32
|
+
if _REPO not in sys.path:
|
|
33
|
+
sys.path.insert(0, _REPO)
|
|
34
|
+
|
|
35
|
+
FAILS = []
|
|
36
|
+
NOTES = []
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _check(name, cond, detail=""):
|
|
40
|
+
if cond:
|
|
41
|
+
print(" PASS " + name)
|
|
42
|
+
else:
|
|
43
|
+
print(" FAIL " + name + " " + str(detail)[:220])
|
|
44
|
+
FAILS.append(name)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _note(name, detail):
|
|
48
|
+
print(" NOTE " + name + " " + str(detail)[:200])
|
|
49
|
+
NOTES.append(name)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
_TMP = tempfile.mkdtemp(prefix="iss578_")
|
|
53
|
+
os.environ.update({"MDCG_ROOT": _TMP + "/brain", "MDCG_STATE_ROOT": _TMP + "/state",
|
|
54
|
+
"MDCG_AUX_ROOT": _TMP + "/auxstore",
|
|
55
|
+
"MDCG_DATA_ROOT": _TMP + "/datastore",
|
|
56
|
+
"MDCG_EXPORT_ROOT": _TMP, "MDCG_SUSTAIN": "0", "MDCG_SLEEP": "0",
|
|
57
|
+
"MDCG_TOKEN_FILE": _TMP + "/auxstore/tokens.json"})
|
|
58
|
+
|
|
59
|
+
from md_cg import crypto # noqa: E402
|
|
60
|
+
os.environ[crypto.MASTER_ENV] = "ab" * 32
|
|
61
|
+
from md_cg import mcp_server as _ms # noqa: E402
|
|
62
|
+
from md_cg.mdcos import MdCGSecure # noqa: E402
|
|
63
|
+
from md_cg.security import Principal # noqa: E402
|
|
64
|
+
|
|
65
|
+
_KEK = b"k" * 32
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _err(fn):
|
|
69
|
+
"""调 fn:正常返回 ("", 结果);抛异常(含 BaseException)返回 (类型名, 文本)。"""
|
|
70
|
+
try:
|
|
71
|
+
return "", fn()
|
|
72
|
+
except BaseException as exc: # noqa: BLE001 —— 变异态不得崩栈
|
|
73
|
+
return type(exc).__name__, str(exc)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
#: 四个 principal:闸的**两侧**都要覆盖(有 read / 无 read / 空 ops / 全域)。
|
|
77
|
+
#: C 是 B 的更极端形态(连 info 都没有)——只看 B 会漏掉 ops_allow 为空这一支。
|
|
78
|
+
_MATRIX = (
|
|
79
|
+
("A 无令牌等价 guest(含 read)",
|
|
80
|
+
dict(role="guest", clearance="internal", can_write=False,
|
|
81
|
+
ops_allow=["info", "route", "read", "recent", "whitebox", "status", "edges"],
|
|
82
|
+
layers_allow=[]),
|
|
83
|
+
"pass"), # guest 本就持 read ⇒ 同闸后仍应放行
|
|
84
|
+
("B 窄化令牌(有 write 无 read)",
|
|
85
|
+
dict(role="designer", clearance="internal", can_write=True,
|
|
86
|
+
ops_allow=["write", "info"], layers_allow=["knowledge"]),
|
|
87
|
+
"deny"), # 修前放行、修后 AccessDenied
|
|
88
|
+
("C 空 ops_allow",
|
|
89
|
+
dict(role="designer", clearance="internal", can_write=True,
|
|
90
|
+
ops_allow=[], layers_allow=["knowledge"]),
|
|
91
|
+
"deny"),
|
|
92
|
+
("D 全域 designer(ops_allow=None)",
|
|
93
|
+
dict(role="designer", clearance="internal", can_write=True,
|
|
94
|
+
ops_allow=None, layers_allow=None),
|
|
95
|
+
"pass"),
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _cg(tag, kw):
|
|
100
|
+
d = os.path.join(_TMP, tag)
|
|
101
|
+
os.makedirs(d, exist_ok=True)
|
|
102
|
+
return MdCGSecure(d + "/root",
|
|
103
|
+
principal=Principal(tenant="default", actor="iss578", **kw),
|
|
104
|
+
master_key=_KEK, autoflush=1)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
print("== G 组 站点 1:闸表对 TOOLS 的**完备性**(静态,双向差集) ==")
|
|
108
|
+
|
|
109
|
+
_tools = [t["name"] for t in _ms.TOOLS]
|
|
110
|
+
_tool_set = set(_tools)
|
|
111
|
+
_toolop = set(_ms._MDCG_TOOL_OP)
|
|
112
|
+
_opreq = set(_ms._MDCG_OP_REQUIRE)
|
|
113
|
+
_missing = sorted(_tool_set - _toolop)
|
|
114
|
+
_extra = sorted(_toolop - _tool_set)
|
|
115
|
+
|
|
116
|
+
_check("G01 ★:TOOLS(%d) 与闸表(%d) 双向差集为空——漏登的工具当场点名(修前漏登 = ['mdcg_service_info'])"
|
|
117
|
+
% (len(_tool_set), len(_toolop)),
|
|
118
|
+
not _missing and not _extra,
|
|
119
|
+
{"漏登": _missing, "多余": _extra})
|
|
120
|
+
_check("G02 派生表与工具表同源同形(_MDCG_OP_REQUIRE 由 _MDCG_TOOL_OP 派生,不得多键/少键)",
|
|
121
|
+
_opreq == _toolop, sorted(_opreq ^ _toolop))
|
|
122
|
+
_check("G03 ★:mdcg_service_info 在闸表内,且与 mdcg_whoami **同档**"
|
|
123
|
+
"(同一份 principal 载荷,闸词不得一有一无)",
|
|
124
|
+
_ms._MDCG_TOOL_OP.get("mdcg_service_info") is not None
|
|
125
|
+
and _ms._MDCG_TOOL_OP.get("mdcg_service_info") == _ms._MDCG_TOOL_OP.get("mdcg_whoami"),
|
|
126
|
+
(_ms._MDCG_TOOL_OP.get("mdcg_service_info"), _ms._MDCG_TOOL_OP.get("mdcg_whoami")))
|
|
127
|
+
|
|
128
|
+
print("== G 组 站点 2:同载荷同闸(行为,逐格对照 whoami) ==")
|
|
129
|
+
|
|
130
|
+
_rows = []
|
|
131
|
+
for _tag, _kw, _expect in _MATRIX:
|
|
132
|
+
_c = _cg(_tag.split()[0], _kw)
|
|
133
|
+
_ts, _ = _err(lambda c=_c: _ms._dispatch(c, "mdcg_service_info", {}))
|
|
134
|
+
_tw, _ = _err(lambda c=_c: _ms._dispatch(c, "mdcg_whoami", {}))
|
|
135
|
+
_rows.append((_tag, _ts or "200", _tw or "200", _expect))
|
|
136
|
+
|
|
137
|
+
_check("G04 ★:service_info 与 whoami 对同一 principal **同判**"
|
|
138
|
+
"(修前 B/C 两格分叉:service_info=200 / whoami=AccessDenied)",
|
|
139
|
+
all(si == wh for _, si, wh, _ in _rows),
|
|
140
|
+
[(t, si, wh) for t, si, wh, _ in _rows if si != wh])
|
|
141
|
+
_check("G05 ★:无 read 的两格对 service_info 是 AccessDenied"
|
|
142
|
+
"(修前 200:身份清单 + token_id/session/ops_allow + 库根绝对路径全量回吐)",
|
|
143
|
+
[si for _, si, _, ex in _rows if ex == "deny"] == ["AccessDenied", "AccessDenied"]
|
|
144
|
+
and [si for _, si, _, ex in _rows if ex == "pass"] == ["200", "200"],
|
|
145
|
+
_rows)
|
|
146
|
+
_check("G06 反向对照:whoami 的读数在任何修法下都不得变化(同闸 ≠ 把 whoami 一起关掉)",
|
|
147
|
+
[wh for _, _, wh, _ in _rows] == ["200", "AccessDenied", "AccessDenied", "200"],
|
|
148
|
+
[(t, wh) for t, _, wh, _ in _rows])
|
|
149
|
+
|
|
150
|
+
print("== G 组 站点 3:载荷收敛观察项(不判红) ==")
|
|
151
|
+
|
|
152
|
+
_guest_cg = _cg("payload", _MATRIX[0][1])
|
|
153
|
+
_, _payload = _err(lambda: _ms._dispatch(_guest_cg, "mdcg_service_info", {}))
|
|
154
|
+
_pr = (_payload or {}).get("principal") or {}
|
|
155
|
+
_exposed = [k for k in ("token_id", "session", "ops_allow", "layers_allow", "harness")
|
|
156
|
+
if k in _pr]
|
|
157
|
+
if _exposed:
|
|
158
|
+
_note("G07 观察:guest 的 service_info 仍带 %s(与 whoami 同载荷)——"
|
|
159
|
+
"「要不要只回版本/计数」属设计取舍,本守卫不判红,交维护者裁定" % _exposed,
|
|
160
|
+
_exposed)
|
|
161
|
+
else:
|
|
162
|
+
_note("G07 观察:guest 的 service_info 已不带头部身份句柄(只回版本/计数)", "clean")
|
|
163
|
+
|
|
164
|
+
# ---------------------------------------------------------------- 定点变异
|
|
165
|
+
_ANCHOR = ' "mdcg_service_info": "read",'
|
|
166
|
+
_EXPECT_RED = ("G01", "G03", "G04", "G05")
|
|
167
|
+
_EOL = chr(13) + chr(10)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _mirror_build():
|
|
171
|
+
"""镜像树:md_cg/ + 仓根 .py(utf8_boot.py 等引导依赖)+ README/pyproject/package.json。
|
|
172
|
+
|
|
173
|
+
只拷 md_cg/ 会在 import 期炸 `No module named 'utf8_boot'`(mcp_server 的引导依赖,
|
|
174
|
+
第 66 行),与 #398 守卫 _mirror_build 的口径保持一致。
|
|
175
|
+
"""
|
|
176
|
+
dst = tempfile.mkdtemp(prefix="iss578_mut_")
|
|
177
|
+
shutil.copytree(os.path.join(_REPO, "md_cg"), os.path.join(dst, "md_cg"),
|
|
178
|
+
ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
|
|
179
|
+
for name in sorted(os.listdir(_REPO)):
|
|
180
|
+
p = os.path.join(_REPO, name)
|
|
181
|
+
if os.path.isfile(p) and (name.endswith(".py")
|
|
182
|
+
or name in ("README.md", "pyproject.toml",
|
|
183
|
+
"package.json")):
|
|
184
|
+
shutil.copy2(p, os.path.join(dst, name))
|
|
185
|
+
return dst
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _run_in(tree):
|
|
189
|
+
"""在镜像树里跑本守卫(禁变异递归),返回 (rc, 红项代号集合, 输出)。"""
|
|
190
|
+
env = dict(os.environ)
|
|
191
|
+
env["PYTHONPATH"] = tree
|
|
192
|
+
env["PYTHONUTF8"] = "1"
|
|
193
|
+
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
194
|
+
env["MDCG_ISSUE578_NO_MUTATE"] = "1"
|
|
195
|
+
for k in ("MDCG_ROOT", "MDCG_STATE_ROOT", "MDCG_AUX_ROOT",
|
|
196
|
+
"MDCG_DATA_ROOT", "MDCG_EXPORT_ROOT", "MDCG_TOKEN_FILE"):
|
|
197
|
+
env.pop(k, None)
|
|
198
|
+
pr = subprocess.run([sys.executable, "-X", "utf8", "-m",
|
|
199
|
+
"md_cg.test_issue578_service_info_gate"],
|
|
200
|
+
capture_output=True, text=True, encoding="utf-8",
|
|
201
|
+
errors="replace", cwd=tree, env=env, timeout=600)
|
|
202
|
+
out = (pr.stdout or "") + (pr.stderr or "")
|
|
203
|
+
reds = sorted({ln.split("FAIL ", 1)[1].split(" ")[0].strip()
|
|
204
|
+
for ln in out.splitlines() if ln.strip().startswith("FAIL ")})
|
|
205
|
+
return pr.returncode, reds, out
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _mutate():
|
|
209
|
+
print("!! 定点变异自证:删掉闸表那一行(回缺陷),守卫必须按指定项转红")
|
|
210
|
+
tree = _mirror_build()
|
|
211
|
+
bad = []
|
|
212
|
+
try:
|
|
213
|
+
rc, reds, out = _run_in(tree)
|
|
214
|
+
print(" 未变异镜像基线:rc=%d 红项=%s(必须 rc=0 且 0 红)" % (rc, reds))
|
|
215
|
+
if rc != 0 or reds:
|
|
216
|
+
bad.append("未变异镜像基线即红")
|
|
217
|
+
print(chr(10).join(out.splitlines()[-20:]))
|
|
218
|
+
p = os.path.join(tree, "md_cg", "mcp_server.py")
|
|
219
|
+
with open(p, encoding="utf-8", newline="") as f:
|
|
220
|
+
bak = f.read()
|
|
221
|
+
eol = chr(13) + chr(10) if (chr(13) + chr(10)) in bak else chr(10)
|
|
222
|
+
anchor = (_ANCHOR + chr(10)).replace(chr(10), eol)
|
|
223
|
+
if bak.count(anchor) != 1:
|
|
224
|
+
bad.append("变异锚点命中 %d 次(要求 1)" % bak.count(anchor))
|
|
225
|
+
print(" ANCHOR-MISS 删行变异:锚点命中 %d 次" % bak.count(anchor))
|
|
226
|
+
else:
|
|
227
|
+
with open(p, "w", encoding="utf-8", newline="") as f:
|
|
228
|
+
f.write(bak.replace(anchor, "", 1))
|
|
229
|
+
for root, _dd, _ff in os.walk(tree):
|
|
230
|
+
if os.path.basename(root) == "__pycache__":
|
|
231
|
+
shutil.rmtree(root, ignore_errors=True)
|
|
232
|
+
rc2, reds2, out2 = _run_in(tree)
|
|
233
|
+
with open(p, "w", encoding="utf-8", newline="") as f:
|
|
234
|
+
f.write(bak)
|
|
235
|
+
if rc2 != 0 and reds2 == sorted(_EXPECT_RED):
|
|
236
|
+
print(" OK 删行变异:红项=%s" % reds2)
|
|
237
|
+
else:
|
|
238
|
+
bad.append("删行变异红项不符")
|
|
239
|
+
print(" MISMATCH 删行变异:实际=%s 期望=%s rc=%d"
|
|
240
|
+
% (reds2, sorted(_EXPECT_RED), rc2))
|
|
241
|
+
for ln in out2.splitlines():
|
|
242
|
+
if ln.strip().startswith("FAIL "):
|
|
243
|
+
print(" " + ln[:170])
|
|
244
|
+
print("定点变异自证:%s(%d 条不合格)" % ("PASS" if not bad else "FAIL", len(bad)))
|
|
245
|
+
return len(bad)
|
|
246
|
+
finally:
|
|
247
|
+
shutil.rmtree(tree, ignore_errors=True)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def main():
|
|
251
|
+
_bad = 0
|
|
252
|
+
if "--mutate" in sys.argv and not os.environ.get("MDCG_ISSUE578_NO_MUTATE"):
|
|
253
|
+
_bad = _mutate()
|
|
254
|
+
shutil.rmtree(_TMP, ignore_errors=True)
|
|
255
|
+
print("")
|
|
256
|
+
if FAILS:
|
|
257
|
+
print("issue #578 结果:%d 失败(%s)" % (len(FAILS), ", ".join(FAILS)))
|
|
258
|
+
else:
|
|
259
|
+
print("issue #578 结果:全部通过(观察项 %d 条未判红)" % len(NOTES))
|
|
260
|
+
return 1 if (FAILS or _bad) else 0
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
if __name__ == "__main__":
|
|
264
|
+
sys.exit(main())
|
|
@@ -10,7 +10,7 @@ A 按 t0 陈旧快照写回——B 的信封无痕抹除:旧密文永久不可
|
|
|
10
10
|
告警无备份无审计。N139(load_error 拒重签)只堵「损坏回落→静默重签」
|
|
11
11
|
形态,并发写回形态无闸。触发面无需攻击者:hive/exec.py:845 每次工具
|
|
12
12
|
调用新建 MdCGSecure → _init_crypto → provision_dek(mdcos.py:3694-3696),
|
|
13
|
-
worker 首次
|
|
13
|
+
worker 首次 cg 与设计者会话首条 private 写并发即中招。
|
|
14
14
|
|
|
15
15
|
红守卫(哑 KEK=os.urandom(32) + 系统临时目录,零真实状态;对齐桩只在
|
|
16
16
|
写点对齐调度、读与签发全走真实代码路径,等价 OS 调度竞态窗口):
|