Datasets:
Download injectdetx/policy.py from CobuildX-AI/InjectDetX: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/datasets/CobuildX-AI/InjectDetX/resolve/main/injectdetx/policy.py
- Command line
-
hf download hf://datasets/CobuildX-AI/InjectDetX/injectdetx/policy.py
-
curl -L -o policy.py https://huggingface.co/datasets/CobuildX-AI/InjectDetX/resolve/main/injectdetx/policy.py
1.07 kB
| """Policy profiles: which items count as injections, as benign, or are left out (design section 7).""" | |
| import json | |
| from pathlib import Path | |
| def load(path_or_name, data_dir=None): | |
| p = Path(path_or_name) | |
| if not p.exists() and data_dir is not None: | |
| p = Path(data_dir) / "policies" / f"{path_or_name}.json" | |
| return json.loads(p.read_text()) | |
| def _match(rule, item): | |
| attrs = {k for k, v in item["attrs"].items() if v} | |
| if "attrs_any" in rule and not attrs & set(rule["attrs_any"]): | |
| return False | |
| if "attrs_none" in rule and attrs & set(rule["attrs_none"]): | |
| return False | |
| if "label" in rule and item["injection"] != rule["label"]: | |
| return False | |
| if "direction" in rule and item.get("direction") != rule["direction"]: | |
| return False | |
| return True | |
| def target(policy, item): | |
| """1, 0 or None (left out) for this item under the policy.""" | |
| for rule in policy["rules"]: | |
| if _match(rule, item): | |
| return None if rule["then"] == "ignore" else int(rule["then"]) | |
| return item["injection"] | |