diff --git a/.changeset/33906-inbound-hook-disabled-holder.md b/.changeset/33906-inbound-hook-disabled-holder.md new file mode 100644 index 00000000000..02f9f094cee --- /dev/null +++ b/.changeset/33906-inbound-hook-disabled-holder.md @@ -0,0 +1,23 @@ +--- +'@objectstack/service-automation': patch +--- + +fix(service-automation)!: the inbound hook door refuses the address of a flow slot whose package is disabled, before anything is enqueued (ADR-0150 D5) + +Clause-②: no (narrowing) +Metadata-protocol: none + + + +**BREAKING** for a sender that posts to the inbound hook of a flow whose package is disabled while the automation engine still holds that flow's slot (the re-sync that follows a disable failed or did not run, or the disable came with no announce). The door answered `202 Accepted` and queued the post, and the queued run was then skipped, so the event was lost while the sender was told it was accepted. It now answers the door's one refusal, `401 INVALID_SIGNATURE`. + +What moves, while the package is disabled: + +- `POST /api/v1/automation/hooks/NAMESPACE/NAME/HOOK` for a name two packages each ship, where NAMESPACE names the disabled package: the self-hosted mount and the hosted face both answer the one refusal, once the body has been read in full. Nothing is enqueued, and the other package's flow of the name is not reached in its place. +- For a name only the disabled package ships, both its qualified address and the bare `POST /api/v1/automation/hooks/NAME/HOOK` answer the same refusal. + +The answer is byte for byte the one the same address gets once the disable's re-sync has withdrawn the slot, and the one every unverified post gets, so it says nothing about which packages are installed or disabled. The cause is written to the server log only, at `info`, once per refused post (`cause: 'disabled-holder'`, with the flow, the package and the slot). + +Senders that retry on a non-2xx keep the event and can redeliver after a re-enable. When the package is enabled again the address answers `202` and runs the flow again, with no re-arm. + +The served package's qualified hook beside a disabled one, a flow no package ships, and a flow switched off with its slot toggle answer as before. The bare hook of a name two packages ship answers the one refusal as before. diff --git a/packages/qa/dogfood/test/inbound-hook-disabled-holder-33906.test.ts b/packages/qa/dogfood/test/inbound-hook-disabled-holder-33906.test.ts new file mode 100644 index 00000000000..1f95f4ec586 --- /dev/null +++ b/packages/qa/dogfood/test/inbound-hook-disabled-holder-33906.test.ts @@ -0,0 +1,345 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [ADR-0150 D5, #33906; seat ruling 6884403301, amendments 2, 3 and 8] END + * TO END: the inbound hook of a flow slot whose package is disabled while the + * slot is still armed (the disable's re-sync has not withdrawn it: its + * announce or protocol read failed, or the disable came with no announce). + * + * The real `AutomationEngine`, built by the real `AutomationServicePlugin` + * `init()` (so its flow-name resolver is the plugin's own, over the real + * `SchemaRegistry`), the real `ApiTriggerPlugin` mounting the self-hosted + * door and registering the `trigger-api` member, the real `ApiTrigger` + * verifying and enqueueing, and the real runtime `HttpDispatcher` forwarding + * the hosted face to that member. A queue delivers each message to the + * trigger's consumer at once, so a run is observed. + * + * - The qualified address of a disabled co-holder's slot answers the door's + * one refusal on both faces, once the body is read: the bytes the address + * gets after the disable's re-sync. Nothing is enqueued and nothing runs; + * the cause is in the server log only. + * - A disabled sole holder's flow: the same, on its qualified and its bare + * address. + * - The enable serves the address again (202), with no re-arm. + * - CONTROL: the served co-holder answers 202 and runs; with nothing + * disabled every address answers as before. + * + * Every value is a probe sentinel, not a credential. + */ + +import { createHmac } from 'node:crypto'; +import { beforeEach, describe, expect, it } from 'vitest'; +import { inboundHookJsonResponse, inboundHookRefusalResponse } from '@objectstack/core'; +import { SchemaRegistry } from '@objectstack/objectql'; +import { HttpDispatcher } from '@objectstack/runtime'; +import { AutomationEngine, AutomationServicePlugin, FlowCredentialChannel } from '@objectstack/service-automation'; +import { ApiTriggerPlugin, HOOKS_ADDRESSED_PATH, HOOKS_PATH } from '@objectstack/trigger-api'; + +const P1 = 'com.acme.alpha'; +const P2 = 'com.acme.beta'; +const SHARED = 'zz_disabled_hook'; +const ALONE = 'zz_disabled_alone'; +const S1 = 'pin-disabled-hook-alpha-71c4'; +const S2 = 'pin-disabled-hook-beta-e09a'; +const S_ALONE = 'pin-disabled-hook-alone-3f58'; + +type Line = { level: string; message: string; fields?: any }; +const lines: Line[] = []; +const logger: any = {}; +for (const level of ['debug', 'info', 'warn', 'error']) { + logger[level] = (message: string, fields?: unknown) => { lines.push({ level, message, fields }); }; +} +logger.child = () => logger; + +const sign = (secret: string, body: string) => { + const t = Math.floor(Date.now() / 1000); + return `t=${t},v1=` + createHmac('sha256', secret).update(`${t}.${body}`, 'utf8').digest('hex'); +}; + +/** start (api hook `h1`, its secret in the channel) → mark(tag) → end. */ +function hooked(name: string, tag: string) { + return { + name, + label: name, + type: 'api', + nodes: [ + { id: 'start', type: 'start', label: 'Start', config: { hookId: 'h1' } }, + { id: 'mark', type: 'mark', label: 'Mark', config: { tag } }, + { id: 'end', type: 'end', label: 'End' }, + ], + edges: [ + { id: 'e1', source: 'start', target: 'mark' }, + { id: 'e2', source: 'mark', target: 'end' }, + ], + }; +} + +/** One active hook-secret row per (flow, package), as the channel stores them: read-only, the hook door writes nothing. */ +function credentialStore(rows: Array<{ flow: string; packageId: string; value: string }>) { + const stored = rows.map((r, i) => ({ + id: `cred_${i}`, flow_name: r.flow, flow_package: r.packageId, node_id: 'start', credential_key: 'secret', state: 'active', value: r.value, + })); + return { + async find(_object: string, query?: { where?: Record; limit?: number }) { + const where = query?.where ?? {}; + const rows = stored.filter((row) => Object.entries(where).every(([k, v]) => (row as Record)[k] === v)); + const page = typeof query?.limit === 'number' ? rows.slice(0, query.limit) : rows; + return page.map(({ value: _value, ...row }) => row); + }, + async resolveSecretField(_object: string, id: string) { + return stored.find((row) => row.id === id)?.value ?? null; + }, + }; +} + +/** An answer as its bytes: status, headers and body. */ +type Snapshot = { status: number; headers: Array<[string, string]>; body: string }; +async function snapshot(res: Response): Promise { + return { status: res.status, headers: [...res.headers.entries()], body: await res.text() }; +} + +let registry: SchemaRegistry; +let engine: AutomationEngine; +let routes: Map Promise) => Promise>; +let dispatcher: HttpDispatcher; +let enqueued: string[]; +let ran: string[]; +let subscribed: string[]; + +/** The hook's address: `ns` absent is the bare hook. */ +type Address = { ns?: string; flowName: string }; + +/** POST through the self-hosted mount, as Hono hands a handler its decoded parameters and renders `c.json`. */ +async function postSelfHosted(address: Address, secret: string): Promise { + const body = JSON.stringify({ probe: address }); + const headers: Record = { 'x-objectstack-signature': sign(secret, body) }; + const params: Record = { flowName: address.flowName, hookId: 'h1', ...(address.ns ? { ns: address.ns } : {}) }; + const c = { + req: { param: (n: string) => params[n], text: async () => body, header: (n: string) => headers[n.toLowerCase()] }, + json: (b: unknown, status: number) => inboundHookJsonResponse(b, status), + }; + const route = routes.get(address.ns ? HOOKS_ADDRESSED_PATH : HOOKS_PATH)!; + return snapshot((await route(c, async () => {})) as Response); +} + +/** POST through the runtime dispatcher (the hosted face: a kernel with no http.server). */ +async function postHosted(address: Address, secret: string): Promise { + const body = JSON.stringify({ probe: address }); + const path = address.ns + ? `/automation/hooks/${address.ns}/${address.flowName}/h1` + : `/automation/hooks/${address.flowName}/h1`; + const request = new Request(`http://tenant.example/api/v1${path}`, { + method: 'POST', + headers: { 'Content-Type': 'application/json', 'x-objectstack-signature': sign(secret, body) }, + body, + }); + const result = await dispatcher.dispatch('POST', path, {}, {}, { request } as never); + expect(result.response, path).toBeUndefined(); + return snapshot(result.result as Response); +} + +const FACES = [['self-hosted', postSelfHosted], ['hosted', postHosted]] as const; + +/** The engine's own line for a post it refused because its slot's package is disabled. */ +const disabledCauses = () => lines.filter((l) => l.fields?.cause === 'disabled-holder'); + +beforeEach(async () => { + routes = new Map(); + lines.length = 0; + enqueued = []; + ran = []; + subscribed = []; + registry = new SchemaRegistry(); + registry.logLevel = 'silent'; + for (const [id, namespace] of [[P1, 'alpha'], [P2, 'beta']]) { + registry.installPackage({ id, name: id, version: '1.0.0', type: 'app', namespace } as never, 'code'); + } + const loaderSets: Record = {}; + + // The queue delivers each message to the trigger's consumer at once. + const consumers = new Map Promise | void>(); + const queue = { + async publish(name: string, data: unknown) { + enqueued.push(name); + await consumers.get(name)?.({ data }); + return `msg_${enqueued.length}`; + }, + async subscribe(name: string, handler: (message: { data: unknown }) => Promise | void) { + subscribed.push(name); + consumers.set(name, handler); + }, + async unsubscribe(name: string) { consumers.delete(name); }, + }; + const app = { post: (path: string, handler: any) => { routes.set(path, handler); } }; + const services: Record = { + queue, + 'http.server': { getRawApp: () => app }, + objectql: { registry }, + }; + const ctx = { + logger, + hook: (event: string, fn: () => Promise) => { if (event === 'kernel:ready') ready.push(fn); }, + getService: (name: string) => { + if (name in services) return services[name]; + throw new Error(`no ${name}`); + }, + registerService: (name: string, service: unknown) => { services[name] = service; }, + }; + const ready: Array<() => Promise> = []; + + // The engine the automation plugin builds, its flow-name resolver the plugin's own. + await new AutomationServicePlugin({ suspendedRunStore: 'memory' }).init(ctx as never); + engine = services.automation as AutomationEngine; + engine.registerNodeExecutor({ + type: 'mark', + async execute(node: { config?: { tag?: string } }) { + ran.push(String(node.config?.tag)); + return { success: true }; + }, + } as never); + engine.setPackagedFlowSource((name, packageId) => { + const holders = loaderSets[name] ?? []; + if (packageId !== undefined) return holders.includes(packageId) ? packageId : undefined; + return holders.length === 1 ? holders[0] : undefined; + }); + const channel = new FlowCredentialChannel(() => credentialStore([ + { flow: SHARED, packageId: P1, value: S1 }, + { flow: SHARED, packageId: P2, value: S2 }, + { flow: ALONE, packageId: P1, value: S_ALONE }, + ]) as never); + await channel.loadIndex(); + engine.setFlowCredentialSource(channel); + + await new ApiTriggerPlugin().start(ctx as never); + for (const fn of ready) await fn(); + expect(services['trigger-api']).toBeDefined(); + + // The hosted face: the runtime dispatcher over a kernel with no http.server. + const { 'http.server': _socket, ...hosted } = services; + const kernel: any = { + getState: () => 'running', + getService: (n: string) => hosted[n] ?? null, + getServiceAsync: async (n: string) => hosted[n] ?? null, + context: { getService: (n: string) => hosted[n] ?? null }, + }; + dispatcher = new HttpDispatcher(kernel, undefined, { enforceProjectMembership: false }); + (dispatcher as any).domainDeps.logger = logger; + + const ship = (packageId: string, body: { name: string }) => { + registry.registerItem('flow', { ...body, _packageId: packageId }, 'name' as never, packageId); + loaderSets[body.name] = [...new Set([...(loaderSets[body.name] ?? []), packageId])]; + engine.registerFlow(body.name, body, { source: 'package', packageId, holder: packageId }); + }; + ship(P1, hooked(SHARED, 'p1')); + ship(P2, hooked(SHARED, 'p2')); + ship(P1, hooked(ALONE, 'alone')); + // The consumer runs the flow after the publish resolves its subscription. + await new Promise((r) => setTimeout(r, 0)); +}); + +describe('#33906 the inbound hook of a disabled holder\'s armed slot, both faces', () => { + it('CONTROL: with nothing disabled, each qualified hook and the sole holder\'s bare hook answer 202 and run', async () => { + for (const [face, post] of FACES) { + ran = []; + expect((await post({ ns: 'alpha', flowName: SHARED }, S1)).status, face).toBe(202); + expect((await post({ ns: 'beta', flowName: SHARED }, S2)).status, face).toBe(202); + expect((await post({ ns: 'alpha', flowName: ALONE }, S_ALONE)).status, face).toBe(202); + expect((await post({ flowName: ALONE }, S_ALONE)).status, face).toBe(202); + expect(ran, face).toEqual(['p1', 'p2', 'alone', 'alone']); + } + expect(disabledCauses()).toEqual([]); + }); + + it('red line: the disabled co-holder\'s qualified address answers the one refusal, the bytes it gets after the re-sync; nothing is enqueued; the cause is logged only', async () => { + registry.disablePackage(P1); + const reference = await snapshot(inboundHookRefusalResponse()); + const armed: Record = {}; + for (const [face, post] of FACES) { + armed[face] = await post({ ns: 'alpha', flowName: SHARED }, S1); + expect(armed[face], face).toEqual(reference); + for (const leak of [P1, 'alpha', 'disabled']) expect(armed[face].body, face).not.toContain(leak); + } + expect(enqueued).toEqual([]); + expect(ran).toEqual([]); + const causes = disabledCauses(); + expect(causes).toHaveLength(2); + expect(causes[0].fields).toEqual({ cause: 'disabled-holder', flow: SHARED, package: P1, slot: `${P1}/${SHARED}` }); + + // The disable's re-sync withdraws the slot: the same address, the same bytes. + engine.withdrawFlowSlot(P1, SHARED); + for (const [face, post] of FACES) expect(await post({ ns: 'alpha', flowName: SHARED }, S1), face).toEqual(armed[face]); + expect(enqueued).toEqual([]); + expect(ran).toEqual([]); + }); + + it('CONTROL: the served co-holder beside the disabled one answers 202 on both faces and runs its own flow', async () => { + registry.disablePackage(P1); + for (const [face, post] of FACES) expect((await post({ ns: 'beta', flowName: SHARED }, S2)).status, face).toBe(202); + expect(enqueued).toEqual([`flow-api:${P2}/${SHARED}`, `flow-api:${P2}/${SHARED}`]); + expect(ran).toEqual(['p2', 'p2']); + expect(disabledCauses()).toEqual([]); + }); + + it('red line: both holders disabled, neither qualified address is served', async () => { + registry.disablePackage(P1); + registry.disablePackage(P2); + const reference = await snapshot(inboundHookRefusalResponse()); + for (const [face, post] of FACES) { + expect(await post({ ns: 'alpha', flowName: SHARED }, S1), face).toEqual(reference); + expect(await post({ ns: 'beta', flowName: SHARED }, S2), face).toEqual(reference); + } + expect(enqueued).toEqual([]); + expect(ran).toEqual([]); + expect(disabledCauses().map((l) => l.fields.package)).toEqual([P1, P2, P1, P2]); + }); + + it('red line: a disabled sole holder\'s flow answers the one refusal on its qualified and its bare address, the bytes they get after the re-sync; nothing is enqueued', async () => { + registry.disablePackage(P1); + const reference = await snapshot(inboundHookRefusalResponse()); + const addresses: Address[] = [{ ns: 'alpha', flowName: ALONE }, { flowName: ALONE }]; + const armed = new Map(); + for (const [face, post] of FACES) { + for (const address of addresses) { + const answer = await post(address, S_ALONE); + expect(answer, `${face} ${JSON.stringify(address)}`).toEqual(reference); + for (const leak of [P1, 'alpha', 'disabled']) expect(answer.body).not.toContain(leak); + armed.set(`${face} ${JSON.stringify(address)}`, answer); + } + } + expect(enqueued).toEqual([]); + expect(ran).toEqual([]); + expect(disabledCauses()).toHaveLength(4); + expect(disabledCauses()[0].fields).toEqual({ cause: 'disabled-holder', flow: ALONE, package: P1, slot: ALONE }); + + engine.withdrawFlowSlot(P1, ALONE); + for (const [face, post] of FACES) { + for (const address of addresses) { + const key = `${face} ${JSON.stringify(address)}`; + expect(await post(address, S_ALONE), key).toEqual(armed.get(key)); + } + } + expect(enqueued).toEqual([]); + expect(ran).toEqual([]); + }); + + it('the enable serves each address again, 202 and a run, with no re-arm', async () => { + const bindings = engine.getActiveTriggerBindings().map((b) => b.flowName).sort(); + const subscriptions = [...subscribed]; + registry.disablePackage(P1); + for (const [face, post] of FACES) { + expect((await post({ ns: 'alpha', flowName: SHARED }, S1)).status, face).toBe(401); + expect((await post({ flowName: ALONE }, S_ALONE)).status, face).toBe(401); + } + expect(ran).toEqual([]); + + registry.enablePackage(P1); + for (const [face, post] of FACES) { + expect((await post({ ns: 'alpha', flowName: SHARED }, S1)).status, face).toBe(202); + expect((await post({ ns: 'alpha', flowName: ALONE }, S_ALONE)).status, face).toBe(202); + expect((await post({ flowName: ALONE }, S_ALONE)).status, face).toBe(202); + } + expect(ran).toEqual(['p1', 'alone', 'alone', 'p1', 'alone', 'alone']); + expect(engine.getActiveTriggerBindings().map((b) => b.flowName).sort()).toEqual(bindings); + expect(subscribed).toEqual(subscriptions); + }); +}); diff --git a/packages/services/service-automation/src/engine.ts b/packages/services/service-automation/src/engine.ts index cbf8da9fdc7..f74765e901f 100644 --- a/packages/services/service-automation/src/engine.ts +++ b/packages/services/service-automation/src/engine.ts @@ -6037,6 +6037,19 @@ export class AutomationEngine implements IAutomationService { * - `refused`: the bare hook of a name two packages hold: every holder, * nothing picked. The door logs the holders and answers its one * refusal: D5's `409` does not apply to this anonymous door. + * + * [#33906; seat ruling 6884403301, amendments 2, 3 and 8] A slot whose + * package is disabled now ({@link isDisabledHolderSlot}) answers no + * address: the qualified address of a disabled co-holder's slot, and + * every address of a disabled sole holder's flow, the bare one included, + * are `unadmitted` — the door's one refusal, answered once the body is + * read in full, the same bytes the address gets once the disable's + * re-sync has withdrawn the slot. Nothing is enqueued, so no post is + * accepted whose run {@link triggerRun} would then skip. The slot is + * judged after the address has picked it, and no other holder is served + * in its place. The cause goes to the server log only, here, once per + * refused post. The question is asked at post time, so the address + * answers again the moment its package is enabled, with no re-arm. */ inboundHookSlot( flowName: string, @@ -6048,7 +6061,12 @@ export class AutomationEngine implements IAutomationService { | { readonly status: 'refused'; readonly candidates: PackageScopedRefusal['candidates'] } { if (isSlotKeySpelling(flowName) || flowName.startsWith('\u0000')) return { status: 'unadmitted' }; this.settleFlowName(flowName); - if (!this.flowSlots.isShared(flowName)) return undefined; + if (!this.flowSlots.isShared(flowName)) { + const packageId = this.flowSlotPackage(flowName); + return packageId !== undefined && this.isDisabledHolderSlot(flowName, packageId) + ? this.disabledHolderHookRefused(flowName, packageId, flowName) + : undefined; + } if (address === undefined) return { status: 'refused', candidates: this.sharedFlowNameRefusal(flowName, null).candidates }; const environment = address.qualifier === ENVIRONMENT_ADDRESS_SEGMENT; const keys = this.flowSlots.keysOf(flowName).filter((key) => { @@ -6057,7 +6075,29 @@ export class AutomationEngine implements IAutomationService { return slot.packageId !== null && address.namespaceOwners.includes(slot.packageId) && this.packagedFlowOwner(key) === slot.packageId; }); - return keys.length === 1 ? { status: 'served', key: keys[0] } : { status: 'unadmitted' }; + if (keys.length !== 1) return { status: 'unadmitted' }; + const packageId = this.flowSlotPackage(keys[0]); + if (packageId !== undefined && this.isDisabledHolderSlot(flowName, packageId)) { + return this.disabledHolderHookRefused(flowName, packageId, keys[0]); + } + return { status: 'served', key: keys[0] }; + } + + /** + * [#33906] {@link inboundHookSlot}'s answer for the slot `key` of package + * `packageId`'s flow `name` while that package is disabled: `unadmitted`, + * its cause logged here at `info` (the sender is told, with the door's one + * refusal, so nothing that claims to be persisted is lost). + */ + private disabledHolderHookRefused(name: string, packageId: string, key: string): { readonly status: 'unadmitted' } { + this.logger.info( + `Inbound hook post for flow '${name}' of package '${packageId}', which is disabled — refused with the ` + + 'one answer every unverified post gets: nothing was enqueued, and no other package\'s flow of the ' + + 'name was reached in its place. A sender that retries on a non-2xx keeps the event and can ' + + 'redeliver it once the package is enabled.', + { cause: 'disabled-holder', flow: name, package: packageId, slot: key }, + ); + return { status: 'unadmitted' }; } /**