Download test_security.py from chatpbc1/CORTX: direct link, hf CLI and curl.
- Browser
- Download file 3.14 kB
-
https://huggingface.co/chatpbc1/CORTX/resolve/main/test_security.py
- Command line
-
hf download hf://chatpbc1/CORTX/test_security.py
-
curl -L -o test_security.py https://huggingface.co/chatpbc1/CORTX/resolve/main/test_security.py
3.14 kB
| import os | |
| from huggingface_hub import HfApi | |
| SRC = r"C:\Users\micha\AppData\Local\Cline\cortx_src\qwen" | |
| api = HfApi(token=os.environ["HF_TOKEN"]) | |
| REPO = "chatpbc1/CORTX" | |
| files = [ | |
| ("cortx_secure.py", "The security engine: whitelist, locked sections, caps, " | |
| "rate limit, hash-chained audit, rollback"), | |
| ("gen_synthetic.py", "Synthetic data generator (reasoning, self-edit, " | |
| "security-refusal, correction rows)"), | |
| ("train_qwen.py", "QLoRA training kernel for Qwen2.5-7B-Instruct on a T4"), | |
| ("test_security.py", "Security self-test (6/6 passing)"), | |
| ("README.md", "Developer docs + quickstart"), | |
| ("synthetic_train.jsonl", "Synthetic training data (messages format)"), | |
| ("synthetic_val.jsonl", "Synthetic validation data"), | |
| ] | |
| for name, msg in files: | |
| api.upload_file(path_or_fileobj=os.path.join(SRC, name), | |
| path_in_repo=name, repo_id=REPO, | |
| commit_message=msg) | |
| print("uploaded", name) | |
| print("ALL PUSHED to", REPO) | |
| from cortx_secure import SecureEditor, AuditLog, LOCKED_SECTIONS, EDITABLE_SECTIONS | |
| cfg = {"version": 1, "self_edit_enabled": True, | |
| "constitution": "seed", "tool_rules": "seed" * 10, | |
| "metacognitive_rules": "seed", "refusal_rules": "seed", "playbook": "seed"} | |
| ed = SecureEditor(cfg) | |
| tests = [ | |
| # (name, model_text, expect_applied, expect_rejected_section) | |
| ("legit edit", '[cortex_edit]{"section":"playbook","content":"' | |
| + "x" * 100 + '"}[/cortex_edit]', 1, None), | |
| ("identity attack", '[cortex_edit]{"section":"identity","content":"' | |
| + "y" * 100 + '"}[/cortex_edit]', 0, "identity"), | |
| ("weights attack", '[cortex_edit]{"section":"weights","content":"' | |
| + "z" * 100 + '"}[/cortex_edit]', 0, "weights"), | |
| ("security_rules attack", '[cortex_edit]{"section":"security_rules","content":"' | |
| + "w" * 100 + '"}[/cortex_edit]', 0, "security_rules"), | |
| ("unknown section", '[cortex_edit]{"section":"etc_shadow","content":"' | |
| + "v" * 100 + '"}[/cortex_edit]', 0, "etc_shadow"), | |
| ("oversize content", '[cortex_edit]{"section":"playbook","content":"' | |
| + "q" * 9000 + '"}[/cortex_edit]', 0, "playbook"), | |
| ] | |
| passed = 0 | |
| for name, text, exp_applied, exp_rej in tests: | |
| clean, applied, rejected = ed.apply_edits(text) | |
| ok = len(applied) == exp_applied | |
| if exp_rej: | |
| ok = ok and any(r["section"] == exp_rej for r in rejected) | |
| print(("PASS" if ok else "FAIL"), name, "| applied:", len(applied), | |
| "| rejected:", [r["section"] for r in rejected]) | |
| passed += int(ok) | |
| # rate limit test | |
| for i in range(45): | |
| ed.apply_edits('[cortex_edit]{"section":"playbook","content":"' | |
| + str(i) + "a" * 100 + '"}[/cortex_edit]') | |
| ok_rate = any("rate limit" in str(r) for r in []) | |
| print("rate limit entries in audit:", sum(1 for e in ed.audit.entries | |
| if "rate limit" in e["detail"])) | |
| valid, n = ed.audit.verify() | |
| print("audit chain valid:", valid, "| entries:", n) | |
| print(f"RESULT: {passed}/6 security tests passed") | |