// Who may press the Posts library's routes (review 1, M1): umtool's own page // in a browser, and nothing else — a request with no browser header is NOT // the operator. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { AGENTS_GO_ELSEWHERE, pressRefusal } from "./caller.mjs"; const h = (o) => new Headers(o); const ENV = {}; test("the page's own fetch presses: a same-origin browser request to this machine", () => { assert.equal(pressRefusal(h({ host: "localhost:3050", origin: "http://localhost:3050", "sec-fetch-site": "same-origin" }), ENV), null); assert.equal(pressRefusal(h({ host: "127.0.0.1:3050", origin: "http://127.0.0.1:3050" }), ENV), null); assert.equal(pressRefusal(h({ host: "localhost:3050", "sec-fetch-site": "same-origin" }), ENV), null); }); test("a request no browser sent is not the operator: curl, a script, an agent, the editor — with a bearer or with nothing", () => { for (const headers of [{}, { host: "localhost:3050" }, { host: "localhost:3050", "content-type": "application/json" }, { host: "localhost:3050", "user-agent": "curl/8" }]) { assert.deepEqual(pressRefusal(h(headers), ENV), { status: 403, error: AGENTS_GO_ELSEWHERE }); } // A bearer is refused even from a browser-shaped request. assert.deepEqual(pressRefusal(h({ host: "localhost:3050", origin: "http://localhost:3050", "sec-fetch-site": "same-origin", authorization: "Bearer x" }), ENV), { status: 403, error: AGENTS_GO_ELSEWHERE }); assert.match(AGENTS_GO_ELSEWHERE, /pnpm ops judge-trending/); assert.match(AGENTS_GO_ELSEWHERE, /"by": "agent"/); }); test("another site's page, a sibling site's, an opaque origin and a host that is not this machine are refused", () => { for (const headers of [ { host: "localhost:3050", origin: "https://example.org", "sec-fetch-site": "cross-site" }, { host: "localhost:3050", origin: "http://localhost:3001", "sec-fetch-site": "same-site" }, { host: "localhost:3050", origin: "null" }, { host: "rebound.example.org", origin: "http://rebound.example.org", "sec-fetch-site": "same-origin" }, ]) { const no = pressRefusal(h(headers), ENV); assert.equal(no?.status, 403, JSON.stringify(headers)); assert.notEqual(no?.error, AGENTS_GO_ELSEWHERE); } });