@furongjun1999/dsh-memory 0.9.6 → 0.9.7

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.
Files changed (216) hide show
  1. package/data/policy.json +38 -38
  2. package/docs/discipline/templates/spec.txt.tmpl +1 -1
  3. package/dsh/cordis.yml.example +139 -139
  4. package/lib/hooks.d.ts +19 -0
  5. package/lib/hooks.js +29 -6
  6. package/lib/index.js +11 -2
  7. package/lib/lib/hook_audit.js +38 -2
  8. package/lib/lib/prompt_safety.js +14 -3
  9. package/lib/tools.d.ts +7 -0
  10. package/lib/tools.js +22 -1
  11. package/md_cg/_guard_kit.py +121 -121
  12. package/md_cg/anchor.py +114 -114
  13. package/md_cg/audit.py +15 -2
  14. package/md_cg/chain.py +3 -2
  15. package/md_cg/config_registry_guard.py +487 -487
  16. package/md_cg/conformance.py +747 -747
  17. package/md_cg/consolidate.py +1793 -1793
  18. package/md_cg/crypto.py +794 -794
  19. package/md_cg/hyperedge.py +262 -262
  20. package/md_cg/interface_schema.py +146 -146
  21. package/md_cg/lifecycle.py +426 -426
  22. package/md_cg/mdcos.py +12 -8
  23. package/md_cg/predict.py +1103 -1103
  24. package/md_cg/protect.py +654 -654
  25. package/md_cg/provenance.py +606 -606
  26. package/md_cg/queue_health.py +130 -130
  27. package/md_cg/refine.py +613 -613
  28. package/md_cg/retrieve.py +128 -128
  29. package/md_cg/revise.py +140 -140
  30. package/md_cg/rollback.py +657 -657
  31. package/md_cg/rollback_cli.py +124 -124
  32. package/md_cg/rotate.py +346 -346
  33. package/md_cg/routing.py +490 -490
  34. package/md_cg/self_surface.py +50 -50
  35. package/md_cg/semantic/canonical.py +144 -144
  36. package/md_cg/semantic/zh_en_atoms.py +141 -141
  37. package/md_cg/sources.py +1091 -1091
  38. package/md_cg/stg.py +3 -2
  39. package/md_cg/test_audit_rotate_parity.py +279 -279
  40. package/md_cg/test_autonomy_modes.py +2013 -2013
  41. package/md_cg/test_cn_recall_grams.py +206 -206
  42. package/md_cg/test_code_kind_forbidden.py +101 -0
  43. package/md_cg/test_condaccept.py +156 -156
  44. package/md_cg/test_condgate.py +141 -141
  45. package/md_cg/test_heartbeat_ledger.py +557 -557
  46. package/md_cg/test_i50e_readside_protection.py +658 -658
  47. package/md_cg/test_issue111_gate_order.py +76 -76
  48. package/md_cg/test_issue111_scoring_contract.py +86 -86
  49. package/md_cg/test_issue121_calibration_bounds.py +293 -293
  50. package/md_cg/test_issue145_sleep_no_silent_rollback.py +167 -167
  51. package/md_cg/test_issue147_pack_id_escape.py +378 -378
  52. package/md_cg/test_issue148_merge_downgrade_leak.py +192 -192
  53. package/md_cg/test_issue149_accel_default_face.py +248 -248
  54. package/md_cg/test_issue150_empty_query_terms.py +328 -328
  55. package/md_cg/test_issue151_153_157_write_cache_index.py +151 -151
  56. package/md_cg/test_issue152_sealed_postings.py +309 -309
  57. package/md_cg/test_issue153b_index_generation_invalidation.py +142 -142
  58. package/md_cg/test_issue157b_index_remainder.py +344 -344
  59. package/md_cg/test_issue189_conclusion_bidirectional.py +223 -223
  60. package/md_cg/test_issue191_import_trust.py +382 -382
  61. package/md_cg/test_issue193_write_scope.py +378 -378
  62. package/md_cg/test_issue212_writeface_gaps.py +349 -349
  63. package/md_cg/test_issue214_reject_sensitivity.py +266 -266
  64. package/md_cg/test_issue215_condition_source_gap.py +272 -272
  65. package/md_cg/test_issue216_clash_routes.py +214 -214
  66. package/md_cg/test_issue217_slot_routes.py +248 -248
  67. package/md_cg/test_issue218_postcondition_value_blind.py +222 -222
  68. package/md_cg/test_issue258_pack_sensitivity.py +468 -468
  69. package/md_cg/test_issue265_coldverify_multiproc.py +438 -438
  70. package/md_cg/test_issue274_read_node_id_none.py +341 -341
  71. package/md_cg/test_issue275_layer_param.py +250 -250
  72. package/md_cg/test_issue277_node_id_traversal.py +384 -384
  73. package/md_cg/test_issue278_neg_separated_tension.py +271 -271
  74. package/md_cg/test_issue278_neg_window.py +294 -294
  75. package/md_cg/test_issue278_negative_layering.py +83 -83
  76. package/md_cg/test_issue284_skip_not_pass.py +291 -291
  77. package/md_cg/test_issue294_flywheel_sensitivity.py +573 -573
  78. package/md_cg/test_issue301_baseline_corpus_gate.py +245 -245
  79. package/md_cg/test_issue303_design_boundaries.py +262 -262
  80. package/md_cg/test_issue315_merge_branch_sensitivity.py +383 -383
  81. package/md_cg/test_issue319_tenant_unregistered_bypass.py +256 -256
  82. package/md_cg/test_issue321_mcp_deep_json.py +304 -304
  83. package/md_cg/test_issue322_node_write_lock.py +727 -727
  84. package/md_cg/test_issue324_write_size_fields.py +214 -214
  85. package/md_cg/test_issue325_audit_poison.py +229 -229
  86. package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -331
  87. package/md_cg/test_issue326_key_perms.py +355 -355
  88. package/md_cg/test_issue326_twin_parser_parity.py +323 -323
  89. package/md_cg/test_issue326_vm_call_frame.py +569 -569
  90. package/md_cg/test_issue345_writelimit_effective_sens.py +233 -233
  91. package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -424
  92. package/md_cg/test_issue348_insight_verify_op.py +199 -199
  93. package/md_cg/test_issue348_merge_rmw_lock.py +345 -345
  94. package/md_cg/test_issue350_casefold_collision.py +235 -235
  95. package/md_cg/test_issue351_ingest_root_fallback.py +201 -201
  96. package/md_cg/test_issue352_imgaudit.py +418 -418
  97. package/md_cg/test_issue352_pathsjson.py +537 -537
  98. package/md_cg/test_issue352_refindex.py +412 -412
  99. package/md_cg/test_issue352_rollback.py +604 -604
  100. package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -327
  101. package/md_cg/test_issue354_rust_index_containment.py +571 -571
  102. package/md_cg/test_issue354_rust_wal_containment.py +606 -606
  103. package/md_cg/test_issue358_truncation_observable.py +247 -247
  104. package/md_cg/test_issue360_tidy_private_excerpt.py +195 -195
  105. package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -494
  106. package/md_cg/test_issue362_neg_condition.py +468 -468
  107. package/md_cg/test_issue363_lexical_idf.py +275 -275
  108. package/md_cg/test_issue370_readcache_assembly.py +488 -488
  109. package/md_cg/test_issue376_heartbeat_failed.py +293 -293
  110. package/md_cg/test_issue377_layer_protect_stamp.py +324 -324
  111. package/md_cg/test_issue378_protected_floor.py +310 -310
  112. package/md_cg/test_issue383_state_event_write_gate.py +462 -462
  113. package/md_cg/test_issue397_sens_eff_wiring.py +435 -435
  114. package/md_cg/test_issue398_toolface_op_parity.py +511 -511
  115. package/md_cg/test_issue399_hive_json_shape.py +317 -317
  116. package/md_cg/test_issue401_wisdom_codegen.py +295 -295
  117. package/md_cg/test_issue404_heal_receipt.py +389 -389
  118. package/md_cg/test_issue409_compact_index_completeness.py +509 -509
  119. package/md_cg/test_issue410_verify_demote_write.py +366 -366
  120. package/md_cg/test_issue411_scrub_idempotence.py +575 -575
  121. package/md_cg/test_issue412_discipline_producer_parity.py +311 -311
  122. package/md_cg/test_issue413_preimage_failclosed.py +239 -239
  123. package/md_cg/test_issue413_preimage_twin.py +339 -339
  124. package/md_cg/test_issue415_deps_gate_probe.py +357 -357
  125. package/md_cg/test_issue416_entry_key_subset.py +525 -525
  126. package/md_cg/test_issue417_twophase_merge_landed.py +423 -423
  127. package/md_cg/test_issue425_ingest_library_path.py +331 -331
  128. package/md_cg/test_issue426c_enqueue_not_silent.py +346 -346
  129. package/md_cg/test_issue426e_restore_overwrite_live.py +310 -310
  130. package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -301
  131. package/md_cg/test_issue427_serve_status_shape.py +191 -191
  132. package/md_cg/test_issue431_432_468_protect_single_point.py +322 -322
  133. package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -278
  134. package/md_cg/test_issue434_435_float_gate.py +90 -90
  135. package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -448
  136. package/md_cg/test_issue43_default_policy.py +874 -874
  137. package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -281
  138. package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -454
  139. package/md_cg/test_issue446_453_403_verified_evidence.py +382 -382
  140. package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -51
  141. package/md_cg/test_issue450_writegate_unknown_sens.py +203 -203
  142. package/md_cg/test_issue451_audit_actor.py +313 -313
  143. package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -299
  144. package/md_cg/test_issue463_ghost_prune_guard.py +259 -259
  145. package/md_cg/test_issue466_basis_registry.py +469 -469
  146. package/md_cg/test_issue467_identity_sensitivity_parity.py +360 -360
  147. package/md_cg/test_issue469_scrub_override_passthrough.py +126 -126
  148. package/md_cg/test_issue470_wal_event_durable.py +427 -427
  149. package/md_cg/test_issue471_identity_readable.py +136 -136
  150. package/md_cg/test_issue472_audit_chain.py +90 -90
  151. package/md_cg/test_issue472_prune_ledger.py +105 -105
  152. package/md_cg/test_issue472_rollback_degraded.py +81 -81
  153. package/md_cg/test_issue491_compose_sandbox.py +460 -460
  154. package/md_cg/test_issue498_merge_trace_stale_index.py +280 -280
  155. package/md_cg/test_issue511_scoreboard_bad_files.py +256 -256
  156. package/md_cg/test_issue521_rotate_lock_suffix.py +110 -110
  157. package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -88
  158. package/md_cg/test_issue525_read_null_semantics.py +52 -52
  159. package/md_cg/test_issue547_append_jsonl_whitelist.py +114 -114
  160. package/md_cg/test_issue547_audit_durable.py +96 -96
  161. package/md_cg/test_issue557_r2_write_entries_size_gate.py +83 -83
  162. package/md_cg/test_issue559_refine_prefix.py +69 -69
  163. package/md_cg/test_issue562_retired_faces.py +150 -0
  164. package/md_cg/test_issue71_queue_backlog.py +376 -376
  165. package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -420
  166. package/md_cg/test_issue82_credential_rules.py +271 -271
  167. package/md_cg/test_issue91_verifier_sandbox.py +314 -314
  168. package/md_cg/test_issue93_remember_redaction.py +282 -282
  169. package/md_cg/test_logref.py +1141 -1141
  170. package/md_cg/test_md_access_parity.py +330 -330
  171. package/md_cg/test_md_cg_no_hive_dep.py +139 -139
  172. package/md_cg/test_mutation_residue.py +119 -119
  173. package/md_cg/test_mutation_rollback.py +835 -835
  174. package/md_cg/test_n123_derive_expiry_chain.py +210 -210
  175. package/md_cg/test_n209_verify_write_face_gates.py +466 -466
  176. package/md_cg/test_n62_tenant_bind_failclosed.py +200 -200
  177. package/md_cg/test_neg_tail_honesty.py +733 -733
  178. package/md_cg/test_p21_tokens.py +294 -294
  179. package/md_cg/test_p27_docindex.py +792 -792
  180. package/md_cg/test_p28_refcheck.py +1053 -1053
  181. package/md_cg/test_p2_six_elements.py +476 -476
  182. package/md_cg/test_p33_ccg_wiring.py +300 -300
  183. package/md_cg/test_p3_legacy_closure.py +438 -438
  184. package/md_cg/test_p40_refine_worklist.py +244 -244
  185. package/md_cg/test_p4_freshness.py +781 -781
  186. package/md_cg/test_p9c_dedup_hints.py +288 -288
  187. package/md_cg/test_policy_required_ccg.py +444 -444
  188. package/md_cg/test_retr_s5.py +312 -312
  189. package/md_cg/test_s1_ast_judge.py +109 -109
  190. package/md_cg/test_state_event_op.py +521 -521
  191. package/md_cg/test_stgidx_index_parity.py +1024 -1024
  192. package/md_cg/test_sustain_bounded.py +652 -652
  193. package/md_cg/test_trust_edge_reachability.py +405 -405
  194. package/md_cg/test_w7_redlines.py +659 -659
  195. package/md_cg/test_write_return_shape.py +919 -919
  196. package/md_cg/test_writepipe.py +228 -228
  197. package/md_cg/trust.py +1114 -1114
  198. package/md_cg/twophase.py +303 -303
  199. package/md_cg/whitebox.py +536 -536
  200. package/md_cg/whitebox_kb/wisdom/compose_engine.py +1634 -1634
  201. package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +318 -318
  202. package/md_cg/whitebox_kb/wisdom/core/parser.py +1082 -1082
  203. package/md_cg/whitebox_kb/wisdom/md_access.py +490 -490
  204. package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +497 -497
  205. package/md_cg/whitebox_kb/wisdom/mini_python.py +995 -995
  206. package/md_cg/whitebox_kb/wisdom/net_units.py +2900 -2900
  207. package/md_cg/whitebox_kb/wisdom/semantic_translate.py +4060 -4060
  208. package/md_cg/writepipe.py +1337 -1337
  209. package/package.json +3 -2
  210. package/scripts/workspace_index.py +328 -0
  211. package/src/hooks.ts +30 -6
  212. package/src/index.ts +14 -5
  213. package/src/lib/hook_audit.ts +282 -256
  214. package/src/lib/mdcg_client.ts +469 -469
  215. package/src/lib/prompt_safety.ts +17 -3
  216. package/src/tools.ts +23 -1
@@ -1,309 +1,309 @@
1
- # -*- coding: utf-8 -*-
2
- """#152① 守卫:密封(加密)库不得被 postings.build 写出明文 bigram。
3
-
4
- 缺陷形态(复现于 HEAD):
5
- md_cg/postings.py 的 build() 经 cg.get() 取**解密后**的 content,把明文 bigram
6
- 写进 <root>/_postings.json。节点正文已加密,而词项索引仍是明文 ⇒ 持该文件者
7
- 可读出内容主题(与全仓「原文明文不出库」立场冲突)。
8
-
9
- 修法(本件):
10
- build() 开头做**密封面探测**——索引中存在加密级节点(entry.sensitivity 属
11
- crypto.ENCRYPTED_LEVELS),或本实例挂密钥面且当前可解密(dek 为真)时,
12
- **不写任何文件**直接拒绝,返回 refused / sealed_nodes / reason 供调用方判因。
13
-
14
- 断言组:
15
- A 缺陷面(HEAD 版应 5 条全红):
16
- A1 密封库 build 后 _postings.json 不存在
17
- A2 返回 rebuilt is False 且 refused == "sealed_nodes"
18
- A3 产物全文无任何明文关键词 bigram
19
- A4 返回 sealed_nodes == 1(拒因可定位到节点数)
20
- A5 _postings_meta.json 同样未写出(拒绝=整体不落盘,不是写一半)
21
- B 不误伤:
22
- B1 非密封库(MdCG + internal 节点)照常 build(rebuilt=True 且文件写出)
23
- B2 密封库但**全 internal 且已 lock**(无加密节点、不可解密)⇒ 放行
24
- B3 抽样构建(limit)在密封库上同样被拒且不写文件
25
- C 判据未放宽:
26
- C1 _encrypted_levels() 与 crypto.ENCRYPTED_LEVELS 一致(小写集合)
27
- C2 internal 节点的 _sealed_node_ids 为空
28
- C3 拒绝路径不产出 partial 快照
29
- D 红基线自证:以 HEAD 版 postings 跑 A 组,应恰 5 条全红;HEAD 已含修复则 SKIP
30
-
31
- 用法:
32
- python -X utf8 -m md_cg.test_issue152_sealed_postings
33
- python -X utf8 -m md_cg.test_issue152_sealed_postings --impl head
34
- python -X utf8 -m md_cg.test_issue152_sealed_postings --mutate
35
- 退出码:0 = 全部按预期;1 = 有断言失败 / 变异未按预期转红;2 = 变异锚点失配。
36
- """
37
- import argparse
38
- import importlib.util
39
- import io
40
- import json
41
- import os
42
- import subprocess
43
- import sys
44
- import tempfile
45
-
46
- sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
47
-
48
- import md_cg # noqa: E402
49
- from md_cg import postings # noqa: E402
50
-
51
- REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
52
- IMPL_REL = os.path.join("md_cg", "postings.py")
53
- SECRET = "绝密项目代号"
54
-
55
- _N_A1 = "A1 密封库:build 后 _postings.json 不存在"
56
- _N_A2 = "A2 密封库:rebuilt is False 且 refused == sealed_nodes"
57
- _N_A3 = "A3 密封库:产物全文无明文关键词 bigram"
58
- _N_A4 = "A4 密封库:sealed_nodes == 1"
59
- _N_A5 = "A5 密封库:_postings_meta.json 同样未写出"
60
- _N_B1 = "B1 非密封库:照常 build(rebuilt=True 且文件写出)"
61
- _N_B2 = "B2 密封库+全 internal+locked:放行(不误伤)"
62
- _N_B3 = "B3 密封库+limit 抽样:同样被拒且不写文件"
63
- _N_C1 = "C1 加密级集合 == crypto.ENCRYPTED_LEVELS"
64
- _N_C2 = "C2 internal 节点的 _sealed_node_ids 为空"
65
- _N_C3 = "C3 拒绝路径不产出 partial 快照"
66
-
67
- _RESULTS = []
68
-
69
-
70
- def _check(name, ok, detail=""):
71
- _RESULTS.append((name, bool(ok), detail))
72
- print(" %s %s%s" % ("[PASS]" if ok else "[FAIL]", name,
73
- (" —— " + detail) if (not ok and detail) else ""))
74
-
75
-
76
- def _use(path):
77
- """把 path 物化为 md_cg.postings 并注入(用于红基线/变异体)。"""
78
- global postings
79
- spec = importlib.util.spec_from_file_location("md_cg.postings", path)
80
- mod = importlib.util.module_from_spec(spec)
81
- sys.modules["md_cg.postings"] = mod
82
- spec.loader.exec_module(mod)
83
- postings = mod
84
- return mod
85
-
86
-
87
- def _materialize_head(tmp):
88
- r = subprocess.run(["git", "-C", REPO, "show", "HEAD:" + IMPL_REL.replace(os.sep, "/")],
89
- capture_output=True)
90
- if r.returncode != 0 or not (r.stdout or b"").strip():
91
- return None, "git show 失败 rc=%s" % r.returncode
92
- p = os.path.join(tmp, "postings_head.py")
93
- with open(p, "wb") as f:
94
- f.write(r.stdout)
95
- return p, ""
96
-
97
-
98
- def _tmp_root(prefix):
99
- root = tempfile.mkdtemp(prefix=prefix)
100
- _rt, _tt = os.path.realpath(root), os.path.realpath(tempfile.gettempdir())
101
- assert _rt.startswith(_tt), "沙箱断言失败:%r 不在 tempdir 内" % _rt
102
- return root
103
-
104
-
105
- def _mk_sealed(root, body=None, sensitivity="secret", lock=False):
106
- from md_cg import mdcos
107
- from md_cg.security import Principal
108
- owner = Principal(tenant="t1", actor="alice", clearance="secret")
109
- cg = mdcos.MdCGSecure(root, principal=owner, master_key=os.urandom(32))
110
- cg.add("s1", body or (SECRET + " 行动方案"), sensitivity=sensitivity)
111
- cg.flush()
112
- if lock:
113
- cg.lock()
114
- return cg
115
-
116
-
117
- def _mk_open(root):
118
- from md_cg import mdcos
119
- cg = mdcos.MdCG(root)
120
- cg.add("n1", "阿尔法 贝塔 伽马", "knowledge")
121
- cg.flush()
122
- return cg
123
-
124
-
125
- def _leaked_terms(root):
126
- """产物里含 SECRET 任一字符的 bigram(文件不存在 ⇒ 空集,即无泄漏)。"""
127
- p = postings.postings_path(root)
128
- if not os.path.exists(p):
129
- return []
130
- try:
131
- with io.open(p, encoding="utf-8") as f:
132
- table = json.load(f)
133
- except Exception:
134
- return ["<unreadable>"]
135
- return sorted({g for g in table if any(c in g for c in SECRET)})
136
-
137
-
138
- def _case_sealed():
139
- root = _tmp_root("iss152_sealed_")
140
- cg = _mk_sealed(root)
141
- st = postings.build(cg)
142
- _check(_N_A1, not os.path.exists(postings.postings_path(root)),
143
- "exists=%s" % os.path.exists(postings.postings_path(root)))
144
- _check(_N_A2, st.get("rebuilt") is False and st.get("refused") == "sealed_nodes",
145
- "rebuilt=%r refused=%r" % (st.get("rebuilt"), st.get("refused")))
146
- leak = _leaked_terms(root)
147
- _check(_N_A3, not leak, "leak=%r" % (leak[:8],))
148
- _check(_N_A4, st.get("sealed_nodes") == 1, "sealed_nodes=%r" % (st.get("sealed_nodes"),))
149
- _check(_N_A5, not os.path.exists(postings.meta_path(root)),
150
- "meta_exists=%s" % os.path.exists(postings.meta_path(root)))
151
- cg.close()
152
-
153
-
154
- def _case_no_false_positive():
155
- root = _tmp_root("iss152_open_")
156
- cg = _mk_open(root)
157
- st = postings.build(cg)
158
- _check(_N_B1, st.get("rebuilt") is True and st.get("nodes", 0) > 0
159
- and os.path.exists(postings.postings_path(root)),
160
- "rebuilt=%r nodes=%r exists=%s" % (st.get("rebuilt"), st.get("nodes"),
161
- os.path.exists(postings.postings_path(root))))
162
- cg.close()
163
- root2 = _tmp_root("iss152_internal_")
164
- cg2 = _mk_sealed(root2, body="阿尔法 贝塔", sensitivity="internal", lock=True)
165
- st2 = postings.build(cg2)
166
- _check(_N_B2, st2.get("rebuilt") is True and st2.get("refused") is None,
167
- "rebuilt=%r refused=%r" % (st2.get("rebuilt"), st2.get("refused")))
168
- cg2.close()
169
- root3 = _tmp_root("iss152_partial_")
170
- cg3 = _mk_sealed(root3)
171
- st3 = postings.build(cg3, limit=1)
172
- _check(_N_B3, st3.get("rebuilt") is False
173
- and not os.path.exists(postings.postings_path(root3)),
174
- "rebuilt=%r exists=%s" % (st3.get("rebuilt"),
175
- os.path.exists(postings.postings_path(root3))))
176
- _check(_N_C3, "partial" not in st3, "keys=%r" % (sorted(st3),))
177
- cg3.close()
178
-
179
-
180
- def _case_criteria():
181
- from md_cg import crypto
182
- want = {str(x).strip().lower() for x in crypto.ENCRYPTED_LEVELS}
183
- _check(_N_C1, postings._encrypted_levels() == want,
184
- "got=%r want=%r" % (sorted(postings._encrypted_levels()), sorted(want)))
185
- _check(_N_C2, postings._sealed_node_ids({"n1": {"sensitivity": "internal"}}) == [],
186
- "got=%r" % (postings._sealed_node_ids({"n1": {"sensitivity": "internal"}}),))
187
-
188
-
189
- _HEAD_EXPECT_RED = [_N_A1, _N_A2, _N_A3, _N_A4, _N_A5]
190
-
191
-
192
- def _baseline_mode(tmp):
193
- """--impl head:以 HEAD 版 postings 跑 A 组,红基线必须 5 条全红。"""
194
- print("!! 红基线取证:以 HEAD 版 postings 跑 A 组——修前缺陷形态必须按点转红\n")
195
- head, why = _materialize_head(tmp)
196
- if head is None:
197
- _check("D 红基线自证:HEAD 版物化", False, why)
198
- return 1
199
- _use(head)
200
- _case_sealed()
201
- reds = {n for n, ok, _d in _RESULTS if not ok}
202
- greens = [n for n, ok, _d in _RESULTS if ok]
203
- if not reds:
204
- print("\nHEAD 版 A 组全绿——HEAD 已含修复,红基线只在本改动提交前可复现(SKIP)")
205
- return 0
206
- if reds == set(_HEAD_EXPECT_RED):
207
- print("\n红基线成立:HEAD 版恰 5 条点名转红(明文 bigram 落盘可复现)")
208
- return 0
209
- print("\n红基线不成立:多红 %r / 少红 %r"
210
- % (sorted(reds - set(_HEAD_EXPECT_RED)), sorted(set(_HEAD_EXPECT_RED) - reds)))
211
- return 1
212
-
213
-
214
- _MUTATIONS = [
215
- ("M1 删除密封面早退块(完全放行)",
216
- " sealed_ids = _sealed_node_ids(nodes)\n"
217
- " if sealed_ids:\n"
218
- " return {\"schema\": SCHEMA, \"nodes\": 0, \"terms\": 0, \"postings\": 0,\n"
219
- " \"rebuilt\": False, \"refused\": \"sealed_nodes\",\n"
220
- " \"sealed_nodes\": len(sealed_ids),\n"
221
- " \"reason\": \"密封面 fail-closed(#152①):密文节点的明文 bigram 不得落进 _postings.json\"}\n"
222
- " if _is_sealed_instance(cg) and getattr(cg, \"dek\", None):\n"
223
- " return {\"schema\": SCHEMA, \"nodes\": 0, \"terms\": 0, \"postings\": 0,\n"
224
- " \"rebuilt\": False, \"refused\": \"sealed_instance_unlocked\", \"sealed_nodes\": 0,\n"
225
- " \"reason\": \"密封面 fail-closed(#152①):实例持密钥可解密,明文 bigram 不得落进 _postings.json\"}\n",
226
- "",
227
- [_N_A1, _N_A2, _N_A3, _N_A4, _N_A5]),
228
- ("M2 节点面判据失效(if sealed_ids → if False)",
229
- " if sealed_ids:",
230
- " if False:",
231
- [_N_A2, _N_A4]),
232
- ("M3 探测函数恒空(_sealed_node_ids 恒返回 [])",
233
- " return [nid for nid, e in (nodes or {}).items()\n"
234
- " if str(((e or {}).get(\"sensitivity\")) or \"\").strip().lower() in lv]",
235
- " return []",
236
- [_N_A2, _N_A4]),
237
- ]
238
-
239
-
240
- def _read_norm(path):
241
- with open(path, encoding="utf-8", newline="") as f:
242
- return f.read().replace("\r\n", "\n")
243
-
244
-
245
- def _write_norm(path, text):
246
- with open(path, "w", encoding="utf-8", newline="\n") as f:
247
- f.write(text)
248
-
249
-
250
- def _mutate_mode(tmp):
251
- print("!! 定点变异自证:逐条把修复点改回退化形态,A 组必须按预期点名转红\n")
252
- src = _read_norm(os.path.join(REPO, IMPL_REL))
253
- anchor_bad = 0
254
- for idx, (label, old, new, expect) in enumerate(_MUTATIONS):
255
- hits = src.count(old)
256
- if hits != 1:
257
- print(" [锚点失配] %s:锚点命中 %d 次(要求恰 1 次)" % (label, hits))
258
- anchor_bad = 2
259
- continue
260
- mut = os.path.join(tmp, "mut_%d_postings.py" % idx)
261
- _write_norm(mut, src.replace(old, new))
262
- keep = list(_RESULTS)
263
- del _RESULTS[:]
264
- try:
265
- _use(mut)
266
- _case_sealed()
267
- except Exception as exc:
268
- _check("变异 %s:场景异常" % label, False, "%s: %s" % (type(exc).__name__, exc))
269
- reds = {n for n, ok, _d in _RESULTS if not ok}
270
- del _RESULTS[:]
271
- _RESULTS[:] = keep
272
- if reds == set(expect):
273
- print(" [PASS] %s —— 恰按 %d 条点名转红" % (label, len(reds)))
274
- else:
275
- print(" [FAIL] %s —— 多红 %r / 少红 %r"
276
- % (label, sorted(reds - set(expect)), sorted(set(expect) - reds)))
277
- _check("变异 %s 按预期点名转红" % label, False,
278
- "多红 %r / 少红 %r" % (sorted(reds - set(expect)), sorted(set(expect) - reds)))
279
- _use(os.path.join(REPO, IMPL_REL))
280
- if anchor_bad:
281
- return anchor_bad
282
- return 0 if all(ok for _n, ok, _d in _RESULTS) else 1
283
-
284
-
285
- def main(argv=None):
286
- ap = argparse.ArgumentParser(description="#152① 密封库 build fail-closed 守卫")
287
- ap.add_argument("--impl", default="", help="指定 postings 实现副本(head = 取 HEAD blob)")
288
- ap.add_argument("--mutate", action="store_true", help="定点变异自证")
289
- a = ap.parse_args(argv)
290
- with tempfile.TemporaryDirectory(prefix="iss152_guard_") as tmp:
291
- if a.mutate:
292
- return _mutate_mode(tmp)
293
- if a.impl == "head":
294
- return _baseline_mode(tmp)
295
- if a.impl:
296
- _use(os.path.abspath(a.impl))
297
- print("== #152① 密封库 postings.build fail-closed 守卫 ==\n")
298
- _case_sealed()
299
- _case_no_false_positive()
300
- _case_criteria()
301
- n_red = sum(1 for _n, ok, _d in _RESULTS if not ok)
302
- print("\n通过 %d / 失败 %d" % (len(_RESULTS) - n_red, n_red))
303
- return 1 if n_red else 0
304
-
305
-
306
- if __name__ == "__main__":
307
- if hasattr(sys.stdout, "reconfigure"):
308
- sys.stdout.reconfigure(encoding="utf-8")
309
- raise SystemExit(main())
1
+ # -*- coding: utf-8 -*-
2
+ """#152① 守卫:密封(加密)库不得被 postings.build 写出明文 bigram。
3
+
4
+ 缺陷形态(复现于 HEAD):
5
+ md_cg/postings.py 的 build() 经 cg.get() 取**解密后**的 content,把明文 bigram
6
+ 写进 <root>/_postings.json。节点正文已加密,而词项索引仍是明文 ⇒ 持该文件者
7
+ 可读出内容主题(与全仓「原文明文不出库」立场冲突)。
8
+
9
+ 修法(本件):
10
+ build() 开头做**密封面探测**——索引中存在加密级节点(entry.sensitivity 属
11
+ crypto.ENCRYPTED_LEVELS),或本实例挂密钥面且当前可解密(dek 为真)时,
12
+ **不写任何文件**直接拒绝,返回 refused / sealed_nodes / reason 供调用方判因。
13
+
14
+ 断言组:
15
+ A 缺陷面(HEAD 版应 5 条全红):
16
+ A1 密封库 build 后 _postings.json 不存在
17
+ A2 返回 rebuilt is False 且 refused == "sealed_nodes"
18
+ A3 产物全文无任何明文关键词 bigram
19
+ A4 返回 sealed_nodes == 1(拒因可定位到节点数)
20
+ A5 _postings_meta.json 同样未写出(拒绝=整体不落盘,不是写一半)
21
+ B 不误伤:
22
+ B1 非密封库(MdCG + internal 节点)照常 build(rebuilt=True 且文件写出)
23
+ B2 密封库但**全 internal 且已 lock**(无加密节点、不可解密)⇒ 放行
24
+ B3 抽样构建(limit)在密封库上同样被拒且不写文件
25
+ C 判据未放宽:
26
+ C1 _encrypted_levels() 与 crypto.ENCRYPTED_LEVELS 一致(小写集合)
27
+ C2 internal 节点的 _sealed_node_ids 为空
28
+ C3 拒绝路径不产出 partial 快照
29
+ D 红基线自证:以 HEAD 版 postings 跑 A 组,应恰 5 条全红;HEAD 已含修复则 SKIP
30
+
31
+ 用法:
32
+ python -X utf8 -m md_cg.test_issue152_sealed_postings
33
+ python -X utf8 -m md_cg.test_issue152_sealed_postings --impl head
34
+ python -X utf8 -m md_cg.test_issue152_sealed_postings --mutate
35
+ 退出码:0 = 全部按预期;1 = 有断言失败 / 变异未按预期转红;2 = 变异锚点失配。
36
+ """
37
+ import argparse
38
+ import importlib.util
39
+ import io
40
+ import json
41
+ import os
42
+ import subprocess
43
+ import sys
44
+ import tempfile
45
+
46
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
47
+
48
+ import md_cg # noqa: E402
49
+ from md_cg import postings # noqa: E402
50
+
51
+ REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
52
+ IMPL_REL = os.path.join("md_cg", "postings.py")
53
+ SECRET = "绝密项目代号"
54
+
55
+ _N_A1 = "A1 密封库:build 后 _postings.json 不存在"
56
+ _N_A2 = "A2 密封库:rebuilt is False 且 refused == sealed_nodes"
57
+ _N_A3 = "A3 密封库:产物全文无明文关键词 bigram"
58
+ _N_A4 = "A4 密封库:sealed_nodes == 1"
59
+ _N_A5 = "A5 密封库:_postings_meta.json 同样未写出"
60
+ _N_B1 = "B1 非密封库:照常 build(rebuilt=True 且文件写出)"
61
+ _N_B2 = "B2 密封库+全 internal+locked:放行(不误伤)"
62
+ _N_B3 = "B3 密封库+limit 抽样:同样被拒且不写文件"
63
+ _N_C1 = "C1 加密级集合 == crypto.ENCRYPTED_LEVELS"
64
+ _N_C2 = "C2 internal 节点的 _sealed_node_ids 为空"
65
+ _N_C3 = "C3 拒绝路径不产出 partial 快照"
66
+
67
+ _RESULTS = []
68
+
69
+
70
+ def _check(name, ok, detail=""):
71
+ _RESULTS.append((name, bool(ok), detail))
72
+ print(" %s %s%s" % ("[PASS]" if ok else "[FAIL]", name,
73
+ (" —— " + detail) if (not ok and detail) else ""))
74
+
75
+
76
+ def _use(path):
77
+ """把 path 物化为 md_cg.postings 并注入(用于红基线/变异体)。"""
78
+ global postings
79
+ spec = importlib.util.spec_from_file_location("md_cg.postings", path)
80
+ mod = importlib.util.module_from_spec(spec)
81
+ sys.modules["md_cg.postings"] = mod
82
+ spec.loader.exec_module(mod)
83
+ postings = mod
84
+ return mod
85
+
86
+
87
+ def _materialize_head(tmp):
88
+ r = subprocess.run(["git", "-C", REPO, "show", "HEAD:" + IMPL_REL.replace(os.sep, "/")],
89
+ capture_output=True)
90
+ if r.returncode != 0 or not (r.stdout or b"").strip():
91
+ return None, "git show 失败 rc=%s" % r.returncode
92
+ p = os.path.join(tmp, "postings_head.py")
93
+ with open(p, "wb") as f:
94
+ f.write(r.stdout)
95
+ return p, ""
96
+
97
+
98
+ def _tmp_root(prefix):
99
+ root = tempfile.mkdtemp(prefix=prefix)
100
+ _rt, _tt = os.path.realpath(root), os.path.realpath(tempfile.gettempdir())
101
+ assert _rt.startswith(_tt), "沙箱断言失败:%r 不在 tempdir 内" % _rt
102
+ return root
103
+
104
+
105
+ def _mk_sealed(root, body=None, sensitivity="secret", lock=False):
106
+ from md_cg import mdcos
107
+ from md_cg.security import Principal
108
+ owner = Principal(tenant="t1", actor="alice", clearance="secret")
109
+ cg = mdcos.MdCGSecure(root, principal=owner, master_key=os.urandom(32))
110
+ cg.add("s1", body or (SECRET + " 行动方案"), sensitivity=sensitivity)
111
+ cg.flush()
112
+ if lock:
113
+ cg.lock()
114
+ return cg
115
+
116
+
117
+ def _mk_open(root):
118
+ from md_cg import mdcos
119
+ cg = mdcos.MdCG(root)
120
+ cg.add("n1", "阿尔法 贝塔 伽马", "knowledge")
121
+ cg.flush()
122
+ return cg
123
+
124
+
125
+ def _leaked_terms(root):
126
+ """产物里含 SECRET 任一字符的 bigram(文件不存在 ⇒ 空集,即无泄漏)。"""
127
+ p = postings.postings_path(root)
128
+ if not os.path.exists(p):
129
+ return []
130
+ try:
131
+ with io.open(p, encoding="utf-8") as f:
132
+ table = json.load(f)
133
+ except Exception:
134
+ return ["<unreadable>"]
135
+ return sorted({g for g in table if any(c in g for c in SECRET)})
136
+
137
+
138
+ def _case_sealed():
139
+ root = _tmp_root("iss152_sealed_")
140
+ cg = _mk_sealed(root)
141
+ st = postings.build(cg)
142
+ _check(_N_A1, not os.path.exists(postings.postings_path(root)),
143
+ "exists=%s" % os.path.exists(postings.postings_path(root)))
144
+ _check(_N_A2, st.get("rebuilt") is False and st.get("refused") == "sealed_nodes",
145
+ "rebuilt=%r refused=%r" % (st.get("rebuilt"), st.get("refused")))
146
+ leak = _leaked_terms(root)
147
+ _check(_N_A3, not leak, "leak=%r" % (leak[:8],))
148
+ _check(_N_A4, st.get("sealed_nodes") == 1, "sealed_nodes=%r" % (st.get("sealed_nodes"),))
149
+ _check(_N_A5, not os.path.exists(postings.meta_path(root)),
150
+ "meta_exists=%s" % os.path.exists(postings.meta_path(root)))
151
+ cg.close()
152
+
153
+
154
+ def _case_no_false_positive():
155
+ root = _tmp_root("iss152_open_")
156
+ cg = _mk_open(root)
157
+ st = postings.build(cg)
158
+ _check(_N_B1, st.get("rebuilt") is True and st.get("nodes", 0) > 0
159
+ and os.path.exists(postings.postings_path(root)),
160
+ "rebuilt=%r nodes=%r exists=%s" % (st.get("rebuilt"), st.get("nodes"),
161
+ os.path.exists(postings.postings_path(root))))
162
+ cg.close()
163
+ root2 = _tmp_root("iss152_internal_")
164
+ cg2 = _mk_sealed(root2, body="阿尔法 贝塔", sensitivity="internal", lock=True)
165
+ st2 = postings.build(cg2)
166
+ _check(_N_B2, st2.get("rebuilt") is True and st2.get("refused") is None,
167
+ "rebuilt=%r refused=%r" % (st2.get("rebuilt"), st2.get("refused")))
168
+ cg2.close()
169
+ root3 = _tmp_root("iss152_partial_")
170
+ cg3 = _mk_sealed(root3)
171
+ st3 = postings.build(cg3, limit=1)
172
+ _check(_N_B3, st3.get("rebuilt") is False
173
+ and not os.path.exists(postings.postings_path(root3)),
174
+ "rebuilt=%r exists=%s" % (st3.get("rebuilt"),
175
+ os.path.exists(postings.postings_path(root3))))
176
+ _check(_N_C3, "partial" not in st3, "keys=%r" % (sorted(st3),))
177
+ cg3.close()
178
+
179
+
180
+ def _case_criteria():
181
+ from md_cg import crypto
182
+ want = {str(x).strip().lower() for x in crypto.ENCRYPTED_LEVELS}
183
+ _check(_N_C1, postings._encrypted_levels() == want,
184
+ "got=%r want=%r" % (sorted(postings._encrypted_levels()), sorted(want)))
185
+ _check(_N_C2, postings._sealed_node_ids({"n1": {"sensitivity": "internal"}}) == [],
186
+ "got=%r" % (postings._sealed_node_ids({"n1": {"sensitivity": "internal"}}),))
187
+
188
+
189
+ _HEAD_EXPECT_RED = [_N_A1, _N_A2, _N_A3, _N_A4, _N_A5]
190
+
191
+
192
+ def _baseline_mode(tmp):
193
+ """--impl head:以 HEAD 版 postings 跑 A 组,红基线必须 5 条全红。"""
194
+ print("!! 红基线取证:以 HEAD 版 postings 跑 A 组——修前缺陷形态必须按点转红\n")
195
+ head, why = _materialize_head(tmp)
196
+ if head is None:
197
+ _check("D 红基线自证:HEAD 版物化", False, why)
198
+ return 1
199
+ _use(head)
200
+ _case_sealed()
201
+ reds = {n for n, ok, _d in _RESULTS if not ok}
202
+ greens = [n for n, ok, _d in _RESULTS if ok]
203
+ if not reds:
204
+ print("\nHEAD 版 A 组全绿——HEAD 已含修复,红基线只在本改动提交前可复现(SKIP)")
205
+ return 0
206
+ if reds == set(_HEAD_EXPECT_RED):
207
+ print("\n红基线成立:HEAD 版恰 5 条点名转红(明文 bigram 落盘可复现)")
208
+ return 0
209
+ print("\n红基线不成立:多红 %r / 少红 %r"
210
+ % (sorted(reds - set(_HEAD_EXPECT_RED)), sorted(set(_HEAD_EXPECT_RED) - reds)))
211
+ return 1
212
+
213
+
214
+ _MUTATIONS = [
215
+ ("M1 删除密封面早退块(完全放行)",
216
+ " sealed_ids = _sealed_node_ids(nodes)\n"
217
+ " if sealed_ids:\n"
218
+ " return {\"schema\": SCHEMA, \"nodes\": 0, \"terms\": 0, \"postings\": 0,\n"
219
+ " \"rebuilt\": False, \"refused\": \"sealed_nodes\",\n"
220
+ " \"sealed_nodes\": len(sealed_ids),\n"
221
+ " \"reason\": \"密封面 fail-closed(#152①):密文节点的明文 bigram 不得落进 _postings.json\"}\n"
222
+ " if _is_sealed_instance(cg) and getattr(cg, \"dek\", None):\n"
223
+ " return {\"schema\": SCHEMA, \"nodes\": 0, \"terms\": 0, \"postings\": 0,\n"
224
+ " \"rebuilt\": False, \"refused\": \"sealed_instance_unlocked\", \"sealed_nodes\": 0,\n"
225
+ " \"reason\": \"密封面 fail-closed(#152①):实例持密钥可解密,明文 bigram 不得落进 _postings.json\"}\n",
226
+ "",
227
+ [_N_A1, _N_A2, _N_A3, _N_A4, _N_A5]),
228
+ ("M2 节点面判据失效(if sealed_ids → if False)",
229
+ " if sealed_ids:",
230
+ " if False:",
231
+ [_N_A2, _N_A4]),
232
+ ("M3 探测函数恒空(_sealed_node_ids 恒返回 [])",
233
+ " return [nid for nid, e in (nodes or {}).items()\n"
234
+ " if str(((e or {}).get(\"sensitivity\")) or \"\").strip().lower() in lv]",
235
+ " return []",
236
+ [_N_A2, _N_A4]),
237
+ ]
238
+
239
+
240
+ def _read_norm(path):
241
+ with open(path, encoding="utf-8", newline="") as f:
242
+ return f.read().replace("\r\n", "\n")
243
+
244
+
245
+ def _write_norm(path, text):
246
+ with open(path, "w", encoding="utf-8", newline="\n") as f:
247
+ f.write(text)
248
+
249
+
250
+ def _mutate_mode(tmp):
251
+ print("!! 定点变异自证:逐条把修复点改回退化形态,A 组必须按预期点名转红\n")
252
+ src = _read_norm(os.path.join(REPO, IMPL_REL))
253
+ anchor_bad = 0
254
+ for idx, (label, old, new, expect) in enumerate(_MUTATIONS):
255
+ hits = src.count(old)
256
+ if hits != 1:
257
+ print(" [锚点失配] %s:锚点命中 %d 次(要求恰 1 次)" % (label, hits))
258
+ anchor_bad = 2
259
+ continue
260
+ mut = os.path.join(tmp, "mut_%d_postings.py" % idx)
261
+ _write_norm(mut, src.replace(old, new))
262
+ keep = list(_RESULTS)
263
+ del _RESULTS[:]
264
+ try:
265
+ _use(mut)
266
+ _case_sealed()
267
+ except Exception as exc:
268
+ _check("变异 %s:场景异常" % label, False, "%s: %s" % (type(exc).__name__, exc))
269
+ reds = {n for n, ok, _d in _RESULTS if not ok}
270
+ del _RESULTS[:]
271
+ _RESULTS[:] = keep
272
+ if reds == set(expect):
273
+ print(" [PASS] %s —— 恰按 %d 条点名转红" % (label, len(reds)))
274
+ else:
275
+ print(" [FAIL] %s —— 多红 %r / 少红 %r"
276
+ % (label, sorted(reds - set(expect)), sorted(set(expect) - reds)))
277
+ _check("变异 %s 按预期点名转红" % label, False,
278
+ "多红 %r / 少红 %r" % (sorted(reds - set(expect)), sorted(set(expect) - reds)))
279
+ _use(os.path.join(REPO, IMPL_REL))
280
+ if anchor_bad:
281
+ return anchor_bad
282
+ return 0 if all(ok for _n, ok, _d in _RESULTS) else 1
283
+
284
+
285
+ def main(argv=None):
286
+ ap = argparse.ArgumentParser(description="#152① 密封库 build fail-closed 守卫")
287
+ ap.add_argument("--impl", default="", help="指定 postings 实现副本(head = 取 HEAD blob)")
288
+ ap.add_argument("--mutate", action="store_true", help="定点变异自证")
289
+ a = ap.parse_args(argv)
290
+ with tempfile.TemporaryDirectory(prefix="iss152_guard_") as tmp:
291
+ if a.mutate:
292
+ return _mutate_mode(tmp)
293
+ if a.impl == "head":
294
+ return _baseline_mode(tmp)
295
+ if a.impl:
296
+ _use(os.path.abspath(a.impl))
297
+ print("== #152① 密封库 postings.build fail-closed 守卫 ==\n")
298
+ _case_sealed()
299
+ _case_no_false_positive()
300
+ _case_criteria()
301
+ n_red = sum(1 for _n, ok, _d in _RESULTS if not ok)
302
+ print("\n通过 %d / 失败 %d" % (len(_RESULTS) - n_red, n_red))
303
+ return 1 if n_red else 0
304
+
305
+
306
+ if __name__ == "__main__":
307
+ if hasattr(sys.stdout, "reconfigure"):
308
+ sys.stdout.reconfigure(encoding="utf-8")
309
+ raise SystemExit(main())