From 62443fd39e276c83622c6f3b985f15066c3eb485 Mon Sep 17 00:00:00 2001 From: hugocasa Date: Mon, 19 Oct 2026 13:24:19 +0200 Subject: [PATCH] fix(cli): name the pusher's identity where a push used to leave it to the server Co-Authored-By: Claude Opus 5.5 --- cli/src/commands/flow/flow.ts | 29 +++++++++++++++++------- cli/src/commands/script/script.ts | 35 ++++++++++++++++++++--------- cli/src/commands/workspace/merge.ts | 14 ++++++++++++ cli/src/core/permissioned_as.ts | 32 ++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 18 deletions(-) diff --git a/cli/src/commands/flow/flow.ts b/cli/src/commands/flow/flow.ts index 98339865be..dd64477fbe 100644 --- a/cli/src/commands/flow/flow.ts +++ b/cli/src/commands/flow/flow.ts @@ -30,7 +30,11 @@ import { FSFSElement, elementsToMap, ignoreF } from "../sync/sync.ts"; import { Flow } from "../../../gen/types.gen.ts"; import { applyExtraPermsDiff } from "../../core/extra_perms.ts"; import type { PermissionedAsContext } from "../../core/permissioned_as.ts"; -import { buildPermissionedAsContext } from "../../core/permissioned_as.ts"; +import { + buildPermissionedAsContext, + legacyPusherEmail, + warnRunAsNotCarried, +} from "../../core/permissioned_as.ts"; import { collectPathScriptPaths, replaceInlineScripts, @@ -260,14 +264,23 @@ export async function pushFlow( on_behalf_of?: string; preserve_on_behalf_of?: boolean; } = {}; - if (permissionedAsContext?.userIsAdminOrDeployer && hasOnBehalfOf) { - if (flow && flow.on_behalf_of_email) { - preserveFields.on_behalf_of_email = flow.on_behalf_of_email; - preserveFields.on_behalf_of = (flow as any).on_behalf_of; - preserveFields.preserve_on_behalf_of = true; - log.info(`Preserving ${flow.on_behalf_of_email} as on_behalf_of for flow ${remotePath}`); + if (permissionedAsContext && hasOnBehalfOf) { + if (flow?.on_behalf_of_email) { + if (permissionedAsContext.userIsAdminOrDeployer) { + preserveFields.on_behalf_of_email = flow.on_behalf_of_email; + preserveFields.on_behalf_of = (flow as any).on_behalf_of; + preserveFields.preserve_on_behalf_of = true; + log.info(`Preserving ${flow.on_behalf_of_email} as on_behalf_of for flow ${remotePath}`); + } else { + // The run-as identity becomes the pusher's, as the pre-push check announced. It has + // to be named: a flow deployed with none runs without one. + preserveFields.on_behalf_of_email = permissionedAsContext.userEmail; + } + } else if (!flow && !localFlow.on_behalf_of_email) { + warnRunAsNotCarried("flow", remotePath); } - // On create: backend applies folder defaults — no client-side resolution needed + } else if (!permissionedAsContext && localFlow.on_behalf_of_email) { + preserveFields.on_behalf_of_email = await legacyPusherEmail(workspace); } // extra_perms is synced independently via /acls/* (see applyExtraPermsDiff) diff --git a/cli/src/commands/script/script.ts b/cli/src/commands/script/script.ts index f38a73d697..73567e3639 100644 --- a/cli/src/commands/script/script.ts +++ b/cli/src/commands/script/script.ts @@ -7,7 +7,11 @@ import { validatePath, } from "../../core/context.ts"; import type { PermissionedAsContext } from "../../core/permissioned_as.ts"; -import { buildPermissionedAsContext } from "../../core/permissioned_as.ts"; +import { + buildPermissionedAsContext, + legacyPusherEmail, + warnRunAsNotCarried, +} from "../../core/permissioned_as.ts"; import { applyExtraPermsDiff } from "../../core/extra_perms.ts"; import { writeFile, stat, mkdir } from "node:fs/promises"; import { Buffer } from "node:buffer"; @@ -647,16 +651,27 @@ export async function handleFile( // workspace tarball strips it); it only ever travels back from the remote row. delete (typed as any)?.on_behalf_of; - if (permissionedAsContext?.userIsAdminOrDeployer && hasOnBehalfOf) { - if (remote && remote.on_behalf_of_email) { - requestBodyCommon.on_behalf_of_email = remote.on_behalf_of_email; - (requestBodyCommon as any).on_behalf_of = ( - remote as any - ).on_behalf_of; - (requestBodyCommon as any).preserve_on_behalf_of = true; - log.info(`Preserving ${remote.on_behalf_of_email} as on_behalf_of for script ${remotePath}`); + if (permissionedAsContext && hasOnBehalfOf) { + if (remote?.on_behalf_of_email) { + if (permissionedAsContext.userIsAdminOrDeployer) { + requestBodyCommon.on_behalf_of_email = remote.on_behalf_of_email; + (requestBodyCommon as any).on_behalf_of = ( + remote as any + ).on_behalf_of; + (requestBodyCommon as any).preserve_on_behalf_of = true; + log.info(`Preserving ${remote.on_behalf_of_email} as on_behalf_of for script ${remotePath}`); + } else { + // The run-as identity becomes the pusher's, as the pre-push check announced. It has + // to be named: a script deployed with none runs without one. + requestBodyCommon.on_behalf_of_email = permissionedAsContext.userEmail; + } + } else if (!remote && !typed?.on_behalf_of_email) { + warnRunAsNotCarried("script", remotePath); } - // On create: backend applies folder defaults — no client-side resolution needed + } else if (!permissionedAsContext && typed?.on_behalf_of_email) { + requestBodyCommon.on_behalf_of_email = await legacyPusherEmail( + workspace.workspaceId + ); } if (remote) { diff --git a/cli/src/commands/workspace/merge.ts b/cli/src/commands/workspace/merge.ts index 9f39bec52c..00f6228312 100644 --- a/cli/src/commands/workspace/merge.ts +++ b/cli/src/commands/workspace/merge.ts @@ -542,6 +542,7 @@ async function mergeWorkspaces( } let successCount = 0; let failCount = 0; + let mergerEmail: string | undefined; // Datatable migrations deployed (not deleted) into the target. Deploying a // migration only upserts its definition — the target schema is unchanged until // the migration is run — so offer to run them afterwards (like the push path). @@ -578,6 +579,19 @@ async function mergeWorkspaces( diff.path, workspaceFrom ); + } else if ( + (diff.kind === "script" || diff.kind === "flow") && + (await getOnBehalfOf( + provider, + diff.kind as DeployKind, + diff.path, + workspaceFrom + )) + ) { + // Without the flag every item is deployed as the merger, and a script or flow has to + // be told so: one deployed naming no identity runs without one. + mergerEmail ??= (await wmill.whoami({ workspace: workspaceTo })).email; + onBehalfOf = mergerEmail; } result = await deployItem( diff --git a/cli/src/core/permissioned_as.ts b/cli/src/core/permissioned_as.ts index dd0b3de8be..87465c7d57 100644 --- a/cli/src/core/permissioned_as.ts +++ b/cli/src/core/permissioned_as.ts @@ -50,6 +50,38 @@ export async function buildPermissionedAsContext( }; } +const pusherEmails = new Map>(); + +/** + * Below syncBehavior v1 a push hands every script or flow it deploys to whoever ran it. The + * backend keeps the identity a deploy names, so the address a v0 file carries is replaced by + * the pusher's own: sent as it is, it would stay, or fail to resolve in another workspace. + */ +export function legacyPusherEmail(workspace: string): Promise { + let email = pusherEmails.get(workspace); + if (!email) { + email = wmill.whoami({ workspace }).then((u) => u.email); + pusherEmails.set(workspace, email); + } + return email; +} + +/** + * From syncBehavior v1 a script or flow file says only *that* it runs on behalf of someone, + * never who, so a push creating it has no identity to send. It is created without one unless + * a folder `default_permissioned_as` rule matches; the pusher is not substituted, since a + * deploy token is often an admin the runnable never ran as. + */ +export function warnRunAsNotCarried(kind: "script" | "flow", path: string): void { + log.warn( + colors.yellow( + `${kind} ${path} runs on behalf of someone in the source, but the file does not say who: ` + + `it is created without a run-as identity unless a folder default_permissioned_as rule matches. ` + + `Set one with \`wmill ${kind} set-permissioned-as ${path} \`.` + ) + ); +} + async function ensureUserCache( workspace: string, cache: Map