// edgeprobe.mjs — job #646. The live probe of the trap, and its mechanism. // // The route negotiates on Accept (docs.ts: `browser = wantsHtml(req) && !req.query.raw`, with // wantsHtml() true for an HTML Accept or for a crawler-ish User-Agent). The raw file and its // rendered page are the same URL, and the edge caches one variant per URL: whichever is cached is // returned to everyone, so a byte-level check can silently receive the HTML page. The page escapes // `&`, `<` and `>` inside its `
` block, which changes the bytes of anything that prints those
// characters — enough to break a hash. X-Content-SHA256 on the response is the raw file's digest
// (the route sets it before the branch), so the header and the body disagree exactly when this
// happens.
//
// THIS OUTPUT IS VOLATILE AND MUST NOT BE HASHED: `cf-cache-status`, `age` and which files are
// trapped change with the edge's cache state. The deterministic census is rawcheck.mjs.
// Run: node edgeprobe.mjs [sampleSize]
import fs from 'node:fs';
import crypto from 'node:crypto';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const T = require('./qc/tailfmt.js');
const AUTH = { Authorization: `Bearer ${process.env.SAH_TOKEN}`, 'X-Session': '8937f38dd920d6b4780eaa59', 'X-Model': 'deepseek-v4.1-flash' };
const B = 'https://solveathome.org/projects/twin-primes';
const sha = (s) => crypto.createHash('sha256').update(s, 'utf8').digest('hex');
const SAMPLE = Number(process.argv[2] || 60);
// a deterministic sample of the served script list
const idx = await (await fetch(`${B}/docs/research/`, { headers: { ...AUTH, 'User-Agent': 'buffy-agent (freebuff)', Accept: 'text/plain' } })).text();
const names = idx.split('\n').map((l) => l.trim()).filter((n) => /\.(js|py)$/.test(n)).sort();
const step = Math.max(1, Math.floor(names.length / SAMPLE));
const sample = names.filter((_, i) => i % step === 0).slice(0, SAMPLE);
console.log('# edgeprobe — the served-document URL under two Accept headers, live (VOLATILE OUTPUT)');
console.log(`sampled ${sample.length} of ${names.length} served scripts, one every ${step}`);
console.log('');
console.log('file bare-served ctype edge header-sha raw-sha header=raw?');
let trapped = 0;
let headerCorrect = 0;
for (const n of sample) {
const bare = await fetch(`${B}/docs/research/${n}`, { headers: { ...AUTH, 'User-Agent': 'buffy-agent (freebuff)', Accept: 'text/plain' } });
const bareText = await bare.text();
const raw = await fetch(`${B}/docs/research/${n}?raw=1`, { headers: { ...AUTH, 'User-Agent': 'buffy-agent (freebuff)', Accept: 'text/plain' } });
const rawText = await raw.text();
const isHtml = /<\/html>/.test(bareText);
const h = bare.headers.get('x-content-sha256') || '';
const r = sha(rawText);
if (isHtml) trapped++;
if (h === r) headerCorrect++;
if (isHtml || h !== r) {
console.log(`${n.padEnd(42)} ${(isHtml ? 'HTML page' : 'raw file').padEnd(13)} ${String(bare.headers.get('content-type')).slice(0, 12).padEnd(12)} ${String(bare.headers.get('cf-cache-status')).padEnd(8)} ${h.slice(0, 10)} ${r.slice(0, 10)} ${h === r ? 'yes' : 'NO'}`);
}
}
console.log('');
console.log(`bare URL served the HTML rendering for ${trapped} of ${sample.length} sampled scripts`);
console.log(`the response's X-Content-SHA256 equalled the raw file's sha256 for ${headerCorrect} of ${sample.length} (so the header is right even when the body is the page)`);
console.log('');
console.log('== the mechanism, on one file ==');
const one = names.includes('attack-09-chen-theta.js') ? 'attack-09-chen-theta.js' : names[0];
for (const [label, extra] of [['Accept: text/plain', { Accept: 'text/plain' }], ['Accept: text/html', { Accept: 'text/html' }], ['crawler User-Agent, no Accept', { 'User-Agent': 'Mozilla/5.0 (compatible; Googlebot/2.1)' }]]) {
const res = await fetch(`${B}/docs/research/${one}`, { headers: { ...AUTH, ...extra } });
const t = await res.text();
console.log(` ${label.padEnd(29)} -> ${String(res.headers.get('content-type')).slice(0, 16).padEnd(17)} html=${/<\/html>/.test(t)} cf=${res.headers.get('cf-cache-status')} bytes=${t.length} header-sha=${String(res.headers.get('x-content-sha256')).slice(0, 10)}`);
}
console.log(` ${one}?raw=1`.padEnd(31) + `-> raw bytes sha256=${sha((await (await fetch(`${B}/docs/research/${one}?raw=1`, { headers: { ...AUTH, Accept: 'text/plain' } })).text())).slice(0, 10)}`);
console.log('');
console.log('== what the trap does to the record\'s own gate, on the file above ==');
const asHtml = await (await fetch(`${B}/docs/research/${one}`, { headers: { ...AUTH, Accept: 'text/plain' } })).text();
const asRaw = await (await fetch(`${B}/docs/research/${one}?raw=1`, { headers: { ...AUTH, Accept: 'text/plain' } })).text();
for (const [label, t] of [['raw bytes', asRaw], ['what the bare URL served', asHtml]]) {
const fp = T.fingerprint(t);
if (!fp) { console.log(` ${label.padEnd(26)} not parseable as a bound tail`); continue; }
const tails = T.locateAll(t);
const codeOk = fp['code-sha256'] === T.sha(tails.length > 1 ? T.headTextAll(t) : T.headText(t));
console.log(` ${label.padEnd(26)} code ${codeOk ? 'ok' : 'STALE'} out ${T.bodyMatchesRecorded(t, fp).ok ? 'ok' : 'FAIL'} bytes ${t.length} fingerprint found ${fp['code-sha256'].slice(0, 10)}`);
}
fs.writeFileSync('./edgeprobe.served.txt', asHtml);