Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Evaluation harness: the review page's keys, and a review server only its own page can write to

Claude5966d4496c28
diff --git a/scripts/eval/README.md b/scripts/eval/README.md
index c8b4575f..d0043256 100644
--- a/scripts/eval/README.md
+++ b/scripts/eval/README.md
@@ -315,8 +315,9 @@ the variant, model or file.
change.
Use **←** / **1** for the left result, **→** / **2** for the right one, **↓** / **0** / **Space**
-for "about the same", **S** to skip and **U** / **Backspace** to undo. On a phone, use the three
-buttons. Ratings save to `review/ratings.json` as you go. Opened as a plain file, the page keeps
+for "about the same", **S** to skip and **U** / **Backspace** to undo. Space and Enter on a focused
+button press that button. On a phone, use the three buttons. The server answers only on 127.0.0.1 /
+localhost, and saves ratings only from its own page. Ratings save to `review/ratings.json` as you go. Opened as a plain file, the page keeps
them in the browser and offers a download. `pnpm eval:report` unblinds the ratings with
`review/key.json` and adds the preference per variant pair to `summary.json`.
diff --git a/scripts/eval/lib/review.mjs b/scripts/eval/lib/review.mjs
index 55ac9979..3c1e1fc9 100644
--- a/scripts/eval/lib/review.mjs
+++ b/scripts/eval/lib/review.mjs
@@ -190,6 +190,11 @@ $("download").addEventListener("click", () => {
document.addEventListener("keydown", (e) => {
if (e.metaKey || e.ctrlKey || e.altKey) return;
const k = e.key;
+ // Space and Enter on a focused button press that button (a keyboard user's "Left is better"), and a focused
+ // clip keeps its own keys: only the page's shortcuts are taken here.
+ const control = e.target instanceof Element ? e.target.closest("button, a, input, select, textarea, summary, video, [contenteditable]") : null;
+ if (control && (k === " " || k === "Enter")) return;
+ if (control && control.tagName === "VIDEO" && k.startsWith("Arrow")) return;
const choice = k === "ArrowLeft" || k === "1" ? "left" : k === "ArrowRight" || k === "2" ? "right" : k === "ArrowDown" || k === "0" || k === " " ? "same" : k === "s" || k === "S" ? "skip" : null;
if (choice) { e.preventDefault(); rate(choice); }
else if (k === "u" || k === "U" || k === "Backspace") { e.preventDefault(); undo(); }
@@ -248,13 +253,26 @@ const TYPES = { ".html": "text/html; charset=utf-8", ".json": "application/json"
*/
export function serveReview(dir, { port = 0, host = "127.0.0.1" } = {}) {
const root = path.join(dir, "review");
+ /** The names this server answers to (set once it listens): another Host is a page that rebound its name here. */
+ let hosts = new Set();
const server = http.createServer(async (req, res) => {
- const url = new URL(req.url ?? "/", "http://local");
const send = (status, body, type = "text/plain; charset=utf-8") => {
res.writeHead(status, { "content-type": type, "cache-control": "no-store" });
res.end(body);
};
+ if (!hosts.has(String(req.headers.host ?? "").toLowerCase())) return send(421, "not this server");
+ let url;
+ try {
+ url = new URL(req.url ?? "/", "http://local");
+ } catch {
+ return send(400, "bad address");
+ }
if (req.method === "POST" && url.pathname === "/ratings") {
+ // Only the review page itself saves ratings: a page from anywhere else can't (a cross-site form or
+ // fetch either sends its own Origin, or can't send JSON without a preflight this server never answers).
+ const origin = req.headers.origin;
+ if (origin && !hosts.has(origin.replace(/^http:\/\//, "").toLowerCase())) return send(403, "ratings are saved from the review page only");
+ if (!/^application\/json\b/i.test(String(req.headers["content-type"] ?? ""))) return send(415, "send the ratings as application/json");
let raw = "";
for await (const c of req) {
raw += c;
@@ -272,7 +290,12 @@ export function serveReview(dir, { port = 0, host = "127.0.0.1" } = {}) {
return send(204, "");
}
if (req.method !== "GET" && req.method !== "HEAD") return send(405, "GET or POST /ratings");
- const rel = decodeURIComponent(url.pathname === "/" ? "index.html" : url.pathname.slice(1));
+ let rel;
+ try {
+ rel = decodeURIComponent(url.pathname === "/" ? "index.html" : url.pathname.slice(1));
+ } catch {
+ return send(400, "bad address");
+ }
const abs = path.resolve(root, rel);
if (!abs.startsWith(root + path.sep) || path.basename(abs) === "key.json") return send(404, "not found");
fs.stat(abs, (err, st) => {
@@ -282,7 +305,13 @@ export function serveReview(dir, { port = 0, host = "127.0.0.1" } = {}) {
fs.createReadStream(abs).pipe(res);
});
});
- return new Promise((resolve) => server.listen(port, host, () => resolve({ server, url: `http://${host}:${server.address().port}/` })));
+ return new Promise((resolve) =>
+ server.listen(port, host, () => {
+ const p = server.address().port;
+ hosts = new Set([`${host}:${p}`, `localhost:${p}`, `127.0.0.1:${p}`].map((h) => h.toLowerCase()));
+ resolve({ server, url: `http://${host}:${p}/` });
+ }),
+ );
}
/** A run's rows, for building pairs against another run. */
diff --git a/tests/e2e/eval-review.mjs b/tests/e2e/eval-review.mjs
index a3def699..0caf407c 100644
--- a/tests/e2e/eval-review.mjs
+++ b/tests/e2e/eval-review.mjs
@@ -124,6 +124,16 @@ try {
const page = await ctx.newPage();
await page.goto(url, { waitUntil: "load" });
await page.waitForSelector("#stage");
+ // A keyboard user who tabs to "Left is better" and presses Space rates left (the button's own action), not "same".
+ const ratingsFile = path.join(runDir, "review", "ratings.json");
+ const before = fs.existsSync(ratingsFile) ? JSON.parse(fs.readFileSync(ratingsFile, "utf8")).ratings.length : 0;
+ await page.focus('[data-choice="left"]');
+ await page.keyboard.press("Space");
+ await page.waitForTimeout(400);
+ const pressed = JSON.parse(fs.readFileSync(ratingsFile, "utf8")).ratings;
+ check(pressed.length === before + 1 && pressed.at(-1).choice === "left", `review: Space on the focused "Left is better" button rates left (${pressed.at(-1)?.choice})`);
+ await page.keyboard.press("u");
+ await page.waitForTimeout(200);
for (let i = 0; i < made.pairs; i++) {
await page.keyboard.press(i < made.pairs / 2 ? "ArrowLeft" : "ArrowRight");
await page.waitForTimeout(180);
diff --git a/tests/eval-harness.test.ts b/tests/eval-harness.test.ts
index 5694a232..81470d5c 100644
--- a/tests/eval-harness.test.ts
+++ b/tests/eval-harness.test.ts
@@ -390,8 +390,27 @@ describe("report, blind review, compare and promote", () => {
expect((await fetch(url)).status).toBe(200);
// Pick the recipe side every time.
const ratings = key.pairs.map((p: { id: string; left: "a" | "b"; a: { label: string } }) => ({ pair: p.id, choice: (p.a.label === "recipe") === (p.left === "a") ? "left" : "right" }));
- expect((await fetch(`${url}ratings`, { method: "POST", body: JSON.stringify({ ratings: [{ pair: "x", choice: "maybe" }] }) })).status).toBe(400);
- expect((await fetch(`${url}ratings`, { method: "POST", body: JSON.stringify({ ratings }) })).status).toBe(204);
+ const json = { "content-type": "application/json" };
+ expect((await fetch(`${url}ratings`, { method: "POST", headers: json, body: JSON.stringify({ ratings: [{ pair: "x", choice: "maybe" }] }) })).status).toBe(400);
+ // Only the review page saves ratings: another site's page sends its own Origin, or plain text (no preflight).
+ const raw = (method: string, p: string, headers: Record<string, string>, body = "") =>
+ new Promise<number>((resolve, reject) => {
+ const req = http.request(new URL(p, url), { method, headers }, (res) => {
+ res.resume();
+ resolve(res.statusCode ?? 0);
+ });
+ req.on("error", reject);
+ req.end(body);
+ });
+ const body = JSON.stringify({ ratings });
+ expect(await raw("POST", "/ratings", { ...json, origin: "https://evil.example" }, body)).toBe(403);
+ expect(await raw("POST", "/ratings", { "content-type": "text/plain" }, body)).toBe(415);
+ // A name rebound to 127.0.0.1 isn't this server; a malformed address is refused, and the server stays up.
+ expect(await raw("GET", "/", { host: "evil.example" })).toBe(421);
+ expect(await raw("GET", "/%E0%A4%A", {})).toBe(400);
+ expect((await fetch(url)).status).toBe(200);
+ expect(fs.existsSync(path.join(runDir, "review", "ratings.json"))).toBe(false);
+ expect((await fetch(`${url}ratings`, { method: "POST", headers: { ...json, origin: url.replace(/\/$/, "") }, body })).status).toBe(204);
} finally {
await new Promise((r) => server.close(r));
}