fix(triage): review round 3: the host checks every URL in a file the module wrote under bundle/, home/, work/ or tmp/, as written or as a URL parser reads it; refuses a backslash in a file JSON.parse refuses; refuses a change to its own bundle files or the rest of the run directory; and a refused start removes work/ and tmp/ too (#1873, lane harness-matrix)
This commit is contained in:
1 parent
6a24cf743d
commit
680bb90af3
3 files changed
+104
-13
No files matched your search
@@ -41,7 +41,9 @@
|
||||
// level is the module's "low", or its first level. These join the route's fenced harnessEnv in
|
||||
// session.json. The key is never among them: #1873 harness-matrix gives bundle() and env() the
|
||||
// real proxy (pi takes it through models.json), and a MOSAIC_ value or any file under the run's
|
||||
// bundle/, home/, work/ or tmp/ that holds the key, raw or JSON-decoded, refuses the start. MOSAIC_CLAUDE_BIN, the pinned binary's absolute path, passes from the
|
||||
// bundle/, home/, work/ or tmp/ that holds the key, raw or JSON-decoded, refuses the start, as
|
||||
// does a URL other than the proxy's in a file the module wrote there, or a change the module made
|
||||
// to the rest of the run directory (session/). MOSAIC_CLAUDE_BIN, the pinned binary's absolute path, passes from the
|
||||
// Console's environment as it does for a bus session. An answer the agent sends has the key, and
|
||||
// anything shaped like it, replaced before it reaches the transcript.
|
||||
//
|
||||
@@ -409,11 +411,17 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
if (!key) throw new Error("no key");
|
||||
const holdsKey = (v) => typeof v === "string" && (v.includes(key) || Boolean(tokenAnywhere && v.match(tokenAnywhere)));
|
||||
// A file's text, and when it parses as JSON every key and string in it, decoded: an escaped key
|
||||
// or URL ("\u0061...", "http:\/\/...") is seen as the harness that parses the file sees it.
|
||||
// or URL ("\u0061...", "http:\/\/...") is seen as the harness that parses the file sees it. A
|
||||
// harness can parse more loosely than JSON.parse (pi takes a byte order mark, comments and
|
||||
// trailing commas), so a file that does not parse may hold no backslash: its escapes would be
|
||||
// decoded by the harness and not here.
|
||||
const decoded = (text) => {
|
||||
const out = [text];
|
||||
let doc;
|
||||
try { doc = JSON.parse(text); } catch { return out; }
|
||||
try { doc = JSON.parse(text); } catch {
|
||||
if (text.includes("\\")) throw new Error("bundle file holds an escape it does not parse");
|
||||
return out;
|
||||
}
|
||||
const stack = [doc];
|
||||
while (stack.length) {
|
||||
const v = stack.pop();
|
||||
@@ -439,13 +447,51 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
const session = { model: admitted.model, provider: admitted.provider ?? "", thinking, toolSocket, ...(admitted.transport ? { proxyTransport: { ...admitted.transport } } : {}) };
|
||||
const models = { triage: { provider: admitted.provider ?? "", model: admitted.model } };
|
||||
const transports = admitted.transport ? { triage: { ...admitted.transport } } : {};
|
||||
// The rest of the run directory (session/, kept across starts) is the module's to leave alone:
|
||||
// what it holds before bundle() must be what it holds after env().
|
||||
const scanned = new Set(["bundle", "home", "work", "tmp"]);
|
||||
const kept = () => {
|
||||
const seen = new Map();
|
||||
for (const name of readdirSync(paths.agent, { recursive: true })) {
|
||||
const rel = String(name);
|
||||
if (scanned.has(rel.split("/")[0])) continue;
|
||||
const st = lstatSync(join(paths.agent, rel));
|
||||
seen.set(rel, `${st.mode}:${st.ino}:${st.size}:${st.mtimeMs}:${st.nlink}`);
|
||||
}
|
||||
return seen;
|
||||
};
|
||||
const before = kept();
|
||||
// The host's own bundle files (prompt, policy, tools) must be as the host wrote them.
|
||||
const hostFiles = new Map(Object.values(files).map((f) => [f, readFileSync(f, "utf8")]));
|
||||
const own = mod.bundle({ dir: bundleDir, files: { ...files }, session: { ...session }, write, quote, models, proxy: { ...proxy }, transports });
|
||||
const ownFiles = plainObject(own?.files) ? own.files : {};
|
||||
const given = mod.env({ files: { ...ownFiles }, policy, tools, session: { ...session }, models, proxy: { ...proxy } });
|
||||
if (!plainObject(given)) throw new Error("env refused");
|
||||
const after = kept();
|
||||
const changed = [...after].filter(([k, v]) => before.get(k) !== v).map(([k]) => k);
|
||||
if (changed.length || after.size !== before.size) {
|
||||
// What the module added or changed goes, and the next start takes a fresh session.
|
||||
for (const k of changed) rmSync(join(paths.agent, k), { recursive: true, force: true });
|
||||
freshSession = true;
|
||||
throw new Error("bundle changed the run directory");
|
||||
}
|
||||
// A module can write by its own means too, and name only some of its files. writeBundle makes
|
||||
// bundle/, home/, work/ and tmp/ fresh just before, so every file in them came from this start:
|
||||
// each is read again, and each must be a regular file with no key in it, raw or decoded.
|
||||
// each is read again, and each must be a regular file with no key in it, raw or decoded, and,
|
||||
// past the host's own bundle files, no URL but the proxy's (its root or /v1): a harness reads
|
||||
// files from its HOME and workspace that it does not declare (pi's ~/.pi/agent/models.json).
|
||||
// These fences see what the module left when env() returns. They do not stop a module that
|
||||
// hides the key (base64, split across files) or writes after: harness modules are reviewed
|
||||
// repo code, and the fences catch their mistakes, not their intent.
|
||||
const proxyUrl = (u) => u === proxy.baseUrl || u === proxy.baseUrl + "/v1";
|
||||
const SPECIAL = new Set(["http:", "https:", "ws:", "wss:", "ftp:", "file:"]);
|
||||
// Every URL a string names, as written ("http://", "http:host", "http:\\host") or as a URL
|
||||
// parser reads the whole string (it drops tabs and newlines anywhere and controls at the ends).
|
||||
const urlsIn = (text) => {
|
||||
const out = [...text.matchAll(/[a-z][a-z0-9+.-]*:\/\/[^\s"'<>\\]*/gi), ...text.matchAll(/\b(?:https?|wss?|ftp|file):[^\s"'<>]*/gi)].map(([u]) => u);
|
||||
if (URL.canParse(text) && SPECIAL.has(new URL(text).protocol)) out.push(text);
|
||||
return out;
|
||||
};
|
||||
const strings = new Map();
|
||||
for (const d of ["bundle", "home", "work", "tmp"]) {
|
||||
const root = join(paths.agent, d);
|
||||
@@ -454,8 +500,11 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
const st = lstatSync(f);
|
||||
if (st.isDirectory()) continue;
|
||||
if (!st.isFile()) throw new Error("bundle file is not a regular file");
|
||||
const all = decoded(readFileSync(f, "utf8"));
|
||||
const text = readFileSync(f, "utf8");
|
||||
if (hostFiles.has(f)) { if (text !== hostFiles.get(f)) throw new Error("bundle changed a host file"); continue; }
|
||||
const all = decoded(text);
|
||||
if (all.some(holdsKey)) throw new Error("bundle file holds the key");
|
||||
if (all.some((t) => urlsIn(t).some((u) => !proxyUrl(u)))) throw new Error("bundle file names another URL");
|
||||
strings.set(f, all);
|
||||
}
|
||||
}
|
||||
@@ -480,10 +529,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
if (!st.isFile() || st.nlink !== 1) throw new Error("bundle file is not a regular file");
|
||||
const all = strings.get(f);
|
||||
if (!all) throw new Error("bundle file was not read");
|
||||
for (const text of all) for (const [u] of text.matchAll(/[a-z][a-z0-9+.-]*:\/\/[^\s"'<>\\]*/gi)) {
|
||||
if (u !== proxy.baseUrl && u !== proxy.baseUrl + "/v1") throw new Error("bundle file names another URL");
|
||||
urlInFile = true;
|
||||
}
|
||||
if (all.some((t) => urlsIn(t).length > 0)) urlInFile = true;
|
||||
harnessFiles.push(f);
|
||||
}
|
||||
if (given.ANTHROPIC_BASE_URL === undefined && !urlInFile) throw new Error("harness takes no proxy URL");
|
||||
@@ -523,7 +569,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
try { ({ env: hostEnv, files: harnessFiles } = hostHarnessEnv(mod, { admitted, bundleDir: join(a, "bundle"), files: bundle, policy, tools, toolSocket: join(a, "tools.sock") })); }
|
||||
catch {
|
||||
log("triage host: route refused: harness-bundle");
|
||||
for (const d of ["bundle", "home"]) rmSync(join(a, d), { recursive: true, force: true });
|
||||
for (const d of ["bundle", "home", "work", "tmp"]) rmSync(join(a, d), { recursive: true, force: true });
|
||||
throw new TriageError("triage-route-invalid", "the model route's answer is not usable");
|
||||
}
|
||||
const run = `triage-${now().toISOString().replace(/[^0-9]/g, "").slice(0, 14)}`;
|
||||
@@ -757,7 +803,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
// Nothing of the refused start stays: the relay, its socket, the bundle and session.json go.
|
||||
relay?.close();
|
||||
relay = null;
|
||||
for (const f of ["relay.sock", "session.json", "bundle", "home"]) rmSync(join(paths.agent, f), { recursive: true, force: true });
|
||||
for (const f of ["relay.sock", "session.json", "bundle", "home", "work", "tmp"]) rmSync(join(paths.agent, f), { recursive: true, force: true });
|
||||
throw new TriageError("triage-route-invalid", "the model route's answer is not usable");
|
||||
}
|
||||
const argv = hostedArgv(list, paths.agent, [node, RUNNER, paths.agent], { bwrap });
|
||||
|
||||
@@ -472,7 +472,7 @@ test("a route that changes after the first check is refused at the binds: nothin
|
||||
assert.ok(logs.includes("triage host: route refused at the binds: adapter"), logs.join(" | "));
|
||||
assert.equal(existsSync(join(root, "spawned")), false);
|
||||
assert.equal(h.running, false);
|
||||
for (const f of ["session.json", "relay.sock", "bundle"]) assert.equal(existsSync(join(agent, f)), false, f);
|
||||
for (const f of ["session.json", "relay.sock", "bundle", "home", "work", "tmp"]) assert.equal(existsSync(join(agent, f)), false, f);
|
||||
const [owner, note, ...rest] = (await h.state()).transcript;
|
||||
assert.deepEqual([owner.from, note.from, note.in_reply_to, note.text, rest.length], ["owner", "host", owner.id, ROUTE_REFUSED, 0]);
|
||||
rmSync(join(root, "install", "adapter.sh"));
|
||||
|
||||
@@ -586,7 +586,8 @@ test("#1873 fence: a pi-shaped answer passes the host's fences, and each fence s
|
||||
assert.ok(r.logs.includes(`triage host: route refused: ${fenceName}`), `${label}: ${r.logs.join(" | ")}`);
|
||||
assert.equal(existsSync(join(rootN, "spawned")), false, `${label}: nothing launched`);
|
||||
assert.equal(existsSync(join(r.dataRoot, "triage", "agent", "home", "harness.json")), false, `${label}: the harness file is removed`);
|
||||
assertNoKey(k, [["host log", r.logs.join("\n")]]);
|
||||
// Nothing the refused start wrote keeps the key: not the log, not a file under the run directory.
|
||||
assertNoKey(k, [["host log", r.logs.join("\n")], ...filesUnder(join(r.dataRoot, "triage", "agent"))]);
|
||||
};
|
||||
await fence("both key names", { module: piLike(), patch: (x) => ({ ...x, env: { ...x.env, ANTHROPIC_AUTH_TOKEN: x.env.ANTHROPIC_API_KEY } }), fenceName: "route-env" });
|
||||
await fence("no key name", { module: piLike(), patch: (x) => ({ ...x, env: { ANTHROPIC_BASE_URL: x.env.ANTHROPIC_BASE_URL } }), fenceName: "route-env" });
|
||||
@@ -633,6 +634,50 @@ test("#1873 fence: a pi-shaped answer passes the host's fences, and each fence s
|
||||
return { files: { harness: f } };
|
||||
} }), fenceName: "harness-bundle" });
|
||||
await fence("a link under home/", { module: piWith(({ dir }) => symlinkSync("/etc/hostname", join(agentOf(dir), "home", "link"))), fenceName: "harness-bundle" });
|
||||
// A URL a harness reads without "//", or that a URL parser reads once it drops a newline.
|
||||
const withUrl = (url) => harnessFile((proxy) => JSON.stringify({ a: proxy.baseUrl, baseUrl: url }));
|
||||
await fence("a harness file that names a URL with no slashes", { module: withUrl("http:api.example.invalid/v1"), fenceName: "harness-bundle" });
|
||||
await fence("a harness file that names a URL with backslashes", { module: withUrl("http:\\\\api.example.invalid/v1"), fenceName: "harness-bundle" });
|
||||
await fence("a harness file that names a URL with a newline in its scheme", { module: withUrl("h\nttp:api.example.invalid/v1"), fenceName: "harness-bundle" });
|
||||
// A file the module does not name, which the harness reads from HOME all the same (pi's
|
||||
// ~/.pi/agent/models.json), while env() names the proxy.
|
||||
const undeclared = (url) => piLike({
|
||||
env: ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_API_KEY: proxy.key, ANTHROPIC_BASE_URL: proxy.baseUrl } : {}) }),
|
||||
bundle: ({ dir, proxy }) => {
|
||||
mkdirSync(join(agentOf(dir), "home", ".pi", "agent"), { recursive: true });
|
||||
writeFileSync(join(agentOf(dir), "home", ".pi", "agent", "models.json"), JSON.stringify({ providers: { x: { baseUrl: url(proxy) } } }), { flag: "wx" });
|
||||
return { files: {} };
|
||||
},
|
||||
});
|
||||
await fence("an unnamed file under home/ that names another URL", { module: undeclared(() => "https://api.example.invalid/v1"), fenceName: "harness-bundle" });
|
||||
// A file JSON.parse refuses but a looser parser takes (pi: a byte order mark, comments, trailing
|
||||
// commas) may hold no escape the host cannot decode.
|
||||
const escapedUrl = (proxy) => `{"a":${JSON.stringify(proxy.baseUrl)},"baseUrl":"http:\\/\\/api.example.invalid\\/v1"}`;
|
||||
await fence("a harness file with a byte order mark and an escaped URL", { module: harnessFile((proxy) => `\uFEFF${escapedUrl(proxy)}`), fenceName: "harness-bundle" });
|
||||
await fence("a harness file with a comment and an escaped URL", { module: harnessFile((proxy) => `// models\n${escapedUrl(proxy)}`), fenceName: "harness-bundle" });
|
||||
await fence("a harness file with a trailing comma and an escaped key", { module: harnessFile((proxy) => `{"a":${JSON.stringify(proxy.baseUrl)},"apiKey":"${escapedKey(proxy.key)}",}`), fenceName: "harness-bundle" });
|
||||
keyOnDisk = null;
|
||||
await fence("a bundle file written through write() with a byte order mark and an escaped key", { module: piWith(({ write, dir, proxy }) => {
|
||||
try { write("auth.json", `\uFEFF{"apiKey":"${escapedKey(proxy.key)}"}`); } finally { keyOnDisk = existsSync(join(dir, "auth.json")); }
|
||||
}), fenceName: "harness-bundle" });
|
||||
assert.equal(keyOnDisk, false, "write() refuses a loose file with an escape before it exists");
|
||||
// session/, kept across starts, is not the module's to write: what it adds goes, and the next
|
||||
// start takes a fresh session.
|
||||
await fence("a file written under session/", { module: piWith(({ dir, proxy }) => writeFileSync(join(agentOf(dir), "session", "auth.txt"), proxy.key, { flag: "wx" })), fenceName: "harness-bundle" });
|
||||
// A URL with no slashes inside a longer line, which no URL parser reads whole.
|
||||
await fence("a plain-text harness file that names a URL with no slashes in a line", { module: harnessFile((proxy) => `${proxy.baseUrl}\nbaseUrl=http:api.example.invalid/v1\n`), fenceName: "harness-bundle" });
|
||||
// The host's own bundle files are the host's: a module that rewrites one is refused.
|
||||
await fence("a module that rewrites the host's policy file", { module: piWith(({ dir, proxy }) => writeFileSync(join(dir, "policy.json"), `{"k":"${escapedKey(proxy.key)}"}`)), fenceName: "harness-bundle" });
|
||||
// The unnamed models.json with only the proxy's URL launches.
|
||||
{
|
||||
const rootN = scratch();
|
||||
const p = await rig(t, { bwrap: spy(rootN), module: undeclared((proxy) => `${proxy.baseUrl}/v1`) });
|
||||
p.setRoute();
|
||||
issue(p.configDir);
|
||||
await p.h.message("hello");
|
||||
await waitFor(() => existsSync(join(rootN, "spawned")));
|
||||
await p.h.stop();
|
||||
}
|
||||
await fence("env changes the key at the host", { module: piLike({ env: ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_API_KEY: proxy.key.startsWith("triage-route-probe") ? proxy.key : `Bearer ${proxy.key}` } : {}) }) }), fenceName: "harness-bundle" });
|
||||
await fence("env drops the key at the host", { module: piLike({ env: ({ proxy }) => ({ FAKE_OK: "1", ...(proxy?.key.startsWith("triage-route-probe") ? { ANTHROPIC_API_KEY: proxy.key } : {}) }) }), fenceName: "harness-bundle" });
|
||||
await fence("env answers another value at the host", { module: piLike({ env: ({ proxy }) => ({ FAKE_OK: proxy && !proxy.key.startsWith("triage-route-probe") ? "2" : "1", ...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}) }) }), fenceName: "harness-bundle" });
|
||||
|
||||
Reference in new issue
Block a user