GPU benchmark: answer only its own page, and print what it sends as plain text

Any site open in the same browser could POST results (printed to the
terminal, escape sequences included, and written to --out), and DNS
rebinding could read the save. The server now checks the Host header and
the origin of a POST, caps the body at 2 MB, drops control characters,
and no longer sends stack traces. The panel no longer shows <sub> tags.
This commit is contained in:
Frederic Jung 2026-11-05 10:30:37 +01:00
parent 57c0594a90
commit f9c85ab0c3
No known key found for this signature in database
3 changed files with 73 additions and 4 deletions

View file

@ -0,0 +1,50 @@
// npm run bench:gpu serves the game on this machine: only its own page may talk to it (no other site in the
// browser, no DNS rebinding), and what it prints or writes holds no terminal control characters.
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const http = require('node:http');
const { spawn } = require('node:child_process');
const ROOT = path.join(__dirname, '..');
function req(port, { method = 'GET', p = '/', host, headers = {}, body } = {}) {
return new Promise((resolve, reject) => {
const r = http.request({ host: '127.0.0.1', port, method, path: p, headers: { host: host || `localhost:${port}`, ...headers } }, (res) => {
let d = ''; res.on('data', (c) => { d += c; }); res.on('end', () => resolve({ status: res.statusCode, body: d }));
});
r.on('error', reject); if (body !== undefined) r.write(body); r.end();
});
}
test('the benchmark server answers only its own page', async (t) => {
const port = 20000 + Math.floor(Math.random() * 20000), out = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'bench-')), 'out.md');
const srv = spawn(process.execPath, [path.join(ROOT, 'scripts', 'gpu-bench.js'), '--port', String(port), '--out', out], { stdio: ['ignore', 'pipe', 'pipe'] });
let log = ''; srv.stdout.on('data', (c) => { log += c; }); srv.stderr.on('data', (c) => { log += c; });
t.after(() => srv.kill());
for (let i = 0; i < 100 && !/Ctrl\+C/.test(log); i++) await new Promise((r) => setTimeout(r, 100));
assert.match(log, /Ctrl\+C/, 'the server started');
// DNS rebinding: a request for another host name gets nothing.
assert.equal((await req(port, { p: '/__bench/save.json', host: `evil.example:${port}` })).status, 421);
assert.equal((await req(port, { p: '/__bench/save.json' })).status, 200);
// A POST from another site, or with no origin at all, is refused and writes nothing.
const md = 'ok\u001b]52;c;Zm9v\u0007\u001b[2Jdone';
const post = (origin) => req(port, { method: 'POST', p: '/__bench/results', headers: { 'content-type': 'text/plain', ...(origin ? { origin } : {}) }, body: JSON.stringify({ md }) });
assert.equal((await post('https://evil.example')).status, 403);
assert.equal((await post(null)).status, 403);
assert.equal(fs.existsSync(out), false);
// Its own page may send results; control characters are dropped before printing and writing.
assert.equal((await post(`http://localhost:${port}`)).status, 204);
const written = fs.readFileSync(out, 'utf8');
assert.match(written, /^ok\]52;c;Zm9v\[2Jdone/);
assert.doesNotMatch(written + log, /[\u001b\u0007]/);
// A malformed address is a plain 400: no stack trace or paths in the answer.
const bad = await req(port, { p: '/%E0' });
assert.equal(bad.status, 400); assert.equal(bad.body, '');
});