@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.
- package/data/policy.json +38 -38
- package/docs/discipline/templates/spec.txt.tmpl +1 -1
- package/dsh/cordis.yml.example +139 -139
- package/lib/hooks.d.ts +19 -0
- package/lib/hooks.js +29 -6
- package/lib/index.js +11 -2
- package/lib/lib/hook_audit.js +38 -2
- package/lib/lib/prompt_safety.js +14 -3
- package/lib/tools.d.ts +7 -0
- package/lib/tools.js +22 -1
- package/md_cg/_guard_kit.py +121 -121
- package/md_cg/anchor.py +114 -114
- package/md_cg/audit.py +15 -2
- package/md_cg/chain.py +3 -2
- package/md_cg/config_registry_guard.py +487 -487
- package/md_cg/conformance.py +747 -747
- package/md_cg/consolidate.py +1793 -1793
- package/md_cg/crypto.py +794 -794
- package/md_cg/hyperedge.py +262 -262
- package/md_cg/interface_schema.py +146 -146
- package/md_cg/lifecycle.py +426 -426
- package/md_cg/mdcos.py +12 -8
- package/md_cg/predict.py +1103 -1103
- package/md_cg/protect.py +654 -654
- package/md_cg/provenance.py +606 -606
- package/md_cg/queue_health.py +130 -130
- package/md_cg/refine.py +613 -613
- package/md_cg/retrieve.py +128 -128
- package/md_cg/revise.py +140 -140
- package/md_cg/rollback.py +657 -657
- package/md_cg/rollback_cli.py +124 -124
- package/md_cg/rotate.py +346 -346
- package/md_cg/routing.py +490 -490
- package/md_cg/self_surface.py +50 -50
- package/md_cg/semantic/canonical.py +144 -144
- package/md_cg/semantic/zh_en_atoms.py +141 -141
- package/md_cg/sources.py +1091 -1091
- package/md_cg/stg.py +3 -2
- package/md_cg/test_audit_rotate_parity.py +279 -279
- package/md_cg/test_autonomy_modes.py +2013 -2013
- package/md_cg/test_cn_recall_grams.py +206 -206
- package/md_cg/test_code_kind_forbidden.py +101 -0
- package/md_cg/test_condaccept.py +156 -156
- package/md_cg/test_condgate.py +141 -141
- package/md_cg/test_heartbeat_ledger.py +557 -557
- package/md_cg/test_i50e_readside_protection.py +658 -658
- package/md_cg/test_issue111_gate_order.py +76 -76
- package/md_cg/test_issue111_scoring_contract.py +86 -86
- package/md_cg/test_issue121_calibration_bounds.py +293 -293
- package/md_cg/test_issue145_sleep_no_silent_rollback.py +167 -167
- package/md_cg/test_issue147_pack_id_escape.py +378 -378
- package/md_cg/test_issue148_merge_downgrade_leak.py +192 -192
- package/md_cg/test_issue149_accel_default_face.py +248 -248
- package/md_cg/test_issue150_empty_query_terms.py +328 -328
- package/md_cg/test_issue151_153_157_write_cache_index.py +151 -151
- package/md_cg/test_issue152_sealed_postings.py +309 -309
- package/md_cg/test_issue153b_index_generation_invalidation.py +142 -142
- package/md_cg/test_issue157b_index_remainder.py +344 -344
- package/md_cg/test_issue189_conclusion_bidirectional.py +223 -223
- package/md_cg/test_issue191_import_trust.py +382 -382
- package/md_cg/test_issue193_write_scope.py +378 -378
- package/md_cg/test_issue212_writeface_gaps.py +349 -349
- package/md_cg/test_issue214_reject_sensitivity.py +266 -266
- package/md_cg/test_issue215_condition_source_gap.py +272 -272
- package/md_cg/test_issue216_clash_routes.py +214 -214
- package/md_cg/test_issue217_slot_routes.py +248 -248
- package/md_cg/test_issue218_postcondition_value_blind.py +222 -222
- package/md_cg/test_issue258_pack_sensitivity.py +468 -468
- package/md_cg/test_issue265_coldverify_multiproc.py +438 -438
- package/md_cg/test_issue274_read_node_id_none.py +341 -341
- package/md_cg/test_issue275_layer_param.py +250 -250
- package/md_cg/test_issue277_node_id_traversal.py +384 -384
- package/md_cg/test_issue278_neg_separated_tension.py +271 -271
- package/md_cg/test_issue278_neg_window.py +294 -294
- package/md_cg/test_issue278_negative_layering.py +83 -83
- package/md_cg/test_issue284_skip_not_pass.py +291 -291
- package/md_cg/test_issue294_flywheel_sensitivity.py +573 -573
- package/md_cg/test_issue301_baseline_corpus_gate.py +245 -245
- package/md_cg/test_issue303_design_boundaries.py +262 -262
- package/md_cg/test_issue315_merge_branch_sensitivity.py +383 -383
- package/md_cg/test_issue319_tenant_unregistered_bypass.py +256 -256
- package/md_cg/test_issue321_mcp_deep_json.py +304 -304
- package/md_cg/test_issue322_node_write_lock.py +727 -727
- package/md_cg/test_issue324_write_size_fields.py +214 -214
- package/md_cg/test_issue325_audit_poison.py +229 -229
- package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -331
- package/md_cg/test_issue326_key_perms.py +355 -355
- package/md_cg/test_issue326_twin_parser_parity.py +323 -323
- package/md_cg/test_issue326_vm_call_frame.py +569 -569
- package/md_cg/test_issue345_writelimit_effective_sens.py +233 -233
- package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -424
- package/md_cg/test_issue348_insight_verify_op.py +199 -199
- package/md_cg/test_issue348_merge_rmw_lock.py +345 -345
- package/md_cg/test_issue350_casefold_collision.py +235 -235
- package/md_cg/test_issue351_ingest_root_fallback.py +201 -201
- package/md_cg/test_issue352_imgaudit.py +418 -418
- package/md_cg/test_issue352_pathsjson.py +537 -537
- package/md_cg/test_issue352_refindex.py +412 -412
- package/md_cg/test_issue352_rollback.py +604 -604
- package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -327
- package/md_cg/test_issue354_rust_index_containment.py +571 -571
- package/md_cg/test_issue354_rust_wal_containment.py +606 -606
- package/md_cg/test_issue358_truncation_observable.py +247 -247
- package/md_cg/test_issue360_tidy_private_excerpt.py +195 -195
- package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -494
- package/md_cg/test_issue362_neg_condition.py +468 -468
- package/md_cg/test_issue363_lexical_idf.py +275 -275
- package/md_cg/test_issue370_readcache_assembly.py +488 -488
- package/md_cg/test_issue376_heartbeat_failed.py +293 -293
- package/md_cg/test_issue377_layer_protect_stamp.py +324 -324
- package/md_cg/test_issue378_protected_floor.py +310 -310
- package/md_cg/test_issue383_state_event_write_gate.py +462 -462
- package/md_cg/test_issue397_sens_eff_wiring.py +435 -435
- package/md_cg/test_issue398_toolface_op_parity.py +511 -511
- package/md_cg/test_issue399_hive_json_shape.py +317 -317
- package/md_cg/test_issue401_wisdom_codegen.py +295 -295
- package/md_cg/test_issue404_heal_receipt.py +389 -389
- package/md_cg/test_issue409_compact_index_completeness.py +509 -509
- package/md_cg/test_issue410_verify_demote_write.py +366 -366
- package/md_cg/test_issue411_scrub_idempotence.py +575 -575
- package/md_cg/test_issue412_discipline_producer_parity.py +311 -311
- package/md_cg/test_issue413_preimage_failclosed.py +239 -239
- package/md_cg/test_issue413_preimage_twin.py +339 -339
- package/md_cg/test_issue415_deps_gate_probe.py +357 -357
- package/md_cg/test_issue416_entry_key_subset.py +525 -525
- package/md_cg/test_issue417_twophase_merge_landed.py +423 -423
- package/md_cg/test_issue425_ingest_library_path.py +331 -331
- package/md_cg/test_issue426c_enqueue_not_silent.py +346 -346
- package/md_cg/test_issue426e_restore_overwrite_live.py +310 -310
- package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -301
- package/md_cg/test_issue427_serve_status_shape.py +191 -191
- package/md_cg/test_issue431_432_468_protect_single_point.py +322 -322
- package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -278
- package/md_cg/test_issue434_435_float_gate.py +90 -90
- package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -448
- package/md_cg/test_issue43_default_policy.py +874 -874
- package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -281
- package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -454
- package/md_cg/test_issue446_453_403_verified_evidence.py +382 -382
- package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -51
- package/md_cg/test_issue450_writegate_unknown_sens.py +203 -203
- package/md_cg/test_issue451_audit_actor.py +313 -313
- package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -299
- package/md_cg/test_issue463_ghost_prune_guard.py +259 -259
- package/md_cg/test_issue466_basis_registry.py +469 -469
- package/md_cg/test_issue467_identity_sensitivity_parity.py +360 -360
- package/md_cg/test_issue469_scrub_override_passthrough.py +126 -126
- package/md_cg/test_issue470_wal_event_durable.py +427 -427
- package/md_cg/test_issue471_identity_readable.py +136 -136
- package/md_cg/test_issue472_audit_chain.py +90 -90
- package/md_cg/test_issue472_prune_ledger.py +105 -105
- package/md_cg/test_issue472_rollback_degraded.py +81 -81
- package/md_cg/test_issue491_compose_sandbox.py +460 -460
- package/md_cg/test_issue498_merge_trace_stale_index.py +280 -280
- package/md_cg/test_issue511_scoreboard_bad_files.py +256 -256
- package/md_cg/test_issue521_rotate_lock_suffix.py +110 -110
- package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -88
- package/md_cg/test_issue525_read_null_semantics.py +52 -52
- package/md_cg/test_issue547_append_jsonl_whitelist.py +114 -114
- package/md_cg/test_issue547_audit_durable.py +96 -96
- package/md_cg/test_issue557_r2_write_entries_size_gate.py +83 -83
- package/md_cg/test_issue559_refine_prefix.py +69 -69
- package/md_cg/test_issue562_retired_faces.py +150 -0
- package/md_cg/test_issue71_queue_backlog.py +376 -376
- package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -420
- package/md_cg/test_issue82_credential_rules.py +271 -271
- package/md_cg/test_issue91_verifier_sandbox.py +314 -314
- package/md_cg/test_issue93_remember_redaction.py +282 -282
- package/md_cg/test_logref.py +1141 -1141
- package/md_cg/test_md_access_parity.py +330 -330
- package/md_cg/test_md_cg_no_hive_dep.py +139 -139
- package/md_cg/test_mutation_residue.py +119 -119
- package/md_cg/test_mutation_rollback.py +835 -835
- package/md_cg/test_n123_derive_expiry_chain.py +210 -210
- package/md_cg/test_n209_verify_write_face_gates.py +466 -466
- package/md_cg/test_n62_tenant_bind_failclosed.py +200 -200
- package/md_cg/test_neg_tail_honesty.py +733 -733
- package/md_cg/test_p21_tokens.py +294 -294
- package/md_cg/test_p27_docindex.py +792 -792
- package/md_cg/test_p28_refcheck.py +1053 -1053
- package/md_cg/test_p2_six_elements.py +476 -476
- package/md_cg/test_p33_ccg_wiring.py +300 -300
- package/md_cg/test_p3_legacy_closure.py +438 -438
- package/md_cg/test_p40_refine_worklist.py +244 -244
- package/md_cg/test_p4_freshness.py +781 -781
- package/md_cg/test_p9c_dedup_hints.py +288 -288
- package/md_cg/test_policy_required_ccg.py +444 -444
- package/md_cg/test_retr_s5.py +312 -312
- package/md_cg/test_s1_ast_judge.py +109 -109
- package/md_cg/test_state_event_op.py +521 -521
- package/md_cg/test_stgidx_index_parity.py +1024 -1024
- package/md_cg/test_sustain_bounded.py +652 -652
- package/md_cg/test_trust_edge_reachability.py +405 -405
- package/md_cg/test_w7_redlines.py +659 -659
- package/md_cg/test_write_return_shape.py +919 -919
- package/md_cg/test_writepipe.py +228 -228
- package/md_cg/trust.py +1114 -1114
- package/md_cg/twophase.py +303 -303
- package/md_cg/whitebox.py +536 -536
- package/md_cg/whitebox_kb/wisdom/compose_engine.py +1634 -1634
- package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +318 -318
- package/md_cg/whitebox_kb/wisdom/core/parser.py +1082 -1082
- package/md_cg/whitebox_kb/wisdom/md_access.py +490 -490
- package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +497 -497
- package/md_cg/whitebox_kb/wisdom/mini_python.py +995 -995
- package/md_cg/whitebox_kb/wisdom/net_units.py +2900 -2900
- package/md_cg/whitebox_kb/wisdom/semantic_translate.py +4060 -4060
- package/md_cg/writepipe.py +1337 -1337
- package/package.json +3 -2
- package/scripts/workspace_index.py +328 -0
- package/src/hooks.ts +30 -6
- package/src/index.ts +14 -5
- package/src/lib/hook_audit.ts +282 -256
- package/src/lib/mdcg_client.ts +469 -469
- package/src/lib/prompt_safety.ts +17 -3
- package/src/tools.ts +23 -1
|
@@ -1,571 +1,571 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
r"""#354 P0-3 / P1-8 守卫(Rust 检索面):_index.json path 物理 containment 与
|
|
3
|
-
索引装载「坏件不连坐」。
|
|
4
|
-
|
|
5
|
-
缺陷(本件修复,2026-10-14)
|
|
6
|
-
----------------------------
|
|
7
|
-
【P0-3】rust/src/store.rs 的条目装载直接 root.join(path) 读盘,**零 containment**:
|
|
8
|
-
· 绝对路径(D:\x / \x / \\server\share)—— std 的 Path::join 遇绝对路径**整体
|
|
9
|
-
替换** root;
|
|
10
|
-
· 相对穿越(../x、a/../../x)。
|
|
11
|
-
实测(修前,真二进制 mdcg-eval --serve):_index.json 里塞 ../sentinel.txt 与
|
|
12
|
-
绝对路径 → info.docs=3(哨兵被当节点正文读入)、查询 zzsentinel 直接命中
|
|
13
|
-
id=哨兵节点 —— 库根之外的**内容回显**(哨兵泄漏)。
|
|
14
|
-
【P1-8】装载面「一个坏件连坐整个装载」:
|
|
15
|
-
· 坏 _index.json(读到/解析失败)→ 旧实现两处 ? 直接让整个装载 Err,进程非零
|
|
16
|
-
退出,合法 _index_log 分片里的节点**全不可检索**;
|
|
17
|
-
· 坏分片(非法 UTF-8 字节,撕裂写残字节)→ 旧实现 read_to_string 整片全有全无,
|
|
18
|
-
整分片被 continue 丢弃,**其内的合法行连同账**(同分片连坐)。
|
|
19
|
-
|
|
20
|
-
本守卫三层
|
|
21
|
-
----------
|
|
22
|
-
[A] 行为面(真二进制 mdcg-eval --serve,端到端):
|
|
23
|
-
A1-A3 穿越形态逐类(相对 ../、绝对、混合分隔符/空串/NUL/盘符相对)不得命中
|
|
24
|
-
哨兵、不得计入 docs、越界条目须有 stderr 留痕;
|
|
25
|
-
A4 零误伤:合法路径照常可检索(修 containment 不得把正常库读坏);
|
|
26
|
-
A5-A7 P1-8:坏 _index.json + 好分片 → 装载不连坐;坏分片内合法行仍重放;
|
|
27
|
-
两源皆好 → 合并语义(同 id 分片覆盖)不变。
|
|
28
|
-
[B] 源断言(结构性,行为面不可用时仍决定 rc):闸在场、闸在**读盘之前**被调用、
|
|
29
|
-
判据含 canonicalize 物理层(不是只比字符串前缀)、临时/坏件路径有留痕。
|
|
30
|
-
[C] 定点变异:拆掉物理层 / 拆掉逐行容错 → 判据必须转红(改源 → cargo build →
|
|
31
|
-
跑守卫转红 → 还原 → 重建),绝不在真实工作树留下未还原改动。
|
|
32
|
-
|
|
33
|
-
运行:
|
|
34
|
-
python -X utf8 -m md_cg.test_issue354_rust_index_containment
|
|
35
|
-
python -X utf8 -m md_cg.test_issue354_rust_index_containment --mutate
|
|
36
|
-
退出码:0 全绿 / 1 有失败。
|
|
37
|
-
环境:MDCG_EVAL_EXE 可指向别处的 mdcg-eval 可执行文件。
|
|
38
|
-
诚实边界:行为面需要 rust/target/release 下的可执行件(gitignored)——本守卫按仓内
|
|
39
|
-
既有口径**自建**(cargo build --release);cargo 与产物俱不可得时行为面显式 SKIP
|
|
40
|
-
并打印构建命令(源断言面 [B] 照判、照决定 rc),不把「没测到」记成绿。
|
|
41
|
-
"""
|
|
42
|
-
from __future__ import annotations
|
|
43
|
-
|
|
44
|
-
import io
|
|
45
|
-
import json
|
|
46
|
-
import os
|
|
47
|
-
import shutil
|
|
48
|
-
import subprocess
|
|
49
|
-
import sys
|
|
50
|
-
import tempfile
|
|
51
|
-
|
|
52
|
-
try:
|
|
53
|
-
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
54
|
-
except AttributeError:
|
|
55
|
-
pass
|
|
56
|
-
|
|
57
|
-
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
58
|
-
_RUST_DIR = os.path.join(_REPO, "rust")
|
|
59
|
-
_STORE_RS = os.path.join(_RUST_DIR, "src", "store.rs")
|
|
60
|
-
|
|
61
|
-
PASS = 0
|
|
62
|
-
FAIL = 0
|
|
63
|
-
FAILS = []
|
|
64
|
-
SKIPS = []
|
|
65
|
-
_SANDBOXES = []
|
|
66
|
-
#: 变异自证用:_SILENT 为真时 check() 只把失败项记进 _VIOL(不打印、不计入读数)。
|
|
67
|
-
_SILENT = False
|
|
68
|
-
_VIOL = []
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
def check(name, cond, detail=""):
|
|
72
|
-
global PASS, FAIL
|
|
73
|
-
if _SILENT:
|
|
74
|
-
if not cond:
|
|
75
|
-
_VIOL.append(name)
|
|
76
|
-
return bool(cond)
|
|
77
|
-
if cond:
|
|
78
|
-
PASS += 1
|
|
79
|
-
print(" [PASS] %s%s" % (name, (" · " + str(detail)) if detail else ""))
|
|
80
|
-
else:
|
|
81
|
-
FAIL += 1
|
|
82
|
-
FAILS.append(name)
|
|
83
|
-
print(" [FAIL] %s · %s" % (name, detail))
|
|
84
|
-
return bool(cond)
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
def skip(name, why):
|
|
88
|
-
SKIPS.append(name)
|
|
89
|
-
print(" [SKIP] %s · %s" % (name, why))
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
def _exe_path():
|
|
93
|
-
env = os.environ.get("MDCG_EVAL_EXE")
|
|
94
|
-
if env:
|
|
95
|
-
return env
|
|
96
|
-
base = os.path.join(_RUST_DIR, "target", "release", "mdcg-eval")
|
|
97
|
-
return base + ".exe" if os.name == "nt" else base
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
EXE = _exe_path()
|
|
101
|
-
_BUILD_CMD = "cargo build --release (在 %s 下执行)" % _RUST_DIR
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
def _ensure_exe():
|
|
105
|
-
"""确保 mdcg-eval 在场;返回是否可用(不可用则行为面 SKIP,源断言面照判)。"""
|
|
106
|
-
if os.path.isfile(EXE):
|
|
107
|
-
return True
|
|
108
|
-
if not shutil.which("cargo") or not os.path.isdir(_RUST_DIR):
|
|
109
|
-
print(" (%s 不在场且 cargo 不可用)" % EXE)
|
|
110
|
-
return False
|
|
111
|
-
print(" 自建:%s" % _BUILD_CMD)
|
|
112
|
-
r = subprocess.run(["cargo", "build", "--release"], cwd=_RUST_DIR,
|
|
113
|
-
capture_output=True, text=True, encoding="utf-8",
|
|
114
|
-
errors="replace", timeout=900)
|
|
115
|
-
if r.returncode != 0:
|
|
116
|
-
print(" 构建失败:%s" % (r.stderr or "")[-400:])
|
|
117
|
-
return False
|
|
118
|
-
return os.path.isfile(EXE)
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
def _mkroot(tag):
|
|
122
|
-
t = tempfile.mkdtemp(prefix="i354idx_%s_" % tag)
|
|
123
|
-
_SANDBOXES.append(t)
|
|
124
|
-
root = os.path.join(t, "lib")
|
|
125
|
-
os.makedirs(os.path.join(root, "knowledge"))
|
|
126
|
-
return t, root
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
def _write(path, text):
|
|
130
|
-
"""写文本或字节(bytes 走二进制口径——坏 UTF-8 分支用)。"""
|
|
131
|
-
d = os.path.dirname(path)
|
|
132
|
-
if d and not os.path.isdir(d):
|
|
133
|
-
os.makedirs(d)
|
|
134
|
-
if isinstance(text, (bytes, bytearray)):
|
|
135
|
-
io.open(path, "wb").write(text)
|
|
136
|
-
else:
|
|
137
|
-
io.open(path, "w", encoding="utf-8", newline="").write(text)
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
def _node(path, nid, body):
|
|
141
|
-
_write(path, "---\nid: \"%s\"\ntags: []\nimportance: 0.5\n---\n%s\n" % (nid, body))
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
def _index_json(root, nodes):
|
|
145
|
-
_write(os.path.join(root, "_index.json"),
|
|
146
|
-
json.dumps({"nodes": nodes}, ensure_ascii=False))
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
def _entry(path, layer="knowledge", imp=0.5):
|
|
150
|
-
return {"path": path, "layer": layer, "tags": [], "importance": imp, "edges": []}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
def _serve(root, reqs):
|
|
154
|
-
"""拉起 mdcg-eval --serve,逐条请求取响应;返回 (响应列表, stderr 文本)。"""
|
|
155
|
-
env = dict(os.environ)
|
|
156
|
-
env.pop("MDCG_TOKEN", None)
|
|
157
|
-
p = subprocess.Popen([EXE, "--root", root, "--serve"],
|
|
158
|
-
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
|
159
|
-
stderr=subprocess.PIPE, text=True, encoding="utf-8",
|
|
160
|
-
errors="replace", env=env)
|
|
161
|
-
out = []
|
|
162
|
-
try:
|
|
163
|
-
for r in reqs:
|
|
164
|
-
try:
|
|
165
|
-
p.stdin.write(json.dumps(r) + "\n")
|
|
166
|
-
p.stdin.flush()
|
|
167
|
-
except (BrokenPipeError, OSError):
|
|
168
|
-
# 子进程已死(如装载失败 exit 1):静默截断请求序列,剩下的槽位
|
|
169
|
-
# 补 None——判据看的是**读数**,不该被调用方的管道异常顶掉
|
|
170
|
-
out.append(None)
|
|
171
|
-
continue
|
|
172
|
-
line = p.stdout.readline()
|
|
173
|
-
out.append(json.loads(line) if line.strip() else None)
|
|
174
|
-
finally:
|
|
175
|
-
try:
|
|
176
|
-
p.stdin.close()
|
|
177
|
-
except Exception: # noqa: BLE001
|
|
178
|
-
pass
|
|
179
|
-
try:
|
|
180
|
-
p.wait(timeout=20)
|
|
181
|
-
except Exception: # noqa: BLE001
|
|
182
|
-
p.kill()
|
|
183
|
-
err = p.stderr.read() if p.stderr else ""
|
|
184
|
-
return out, err
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
def _hits(resp):
|
|
188
|
-
return (resp or {}).get("hits") or []
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
# ============================ [A] 行为面:P0-3 ============================
|
|
192
|
-
|
|
193
|
-
TOKEN = "zzsentinel354"
|
|
194
|
-
SENT_ID = "哨兵节点354"
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
#: 探针查询:越界条目在场 / 不在场两种库都必须给出**逐位一致**的读数。
|
|
198
|
-
_PROBES = (TOKEN, "SENTINEL-LEAK-354", "zznormal354")
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
def _readings(root):
|
|
202
|
-
"""跑一组固定请求,把可观测面带成可比较的元组:((docs, 候选), [(id,path,score)...]...)。
|
|
203
|
-
|
|
204
|
-
为什么用「有/无越界条目的两库读数对拍」而不是「查询必须零命中」:本引擎在词法
|
|
205
|
-
全空时会走 LIKE 兜底池(合法节点照样返回),故「零命中」不是可用判据;而
|
|
206
|
-
「越界条目不得改变任何读数」是自校准的强判据——修前哨兵被当正文读入时,其
|
|
207
|
-
importance=0.99 + 正文命中会直接改写 top-1(实测 hits=[哨兵节点354])。
|
|
208
|
-
"""
|
|
209
|
-
out, err = _serve(root, [{"op": "info"}]
|
|
210
|
-
+ [{"op": "search", "query": q, "k": 10} for q in _PROBES])
|
|
211
|
-
info = out[0] or {}
|
|
212
|
-
rows = []
|
|
213
|
-
for r in out[1:]:
|
|
214
|
-
rows.append(sorted((h.get("id"), h.get("path"), h.get("score"))
|
|
215
|
-
for h in _hits(r)))
|
|
216
|
-
return (info.get("docs"), info.get("candidates")), rows, err
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
def _traversal_case(name, evil_paths, tag="trav"):
|
|
220
|
-
t = tempfile.mkdtemp(prefix="i354idx_%s_" % tag)
|
|
221
|
-
_SANDBOXES.append(t)
|
|
222
|
-
sentinel = os.path.join(t, "sentinel.txt")
|
|
223
|
-
_node(sentinel, SENT_ID, "# 正文:%s 沙箱外哨兵内容 SENTINEL-LEAK-354" % TOKEN)
|
|
224
|
-
root_evil = os.path.join(t, "lib_evil")
|
|
225
|
-
root_ctrl = os.path.join(t, "lib_ctrl")
|
|
226
|
-
for root in (root_evil, root_ctrl):
|
|
227
|
-
os.makedirs(os.path.join(root, "knowledge"))
|
|
228
|
-
_node(os.path.join(root, "knowledge", "good.md"), "good",
|
|
229
|
-
"# 正文:正常内容 zznormal354")
|
|
230
|
-
nodes = {"good": _entry("knowledge/good.md")}
|
|
231
|
-
for i, p in enumerate(evil_paths):
|
|
232
|
-
# ABS 标记:换成真实存在的库外哨兵绝对路径(绝对路径条目在**修前**正是
|
|
233
|
-
# 因为 Path::join 整体替换 root 而读到它;修后须在词法层即被拒)
|
|
234
|
-
nodes["evil%d" % i] = _entry(sentinel if p == "ABS" else p, imp=0.99)
|
|
235
|
-
_index_json(root_evil, nodes)
|
|
236
|
-
_index_json(root_ctrl, {"good": _entry("knowledge/good.md")})
|
|
237
|
-
head_e, rows_e, err_e = _readings(root_evil)
|
|
238
|
-
head_c, rows_c, _err_c = _readings(root_ctrl)
|
|
239
|
-
flat = [x for r in rows_e for x in r]
|
|
240
|
-
check("%s:哨兵节点名不得出现在任何命中里(泄漏面)" % name,
|
|
241
|
-
SENT_ID not in [x[0] for x in flat], flat[:4])
|
|
242
|
-
check("%s:越界条目在场与不在场,检索读数必须逐位一致" % name,
|
|
243
|
-
rows_e == rows_c, {"with": rows_e[:2], "without": rows_c[:2]})
|
|
244
|
-
# 注意:候选数(candidates)本就含被拒条目(它们是索引里的真实条目,只是
|
|
245
|
-
# 读不出正文),故这面只钉**已装载文档数**——泄漏面正是「越界文件被当正文读入」。
|
|
246
|
-
check("%s:已装载文档数不得因越界条目改变" % name,
|
|
247
|
-
head_e[0] == head_c[0], {"with": head_e, "without": head_c})
|
|
248
|
-
check("%s:越界条目须有 stderr 留痕(可诊断,不是静默丢弃)" % name,
|
|
249
|
-
"越界被拒" in err_e and "P0-3" in err_e, err_e.strip()[-160:])
|
|
250
|
-
check("%s:合法节点照常可检索(零误伤)" % name,
|
|
251
|
-
[h.get("id") for h in _hits(_serve(root_ctrl, [{"op": "search",
|
|
252
|
-
"query": "zznormal354",
|
|
253
|
-
"k": 10}])[0][0])] == ["good"],
|
|
254
|
-
rows_e[-1] if rows_e else None)
|
|
255
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
def a_relative():
|
|
259
|
-
print("== A1 P0-3:相对穿越(../ 与 a/../..)==")
|
|
260
|
-
_traversal_case("A1", ["../sentinel.txt", "knowledge/../../sentinel.txt",
|
|
261
|
-
"..\\sentinel.txt"])
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
def a_absolute():
|
|
265
|
-
print("== A2 P0-3:绝对路径 / 根相对 / UNC ==")
|
|
266
|
-
_traversal_case("A2", ["ABS", os.sep + "definitely-not-here.txt",
|
|
267
|
-
"\\\\server\\share\\x.md"], tag="abs")
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
def a_pathological():
|
|
271
|
-
print("== A3 P0-3:病理 path(空串 / NUL / 盘符相对 / 库内目录)不得崩不得读 ==")
|
|
272
|
-
t, root = _mkroot("patho")
|
|
273
|
-
nodes = {"good": _entry("knowledge/good.md"),
|
|
274
|
-
"empty": _entry(""),
|
|
275
|
-
"nul": _entry("knowledge/" + chr(0) + "x.md"),
|
|
276
|
-
"drvrel": _entry("C:sentinel.txt"),
|
|
277
|
-
"is_dir": _entry("knowledge")}
|
|
278
|
-
_index_json(root, nodes)
|
|
279
|
-
_node(os.path.join(root, "knowledge", "good.md"), "good", "# 正文:zznormal354")
|
|
280
|
-
out, err = _serve(root, [{"op": "info"},
|
|
281
|
-
{"op": "search", "query": "zznormal354", "k": 10},
|
|
282
|
-
{"op": "search", "query": TOKEN, "k": 10}])
|
|
283
|
-
check("A3 病态 path 不致进程崩溃(info 仍应答)",
|
|
284
|
-
(out[0] or {}).get("ok") is True, out[0])
|
|
285
|
-
check("A3 库内目录条目(knowledge)不得被当节点读入",
|
|
286
|
-
[h.get("id") for h in _hits(out[1])] == ["good"],
|
|
287
|
-
[h.get("id") for h in _hits(out[1])])
|
|
288
|
-
check("A3 病态 path 不泄漏哨兵",
|
|
289
|
-
SENT_ID not in [h.get("id") for h in _hits(out[2])], _hits(out[2]))
|
|
290
|
-
check("A3 病态 path 亦须留痕(词法层即拒)",
|
|
291
|
-
"越界被拒" in err, err.strip()[-160:])
|
|
292
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
def a_symlink_escape():
|
|
296
|
-
print("== A4 P0-3:符号链接指向库外(物理层判据的判别面)==")
|
|
297
|
-
t, root = _mkroot("link")
|
|
298
|
-
outside = os.path.join(t, "outside")
|
|
299
|
-
os.makedirs(outside)
|
|
300
|
-
_node(os.path.join(outside, "secret.md"), SENT_ID,
|
|
301
|
-
"# 正文:%s SENTINEL-LEAK-354" % TOKEN)
|
|
302
|
-
link = os.path.join(root, "knowledge", "link.md")
|
|
303
|
-
made = False
|
|
304
|
-
try:
|
|
305
|
-
os.symlink(os.path.join(outside, "secret.md"), link)
|
|
306
|
-
made = True
|
|
307
|
-
except (OSError, NotImplementedError) as e: # noqa: BLE001
|
|
308
|
-
skip("A4 符号链接穿越", "本机无法创建符号链接(需开发者模式/权限):%s" % e)
|
|
309
|
-
if made:
|
|
310
|
-
_index_json(root, {"evil_link": _entry("knowledge/link.md", imp=0.99),
|
|
311
|
-
"good": _entry("knowledge/good.md")})
|
|
312
|
-
_node(os.path.join(root, "knowledge", "good.md"), "good",
|
|
313
|
-
"# 正文:zznormal354")
|
|
314
|
-
out, err = _serve(root, [{"op": "info"},
|
|
315
|
-
{"op": "search", "query": TOKEN, "k": 10}])
|
|
316
|
-
check("A4 符号链接指向库外不得被读(词法看不出,物理层才拦得住)",
|
|
317
|
-
SENT_ID not in [h.get("id") for h in _hits(out[1])], _hits(out[1]))
|
|
318
|
-
check("A4 越界留痕", "越界被拒" in err, err.strip()[-160:])
|
|
319
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
# ============================ [A] 行为面:P1-8 ============================
|
|
323
|
-
|
|
324
|
-
def _shard(nid, path, t):
|
|
325
|
-
return json.dumps({"id": nid, "_t": t, "_s": 1,
|
|
326
|
-
"e": _entry(path)}, ensure_ascii=False) + "\n"
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
def a_bad_index_json():
|
|
330
|
-
print("== A5 P1-8:坏 _index.json 不得连坐好分片(旧实现整装载 Err)==")
|
|
331
|
-
t, root = _mkroot("badidx")
|
|
332
|
-
_write(os.path.join(root, "_index.json"), b'{"nodes": {BROKEN!!!')
|
|
333
|
-
_write(os.path.join(root, "_index_log", "2000-good.log"),
|
|
334
|
-
_shard("shard_node", "knowledge/shard_node.md", 2.0))
|
|
335
|
-
_node(os.path.join(root, "knowledge", "shard_node.md"), "shard_node",
|
|
336
|
-
"# 正文:zzshard354")
|
|
337
|
-
out, err = _serve(root, [{"op": "info"},
|
|
338
|
-
{"op": "search", "query": "zzshard354", "k": 5}])
|
|
339
|
-
check("A5 装载不因坏 _index.json 失败(进程存活且命中分片节点)",
|
|
340
|
-
[h.get("id") for h in _hits(out[1])] == ["shard_node"],
|
|
341
|
-
{"info": out[0], "hits": _hits(out[1]), "stderr": err.strip()[-200:]})
|
|
342
|
-
check("A5 降级须留痕(点名本源)", "降级跳过本源" in err and "_index.json" in err,
|
|
343
|
-
err.strip()[-200:])
|
|
344
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
def a_bad_shard_lines():
|
|
348
|
-
print("== A6 P1-8:坏分片内的合法行不得被连坐丢弃 ==")
|
|
349
|
-
t, root = _mkroot("badshard")
|
|
350
|
-
_index_json(root, {"idx_node": _entry("knowledge/idx_node.md")})
|
|
351
|
-
_node(os.path.join(root, "knowledge", "idx_node.md"), "idx_node", "# 正文:zzidx354")
|
|
352
|
-
logd = os.path.join(root, "_index_log")
|
|
353
|
-
good = _shard("n3", "knowledge/n3.md", 3.0)
|
|
354
|
-
_write(os.path.join(logd, "1000-mixed.log"),
|
|
355
|
-
good.encode("utf-8") + b'{"id":"bad",\xff\xfe"x":1}\n'
|
|
356
|
-
+ _shard("n4", "knowledge/n4.md", 4.0).encode("utf-8"))
|
|
357
|
-
_node(os.path.join(root, "knowledge", "n3.md"), "n3", "# 正文:zzthree354")
|
|
358
|
-
_node(os.path.join(root, "knowledge", "n4.md"), "n4", "# 正文:zzfour354")
|
|
359
|
-
out, err = _serve(root, [{"op": "info"},
|
|
360
|
-
{"op": "search", "query": "zzthree354", "k": 5},
|
|
361
|
-
{"op": "search", "query": "zzfour354", "k": 5}])
|
|
362
|
-
check("A6 坏行之前的合法行仍重放(n3)",
|
|
363
|
-
[h.get("id") for h in _hits(out[1])] == ["n3"], _hits(out[1]))
|
|
364
|
-
check("A6 坏行之后的合法行仍重放(n4——同一分片不连坐)",
|
|
365
|
-
[h.get("id") for h in _hits(out[2])] == ["n4"], _hits(out[2]))
|
|
366
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
def a_sources_merge():
|
|
370
|
-
print("== A7 P1-8 阴性面:两源俱好时合并语义不变(分片覆盖同 id)==")
|
|
371
|
-
t, root = _mkroot("merge")
|
|
372
|
-
_index_json(root, {"n1": _entry("knowledge/n1.md", imp=0.1)})
|
|
373
|
-
_node(os.path.join(root, "knowledge", "n1.md"), "n1", "# 正文:zzone354")
|
|
374
|
-
_write(os.path.join(root, "_index_log", "3000-up.log"),
|
|
375
|
-
_shard("n1", "knowledge/n1.md", 3.0))
|
|
376
|
-
out, err = _serve(root, [{"op": "info"},
|
|
377
|
-
{"op": "search", "query": "zzone354", "k": 5}])
|
|
378
|
-
check("A7 同 id 分片覆盖后仍单条命中(合并语义不变)",
|
|
379
|
-
[h.get("id") for h in _hits(out[1])] == ["n1"], _hits(out[1]))
|
|
380
|
-
check("A7 无坏件不得出现任何降级/越界告警(零噪声)",
|
|
381
|
-
"降级" not in err and "越界被拒" not in err, err.strip()[-160:])
|
|
382
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
# ============================ [B] 源断言 ============================
|
|
386
|
-
|
|
387
|
-
def _src():
|
|
388
|
-
return io.open(_STORE_RS, encoding="utf-8", errors="replace").read()
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
def b_source_face():
|
|
392
|
-
print("== B 源断言(行为面不可用时仍决定 rc)==")
|
|
393
|
-
src = _src()
|
|
394
|
-
check("B1 containment 闸在场(resolve_in_root)", "fn resolve_in_root(" in src)
|
|
395
|
-
check("B2 闸在 read_doc 内被调用(读盘之前)",
|
|
396
|
-
"resolve_in_root(root, croot, &e.path)" in src)
|
|
397
|
-
check("B3 物理层判据在场:canonicalize(不是只比字符串前缀)",
|
|
398
|
-
"std::fs::canonicalize(&cand)" in src and "starts_with(croot)" in src)
|
|
399
|
-
check("B4 词法层:拒绝对/根相对/盘符前缀与 .. 组件",
|
|
400
|
-
"Component::Prefix(_)" in src and "Component::RootDir" in src
|
|
401
|
-
and "Component::ParentDir" in src)
|
|
402
|
-
check("B5 越界分类与调用方处理(OutOfRoot 不读盘)",
|
|
403
|
-
"PathGate::OutOfRoot" in src and "DocLoad::OutOfRoot" in src)
|
|
404
|
-
check("B5b 物理层判据本体:包含性比较 + 过闸才放行",
|
|
405
|
-
"if ccand == *croot || ccand.starts_with(croot)" in src
|
|
406
|
-
and "PathGate::Allowed(cand)" in src)
|
|
407
|
-
# 两条臂**分别**断言:任一条被改回 ? 传播都要转红(V3 首版只钉了「存在」,
|
|
408
|
-
# 变异体走的是另一条臂 ⇒ 判据没打红——量具缺陷,已分列)
|
|
409
|
-
check("B6a P1-8:_index.json **读失败**臂降级跳过(不再 ? 掉整装载)",
|
|
410
|
-
"不可读,降级跳过本源" in src)
|
|
411
|
-
check("B6b P1-8:_index.json **解析失败**臂降级跳过(不再 ? 掉整装载)",
|
|
412
|
-
"解析失败,降级跳过本源" in src)
|
|
413
|
-
check("B7 P1-8:分片逐行容错解码(from_utf8 单行判)",
|
|
414
|
-
"std::str::from_utf8(chunk)" in src)
|
|
415
|
-
check("B8 反陈化:标注 issue #354 与本缺陷面",
|
|
416
|
-
"#354 P0-3" in src and "#354 P1-8" in src)
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
# ============================ 主流程 ============================
|
|
420
|
-
|
|
421
|
-
_BEHAVIOR = (a_relative, a_absolute, a_pathological, a_symlink_escape,
|
|
422
|
-
a_bad_index_json, a_bad_shard_lines, a_sources_merge)
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
def _run_groups(behavior_ok):
|
|
426
|
-
global PASS, FAIL
|
|
427
|
-
PASS = 0
|
|
428
|
-
FAIL = 0
|
|
429
|
-
del FAILS[:]
|
|
430
|
-
del SKIPS[:]
|
|
431
|
-
for g in _BEHAVIOR:
|
|
432
|
-
if not behavior_ok:
|
|
433
|
-
skip(g.__name__, "mdcg-eval 可执行件不可得(构建命令:%s)" % _BUILD_CMD)
|
|
434
|
-
continue
|
|
435
|
-
try:
|
|
436
|
-
g()
|
|
437
|
-
except Exception as exc: # noqa: BLE001
|
|
438
|
-
check("断言组 %s 抛异常:%s" % (g.__name__, exc), False)
|
|
439
|
-
import traceback
|
|
440
|
-
print(traceback.format_exc()[-700:])
|
|
441
|
-
b_source_face()
|
|
442
|
-
return len(FAILS)
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
def main():
|
|
446
|
-
behavior_ok = _ensure_exe()
|
|
447
|
-
if not behavior_ok:
|
|
448
|
-
print("!! 行为面 SKIP:%s 不在场且无法自建 —— 构建命令:%s"
|
|
449
|
-
% (EXE, _BUILD_CMD))
|
|
450
|
-
n = _run_groups(behavior_ok)
|
|
451
|
-
print()
|
|
452
|
-
print("issue354 Rust 索引面守卫(P0-3 containment / P1-8 坏件不连坐):"
|
|
453
|
-
"%d 通过,%d 失败,%d 跳过" % (PASS, n, len(SKIPS)))
|
|
454
|
-
return 1 if n else 0
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
# ============================ [C] 定点变异自证 ============================
|
|
458
|
-
|
|
459
|
-
_MUTATIONS = (
|
|
460
|
-
# 锚点用 ` 占位反引号(本文件构造时替换)——Rust 注释里的 markdown 反引号
|
|
461
|
-
# 必须逐字进锚点,否则 ANCHOR-MISS 会让变异自证静默失效。
|
|
462
|
-
("V1 拆掉物理层 containment(只留词法层)",
|
|
463
|
-
" let Ok(ccand) = std::fs::canonicalize(&cand) else {\n"
|
|
464
|
-
" return PathGate::Unresolvable;\n"
|
|
465
|
-
" };\n"
|
|
466
|
-
" // `starts_with` 按**组件**比较,故 `<root>/../lib2` 一类的兄弟前缀不会被误判;\n"
|
|
467
|
-
" // `ccand == croot` 单列是因为 `Path::starts_with(自身)` 恒真,但显式写出更清楚。\n"
|
|
468
|
-
" if ccand == *croot || ccand.starts_with(croot) {\n"
|
|
469
|
-
" PathGate::Allowed(cand)\n"
|
|
470
|
-
" } else {\n"
|
|
471
|
-
" PathGate::OutOfRoot\n"
|
|
472
|
-
" }\n",
|
|
473
|
-
" PathGate::Allowed(root.join(p))",
|
|
474
|
-
"B3", "B5b"),
|
|
475
|
-
("V2 坏行改判为整分片中断(回到坏行连坐)",
|
|
476
|
-
" let Ok(s) = std::str::from_utf8(chunk) else {\n"
|
|
477
|
-
" continue; // 非法 UTF-8 行:只作废本行(不连坐同分片其余行)\n"
|
|
478
|
-
" };\n",
|
|
479
|
-
" let Ok(s) = std::str::from_utf8(chunk) else {\n"
|
|
480
|
-
" break; // 变异:坏行连坐整分片\n"
|
|
481
|
-
" };\n",
|
|
482
|
-
"A6"),
|
|
483
|
-
("V3 _index.json 解析失败臂改回 ? 传播(回到整装载 Err)",
|
|
484
|
-
" Err(e) => eprintln!(\n"
|
|
485
|
-
" \"[store] _index.json 解析失败,降级跳过本源(issue #354 P1-8):{}:{e}\",\n"
|
|
486
|
-
" idx_json.display()\n"
|
|
487
|
-
" ),\n",
|
|
488
|
-
" Err(e) => return Err(format!(\"解析 _index.json 失败: {e}\")),\n",
|
|
489
|
-
"B6b", "A5"),
|
|
490
|
-
)
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
def _rebuild():
|
|
494
|
-
r = subprocess.run(["cargo", "build", "--release"], cwd=_RUST_DIR,
|
|
495
|
-
capture_output=True, text=True, encoding="utf-8",
|
|
496
|
-
errors="replace", timeout=900)
|
|
497
|
-
return r.returncode == 0, (r.stderr or "")[-400:]
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
def _mutate():
|
|
501
|
-
bad = []
|
|
502
|
-
if not os.path.isfile(_STORE_RS):
|
|
503
|
-
print(" SOURCE-MISSING:%s" % _STORE_RS)
|
|
504
|
-
return 1
|
|
505
|
-
if not shutil.which("cargo"):
|
|
506
|
-
print(" NO-CARGO:变异自证需要 cargo(改源后必须重建,否则跑的还是旧二进制)")
|
|
507
|
-
return 1
|
|
508
|
-
with io.open(_STORE_RS, encoding="utf-8", newline="") as f:
|
|
509
|
-
pristine = f.read()
|
|
510
|
-
global _SILENT
|
|
511
|
-
for name, anchor, repl, *want in _MUTATIONS:
|
|
512
|
-
with io.open(_STORE_RS, encoding="utf-8", newline="") as f:
|
|
513
|
-
orig = f.read()
|
|
514
|
-
NL = "\r\n" if "\r\n" in orig else "\n"
|
|
515
|
-
a_ = anchor.replace("\n", NL)
|
|
516
|
-
r_ = repl.replace("\n", NL)
|
|
517
|
-
if a_ not in orig:
|
|
518
|
-
print(" ANCHOR-MISS %s" % name)
|
|
519
|
-
bad.append(name)
|
|
520
|
-
continue
|
|
521
|
-
ok_build = False
|
|
522
|
-
try:
|
|
523
|
-
with io.open(_STORE_RS, "w", encoding="utf-8", newline="") as f:
|
|
524
|
-
f.write(orig.replace(a_, r_, 1))
|
|
525
|
-
ok_build, blog = _rebuild()
|
|
526
|
-
if not ok_build:
|
|
527
|
-
print(" BUILD-FAIL %s:%s" % (name, blog))
|
|
528
|
-
bad.append(name + "(构建失败)")
|
|
529
|
-
continue
|
|
530
|
-
_SILENT = True
|
|
531
|
-
del _VIOL[:]
|
|
532
|
-
_run_groups(True)
|
|
533
|
-
reds = list(_VIOL)
|
|
534
|
-
hit = [w for w in want if any(x.startswith(w) for x in reds)]
|
|
535
|
-
print(" %-4s %-46s 红项=%-3d 命中=%s"
|
|
536
|
-
% ("OK" if len(hit) == len(want) else "BAD", name, len(reds),
|
|
537
|
-
",".join(hit) if hit else "无"))
|
|
538
|
-
if len(hit) != len(want):
|
|
539
|
-
print(" 期望红:%s;实红:%s" % (list(want), reds[:8]))
|
|
540
|
-
bad.append(name)
|
|
541
|
-
finally:
|
|
542
|
-
_SILENT = False
|
|
543
|
-
with io.open(_STORE_RS, "w", encoding="utf-8", newline="") as f:
|
|
544
|
-
f.write(pristine)
|
|
545
|
-
with io.open(_STORE_RS, encoding="utf-8", newline="") as f:
|
|
546
|
-
if f.read() != pristine:
|
|
547
|
-
print(" RESTORE-FAILED %s" % name)
|
|
548
|
-
bad.append(name + "(恢复失败)")
|
|
549
|
-
_rebuild()
|
|
550
|
-
if bad:
|
|
551
|
-
print()
|
|
552
|
-
print("定点变异自证:FAIL -- %s" % "、".join(bad))
|
|
553
|
-
return 1
|
|
554
|
-
print()
|
|
555
|
-
print("定点变异自证:PASS(每处判据都被打红)")
|
|
556
|
-
return 0
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
def _cleanup():
|
|
560
|
-
for t in _SANDBOXES:
|
|
561
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
if __name__ == "__main__":
|
|
565
|
-
if "--mutate" in sys.argv:
|
|
566
|
-
rc = _mutate()
|
|
567
|
-
_cleanup()
|
|
568
|
-
sys.exit(rc)
|
|
569
|
-
rc = main()
|
|
570
|
-
_cleanup()
|
|
571
|
-
sys.exit(rc)
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
r"""#354 P0-3 / P1-8 守卫(Rust 检索面):_index.json path 物理 containment 与
|
|
3
|
+
索引装载「坏件不连坐」。
|
|
4
|
+
|
|
5
|
+
缺陷(本件修复,2026-10-14)
|
|
6
|
+
----------------------------
|
|
7
|
+
【P0-3】rust/src/store.rs 的条目装载直接 root.join(path) 读盘,**零 containment**:
|
|
8
|
+
· 绝对路径(D:\x / \x / \\server\share)—— std 的 Path::join 遇绝对路径**整体
|
|
9
|
+
替换** root;
|
|
10
|
+
· 相对穿越(../x、a/../../x)。
|
|
11
|
+
实测(修前,真二进制 mdcg-eval --serve):_index.json 里塞 ../sentinel.txt 与
|
|
12
|
+
绝对路径 → info.docs=3(哨兵被当节点正文读入)、查询 zzsentinel 直接命中
|
|
13
|
+
id=哨兵节点 —— 库根之外的**内容回显**(哨兵泄漏)。
|
|
14
|
+
【P1-8】装载面「一个坏件连坐整个装载」:
|
|
15
|
+
· 坏 _index.json(读到/解析失败)→ 旧实现两处 ? 直接让整个装载 Err,进程非零
|
|
16
|
+
退出,合法 _index_log 分片里的节点**全不可检索**;
|
|
17
|
+
· 坏分片(非法 UTF-8 字节,撕裂写残字节)→ 旧实现 read_to_string 整片全有全无,
|
|
18
|
+
整分片被 continue 丢弃,**其内的合法行连同账**(同分片连坐)。
|
|
19
|
+
|
|
20
|
+
本守卫三层
|
|
21
|
+
----------
|
|
22
|
+
[A] 行为面(真二进制 mdcg-eval --serve,端到端):
|
|
23
|
+
A1-A3 穿越形态逐类(相对 ../、绝对、混合分隔符/空串/NUL/盘符相对)不得命中
|
|
24
|
+
哨兵、不得计入 docs、越界条目须有 stderr 留痕;
|
|
25
|
+
A4 零误伤:合法路径照常可检索(修 containment 不得把正常库读坏);
|
|
26
|
+
A5-A7 P1-8:坏 _index.json + 好分片 → 装载不连坐;坏分片内合法行仍重放;
|
|
27
|
+
两源皆好 → 合并语义(同 id 分片覆盖)不变。
|
|
28
|
+
[B] 源断言(结构性,行为面不可用时仍决定 rc):闸在场、闸在**读盘之前**被调用、
|
|
29
|
+
判据含 canonicalize 物理层(不是只比字符串前缀)、临时/坏件路径有留痕。
|
|
30
|
+
[C] 定点变异:拆掉物理层 / 拆掉逐行容错 → 判据必须转红(改源 → cargo build →
|
|
31
|
+
跑守卫转红 → 还原 → 重建),绝不在真实工作树留下未还原改动。
|
|
32
|
+
|
|
33
|
+
运行:
|
|
34
|
+
python -X utf8 -m md_cg.test_issue354_rust_index_containment
|
|
35
|
+
python -X utf8 -m md_cg.test_issue354_rust_index_containment --mutate
|
|
36
|
+
退出码:0 全绿 / 1 有失败。
|
|
37
|
+
环境:MDCG_EVAL_EXE 可指向别处的 mdcg-eval 可执行文件。
|
|
38
|
+
诚实边界:行为面需要 rust/target/release 下的可执行件(gitignored)——本守卫按仓内
|
|
39
|
+
既有口径**自建**(cargo build --release);cargo 与产物俱不可得时行为面显式 SKIP
|
|
40
|
+
并打印构建命令(源断言面 [B] 照判、照决定 rc),不把「没测到」记成绿。
|
|
41
|
+
"""
|
|
42
|
+
from __future__ import annotations
|
|
43
|
+
|
|
44
|
+
import io
|
|
45
|
+
import json
|
|
46
|
+
import os
|
|
47
|
+
import shutil
|
|
48
|
+
import subprocess
|
|
49
|
+
import sys
|
|
50
|
+
import tempfile
|
|
51
|
+
|
|
52
|
+
try:
|
|
53
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
54
|
+
except AttributeError:
|
|
55
|
+
pass
|
|
56
|
+
|
|
57
|
+
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
58
|
+
_RUST_DIR = os.path.join(_REPO, "rust")
|
|
59
|
+
_STORE_RS = os.path.join(_RUST_DIR, "src", "store.rs")
|
|
60
|
+
|
|
61
|
+
PASS = 0
|
|
62
|
+
FAIL = 0
|
|
63
|
+
FAILS = []
|
|
64
|
+
SKIPS = []
|
|
65
|
+
_SANDBOXES = []
|
|
66
|
+
#: 变异自证用:_SILENT 为真时 check() 只把失败项记进 _VIOL(不打印、不计入读数)。
|
|
67
|
+
_SILENT = False
|
|
68
|
+
_VIOL = []
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def check(name, cond, detail=""):
|
|
72
|
+
global PASS, FAIL
|
|
73
|
+
if _SILENT:
|
|
74
|
+
if not cond:
|
|
75
|
+
_VIOL.append(name)
|
|
76
|
+
return bool(cond)
|
|
77
|
+
if cond:
|
|
78
|
+
PASS += 1
|
|
79
|
+
print(" [PASS] %s%s" % (name, (" · " + str(detail)) if detail else ""))
|
|
80
|
+
else:
|
|
81
|
+
FAIL += 1
|
|
82
|
+
FAILS.append(name)
|
|
83
|
+
print(" [FAIL] %s · %s" % (name, detail))
|
|
84
|
+
return bool(cond)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def skip(name, why):
|
|
88
|
+
SKIPS.append(name)
|
|
89
|
+
print(" [SKIP] %s · %s" % (name, why))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _exe_path():
|
|
93
|
+
env = os.environ.get("MDCG_EVAL_EXE")
|
|
94
|
+
if env:
|
|
95
|
+
return env
|
|
96
|
+
base = os.path.join(_RUST_DIR, "target", "release", "mdcg-eval")
|
|
97
|
+
return base + ".exe" if os.name == "nt" else base
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
EXE = _exe_path()
|
|
101
|
+
_BUILD_CMD = "cargo build --release (在 %s 下执行)" % _RUST_DIR
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _ensure_exe():
|
|
105
|
+
"""确保 mdcg-eval 在场;返回是否可用(不可用则行为面 SKIP,源断言面照判)。"""
|
|
106
|
+
if os.path.isfile(EXE):
|
|
107
|
+
return True
|
|
108
|
+
if not shutil.which("cargo") or not os.path.isdir(_RUST_DIR):
|
|
109
|
+
print(" (%s 不在场且 cargo 不可用)" % EXE)
|
|
110
|
+
return False
|
|
111
|
+
print(" 自建:%s" % _BUILD_CMD)
|
|
112
|
+
r = subprocess.run(["cargo", "build", "--release"], cwd=_RUST_DIR,
|
|
113
|
+
capture_output=True, text=True, encoding="utf-8",
|
|
114
|
+
errors="replace", timeout=900)
|
|
115
|
+
if r.returncode != 0:
|
|
116
|
+
print(" 构建失败:%s" % (r.stderr or "")[-400:])
|
|
117
|
+
return False
|
|
118
|
+
return os.path.isfile(EXE)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _mkroot(tag):
|
|
122
|
+
t = tempfile.mkdtemp(prefix="i354idx_%s_" % tag)
|
|
123
|
+
_SANDBOXES.append(t)
|
|
124
|
+
root = os.path.join(t, "lib")
|
|
125
|
+
os.makedirs(os.path.join(root, "knowledge"))
|
|
126
|
+
return t, root
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _write(path, text):
|
|
130
|
+
"""写文本或字节(bytes 走二进制口径——坏 UTF-8 分支用)。"""
|
|
131
|
+
d = os.path.dirname(path)
|
|
132
|
+
if d and not os.path.isdir(d):
|
|
133
|
+
os.makedirs(d)
|
|
134
|
+
if isinstance(text, (bytes, bytearray)):
|
|
135
|
+
io.open(path, "wb").write(text)
|
|
136
|
+
else:
|
|
137
|
+
io.open(path, "w", encoding="utf-8", newline="").write(text)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _node(path, nid, body):
|
|
141
|
+
_write(path, "---\nid: \"%s\"\ntags: []\nimportance: 0.5\n---\n%s\n" % (nid, body))
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _index_json(root, nodes):
|
|
145
|
+
_write(os.path.join(root, "_index.json"),
|
|
146
|
+
json.dumps({"nodes": nodes}, ensure_ascii=False))
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _entry(path, layer="knowledge", imp=0.5):
|
|
150
|
+
return {"path": path, "layer": layer, "tags": [], "importance": imp, "edges": []}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _serve(root, reqs):
|
|
154
|
+
"""拉起 mdcg-eval --serve,逐条请求取响应;返回 (响应列表, stderr 文本)。"""
|
|
155
|
+
env = dict(os.environ)
|
|
156
|
+
env.pop("MDCG_TOKEN", None)
|
|
157
|
+
p = subprocess.Popen([EXE, "--root", root, "--serve"],
|
|
158
|
+
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
|
159
|
+
stderr=subprocess.PIPE, text=True, encoding="utf-8",
|
|
160
|
+
errors="replace", env=env)
|
|
161
|
+
out = []
|
|
162
|
+
try:
|
|
163
|
+
for r in reqs:
|
|
164
|
+
try:
|
|
165
|
+
p.stdin.write(json.dumps(r) + "\n")
|
|
166
|
+
p.stdin.flush()
|
|
167
|
+
except (BrokenPipeError, OSError):
|
|
168
|
+
# 子进程已死(如装载失败 exit 1):静默截断请求序列,剩下的槽位
|
|
169
|
+
# 补 None——判据看的是**读数**,不该被调用方的管道异常顶掉
|
|
170
|
+
out.append(None)
|
|
171
|
+
continue
|
|
172
|
+
line = p.stdout.readline()
|
|
173
|
+
out.append(json.loads(line) if line.strip() else None)
|
|
174
|
+
finally:
|
|
175
|
+
try:
|
|
176
|
+
p.stdin.close()
|
|
177
|
+
except Exception: # noqa: BLE001
|
|
178
|
+
pass
|
|
179
|
+
try:
|
|
180
|
+
p.wait(timeout=20)
|
|
181
|
+
except Exception: # noqa: BLE001
|
|
182
|
+
p.kill()
|
|
183
|
+
err = p.stderr.read() if p.stderr else ""
|
|
184
|
+
return out, err
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _hits(resp):
|
|
188
|
+
return (resp or {}).get("hits") or []
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
# ============================ [A] 行为面:P0-3 ============================
|
|
192
|
+
|
|
193
|
+
TOKEN = "zzsentinel354"
|
|
194
|
+
SENT_ID = "哨兵节点354"
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
#: 探针查询:越界条目在场 / 不在场两种库都必须给出**逐位一致**的读数。
|
|
198
|
+
_PROBES = (TOKEN, "SENTINEL-LEAK-354", "zznormal354")
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _readings(root):
|
|
202
|
+
"""跑一组固定请求,把可观测面带成可比较的元组:((docs, 候选), [(id,path,score)...]...)。
|
|
203
|
+
|
|
204
|
+
为什么用「有/无越界条目的两库读数对拍」而不是「查询必须零命中」:本引擎在词法
|
|
205
|
+
全空时会走 LIKE 兜底池(合法节点照样返回),故「零命中」不是可用判据;而
|
|
206
|
+
「越界条目不得改变任何读数」是自校准的强判据——修前哨兵被当正文读入时,其
|
|
207
|
+
importance=0.99 + 正文命中会直接改写 top-1(实测 hits=[哨兵节点354])。
|
|
208
|
+
"""
|
|
209
|
+
out, err = _serve(root, [{"op": "info"}]
|
|
210
|
+
+ [{"op": "search", "query": q, "k": 10} for q in _PROBES])
|
|
211
|
+
info = out[0] or {}
|
|
212
|
+
rows = []
|
|
213
|
+
for r in out[1:]:
|
|
214
|
+
rows.append(sorted((h.get("id"), h.get("path"), h.get("score"))
|
|
215
|
+
for h in _hits(r)))
|
|
216
|
+
return (info.get("docs"), info.get("candidates")), rows, err
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _traversal_case(name, evil_paths, tag="trav"):
|
|
220
|
+
t = tempfile.mkdtemp(prefix="i354idx_%s_" % tag)
|
|
221
|
+
_SANDBOXES.append(t)
|
|
222
|
+
sentinel = os.path.join(t, "sentinel.txt")
|
|
223
|
+
_node(sentinel, SENT_ID, "# 正文:%s 沙箱外哨兵内容 SENTINEL-LEAK-354" % TOKEN)
|
|
224
|
+
root_evil = os.path.join(t, "lib_evil")
|
|
225
|
+
root_ctrl = os.path.join(t, "lib_ctrl")
|
|
226
|
+
for root in (root_evil, root_ctrl):
|
|
227
|
+
os.makedirs(os.path.join(root, "knowledge"))
|
|
228
|
+
_node(os.path.join(root, "knowledge", "good.md"), "good",
|
|
229
|
+
"# 正文:正常内容 zznormal354")
|
|
230
|
+
nodes = {"good": _entry("knowledge/good.md")}
|
|
231
|
+
for i, p in enumerate(evil_paths):
|
|
232
|
+
# ABS 标记:换成真实存在的库外哨兵绝对路径(绝对路径条目在**修前**正是
|
|
233
|
+
# 因为 Path::join 整体替换 root 而读到它;修后须在词法层即被拒)
|
|
234
|
+
nodes["evil%d" % i] = _entry(sentinel if p == "ABS" else p, imp=0.99)
|
|
235
|
+
_index_json(root_evil, nodes)
|
|
236
|
+
_index_json(root_ctrl, {"good": _entry("knowledge/good.md")})
|
|
237
|
+
head_e, rows_e, err_e = _readings(root_evil)
|
|
238
|
+
head_c, rows_c, _err_c = _readings(root_ctrl)
|
|
239
|
+
flat = [x for r in rows_e for x in r]
|
|
240
|
+
check("%s:哨兵节点名不得出现在任何命中里(泄漏面)" % name,
|
|
241
|
+
SENT_ID not in [x[0] for x in flat], flat[:4])
|
|
242
|
+
check("%s:越界条目在场与不在场,检索读数必须逐位一致" % name,
|
|
243
|
+
rows_e == rows_c, {"with": rows_e[:2], "without": rows_c[:2]})
|
|
244
|
+
# 注意:候选数(candidates)本就含被拒条目(它们是索引里的真实条目,只是
|
|
245
|
+
# 读不出正文),故这面只钉**已装载文档数**——泄漏面正是「越界文件被当正文读入」。
|
|
246
|
+
check("%s:已装载文档数不得因越界条目改变" % name,
|
|
247
|
+
head_e[0] == head_c[0], {"with": head_e, "without": head_c})
|
|
248
|
+
check("%s:越界条目须有 stderr 留痕(可诊断,不是静默丢弃)" % name,
|
|
249
|
+
"越界被拒" in err_e and "P0-3" in err_e, err_e.strip()[-160:])
|
|
250
|
+
check("%s:合法节点照常可检索(零误伤)" % name,
|
|
251
|
+
[h.get("id") for h in _hits(_serve(root_ctrl, [{"op": "search",
|
|
252
|
+
"query": "zznormal354",
|
|
253
|
+
"k": 10}])[0][0])] == ["good"],
|
|
254
|
+
rows_e[-1] if rows_e else None)
|
|
255
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def a_relative():
|
|
259
|
+
print("== A1 P0-3:相对穿越(../ 与 a/../..)==")
|
|
260
|
+
_traversal_case("A1", ["../sentinel.txt", "knowledge/../../sentinel.txt",
|
|
261
|
+
"..\\sentinel.txt"])
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def a_absolute():
|
|
265
|
+
print("== A2 P0-3:绝对路径 / 根相对 / UNC ==")
|
|
266
|
+
_traversal_case("A2", ["ABS", os.sep + "definitely-not-here.txt",
|
|
267
|
+
"\\\\server\\share\\x.md"], tag="abs")
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def a_pathological():
|
|
271
|
+
print("== A3 P0-3:病理 path(空串 / NUL / 盘符相对 / 库内目录)不得崩不得读 ==")
|
|
272
|
+
t, root = _mkroot("patho")
|
|
273
|
+
nodes = {"good": _entry("knowledge/good.md"),
|
|
274
|
+
"empty": _entry(""),
|
|
275
|
+
"nul": _entry("knowledge/" + chr(0) + "x.md"),
|
|
276
|
+
"drvrel": _entry("C:sentinel.txt"),
|
|
277
|
+
"is_dir": _entry("knowledge")}
|
|
278
|
+
_index_json(root, nodes)
|
|
279
|
+
_node(os.path.join(root, "knowledge", "good.md"), "good", "# 正文:zznormal354")
|
|
280
|
+
out, err = _serve(root, [{"op": "info"},
|
|
281
|
+
{"op": "search", "query": "zznormal354", "k": 10},
|
|
282
|
+
{"op": "search", "query": TOKEN, "k": 10}])
|
|
283
|
+
check("A3 病态 path 不致进程崩溃(info 仍应答)",
|
|
284
|
+
(out[0] or {}).get("ok") is True, out[0])
|
|
285
|
+
check("A3 库内目录条目(knowledge)不得被当节点读入",
|
|
286
|
+
[h.get("id") for h in _hits(out[1])] == ["good"],
|
|
287
|
+
[h.get("id") for h in _hits(out[1])])
|
|
288
|
+
check("A3 病态 path 不泄漏哨兵",
|
|
289
|
+
SENT_ID not in [h.get("id") for h in _hits(out[2])], _hits(out[2]))
|
|
290
|
+
check("A3 病态 path 亦须留痕(词法层即拒)",
|
|
291
|
+
"越界被拒" in err, err.strip()[-160:])
|
|
292
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def a_symlink_escape():
|
|
296
|
+
print("== A4 P0-3:符号链接指向库外(物理层判据的判别面)==")
|
|
297
|
+
t, root = _mkroot("link")
|
|
298
|
+
outside = os.path.join(t, "outside")
|
|
299
|
+
os.makedirs(outside)
|
|
300
|
+
_node(os.path.join(outside, "secret.md"), SENT_ID,
|
|
301
|
+
"# 正文:%s SENTINEL-LEAK-354" % TOKEN)
|
|
302
|
+
link = os.path.join(root, "knowledge", "link.md")
|
|
303
|
+
made = False
|
|
304
|
+
try:
|
|
305
|
+
os.symlink(os.path.join(outside, "secret.md"), link)
|
|
306
|
+
made = True
|
|
307
|
+
except (OSError, NotImplementedError) as e: # noqa: BLE001
|
|
308
|
+
skip("A4 符号链接穿越", "本机无法创建符号链接(需开发者模式/权限):%s" % e)
|
|
309
|
+
if made:
|
|
310
|
+
_index_json(root, {"evil_link": _entry("knowledge/link.md", imp=0.99),
|
|
311
|
+
"good": _entry("knowledge/good.md")})
|
|
312
|
+
_node(os.path.join(root, "knowledge", "good.md"), "good",
|
|
313
|
+
"# 正文:zznormal354")
|
|
314
|
+
out, err = _serve(root, [{"op": "info"},
|
|
315
|
+
{"op": "search", "query": TOKEN, "k": 10}])
|
|
316
|
+
check("A4 符号链接指向库外不得被读(词法看不出,物理层才拦得住)",
|
|
317
|
+
SENT_ID not in [h.get("id") for h in _hits(out[1])], _hits(out[1]))
|
|
318
|
+
check("A4 越界留痕", "越界被拒" in err, err.strip()[-160:])
|
|
319
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
# ============================ [A] 行为面:P1-8 ============================
|
|
323
|
+
|
|
324
|
+
def _shard(nid, path, t):
|
|
325
|
+
return json.dumps({"id": nid, "_t": t, "_s": 1,
|
|
326
|
+
"e": _entry(path)}, ensure_ascii=False) + "\n"
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def a_bad_index_json():
|
|
330
|
+
print("== A5 P1-8:坏 _index.json 不得连坐好分片(旧实现整装载 Err)==")
|
|
331
|
+
t, root = _mkroot("badidx")
|
|
332
|
+
_write(os.path.join(root, "_index.json"), b'{"nodes": {BROKEN!!!')
|
|
333
|
+
_write(os.path.join(root, "_index_log", "2000-good.log"),
|
|
334
|
+
_shard("shard_node", "knowledge/shard_node.md", 2.0))
|
|
335
|
+
_node(os.path.join(root, "knowledge", "shard_node.md"), "shard_node",
|
|
336
|
+
"# 正文:zzshard354")
|
|
337
|
+
out, err = _serve(root, [{"op": "info"},
|
|
338
|
+
{"op": "search", "query": "zzshard354", "k": 5}])
|
|
339
|
+
check("A5 装载不因坏 _index.json 失败(进程存活且命中分片节点)",
|
|
340
|
+
[h.get("id") for h in _hits(out[1])] == ["shard_node"],
|
|
341
|
+
{"info": out[0], "hits": _hits(out[1]), "stderr": err.strip()[-200:]})
|
|
342
|
+
check("A5 降级须留痕(点名本源)", "降级跳过本源" in err and "_index.json" in err,
|
|
343
|
+
err.strip()[-200:])
|
|
344
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def a_bad_shard_lines():
|
|
348
|
+
print("== A6 P1-8:坏分片内的合法行不得被连坐丢弃 ==")
|
|
349
|
+
t, root = _mkroot("badshard")
|
|
350
|
+
_index_json(root, {"idx_node": _entry("knowledge/idx_node.md")})
|
|
351
|
+
_node(os.path.join(root, "knowledge", "idx_node.md"), "idx_node", "# 正文:zzidx354")
|
|
352
|
+
logd = os.path.join(root, "_index_log")
|
|
353
|
+
good = _shard("n3", "knowledge/n3.md", 3.0)
|
|
354
|
+
_write(os.path.join(logd, "1000-mixed.log"),
|
|
355
|
+
good.encode("utf-8") + b'{"id":"bad",\xff\xfe"x":1}\n'
|
|
356
|
+
+ _shard("n4", "knowledge/n4.md", 4.0).encode("utf-8"))
|
|
357
|
+
_node(os.path.join(root, "knowledge", "n3.md"), "n3", "# 正文:zzthree354")
|
|
358
|
+
_node(os.path.join(root, "knowledge", "n4.md"), "n4", "# 正文:zzfour354")
|
|
359
|
+
out, err = _serve(root, [{"op": "info"},
|
|
360
|
+
{"op": "search", "query": "zzthree354", "k": 5},
|
|
361
|
+
{"op": "search", "query": "zzfour354", "k": 5}])
|
|
362
|
+
check("A6 坏行之前的合法行仍重放(n3)",
|
|
363
|
+
[h.get("id") for h in _hits(out[1])] == ["n3"], _hits(out[1]))
|
|
364
|
+
check("A6 坏行之后的合法行仍重放(n4——同一分片不连坐)",
|
|
365
|
+
[h.get("id") for h in _hits(out[2])] == ["n4"], _hits(out[2]))
|
|
366
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def a_sources_merge():
|
|
370
|
+
print("== A7 P1-8 阴性面:两源俱好时合并语义不变(分片覆盖同 id)==")
|
|
371
|
+
t, root = _mkroot("merge")
|
|
372
|
+
_index_json(root, {"n1": _entry("knowledge/n1.md", imp=0.1)})
|
|
373
|
+
_node(os.path.join(root, "knowledge", "n1.md"), "n1", "# 正文:zzone354")
|
|
374
|
+
_write(os.path.join(root, "_index_log", "3000-up.log"),
|
|
375
|
+
_shard("n1", "knowledge/n1.md", 3.0))
|
|
376
|
+
out, err = _serve(root, [{"op": "info"},
|
|
377
|
+
{"op": "search", "query": "zzone354", "k": 5}])
|
|
378
|
+
check("A7 同 id 分片覆盖后仍单条命中(合并语义不变)",
|
|
379
|
+
[h.get("id") for h in _hits(out[1])] == ["n1"], _hits(out[1]))
|
|
380
|
+
check("A7 无坏件不得出现任何降级/越界告警(零噪声)",
|
|
381
|
+
"降级" not in err and "越界被拒" not in err, err.strip()[-160:])
|
|
382
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
# ============================ [B] 源断言 ============================
|
|
386
|
+
|
|
387
|
+
def _src():
|
|
388
|
+
return io.open(_STORE_RS, encoding="utf-8", errors="replace").read()
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
def b_source_face():
|
|
392
|
+
print("== B 源断言(行为面不可用时仍决定 rc)==")
|
|
393
|
+
src = _src()
|
|
394
|
+
check("B1 containment 闸在场(resolve_in_root)", "fn resolve_in_root(" in src)
|
|
395
|
+
check("B2 闸在 read_doc 内被调用(读盘之前)",
|
|
396
|
+
"resolve_in_root(root, croot, &e.path)" in src)
|
|
397
|
+
check("B3 物理层判据在场:canonicalize(不是只比字符串前缀)",
|
|
398
|
+
"std::fs::canonicalize(&cand)" in src and "starts_with(croot)" in src)
|
|
399
|
+
check("B4 词法层:拒绝对/根相对/盘符前缀与 .. 组件",
|
|
400
|
+
"Component::Prefix(_)" in src and "Component::RootDir" in src
|
|
401
|
+
and "Component::ParentDir" in src)
|
|
402
|
+
check("B5 越界分类与调用方处理(OutOfRoot 不读盘)",
|
|
403
|
+
"PathGate::OutOfRoot" in src and "DocLoad::OutOfRoot" in src)
|
|
404
|
+
check("B5b 物理层判据本体:包含性比较 + 过闸才放行",
|
|
405
|
+
"if ccand == *croot || ccand.starts_with(croot)" in src
|
|
406
|
+
and "PathGate::Allowed(cand)" in src)
|
|
407
|
+
# 两条臂**分别**断言:任一条被改回 ? 传播都要转红(V3 首版只钉了「存在」,
|
|
408
|
+
# 变异体走的是另一条臂 ⇒ 判据没打红——量具缺陷,已分列)
|
|
409
|
+
check("B6a P1-8:_index.json **读失败**臂降级跳过(不再 ? 掉整装载)",
|
|
410
|
+
"不可读,降级跳过本源" in src)
|
|
411
|
+
check("B6b P1-8:_index.json **解析失败**臂降级跳过(不再 ? 掉整装载)",
|
|
412
|
+
"解析失败,降级跳过本源" in src)
|
|
413
|
+
check("B7 P1-8:分片逐行容错解码(from_utf8 单行判)",
|
|
414
|
+
"std::str::from_utf8(chunk)" in src)
|
|
415
|
+
check("B8 反陈化:标注 issue #354 与本缺陷面",
|
|
416
|
+
"#354 P0-3" in src and "#354 P1-8" in src)
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
# ============================ 主流程 ============================
|
|
420
|
+
|
|
421
|
+
_BEHAVIOR = (a_relative, a_absolute, a_pathological, a_symlink_escape,
|
|
422
|
+
a_bad_index_json, a_bad_shard_lines, a_sources_merge)
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
def _run_groups(behavior_ok):
|
|
426
|
+
global PASS, FAIL
|
|
427
|
+
PASS = 0
|
|
428
|
+
FAIL = 0
|
|
429
|
+
del FAILS[:]
|
|
430
|
+
del SKIPS[:]
|
|
431
|
+
for g in _BEHAVIOR:
|
|
432
|
+
if not behavior_ok:
|
|
433
|
+
skip(g.__name__, "mdcg-eval 可执行件不可得(构建命令:%s)" % _BUILD_CMD)
|
|
434
|
+
continue
|
|
435
|
+
try:
|
|
436
|
+
g()
|
|
437
|
+
except Exception as exc: # noqa: BLE001
|
|
438
|
+
check("断言组 %s 抛异常:%s" % (g.__name__, exc), False)
|
|
439
|
+
import traceback
|
|
440
|
+
print(traceback.format_exc()[-700:])
|
|
441
|
+
b_source_face()
|
|
442
|
+
return len(FAILS)
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def main():
|
|
446
|
+
behavior_ok = _ensure_exe()
|
|
447
|
+
if not behavior_ok:
|
|
448
|
+
print("!! 行为面 SKIP:%s 不在场且无法自建 —— 构建命令:%s"
|
|
449
|
+
% (EXE, _BUILD_CMD))
|
|
450
|
+
n = _run_groups(behavior_ok)
|
|
451
|
+
print()
|
|
452
|
+
print("issue354 Rust 索引面守卫(P0-3 containment / P1-8 坏件不连坐):"
|
|
453
|
+
"%d 通过,%d 失败,%d 跳过" % (PASS, n, len(SKIPS)))
|
|
454
|
+
return 1 if n else 0
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
# ============================ [C] 定点变异自证 ============================
|
|
458
|
+
|
|
459
|
+
_MUTATIONS = (
|
|
460
|
+
# 锚点用 ` 占位反引号(本文件构造时替换)——Rust 注释里的 markdown 反引号
|
|
461
|
+
# 必须逐字进锚点,否则 ANCHOR-MISS 会让变异自证静默失效。
|
|
462
|
+
("V1 拆掉物理层 containment(只留词法层)",
|
|
463
|
+
" let Ok(ccand) = std::fs::canonicalize(&cand) else {\n"
|
|
464
|
+
" return PathGate::Unresolvable;\n"
|
|
465
|
+
" };\n"
|
|
466
|
+
" // `starts_with` 按**组件**比较,故 `<root>/../lib2` 一类的兄弟前缀不会被误判;\n"
|
|
467
|
+
" // `ccand == croot` 单列是因为 `Path::starts_with(自身)` 恒真,但显式写出更清楚。\n"
|
|
468
|
+
" if ccand == *croot || ccand.starts_with(croot) {\n"
|
|
469
|
+
" PathGate::Allowed(cand)\n"
|
|
470
|
+
" } else {\n"
|
|
471
|
+
" PathGate::OutOfRoot\n"
|
|
472
|
+
" }\n",
|
|
473
|
+
" PathGate::Allowed(root.join(p))",
|
|
474
|
+
"B3", "B5b"),
|
|
475
|
+
("V2 坏行改判为整分片中断(回到坏行连坐)",
|
|
476
|
+
" let Ok(s) = std::str::from_utf8(chunk) else {\n"
|
|
477
|
+
" continue; // 非法 UTF-8 行:只作废本行(不连坐同分片其余行)\n"
|
|
478
|
+
" };\n",
|
|
479
|
+
" let Ok(s) = std::str::from_utf8(chunk) else {\n"
|
|
480
|
+
" break; // 变异:坏行连坐整分片\n"
|
|
481
|
+
" };\n",
|
|
482
|
+
"A6"),
|
|
483
|
+
("V3 _index.json 解析失败臂改回 ? 传播(回到整装载 Err)",
|
|
484
|
+
" Err(e) => eprintln!(\n"
|
|
485
|
+
" \"[store] _index.json 解析失败,降级跳过本源(issue #354 P1-8):{}:{e}\",\n"
|
|
486
|
+
" idx_json.display()\n"
|
|
487
|
+
" ),\n",
|
|
488
|
+
" Err(e) => return Err(format!(\"解析 _index.json 失败: {e}\")),\n",
|
|
489
|
+
"B6b", "A5"),
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
def _rebuild():
|
|
494
|
+
r = subprocess.run(["cargo", "build", "--release"], cwd=_RUST_DIR,
|
|
495
|
+
capture_output=True, text=True, encoding="utf-8",
|
|
496
|
+
errors="replace", timeout=900)
|
|
497
|
+
return r.returncode == 0, (r.stderr or "")[-400:]
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
def _mutate():
|
|
501
|
+
bad = []
|
|
502
|
+
if not os.path.isfile(_STORE_RS):
|
|
503
|
+
print(" SOURCE-MISSING:%s" % _STORE_RS)
|
|
504
|
+
return 1
|
|
505
|
+
if not shutil.which("cargo"):
|
|
506
|
+
print(" NO-CARGO:变异自证需要 cargo(改源后必须重建,否则跑的还是旧二进制)")
|
|
507
|
+
return 1
|
|
508
|
+
with io.open(_STORE_RS, encoding="utf-8", newline="") as f:
|
|
509
|
+
pristine = f.read()
|
|
510
|
+
global _SILENT
|
|
511
|
+
for name, anchor, repl, *want in _MUTATIONS:
|
|
512
|
+
with io.open(_STORE_RS, encoding="utf-8", newline="") as f:
|
|
513
|
+
orig = f.read()
|
|
514
|
+
NL = "\r\n" if "\r\n" in orig else "\n"
|
|
515
|
+
a_ = anchor.replace("\n", NL)
|
|
516
|
+
r_ = repl.replace("\n", NL)
|
|
517
|
+
if a_ not in orig:
|
|
518
|
+
print(" ANCHOR-MISS %s" % name)
|
|
519
|
+
bad.append(name)
|
|
520
|
+
continue
|
|
521
|
+
ok_build = False
|
|
522
|
+
try:
|
|
523
|
+
with io.open(_STORE_RS, "w", encoding="utf-8", newline="") as f:
|
|
524
|
+
f.write(orig.replace(a_, r_, 1))
|
|
525
|
+
ok_build, blog = _rebuild()
|
|
526
|
+
if not ok_build:
|
|
527
|
+
print(" BUILD-FAIL %s:%s" % (name, blog))
|
|
528
|
+
bad.append(name + "(构建失败)")
|
|
529
|
+
continue
|
|
530
|
+
_SILENT = True
|
|
531
|
+
del _VIOL[:]
|
|
532
|
+
_run_groups(True)
|
|
533
|
+
reds = list(_VIOL)
|
|
534
|
+
hit = [w for w in want if any(x.startswith(w) for x in reds)]
|
|
535
|
+
print(" %-4s %-46s 红项=%-3d 命中=%s"
|
|
536
|
+
% ("OK" if len(hit) == len(want) else "BAD", name, len(reds),
|
|
537
|
+
",".join(hit) if hit else "无"))
|
|
538
|
+
if len(hit) != len(want):
|
|
539
|
+
print(" 期望红:%s;实红:%s" % (list(want), reds[:8]))
|
|
540
|
+
bad.append(name)
|
|
541
|
+
finally:
|
|
542
|
+
_SILENT = False
|
|
543
|
+
with io.open(_STORE_RS, "w", encoding="utf-8", newline="") as f:
|
|
544
|
+
f.write(pristine)
|
|
545
|
+
with io.open(_STORE_RS, encoding="utf-8", newline="") as f:
|
|
546
|
+
if f.read() != pristine:
|
|
547
|
+
print(" RESTORE-FAILED %s" % name)
|
|
548
|
+
bad.append(name + "(恢复失败)")
|
|
549
|
+
_rebuild()
|
|
550
|
+
if bad:
|
|
551
|
+
print()
|
|
552
|
+
print("定点变异自证:FAIL -- %s" % "、".join(bad))
|
|
553
|
+
return 1
|
|
554
|
+
print()
|
|
555
|
+
print("定点变异自证:PASS(每处判据都被打红)")
|
|
556
|
+
return 0
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
def _cleanup():
|
|
560
|
+
for t in _SANDBOXES:
|
|
561
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
if __name__ == "__main__":
|
|
565
|
+
if "--mutate" in sys.argv:
|
|
566
|
+
rc = _mutate()
|
|
567
|
+
_cleanup()
|
|
568
|
+
sys.exit(rc)
|
|
569
|
+
rc = main()
|
|
570
|
+
_cleanup()
|
|
571
|
+
sys.exit(rc)
|