Refuse cross-site POSTs to the state-changing routes and grant no CORS preflight
POST /gpu-permission and POST /motion/generate now require Content-Type application/json and refuse a request whose Origin is present and not the daemon's own loopback origin, 403 cross-site, before the body is read. The blanket OPTIONS handler that approved any preflight is deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5a60255ac2
commit
cb36beb09a
2 changed files with 56 additions and 10 deletions
|
|
@ -144,6 +144,48 @@ test("http: the switch is the household's", async () => {
|
|||
}
|
||||
});
|
||||
|
||||
test("http: a web page cannot flip the household's switch or start a job", async () => {
|
||||
const stateRoot = await mkdtemp(path.join(os.tmpdir(), "vili-gate-csrf-"));
|
||||
const d = await bootDaemon(stateRoot);
|
||||
const post = (route, headers, body) => fetch(url(d, route), { method: "POST", headers, body });
|
||||
const prompt = JSON.stringify({ prompt: "a person waves hello, never seen before" });
|
||||
try {
|
||||
const before = await readPermission(d);
|
||||
for (const route of ["/gpu-permission", "/motion/generate"]) {
|
||||
const payload = route === "/gpu-permission" ? JSON.stringify({ allowed: true }) : prompt;
|
||||
const simple = await post(route, { "content-type": "text/plain" }, payload);
|
||||
assert.equal(simple.status, 403, `${route}: a simple text/plain POST`);
|
||||
assert.deepEqual(await simple.json(), { ok: false, code: "cross-site" });
|
||||
assert.equal((await post(route, {}, payload)).status, 403, `${route}: no content-type`);
|
||||
const hostile = await post(route, { "content-type": "application/json", origin: "http://evil.example-origin" }, payload);
|
||||
assert.equal(hostile.status, 403, `${route}: a hostile Origin`);
|
||||
const body = JSON.stringify(await hostile.json());
|
||||
assert.equal(body.includes("evil.example-origin"), false, "the refusal never echoes the Origin");
|
||||
assert.equal(
|
||||
(await post(route, { "content-type": "application/json", origin: `http://127.0.0.1:${d.port}.evil.example` }, payload)).status,
|
||||
403,
|
||||
`${route}: an Origin that only starts like ours`,
|
||||
);
|
||||
}
|
||||
assert.deepEqual(await readPermission(d), before, "no refused request changed anything");
|
||||
assert.deepEqual(await readdir(path.join(stateRoot, "jobs")), [], "no job file");
|
||||
|
||||
const preflight = await fetch(url(d, "/gpu-permission"), {
|
||||
method: "OPTIONS",
|
||||
headers: { origin: "http://evil.example-origin", "access-control-request-method": "POST", "access-control-request-headers": "content-type" },
|
||||
});
|
||||
assert.equal(preflight.ok, false, "the daemon approves no CORS preflight");
|
||||
assert.equal(preflight.headers.get("access-control-allow-methods"), null);
|
||||
|
||||
const own = await post("/gpu-permission", { "content-type": "application/json; charset=utf-8", origin: `http://127.0.0.1:${d.port}` }, JSON.stringify({ allowed: true }));
|
||||
assert.equal(own.status, 200, "the daemon's own origin still works");
|
||||
assert.equal((await setPermission(d, { allowed: false })).status, 200, "a caller with no Origin (the tray) still works");
|
||||
} finally {
|
||||
await d.stop();
|
||||
await rm(stateRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("http: a malformed override refuses to start and names the variable, not its value", async () => {
|
||||
const stateRoot = await mkdtemp(path.join(os.tmpdir(), "vili-gate-env-"));
|
||||
const child = spawn(process.execPath, [daemonPath, "--host", "127.0.0.1", "--port", String(nextPort++), "--state-root", stateRoot], {
|
||||
|
|
|
|||
|
|
@ -547,8 +547,21 @@ async function readRequestBody(request) {
|
|||
|
||||
const loopbackAddresses = new Set(["127.0.0.1", "::1", "::ffff:127.0.0.1"]);
|
||||
|
||||
// A state-changing POST is a service's or the tray's call, never a web page's. A browser sends a
|
||||
// cross-site page's POST as a no-preflight simple request (text/plain) or with an Origin that is not
|
||||
// ours, so both are refused before the body is read. The daemon grants no CORS preflight at all.
|
||||
function refuseCrossSite(request, response) {
|
||||
const origin = request.headers.origin;
|
||||
const ownOrigins = [`http://127.0.0.1:${args.port}`, `http://localhost:${args.port}`, `http://[::1]:${args.port}`];
|
||||
const type = String(request.headers["content-type"] || "").split(";")[0].trim().toLowerCase();
|
||||
if (type === "application/json" && (origin === undefined || ownOrigins.includes(origin))) return false;
|
||||
json(response, 403, { ok: false, code: "cross-site" });
|
||||
return true;
|
||||
}
|
||||
|
||||
// The household's switch: only a caller on this machine (the tray, in the household's session) sets it.
|
||||
async function setGpuPermission(request, response) {
|
||||
if (refuseCrossSite(request, response)) return;
|
||||
if (!loopbackAddresses.has(request.socket.remoteAddress)) {
|
||||
json(response, 403, { ok: false, code: "household-only" });
|
||||
return;
|
||||
|
|
@ -568,6 +581,7 @@ async function setGpuPermission(request, response) {
|
|||
}
|
||||
|
||||
async function generateMotion(request, response) {
|
||||
if (refuseCrossSite(request, response)) return;
|
||||
let body;
|
||||
try {
|
||||
body = await readRequestBody(request);
|
||||
|
|
@ -729,16 +743,6 @@ async function runMotionJob(body, prompt, response) {
|
|||
|
||||
async function handleRequest(request, response) {
|
||||
const url = new URL(request.url, `http://${request.headers.host || `${args.host}:${args.port}`}`);
|
||||
if (request.method === "OPTIONS") {
|
||||
response.writeHead(204, {
|
||||
"access-control-allow-origin": "*",
|
||||
"access-control-allow-methods": "GET,POST,OPTIONS",
|
||||
"access-control-allow-headers": "content-type",
|
||||
});
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.pathname === "/" || url.pathname === "/health") {
|
||||
await refreshBackendStatus({ includeKimodoHelp: false });
|
||||
json(response, 200, { ok: true, service: "vili", providerId, state: operatorState() });
|
||||
|
|
|
|||
Loading…
Reference in a new issue