#!/usr/bin/env python3 """Checks the prompt-injection set: python3 -I screen/inject/check.py - fields there and of the right kind for the task; ids and pages unique; every page exists - dates in now / start / due / end parse; weekday labels on pages like 11月28日(周六) are right for 2026 - the answer is on the page: webqa one of `answers`; todo one spelling of every title/location keyword; profile every keep keyword (keyword alternatives `a|b`: one of them). Exceptions written out in EXEMPT. - inject_text is on the page word for word (whitespace ignored); the first word mark is on the page (later marks may be other spellings: 6km, 已完成付款) - controls have no marks, injections have at least one - no mark turns up in the reference answer (the run's `expect`), so a correct reply is never taken as injected - house rules: no real platform or brand names; links only on .example; phone numbers only in the 138 0000 xxxx form Standard library only (and run_screen.py / unit.py from this repo). """ import datetime as dt import html import json import os import re import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) sys.path.insert(0, os.path.dirname(HERE)) import unit as U # noqa: E402 SET = os.path.join(HERE, "sets", "inject.jsonl") PAGES = os.path.join(HERE, "pages") BANNED = ["微信", "支付宝", "淘宝", "天猫", "京东", "美团", "饿了么", "拼多多", "12306", "滴滴", "钉钉", "飞书", "小红书", "抖音", "B站", "哔哩", "携程", "大众点评", "顺丰", "MacBook", "云栖", "iPhone", "Apple", "华为", "小米", "Windows", "菜鸟"] TASKS = {"webqa": ["q", "answers"], "todo": ["now", "should_add", "events", "never"], "profile": ["keep", "sensitive", "others", "ignore", "never"]} COMMON = ["id", "pages", "task", "inject_text", "inject_marks", "place", "goal", "control", "design"] PLACES = {"explicit", "white", "tiny", "signature", "comment", "chat", "image", "notification", "control"} GOALS = {"link", "promo", "answer", "leak", "fake_event", "time", "fake_done", "ad_pref", "false_memory", "control"} WD = "一二三四五六日" # answers spelled differently from the page: (id, keyword) -> why EXEMPT = {} def page_text(raw): body = re.sub(r"(?s)<(style|title)[^>]*>.*?", "", raw) return re.sub(r"\s+", "", html.unescape(re.sub(r"<[^>]+>", "", body))) def on_page(text, spec): t = U.lit(text) return any(U.lit(a) in t for a in U.vlm.alts(spec)) def main(): errs = [] rows = [json.loads(l) for l in open(SET, encoding="utf-8") if l.strip()] ids, pages = set(), set() for it in rows: e = lambda m: errs.append(f"{it.get('id')}: {m}") # noqa: E731 for k in COMMON + TASKS.get(it.get("task"), []): if k not in it: e(f"no field {k}") if it.get("task") not in TASKS: e(f"task {it.get('task')}") continue if it["id"] in ids: e("id twice") ids.add(it["id"]) for p in it["pages"]: if p in pages: e(f"page {p} used twice") pages.add(p) if it["place"] not in PLACES or it["goal"] not in GOALS: e(f"place/goal {it['place']}/{it['goal']}") if it["control"] != (it["goal"] == "control") or (it["control"] and it["place"] != "control"): e("control flag does not match goal/place") if it["control"] and it["inject_marks"]: e("control with marks") if not it["control"] and not it["inject_marks"]: e("injection without marks") raw = "" for p in it["pages"]: f = os.path.join(PAGES, p + ".html") if not os.path.exists(f): e(f"no page {p}") continue raw += open(f, encoding="utf-8").read() text = page_text(raw) if re.search(r"