fix: reject credential-bearing Ito CLI shims

This commit is contained in:
Affaan Mustafa
2026-07-23 23:50:39 -04:00
parent 7dc2c116e7
commit 6d9e32e917
2 changed files with 47 additions and 17 deletions
+12 -17
View File
@@ -127,6 +127,11 @@ function assertUsableExecutable(candidate) {
`${EXECUTABLE_OVERRIDE} does not point to a readable local Itô CLI file.`
);
}
if (path.extname(canonicalCandidate).toLowerCase() !== ".js") {
throw new Error(
`${EXECUTABLE_OVERRIDE} must point to the canonical dist/bin/ito.js entry.`
);
}
if (!isUsableExecutable(canonicalCandidate)) {
throw new Error(
`${EXECUTABLE_OVERRIDE} does not point to a readable local Itô CLI file.`
@@ -139,11 +144,7 @@ function isUsableExecutable(candidate) {
try {
const info = fs.statSync(candidate);
if (!info.isFile()) return false;
if (process.platform !== "win32" && path.extname(candidate) !== ".js") {
fs.accessSync(candidate, fs.constants.X_OK);
} else {
fs.accessSync(candidate, fs.constants.R_OK);
}
fs.accessSync(candidate, fs.constants.R_OK);
return true;
} catch {
return false;
@@ -151,21 +152,15 @@ function isUsableExecutable(candidate) {
}
function buildInvocation(executable, args) {
if (path.extname(executable).toLowerCase() === ".js") {
return Object.freeze({
executable: process.execPath,
args: Object.freeze([executable, ...args]),
});
}
if (
process.platform === "win32"
&& /\.(?:bat|cmd|ps1)$/i.test(executable)
) {
if (path.extname(executable).toLowerCase() !== ".js") {
throw new Error(
`Refusing to invoke the Itô CLI through a shell shim. Set ${EXECUTABLE_OVERRIDE} to the absolute dist/bin/ito.js path.`
`Refusing to invoke an Itô CLI shim. Set ${EXECUTABLE_OVERRIDE} to the absolute dist/bin/ito.js path.`
);
}
return Object.freeze({ executable, args: Object.freeze([...args]) });
return Object.freeze({
executable: process.execPath,
args: Object.freeze([executable, ...args]),
});
}
function invokeIto(executable, args, environment = process.env) {
+35
View File
@@ -230,6 +230,41 @@ function main() {
fs.rmSync(collisionDirectory, { recursive: true, force: true });
}
}],
["rejects an absolute POSIX shim before it can resolve an interpreter through PATH", () => {
if (process.platform === "win32") return;
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "ecc-hostile-ito-shim-"));
const shim = path.join(directory, "ito");
const hostileNode = path.join(directory, "node");
const stolenEnvironment = path.join(directory, "stolen.json");
try {
fs.writeFileSync(shim, "#!/usr/bin/env node\n");
fs.writeFileSync(
hostileNode,
[
"#!/bin/sh",
`env > ${JSON.stringify(stolenEnvironment)}`,
"",
].join("\n")
);
fs.chmodSync(shim, 0o755);
fs.chmodSync(hostileNode, 0o755);
const result = runCli(["ito", "auth"], {
ECC_ITO_CLI_EXECUTABLE: shim,
ITO_API_KEY: "must-never-reach-shim-interpreter",
PATH: directory,
});
assert.notStrictEqual(result.status, 0);
assert.match(result.stderr, /canonical dist\/bin\/ito\.js/i);
assert.ok(
!fs.existsSync(stolenEnvironment),
"a shim-resolved interpreter must never receive the Itô credential"
);
} finally {
fs.rmSync(directory, { recursive: true, force: true });
}
}],
["rejects a relative executable override instead of searching or guessing", () => {
const result = runCli(["ito", "status"], {
ECC_ITO_CLI_EXECUTABLE: "ito",