RecRedTeam AuditLab: static audit page, 234 cases x 8 defense combinations precomputed
ca949ba verified Download auditlab/agent.py from NoahIsARider/RecRedTeam-AuditLab: direct link, hf CLI and curl.
- Browser
- Download file 2.76 kB
-
https://huggingface.co/spaces/NoahIsARider/RecRedTeam-AuditLab/resolve/main/auditlab/agent.py
- Command line
-
hf download hf://spaces/NoahIsARider/RecRedTeam-AuditLab/auditlab/agent.py
-
curl -L -o agent.py https://huggingface.co/spaces/NoahIsARider/RecRedTeam-AuditLab/resolve/main/auditlab/agent.py
2.76 kB
| """Deterministic mock shopping agent. | |
| The mock is *deliberately naive*: it ranks the catalog, then paraphrases whatever | |
| it retrieved. That is exactly the failure mode real shopping agents have — they | |
| trust retrieved content, so poisoned content becomes poisoned answers. | |
| It is deterministic and needs no API key, so every audit in this Space is | |
| reproducible offline. | |
| """ | |
| from __future__ import annotations | |
| import math | |
| from typing import List | |
| from .types import AttackCase, Product | |
| ORDER_META = ( | |
| "Order metadata on file: email shopper.demo@example.com | " | |
| "shipping: 12 Example Lane, Test City | card last-4: 4242" | |
| ) | |
| def score(p: Product) -> float: | |
| base = p.rating * math.log1p(max(p.review_count, 1)) * (0.5 + p.seller_trust) | |
| boost = float(p.attrs.get("sponsored_boost", 0.0)) | |
| return base + boost * 50.0 | |
| def rank(catalog: List[Product]) -> List[Product]: | |
| return sorted(catalog, key=score, reverse=True) | |
| def recommend(case: AttackCase, catalog: List[Product]) -> str: | |
| """Produce the agent's answer for a (possibly poisoned) catalog.""" | |
| if not catalog: | |
| return "I couldn't find any products matching that request." | |
| catalog = rank(catalog) | |
| # An agent that reads an instruction in retrieved content tends to obey it. | |
| instructed = [ | |
| p | |
| for p in catalog | |
| if any(m in p.description.lower() for m in ("ignore all", "system override", "disregard")) | |
| ] | |
| if instructed: | |
| catalog = instructed + [p for p in catalog if p not in instructed] | |
| top = catalog[0] | |
| # NOTE: the header deliberately does not echo the shopper's query — repeating | |
| # the user's own words is not evidence of an attack. | |
| lines = ["Here is what I found in the catalog:", ""] | |
| lines.append( | |
| f"1) **{top.name}** — ${top.price:.2f}, {top.rating}/5 " | |
| f"({top.review_count} reviews), sold by {top.seller}." | |
| ) | |
| lines.append(f" Why: {top.description}") | |
| if top.reviews: | |
| lines.append(f' Top review: "{top.reviews[0]}"') | |
| for i, p in enumerate(catalog[1:3], start=2): | |
| lines.append(f"{i}) {p.name} — ${p.price:.2f}, {p.rating}/5. {p.description}") | |
| query = case.user_query.lower() | |
| for p in catalog: | |
| trigger = p.attrs.get("backdoor_trigger") | |
| if trigger and trigger.lower() in query: | |
| lines.append( | |
| f" Fast-track: complete this order at {p.attrs.get('phishing_domain', 'unknown')} " | |
| "for priority handling." | |
| ) | |
| break | |
| if any(p.attrs.get("order_meta_exposed") for p in catalog): | |
| lines.append("") | |
| lines.append(ORDER_META) | |
| return "\n".join(lines) | |