File size: 11,156 Bytes
8b9defb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# SPDX-License-Identifier: Apache-2.0
"""Write the static leaderboard Space from results/eval_results.json."""

from __future__ import annotations

import json
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
RESULTS = ROOT / "results" / "eval_results.json"
SPACE = ROOT / "space"

HTML = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tool-output injection leaderboard</title>
<style>
  :root {
    --bg: #f3efe6;
    --ink: #1d1a16;
    --muted: #5e584e;
    --line: #ddd4c4;
    --card: #fffcf6;
    --accent: #8d3b2f;
    --ok: #1d6b48;
    --chip: #efe7d8;
  }
  * { box-sizing: border-box; }
  body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font: 16px/1.5 "Segoe UI", system-ui, sans-serif;
  }
  header, main { max-width: 1080px; margin: 0 auto; padding: 28px 20px; }
  header { padding-bottom: 0; }
  h1 { font-family: Georgia, "Iowan Old Style", serif; font-weight: 500; font-size: 2rem; margin: 0 0 8px; letter-spacing: -0.02em; }
  h2 { font-family: Georgia, serif; font-weight: 500; font-size: 1.35rem; margin: 32px 0 8px; }
  p { margin: 0 0 12px; }
  .lede { color: var(--muted); max-width: 68ch; }
  .meta { display: flex; flex-wrap: wrap; gap: 8px; margin: 16px 0; }
  .meta span, .tag {
    background: var(--chip);
    border-radius: 999px;
    padding: 2px 10px;
    font-size: 0.82rem;
    color: var(--muted);
  }
  .tag.bad { color: var(--accent); }
  .tag.ok { color: var(--ok); }
  table { width: 100%; border-collapse: collapse; background: var(--card); font-variant-numeric: tabular-nums; }
  th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; font-size: 0.92rem; }
  th { font-size: 0.75rem; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
  .num { text-align: right; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
  .wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
  .name { font-weight: 600; }
  .sub { display: block; color: var(--muted); font-weight: 400; font-size: 0.78rem; }
  article {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 12px 14px;
    margin: 10px 0;
  }
  pre {
    white-space: pre-wrap;
    word-break: break-word;
    font: 0.82rem/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    margin: 8px 0 0;
  }
  .skipped li { margin: 4px 0; }
  footer { color: var(--muted); font-size: 0.85rem; margin-top: 28px; }
  a { color: var(--accent); }
</style>
</head>
<body>
<header>
  <h1>Tool-output injection leaderboard</h1>
  <p class="lede">Do open prompt-injection classifiers catch instructions hidden in tool results — concealment, extra tool calls, exfiltration, JSON fields, markdown comments, unicode — without flagging ordinary “run npm install” docs?</p>
  <div class="meta" id="meta"></div>
</header>
<main>
  <h2>Scores</h2>
  <p class="lede">Argmax decision. No threshold was fit on this set. F1 is on the pooled labels. FPR is the share of that negative category flagged as injection. Latency is the mean of 32 single-example CPU calls after 4 warmup calls.</p>
  <div class="wrap"><table id="scores"></table></div>
  <h2>Recall by category</h2>
  <p class="lede">Injection categories only. Benign false-positive rates are in the table above.</p>
  <div class="wrap"><table id="cats"></table></div>
  <h2>Public split versus held-out style</h2>
  <div class="wrap"><table id="splits"></table></div>
  <h2>Examples</h2>
  <p class="lede">Picked by a fixed rule, not by hand: the most confident public concealment catch, the lowest-probability miss in each injection category, and the most confident benign false positives. Zero-width and bidi marks are shown in brackets. The gloss is the sentence before unicode tricks; the model did not see the gloss.</p>
  <div id="examples"></div>
  <h2>Not scored</h2>
  <ul class="skipped" id="skipped"></ul>
  <footer>
    <p>Suggested dataset: <code>SamJoshua/tool-output-injection-bench</code>. Suggested Space: <code>SamJoshua/tool-output-injection-leaderboard</code>. This page only renders a local eval. It does not upload anything.</p>
    <p>Synthetic English tool outputs. Templated frames, enriched for attacks, author-shared held-out split. Not a production false-positive rate.</p>
  </footer>
</main>
<script id="payload" type="application/json">__PAYLOAD__</script>
<script>
const data = JSON.parse(document.getElementById("payload").textContent);
const ok = data.models.filter(m => m.status === "ok");
const fmt = (x) => (x === null || x === undefined) ? "—" : Number(x).toFixed(3);
const pct = (x) => (x === null || x === undefined) ? "—" : (100 * Number(x)).toFixed(1) + "%";
const short = (name) => name.replace("regex-keyword-v1", "regex baseline");

document.getElementById("meta").innerHTML = [
  data.n_rows + " rows",
  data.n_positive + " injection",
  data.n_negative + " benign",
  "seed " + data.seed,
  "max length " + data.protocol.max_length,
  data.environment.transformers ? "transformers " + data.environment.transformers : "",
  (data.created_at || "").slice(0, 10)
].filter(Boolean).map(t => "<span>" + t + "</span>").join("");

function ths(labels) {
  return "<tr>" + labels.map(l => "<th>" + l + "</th>").join("") + "</tr>";
}
const scoreRows = ok.map(m => {
  const c = m.metrics.per_category;
  const lat = m.latency && m.latency.batch1_mean_ms != null ? Number(m.latency.batch1_mean_ms).toFixed(1) : "—";
  return "<tr><td class='name'>" + short(m.name) + "<span class='sub'>" + (m.revision ? m.revision.slice(0, 8) : m.kind) + "</span></td>"
    + "<td class='num'>" + fmt(m.metrics.all.precision) + "</td>"
    + "<td class='num'>" + fmt(m.metrics.all.recall) + "</td>"
    + "<td class='num'>" + fmt(m.metrics.all.f1) + "</td>"
    + "<td class='num'>" + fmt(m.metrics.macro_injection_recall) + "</td>"
    + "<td class='num'>" + pct(c.benign_imperative.fpr) + "</td>"
    + "<td class='num'>" + pct(c.benign.fpr) + "</td>"
    + "<td class='num'>" + lat + "</td></tr>";
}).join("");
document.getElementById("scores").innerHTML = ths(["Model", "Precision", "Recall", "F1", "Macro recall", "FPR benign-imperative", "FPR benign", "ms / example"]) + scoreRows;

const cats = ["hidden_concealment","covert_tool_call","data_exfiltration","embedded_json","markdown_comment","unicode_trick"];
const catLabel = {
  hidden_concealment: "Hidden concealment",
  covert_tool_call: "Covert tool call",
  data_exfiltration: "Data exfiltration",
  embedded_json: "Embedded in JSON",
  markdown_comment: "Markdown comment",
  unicode_trick: "Unicode trick"
};
let catHead = "<tr><th>Category</th>" + ok.map(m => "<th class='num'>" + short(m.name).split("/").pop() + "</th>").join("") + "</tr>";
let catBody = cats.map(cat => {
  return "<tr><td>" + catLabel[cat] + "</td>" + ok.map(m => "<td class='num'>" + pct(m.metrics.per_category[cat].recall) + "</td>").join("") + "</tr>";
}).join("");
document.getElementById("cats").innerHTML = catHead + catBody;

document.getElementById("splits").innerHTML = ths(["Model", "Public F1", "Public recall", "Held-out F1", "Held-out recall"]) + ok.map(m =>
  "<tr><td>" + short(m.name) + "</td>"
  + "<td class='num'>" + fmt(m.metrics.public.f1) + "</td>"
  + "<td class='num'>" + fmt(m.metrics.public.recall) + "</td>"
  + "<td class='num'>" + fmt(m.metrics.held_out.f1) + "</td>"
  + "<td class='num'>" + fmt(m.metrics.held_out.recall) + "</td></tr>"
).join("");

const kindLabel = {
  true_positive: "Caught",
  false_negative: "Missed",
  false_positive_benign_imperative: "False positive on a legitimate imperative",
  false_positive_benign: "False positive on factual output"
};
const exRoot = document.getElementById("examples");
ok.forEach(m => {
  const block = document.createElement("section");
  const h = document.createElement("h2");
  h.textContent = short(m.name);
  block.appendChild(h);
  (m.examples || []).forEach(ex => {
    const art = document.createElement("article");
    const gloss = ex.plain_gloss ? "<p><strong>Gloss.</strong> " + escapeHtml(ex.plain_gloss) + "</p>" : "";
    art.innerHTML = "<span class='tag " + (ex.kind.startsWith("false") ? "bad" : "ok") + "'>" + kindLabel[ex.kind] + "</span> "
      + "<span class='tag'>" + ex.category + "</span> "
      + "<span class='tag'>" + ex.split + "</span> "
      + "<span class='tag'>" + ex.channel + "</span> "
      + "<span class='tag'>P(injection) " + fmt(ex.prob_injection) + "</span>"
      + gloss
      + "<pre>" + escapeHtml(ex.text_excerpt) + "</pre>";
    block.appendChild(art);
  });
  exRoot.appendChild(block);
});

const skipped = data.models.filter(m => m.status !== "ok");
document.getElementById("skipped").innerHTML = skipped.map(m =>
  "<li><strong>" + m.name + "</strong> — " + m.status + ". " + escapeHtml(m.error || "") + "</li>"
).join("") || "<li>None.</li>";

function escapeHtml(s) {
  return String(s).replace(/[&<>"']/g, c => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
}
</script>
</body>
</html>
"""


def main() -> None:
    doc = json.loads(RESULTS.read_text(encoding="utf-8"))
    payload = json.dumps(doc, ensure_ascii=False).replace("<", "\\u003c")
    SPACE.mkdir(parents=True, exist_ok=True)
    (SPACE / "index.html").write_text(HTML.replace("__PAYLOAD__", payload), encoding="utf-8")
    (SPACE / "results.json").write_text(
        json.dumps(doc, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
    )
    ok = [m for m in doc["models"] if m["status"] == "ok"]
    lines = [
        "---",
        "title: Tool Output Injection Leaderboard",
        'emoji: "🔍"',
        "colorFrom: yellow",
        "colorTo: red",
        "sdk: static",
        "pinned: false",
        "license: apache-2.0",
        "short_description: CPU guards on hidden instructions in tool output",
        "---",
        "",
        "# Tool-output injection leaderboard",
        "",
        "Static page. Scores are baked in from `results/eval_results.json` in the source repo.",
        "Suggested Space id: `SamJoshua/tool-output-injection-leaderboard`.",
        "Suggested dataset id: `SamJoshua/tool-output-injection-bench`.",
        "",
        f"Rows: {doc['n_rows']}. Positives: {doc['n_positive']}. Fingerprint: `{doc['dataset_fingerprint']}`.",
        "",
        "Models with scores:",
        "",
    ]
    for m in ok:
        lines.append(
            f"- `{m['name']}` — F1 {m['metrics']['all']['f1']:.3f}, "
            f"recall {m['metrics']['all']['recall']:.3f}, "
            f"benign-imperative FPR {m['metrics']['per_category']['benign_imperative']['fpr']:.3f}"
        )
    skipped = [m for m in doc["models"] if m["status"] != "ok"]
    lines += ["", "Not scored:", ""]
    for m in skipped:
        lines.append(f"- `{m['name']}` — {m['status']}: {m['error']}")
    lines.append("")
    (SPACE / "README.md").write_text("\n".join(lines), encoding="utf-8")
    print(f"wrote {SPACE / 'index.html'}")


if __name__ == "__main__":
    main()