Bound ordinary Restricted Execution engine socket directories (#503)

This commit is contained in:
Quixos Development Agent 01f26965-84fa-4a79-8069-725682c13065
2026-10-29 12:30:56 +00:00
parent 3bb4324aa0
commit 43fcf6b27a
2 changed files with 120 additions and 13 deletions
+47 -2
View File
@@ -1,11 +1,12 @@
import test from "node:test";
import assert from "node:assert/strict";
import { PassThrough } from "node:stream";
import { mkdtemp, writeFile, rm } from "node:fs/promises";
import { mkdtemp, writeFile, rm, mkdir, readdir, lstat } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { join, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { serveRestrictedRegistry, RestrictedEngineBudget } from "./service.mjs";
import { socketDirectory, checkedSocketPath } from "../socket-address.mjs";
const workerd = process.env.QUIXOS_TEST_WORKERD;
if (!workerd) throw Error("Set QUIXOS_TEST_WORKERD to the pinned engine");
@@ -95,6 +96,50 @@ async function fixture(source, options = {}) {
}
const registry = (code) => `export const registry={exports:['test'],async invoke(request,channel){${code}}};`;
test("real engine sockets and retirement are independent of deep Unicode TMPDIR", async () => {
const root = await mkdtemp(join(tmpdir(), "restricted-deep-"));
const deep = join(root, "保留された状態".repeat(12));
await mkdir(deep);
const oldTmp = process.env.TMPDIR;
const socketRoot = dirname(socketDirectory({ kind: "qualification-probe" }));
const names = async () =>
readdir(socketRoot).catch((error) => {
if (error.code !== "ENOENT") throw error;
return [];
});
const before = new Set(await names());
let f, namespace;
process.env.TMPDIR = deep;
try {
assert.ok(Buffer.byteLength(join(deep, "qx-engine-XXXXXX", "engine.sock")) > 100);
f = await fixture(registry("return request.input;"));
f.start("deep runtime");
const result = await f.receive();
assert.equal(result.status, "returned");
assert.equal(result.output, "deep runtime");
assert.equal(f.stats().starts, 1);
const created = (await names()).filter((name) => !before.has(name));
assert.equal(created.length, 1);
namespace = join(socketRoot, created[0]);
const engines = await readdir(namespace);
assert.equal(engines.length, 1);
for (const name of ["broker.sock", "engine.sock"]) {
const endpoint = checkedSocketPath(join(namespace, engines[0], name));
assert.ok((await lstat(endpoint)).isSocket());
}
} finally {
try {
await f?.close();
} finally {
if (oldTmp === undefined) delete process.env.TMPDIR;
else process.env.TMPDIR = oldTmp;
}
}
await assert.rejects(lstat(namespace), { code: "ENOENT" });
assert.ok((await lstat(deep)).isDirectory());
await rm(root, { recursive: true, force: true });
});
test("uncertain OS cleanup cannot release engine capacity", async () => {
const budget = new RestrictedEngineBudget(1, async () => ({
async release() {