fix(row 332): proxy first check runs every key-independent refusal; testProxy test option (Vikunja #774)
F1: with system.proxy set, the first launcher check no longer returns before the account, sandbox, capacity and limit checks. It skips only the harness preflight and the session proxy key match, which need the key P2 admits in startLocked. A launch that can never start now refuses before authorizeLaunch, the workspace and checkouts, and modelPreflight. The first proxy pass accepts only credential "proxy" for a declared account, tries the fallback model when the primary refuses, and lets mayLaunch key-quota-no-model-key through so P2 can issue the first key. The final check is unchanged and still runs before newRun and spawn. F4: the proxy test option is renamed testProxy and createLauncher throws test-proxy-refused when it is given with system.proxy. launch-account.test.mjs is a second caller, so the option stays. Tests: proxy-launch-order.test.mjs (6), launcher.test.mjs F4 throw test; the managed launch fixture gains bwrap, admit and trace.ops.
This commit is contained in:
1 parent
1ff45c4ad6
commit
85ecdfbde5
5 files changed
+228
-53
No files matched your search
@@ -219,9 +219,11 @@ export function createLauncher({
|
||||
host,
|
||||
system,
|
||||
env = process.env,
|
||||
// P2 supplies only {baseUrl,key}; a factory may supply the current agent's
|
||||
// context per check. P3 never reads a key store or calls the proxy itself.
|
||||
proxy = null,
|
||||
// Tests only (#774 F4): a static session proxy context {baseUrl,key}, or a
|
||||
// factory giving one per check, for a provider "proxy" route while
|
||||
// system.proxy is unset. It skips P2 admission, so createLauncher refuses it
|
||||
// together with system.proxy. P3 never reads a key store or calls the proxy.
|
||||
testProxy = null,
|
||||
tracker = false,
|
||||
launches = true,
|
||||
makeLoader = createLoader,
|
||||
@@ -236,6 +238,8 @@ export function createLauncher({
|
||||
stopOptions = {},
|
||||
log = (l) => process.stderr.write(`mosaic-bus: ${l}\n`),
|
||||
}) {
|
||||
if (testProxy !== null && system.proxy)
|
||||
throw Object.assign(new Error("testProxy skips P2 admission, so it is refused with system.proxy set"), { code: "test-proxy-refused" });
|
||||
const dataRoot = system.dataRoot;
|
||||
function admission(generation = null) {
|
||||
try { return assertProcessing(dataRoot, generation); }
|
||||
@@ -395,14 +399,81 @@ export function createLauncher({
|
||||
let harness = await harnessOf(primary);
|
||||
let route = "primary";
|
||||
let routeReason = null;
|
||||
// The first check validates the request before authorization/workspace work.
|
||||
// Managed credentials are acquired once in startLocked, then supplied to
|
||||
// the final check. Never reject proxy-only login using upstream host env.
|
||||
if (system.proxy && !proxyReady)
|
||||
// A launch is checked twice. Without system.proxy both are full checks: the first before the
|
||||
// authorization, workspace and checkout work, the final one in startLocked before newRun. With
|
||||
// system.proxy the first check skips only what needs the key P2 admits in startLocked: the
|
||||
// harness preflight and the match of the session proxy key against launch.account.name. It
|
||||
// still runs the account declaration (credential "proxy" only), the sandbox, capacity, the
|
||||
// limit checks and admission, so a launch that can never start costs no authorization record,
|
||||
// checkout or key (#774 F1). The final check is always full, on the admitted key. Never reject
|
||||
// proxy-only login using upstream host env.
|
||||
const early = Boolean(system.proxy) && !proxyReady;
|
||||
// The checks that follow the route's preflight. The parameters shadow the outer route on
|
||||
// purpose: the first check of a proxy launch gates a route it hasn't chosen. sessionProxy is
|
||||
// null there, and the account must then name credential "proxy".
|
||||
const gate = async (model, harness, sessionProxy) => {
|
||||
const proxied = early || Boolean(sessionProxy);
|
||||
// Row 126 (#1620): only a launch on a harness whose module counts the account spends the named
|
||||
// account. The bus environment must hold the named set and no other credential variable, so the
|
||||
// session can't log in some other way. Messages name variables, never values.
|
||||
const account = call(model.harness, "countsAccount", () => harness.countsAccount()) === true ? b.launch.account?.name ?? null : null;
|
||||
if (account !== null) {
|
||||
// The usage records keep this name (bus schema 3i), so it must never be a credential's value.
|
||||
const holds = [...new Set(SESSION_CREDENTIALS.flat())].filter((k) => env[k] === account);
|
||||
if (sessionProxy?.key === account) holds.push("session proxy key");
|
||||
if (holds.length) throw new Refusal("account-is-credential", `launch.account.name is the value of ${holds.join(", ")}; it names the account, never its credential`);
|
||||
if (proxied || b.launch.account.credential === "proxy") {
|
||||
if (!proxied || b.launch.account.credential !== "proxy")
|
||||
throw new Refusal("account-credential-missing", `account ${account} needs the declared proxy credential`);
|
||||
} else {
|
||||
const named = b.launch.account.credential.split("+");
|
||||
if (!SESSION_CREDENTIALS.some((set) => set.join("+") === named.join("+")) || !named.every((k) => Boolean(env[k])))
|
||||
throw new Refusal("account-credential-missing", `account ${account} names ${named.join(" with ")}, which the bus host doesn't hold as a session login`);
|
||||
const other = [...new Set(SESSION_CREDENTIALS.flat())].filter((k) => !named.includes(k) && Boolean(env[k]));
|
||||
if (other.length) throw new Refusal("account-credential-ambiguous", `account ${account} names ${named.join(" with ")}, but the bus host also sets ${other.join(", ")}`);
|
||||
}
|
||||
}
|
||||
const unavailable = sandboxProblem({ bwrap });
|
||||
if (unavailable) throw new Refusal("sandbox-unavailable", unavailable);
|
||||
const hit=limitHit(registry.sessions,b.id,b.launch.limits,model);
|
||||
if(hit){const error=new Refusal("capacity-full",JSON.stringify(hit));error.limit=hit;throw error;}
|
||||
// Row 125 (#1619, REQ-LIMIT-4): the limit checks. An answer that isn't an allow refuses,
|
||||
// and so does a bus that can't answer (fail closed, lead decision 150).
|
||||
let may;
|
||||
try {
|
||||
may = await host.op({ op: "mayLaunch", business: b.id, instance, provider: model.provider, model: model.model, account });
|
||||
} catch (e) {
|
||||
throw new Refusal("limit-check-failed", `bus: ${e.code ?? "launch-op-refused"}`);
|
||||
}
|
||||
// Row 333: under a daily key quota the bus refuses key-quota-no-model-key for an agent P2
|
||||
// hasn't issued a key yet. Only the first check of a proxy launch lets that one answer pass:
|
||||
// startLocked's modelPreflight issues the key, and the final check asks again with it.
|
||||
if (early && may?.allow === false && may.check === null && may.code === "key-quota-no-model-key") return account;
|
||||
if (may?.allow !== true) {
|
||||
if (may?.hit) { const error = new Refusal("limit-reached", JSON.stringify(may.hit)); error.limit = { check: may.check, ...may.hit }; throw error; }
|
||||
throw new Refusal("limit-check-failed", `${may?.check ?? "bus"}: ${may?.code ?? "malformed-answer"}`);
|
||||
}
|
||||
return account;
|
||||
};
|
||||
if (early) {
|
||||
// The final check takes the fallback only when the primary's preflight fails, which this check
|
||||
// can't know yet, so it refuses only when neither route would pass, with the primary's reason.
|
||||
try {
|
||||
await gate(primary, harness, null);
|
||||
} catch (e) {
|
||||
if (!(e instanceof Refusal) || !fallback) throw e;
|
||||
try {
|
||||
await gate(fallback, await harnessOf(fallback), null);
|
||||
} catch (also) {
|
||||
throw also instanceof Refusal ? e : also;
|
||||
}
|
||||
}
|
||||
admission(admissionGeneration);
|
||||
return { resolved, model, account: null, harness, route, routeReason, admissionGeneration };
|
||||
}
|
||||
const proxyFor = async (selected) => {
|
||||
if (!system.proxy && selected.provider !== "proxy") return null;
|
||||
const context = system.proxy ? proxyContext : typeof proxy === "function" ? await proxy({ business: b.id, instance }) : proxy;
|
||||
const context = system.proxy ? proxyContext : typeof testProxy === "function" ? await testProxy({ business: b.id, instance }) : testProxy;
|
||||
if (!context || typeof context.key !== "string" || !context.key
|
||||
|| typeof context.baseUrl !== "string" || !/^http:\/\/127\.0\.0\.1:[0-9]+$/.test(context.baseUrl)) {
|
||||
throw new Refusal("no-session-credential", "proxy model needs its own session proxy context");
|
||||
@@ -425,42 +496,7 @@ export function createLauncher({
|
||||
if (also) throw new Refusal(also.code, `fallback ${fallback.harness}: ${also.message} (primary ${primary.harness}: ${failed.message})`);
|
||||
[model, harness, route, routeReason] = [fallback, second, "fallback", failed.code];
|
||||
}
|
||||
// Row 126 (#1620): only a launch on a harness whose module counts the account spends the named
|
||||
// account. The bus environment must hold the named set and no other credential variable, so the
|
||||
// session can't log in some other way. Messages name variables, never values.
|
||||
const account = call(model.harness, "countsAccount", () => harness.countsAccount()) === true ? b.launch.account?.name ?? null : null;
|
||||
if (account !== null) {
|
||||
// The usage records keep this name (bus schema 3i), so it must never be a credential's value.
|
||||
const holds = [...new Set(SESSION_CREDENTIALS.flat())].filter((k) => env[k] === account);
|
||||
if (sessionProxy?.key === account) holds.push("session proxy key");
|
||||
if (holds.length) throw new Refusal("account-is-credential", `launch.account.name is the value of ${holds.join(", ")}; it names the account, never its credential`);
|
||||
if (sessionProxy || b.launch.account.credential === "proxy") {
|
||||
if (!sessionProxy || b.launch.account.credential !== "proxy")
|
||||
throw new Refusal("account-credential-missing", `account ${account} needs the declared proxy credential`);
|
||||
} else {
|
||||
const named = b.launch.account.credential.split("+");
|
||||
if (!SESSION_CREDENTIALS.some((set) => set.join("+") === named.join("+")) || !named.every((k) => Boolean(env[k])))
|
||||
throw new Refusal("account-credential-missing", `account ${account} names ${named.join(" with ")}, which the bus host doesn't hold as a session login`);
|
||||
const other = [...new Set(SESSION_CREDENTIALS.flat())].filter((k) => !named.includes(k) && Boolean(env[k]));
|
||||
if (other.length) throw new Refusal("account-credential-ambiguous", `account ${account} names ${named.join(" with ")}, but the bus host also sets ${other.join(", ")}`);
|
||||
}
|
||||
}
|
||||
const unavailable = sandboxProblem({ bwrap });
|
||||
if (unavailable) throw new Refusal("sandbox-unavailable", unavailable);
|
||||
const hit=limitHit(registry.sessions,b.id,b.launch.limits,model);
|
||||
if(hit){const error=new Refusal("capacity-full",JSON.stringify(hit));error.limit=hit;throw error;}
|
||||
// Row 125 (#1619, REQ-LIMIT-4): the limit checks. An answer that isn't an allow refuses,
|
||||
// and so does a bus that can't answer (fail closed, lead decision 150).
|
||||
let may;
|
||||
try {
|
||||
may = await host.op({ op: "mayLaunch", business: b.id, instance, provider: model.provider, model: model.model, account });
|
||||
} catch (e) {
|
||||
throw new Refusal("limit-check-failed", `bus: ${e.code ?? "launch-op-refused"}`);
|
||||
}
|
||||
if (may?.allow !== true) {
|
||||
if (may?.hit) { const error = new Refusal("limit-reached", JSON.stringify(may.hit)); error.limit = { check: may.check, ...may.hit }; throw error; }
|
||||
throw new Refusal("limit-check-failed", `${may?.check ?? "bus"}: ${may?.code ?? "malformed-answer"}`);
|
||||
}
|
||||
const account = await gate(model, harness, sessionProxy);
|
||||
admission(admissionGeneration);
|
||||
return { resolved, model, account, harness, route, routeReason, admissionGeneration, sessionProxy };
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user