diff --git a/packages/ae-devtools-cli/a8n-transport/attached-session.mjs b/packages/ae-devtools-cli/a8n-transport/attached-session.mjs index 12d490c..73efc6d 100644 --- a/packages/ae-devtools-cli/a8n-transport/attached-session.mjs +++ b/packages/ae-devtools-cli/a8n-transport/attached-session.mjs @@ -807,6 +807,20 @@ export async function openAttachedSliceSession(options = {}) { aeA8nSource, aeA8nHead, tracePath: ae.tracePath, sessionRoot, installDir, appPath, target, attached: true, + // Host-side project guard (Issue #385): the attested + // app.project.file.fsName (ae-a8n's project.info reports exactly + // that string). dev-loop surfaces forward it into runOperation, + // which injects it into the consent-bound source so the host + // re-verifies project identity inside the same evaluation that + // performs the op — closing the guard→eval TOCTOU window. + // Null when the open project has no file (unsaved) or identity + // was unreadable; hostProjectGuardRequired then refuses + // mutations that cannot be host-verified. + hostProjectGuard: + project !== null && typeof project.file === "string" && project.file.length > 0 + ? project.file + : null, + hostProjectGuardRequired: true, // Re-validation immediately before each mutating dispatch — // dev-loop surfaces forward this as `targetGuard` into // runOperation, which invokes it after consent approval and diff --git a/packages/ae-devtools-cli/dev-loop/devloop.mjs b/packages/ae-devtools-cli/dev-loop/devloop.mjs index 8eb26f4..08fc069 100644 --- a/packages/ae-devtools-cli/dev-loop/devloop.mjs +++ b/packages/ae-devtools-cli/dev-loop/devloop.mjs @@ -178,6 +178,7 @@ export async function verify(input = {}) { const { runner, pin, projectIdentity, consentProvider, trustedScriptOptIn, scriptProvenance, readOpId = "read.projectSummary", readArgs = {}, requestId, + hostProjectGuard, hostProjectGuardRequired, } = input ?? {}; const op = (() => { try { return getOperation(readOpId); } catch { return null; } })(); if (!op || op.mutating) { @@ -192,5 +193,6 @@ export async function verify(input = {}) { return runOperation({ opId: readOpId, args: readArgs, runner, pin, projectIdentity, consentProvider, trustedScriptOptIn, scriptProvenance, requestId, + hostProjectGuard, hostProjectGuardRequired, }); } diff --git a/packages/ae-devtools-cli/dev-loop/session-open.mjs b/packages/ae-devtools-cli/dev-loop/session-open.mjs index 4b11f78..ea2030d 100644 --- a/packages/ae-devtools-cli/dev-loop/session-open.mjs +++ b/packages/ae-devtools-cli/dev-loop/session-open.mjs @@ -43,12 +43,18 @@ const EXP = join(HERE, ".."); * { kind, evidenceLevel, runner, pin, close, meta, slice, policy } — * `slice` is the raw slice-session result (a8n kinds) or null (fake); * `policy` carries session-imposed dispatch limits (attached sessions - * are read-only, Issue #366). + * are read-only, Issue #366). `hostProjectGuard` / + * `hostProjectGuardRequired` carry the session-attested project file + * identity for the host-side guard (Issue #385); null/false for fake + * and owned sessions. */ export async function openSession(kind = "fake", options = {}) { if (kind === "fake") { const s = openFakeSession(); - return { ...s, meta: s.meta ?? null, slice: null, policy: null, targetGuard: null }; + return { + ...s, meta: s.meta ?? null, slice: null, policy: null, targetGuard: null, + hostProjectGuard: null, hostProjectGuardRequired: false, + }; } if (kind === "a8n-attached") { const tdir = join(EXP, "a8n-transport", "attached-session.mjs"); @@ -93,6 +99,15 @@ export async function openSession(kind = "fake", options = {}) { // runOperation immediately before each mutating dispatch. Null // for sessions without an attested target identity. targetGuard: typeof s.revalidateTarget === "function" ? s.revalidateTarget : null, + // Host-side project guard (Issue #385): the session-attested + // app.project.file.fsName forwarded into runOperation, which + // injects it into the consent-bound source. Required on the + // attached lane — mutations without an attestable project file + // refuse before consent. + hostProjectGuard: typeof s.hostProjectGuard === "string" && s.hostProjectGuard.length > 0 + ? s.hostProjectGuard + : null, + hostProjectGuardRequired: s.hostProjectGuardRequired === true, }; } if (kind === "a8n") { @@ -129,6 +144,8 @@ export async function openSession(kind = "fake", options = {}) { slice: s, policy: null, targetGuard: null, + hostProjectGuard: null, + hostProjectGuardRequired: false, }; } const e = new Error(`unknown session kind '${kind}' (fake|a8n|a8n-attached)`); diff --git a/packages/ae-devtools-cli/devtools-cli/cli.mjs b/packages/ae-devtools-cli/devtools-cli/cli.mjs index 2278a42..ae44ddc 100644 --- a/packages/ae-devtools-cli/devtools-cli/cli.mjs +++ b/packages/ae-devtools-cli/devtools-cli/cli.mjs @@ -433,7 +433,16 @@ async function main() { const shared = { runner: state.session.runner, pin: state.session.pin, - projectIdentity: "cli-devtools", + // The consent scope names the REAL project when the session can + // attest it (attached lane, Issue #385); static labels remain + // for sessions without a project-file identity. + projectIdentity: state.session.hostProjectGuard ?? "cli-devtools", + // Host-side project guard (Issue #385): injected into the + // consent-bound source so the host re-verifies + // app.project.file.fsName inside the same evaluation as the + // op — closes the targetGuard→eval TOCTOU window. + hostProjectGuard: state.session.hostProjectGuard ?? null, + hostProjectGuardRequired: state.session.hostProjectGuardRequired === true, consentProvider: consentProviderFor(flags), consentAudit, trustedScriptOptIn: true, diff --git a/packages/ae-devtools-cli/mcp-server/tools.mjs b/packages/ae-devtools-cli/mcp-server/tools.mjs index 5513bd4..5065295 100644 --- a/packages/ae-devtools-cli/mcp-server/tools.mjs +++ b/packages/ae-devtools-cli/mcp-server/tools.mjs @@ -249,7 +249,9 @@ export async function callTool(name, args = {}, ctx = {}) { args: args.args ?? {}, runner: session.runner, pin: session.pin, - projectIdentity: "mcp-devtools", + projectIdentity: session.hostProjectGuard ?? "mcp-devtools", + hostProjectGuard: session.hostProjectGuard ?? null, + hostProjectGuardRequired: session.hostProjectGuardRequired === true, consentProvider: async () => ({ approved: true }), consentAudit: ctx.consentAudit ?? null, trustedScriptOptIn: true, @@ -287,7 +289,9 @@ export async function callTool(name, args = {}, ctx = {}) { readArgs: args.args ?? {}, runner: session.runner, pin: session.pin, - projectIdentity: "mcp-devtools", + projectIdentity: session.hostProjectGuard ?? "mcp-devtools", + hostProjectGuard: session.hostProjectGuard ?? null, + hostProjectGuardRequired: session.hostProjectGuardRequired === true, consentProvider: async () => ({ approved: true }), consentAudit: ctx.consentAudit ?? null, trustedScriptOptIn: true, diff --git a/packages/ae-devtools-cli/scripting-adapters/execute.mjs b/packages/ae-devtools-cli/scripting-adapters/execute.mjs index 4b68f87..686d2c6 100644 --- a/packages/ae-devtools-cli/scripting-adapters/execute.mjs +++ b/packages/ae-devtools-cli/scripting-adapters/execute.mjs @@ -51,6 +51,35 @@ export function toProgramSource(body) { return `(function(){\n${body}\n})()`; } +/** + * Host-side project identity guard (Issue #385). + * + * The attached-session targetGuard re-proves the target immediately + * before dispatch, but the generated ExtendScript evaluates later — a + * project switch in that window would write into a project the consent + * scope never named. When the session can attest the open project's + * file, the expected `app.project.file.fsName` is injected into the + * SAME evaluation that performs the operation: the guard runs before + * the op body and returns the adapter failure shape (the same JSON + * contract __adp_json emits) on mismatch, so the outcome is a + * structured FAILED with adapterError.code PROJECT_IDENTITY_MISMATCH + * and no write ever ran. + * + * The literal is part of the consent-bound source — approval covers + * exactly this check. ES3 only (var / typeof / ternary): no JSON, no + * let/const inside the host program. + */ +export function withHostProjectGuard(programSource, expectedFsName) { + return `(function(){ +var __adp_guardFile = app.project.file; +var __adp_guardName = (__adp_guardFile !== null && __adp_guardFile !== undefined && typeof __adp_guardFile.fsName === "string") ? String(__adp_guardFile.fsName) : null; +if (__adp_guardName !== ${JSON.stringify(expectedFsName)}) { + return '{"ok":false,"error":{"code":"PROJECT_IDENTITY_MISMATCH","message":"active project file does not match the consented project identity; the operation was refused before any write"}}'; +} +return ${programSource}; +})()`; +} + /** * @param {object} input * opId — registry operation id (e.g. "item.addComp") @@ -64,6 +93,14 @@ export function toProgramSource(body) { * trustedScriptOptIn / scriptProvenance — forwarded to the runner * (required by the trusted-dev-script consent class) * requestId — optional caller-chosen id + * hostProjectGuard — expected app.project.file.fsName attested by the + * session (attached lane, Issue #385); injected + * into the consent-bound source so the host + * re-verifies project identity in the same + * evaluation as the operation + * hostProjectGuardRequired — when true, mutating ops refuse + * (PROJECT_GUARD_UNAVAILABLE) if no guard value + * could be attested (unsaved/unreadable project) */ export async function runOperation(input = {}) { const { opId, args = {}, runner, pin, projectIdentity } = input; @@ -89,6 +126,25 @@ export async function runOperation(input = {}) { } source = toProgramSource(source); + // Host project guard (Issue #385): `hostProjectGuard` is the + // session-attested app.project.file.fsName; when present it is + // injected into the consent-bound source below. When the session + // REQUIRES a guard (attached lane) but cannot attest a project file + // (unsaved or unreadable project), mutating ops refuse before + // consent — a write that cannot be host-verified must not dispatch. + const hostProjectGuard = typeof input.hostProjectGuard === "string" && input.hostProjectGuard.length > 0 + ? input.hostProjectGuard + : null; + if (input.hostProjectGuardRequired === true && op.mutating === true && hostProjectGuard === null) { + return refused("PROJECT_GUARD_UNAVAILABLE", + `'${opId}' mutates project state but this session cannot attest a host-verifiable project file ` + + "(project unsaved or identity unreadable); mutations require a saved, attested project", + { opId }); + } + if (hostProjectGuard !== null) { + source = withHostProjectGuard(source, hostProjectGuard); + } + // Consent scope is computed here so the provider approves exactly // this dispatch — it cannot substitute different args or source. let scope; diff --git a/packages/ae-devtools-cli/scripting-adapters/tests/execute.test.mjs b/packages/ae-devtools-cli/scripting-adapters/tests/execute.test.mjs index 9ccef56..4e4ecf0 100644 --- a/packages/ae-devtools-cli/scripting-adapters/tests/execute.test.mjs +++ b/packages/ae-devtools-cli/scripting-adapters/tests/execute.test.mjs @@ -1354,3 +1354,71 @@ test("issue #299 batch: render/output-module templates — apply/save/list, mult assert.equal(dq.outcome, "COMPLETED"); assert.notEqual((await go("render.listOutputModules", { compName: "C" })).outcome, "COMPLETED"); }); + +// ---- Issue #385: host-side project identity guard ---- + +const COMP_ARGS = { name: "Guarded", width: 100, height: 100, pixelAspect: 1, duration: 5, frameRate: 30 }; + +test("host project guard: matching fsName completes and binds the guard into consent source", async () => { + const app = createAdapterFakeApp(); + app.project.file = { fsName: "/tmp/pinned.aep" }; + const t = baseInput(app, { hostProjectGuard: "/tmp/pinned.aep" }); + const consentCalls = []; + const provider = approvingProvider(consentCalls); + const r = await runOperation({ + opId: "item.addComp", args: COMP_ARGS, + consentProvider: provider, confirmDestructive: true, ...t, + }); + assert.equal(r.outcome, "COMPLETED", JSON.stringify(r)); + assert.equal(app.project.items.length, 1); + // The consent-approved source IS the guarded program — the pin is + // inside the same evaluation the host runs. + assert.match(consentCalls[0].source, /__adp_guardFile/); + assert.match(consentCalls[0].source, /\/tmp\/pinned\.aep/); +}); + +test("host project guard: project switched after consent fails before any write", async () => { + const app = createAdapterFakeApp(); + // The host's ACTIVE project differs from the attested guard value — + // the TOCTOU window the guard exists to close. + app.project.file = { fsName: "/tmp/other.aep" }; + const t = baseInput(app, { hostProjectGuard: "/tmp/pinned.aep" }); + const r = await runOperation({ + opId: "item.addComp", args: COMP_ARGS, + consentProvider: approvingProvider(), confirmDestructive: true, ...t, + }); + assert.equal(r.outcome, "FAILED"); + assert.equal(r.adapterError?.code, "PROJECT_IDENTITY_MISMATCH"); + // No write ran: the comp was never added. + assert.equal(app.project.items.length, 0); +}); + +test("hostProjectGuardRequired refuses mutating ops without an attested file; reads unaffected", async () => { + const app = createAdapterFakeApp(); + const t = baseInput(app, { hostProjectGuard: null, hostProjectGuardRequired: true }); + const consentCalls = []; + const provider = approvingProvider(consentCalls); + const r = await runOperation({ + opId: "item.addComp", args: COMP_ARGS, + consentProvider: provider, confirmDestructive: true, ...t, + }); + assert.equal(r.status, "refused"); + assert.equal(r.reason, "PROJECT_GUARD_UNAVAILABLE"); + assert.equal(t.transport.calls.length, 0, "refusal must precede dispatch"); + assert.equal(consentCalls.length, 0, "refusal must precede consent"); + // Read ops on the same session still work — the requirement is + // mutation-scoped. + const v = await runOperation({ opId: "read.projectSummary", consentProvider: provider, ...t }); + assert.equal(v.outcome, "COMPLETED"); +}); + +test("hostProjectGuardRequired + attested guard lets mutations through", async () => { + const app = createAdapterFakeApp(); + app.project.file = { fsName: "/tmp/pinned.aep" }; + const t = baseInput(app, { hostProjectGuard: "/tmp/pinned.aep", hostProjectGuardRequired: true }); + const r = await runOperation({ + opId: "item.addComp", args: COMP_ARGS, + consentProvider: approvingProvider(), confirmDestructive: true, ...t, + }); + assert.equal(r.outcome, "COMPLETED", JSON.stringify(r)); +});