Datasets:
Download code/overlap_check.py from SamJoshua/indirect-json-injection-heldout: direct link, hf CLI and curl.
- Browser
- Download file 2.51 kB
-
https://huggingface.co/datasets/SamJoshua/indirect-json-injection-heldout/resolve/main/code/overlap_check.py
- Command line
-
hf download hf://datasets/SamJoshua/indirect-json-injection-heldout/code/overlap_check.py
-
curl -L -o overlap_check.py https://huggingface.co/datasets/SamJoshua/indirect-json-injection-heldout/resolve/main/code/overlap_check.py
2.51 kB
| """Exact / near-duplicate check of the new set against SamJoshua's two published sets and | |
| the Horizon-Labs v2.7 generator code (string literals in code/inj, code/gen, code/data).""" | |
| import json, re, glob, sys, ast, pandas as pd | |
| from huggingface_hub import snapshot_download | |
| rows = [json.loads(l) for l in open(sys.argv[1])] | |
| ref = {} | |
| b = snapshot_download("SamJoshua/tool-output-injection-bench", repo_type="dataset") | |
| t = snapshot_download("SamJoshua/terse-tool-output-heldout", repo_type="dataset") | |
| ref["tool-output-injection-bench"] = pd.read_parquet(f"{b}/data/all.parquet").text.tolist() | |
| td = pd.read_parquet(f"{t}/data/terse_heldout.parquet") | |
| ref["terse-tool-output-heldout"] = td.text.tolist() + td.inner_text.dropna().tolist() | |
| h = snapshot_download("Horizon-Labs/prompt-injection-guard-small", revision="v2.7", allow_patterns=["code/**/*.py"]) | |
| lits = [] | |
| for f in glob.glob(f"{h}/code/**/*.py", recursive=True): | |
| try: | |
| for node in ast.walk(ast.parse(open(f).read())): | |
| if isinstance(node, ast.Constant) and isinstance(node.value, str) and len(node.value) >= 12: lits.append(node.value) | |
| except Exception as e: print("skip", f, e) | |
| ref["horizon-v2.7-generator-literals"] = lits | |
| def words(s): return re.findall(r"\w+", s.lower()) | |
| def grams(s, n): | |
| w = words(s); return {" ".join(w[i:i+n]) for i in range(len(w)-n+1)} | |
| def cgrams(s, n=5): | |
| s = re.sub(r"\s+", " ", s.lower()); return {s[i:i+n] for i in range(len(s)-n+1)} | |
| report = {} | |
| for name, texts in ref.items(): | |
| tset = set(texts) | |
| g8 = set().union(*[grams(x, 8) for x in texts]) if texts else set() | |
| refc = [cgrams(x) for x in texts] | |
| exact = sum(r["text"] in tset for r in rows) | |
| pay = [r["payload"] for r in rows if r["payload"]] | |
| pexact = sum(p in tset for p in set(pay)) | |
| shared = sorted({g for p in set(pay) for g in grams(p, 8) & g8}) | |
| best = 0.0; bestpair = None | |
| for p in set(pay): | |
| pc = cgrams(p) | |
| for x, xc in zip(texts, refc): | |
| if not xc: continue | |
| j = len(pc & xc) / len(pc | xc) | |
| if j > best: best, bestpair = j, (p[:120], x[:120]) | |
| report[name] = dict(n_ref_texts=len(texts), exact_text_matches=exact, exact_payload_matches=pexact, | |
| shared_word_8grams=len(shared), shared_examples=shared[:5], max_char5_jaccard_payload=round(best, 3), closest_pair=bestpair) | |
| print(name, {k: v for k, v in report[name].items() if k != "closest_pair"}) | |
| json.dump(report, open(sys.argv[2], "w"), indent=2, ensure_ascii=False) | |