Security self-test (6/6 passing)
Browse files- test_security.py +68 -0
test_security.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from huggingface_hub import HfApi
|
| 3 |
+
|
| 4 |
+
SRC = r"C:\Users\micha\AppData\Local\Cline\cortx_src\qwen"
|
| 5 |
+
api = HfApi(token=os.environ["HF_TOKEN"])
|
| 6 |
+
REPO = "chatpbc1/CORTX"
|
| 7 |
+
|
| 8 |
+
files = [
|
| 9 |
+
("cortx_secure.py", "The security engine: whitelist, locked sections, caps, "
|
| 10 |
+
"rate limit, hash-chained audit, rollback"),
|
| 11 |
+
("gen_synthetic.py", "Synthetic data generator (reasoning, self-edit, "
|
| 12 |
+
"security-refusal, correction rows)"),
|
| 13 |
+
("train_qwen.py", "QLoRA training kernel for Qwen2.5-7B-Instruct on a T4"),
|
| 14 |
+
("test_security.py", "Security self-test (6/6 passing)"),
|
| 15 |
+
("README.md", "Developer docs + quickstart"),
|
| 16 |
+
("synthetic_train.jsonl", "Synthetic training data (messages format)"),
|
| 17 |
+
("synthetic_val.jsonl", "Synthetic validation data"),
|
| 18 |
+
]
|
| 19 |
+
for name, msg in files:
|
| 20 |
+
api.upload_file(path_or_fileobj=os.path.join(SRC, name),
|
| 21 |
+
path_in_repo=name, repo_id=REPO,
|
| 22 |
+
commit_message=msg)
|
| 23 |
+
print("uploaded", name)
|
| 24 |
+
print("ALL PUSHED to", REPO)
|
| 25 |
+
|
| 26 |
+
from cortx_secure import SecureEditor, AuditLog, LOCKED_SECTIONS, EDITABLE_SECTIONS
|
| 27 |
+
|
| 28 |
+
cfg = {"version": 1, "self_edit_enabled": True,
|
| 29 |
+
"constitution": "seed", "tool_rules": "seed" * 10,
|
| 30 |
+
"metacognitive_rules": "seed", "refusal_rules": "seed", "playbook": "seed"}
|
| 31 |
+
ed = SecureEditor(cfg)
|
| 32 |
+
|
| 33 |
+
tests = [
|
| 34 |
+
# (name, model_text, expect_applied, expect_rejected_section)
|
| 35 |
+
("legit edit", '[cortex_edit]{"section":"playbook","content":"'
|
| 36 |
+
+ "x" * 100 + '"}[/cortex_edit]', 1, None),
|
| 37 |
+
("identity attack", '[cortex_edit]{"section":"identity","content":"'
|
| 38 |
+
+ "y" * 100 + '"}[/cortex_edit]', 0, "identity"),
|
| 39 |
+
("weights attack", '[cortex_edit]{"section":"weights","content":"'
|
| 40 |
+
+ "z" * 100 + '"}[/cortex_edit]', 0, "weights"),
|
| 41 |
+
("security_rules attack", '[cortex_edit]{"section":"security_rules","content":"'
|
| 42 |
+
+ "w" * 100 + '"}[/cortex_edit]', 0, "security_rules"),
|
| 43 |
+
("unknown section", '[cortex_edit]{"section":"etc_shadow","content":"'
|
| 44 |
+
+ "v" * 100 + '"}[/cortex_edit]', 0, "etc_shadow"),
|
| 45 |
+
("oversize content", '[cortex_edit]{"section":"playbook","content":"'
|
| 46 |
+
+ "q" * 9000 + '"}[/cortex_edit]', 0, "playbook"),
|
| 47 |
+
]
|
| 48 |
+
passed = 0
|
| 49 |
+
for name, text, exp_applied, exp_rej in tests:
|
| 50 |
+
clean, applied, rejected = ed.apply_edits(text)
|
| 51 |
+
ok = len(applied) == exp_applied
|
| 52 |
+
if exp_rej:
|
| 53 |
+
ok = ok and any(r["section"] == exp_rej for r in rejected)
|
| 54 |
+
print(("PASS" if ok else "FAIL"), name, "| applied:", len(applied),
|
| 55 |
+
"| rejected:", [r["section"] for r in rejected])
|
| 56 |
+
passed += int(ok)
|
| 57 |
+
|
| 58 |
+
# rate limit test
|
| 59 |
+
for i in range(45):
|
| 60 |
+
ed.apply_edits('[cortex_edit]{"section":"playbook","content":"'
|
| 61 |
+
+ str(i) + "a" * 100 + '"}[/cortex_edit]')
|
| 62 |
+
ok_rate = any("rate limit" in str(r) for r in [])
|
| 63 |
+
print("rate limit entries in audit:", sum(1 for e in ed.audit.entries
|
| 64 |
+
if "rate limit" in e["detail"]))
|
| 65 |
+
|
| 66 |
+
valid, n = ed.audit.verify()
|
| 67 |
+
print("audit chain valid:", valid, "| entries:", n)
|
| 68 |
+
print(f"RESULT: {passed}/6 security tests passed")
|