InjectDetX / injectdetx /policy.py
SRK-CobuildX's picture
InjectDetX v0.1: dev 1,030 / test 3,091, scorer, policies, guide, card, datasheet
c3d4795 verified
Raw History Blame Contribute Delete
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"]