@@ -65,7 +65,8 @@ import {
6565// Who may START a flow by name — the one rule the trigger door, this door and
6666// the declared endpoint share (the maintainer's rulings, letters B and A, and
6767// the flow's own `requiredPermissions`).
68- import { flowStartRefusal } from './flow-start-admission.js' ;
68+ import { flowRunsElevated , flowStartRefusal } from './flow-start-admission.js' ;
69+ import { validationFailure } from './validation-failure.js' ;
6970// [ADR-0066 D4] The capability arithmetic the action gate and the flow start
7071// gate share.
7172import { missingCapabilities } from './required-capabilities.js' ;
@@ -2278,6 +2279,41 @@ function flowActionCallerParamKeys(action: any, objectName: string, params: Reco
22782279 return Object . keys ( params ) . filter ( ( key ) => ! rowIdKeys . has ( key ) ) ;
22792280}
22802281
2282+ /**
2283+ * The row slots the CALLER's own `params` fill at this door, as flow start
2284+ * admission's third question reads them ({@link refuseUnreadableFlowStartRecords}):
2285+ * each row-id key ({@link flowActionRowIdKeys}) the caller's bag names, whose
2286+ * value reaches the run over the seeded id because the caller's params win
2287+ * ({@link seedFlowActionParams}). Two cases name none:
2288+ *
2289+ * - an action whose `recordIdField` names a field other than `id`: the door
2290+ * seeds every row slot with that field's VALUE, never an id of this
2291+ * object, so there is no row of this object to load a slot's value as;
2292+ * - a value equal to the routed `recordId` this door's subject load already
2293+ * delivered (an object-bound action): that row was judged by that load.
2294+ */
2295+ function flowActionCallerRecordKeys (
2296+ action : any ,
2297+ objectName : string ,
2298+ params : Record < string , unknown > ,
2299+ recordId : string | undefined ,
2300+ subject : ActionSubjectRecordLoad ,
2301+ ) : Array < readonly [ string , unknown ] > {
2302+ const idField : string = typeof action ?. recordIdField === 'string' && action . recordIdField ? action . recordIdField : 'id' ;
2303+ if ( idField !== 'id' ) return [ ] ;
2304+ const judged = recordId !== undefined && recordId !== '' && ! isObjectLessActionKey ( objectName ) && ! subject . recordLoadDenied
2305+ ? recordId
2306+ : undefined ;
2307+ const keys : Array < readonly [ string , unknown ] > = [ ] ;
2308+ for ( const key of flowActionRowIdKeys ( action , objectName ) ) {
2309+ if ( ! Object . prototype . hasOwnProperty . call ( params , key ) ) continue ;
2310+ const value = params [ key ] ;
2311+ if ( judged !== undefined && ( typeof value === 'string' || typeof value === 'number' ) && String ( value ) === judged ) continue ;
2312+ keys . push ( [ key , value ] ) ;
2313+ }
2314+ return keys ;
2315+ }
2316+
22812317/**
22822318 * Brand for {@link FlowActionRefusal} — `Symbol.for`, so recognition keeps
22832319 * working even if two module instances of this file ever coexist (src vs
@@ -2494,6 +2530,21 @@ export async function dispatchFlowAction(deps: ActionExecutionDeps,
24942530 code : startRefusal . code ,
24952531 } ) ;
24962532 }
2533+ // Flow start admission's third question, the row the start addresses
2534+ // (`flow-start-admission.ts`): the routed row was judged by this door's
2535+ // subject load upstream, so what is asked here is every row slot the
2536+ // CALLER's own params fill, whose value reaches the run over that row's id.
2537+ // Each is loaded in the caller's scope by the read the subject load makes
2538+ // (the request's own driver, environment and identity, as both entrances
2539+ // load the routed row), and refused with that load's 404 before the run
2540+ // exists. Thrown with `code` + `status`, like the refusals above.
2541+ await refuseUnreadableFlowStartRecords (
2542+ automation ,
2543+ action . target ,
2544+ { objectName, recordKeys : flowActionCallerRecordKeys ( action , objectName , params , recordId , subject ) } ,
2545+ ec ,
2546+ ( object , id ) => callData ( deps , requestContext , 'get' , { object, id } , requestContext ?. dataDriver , envId , ec ) ,
2547+ ) ;
24972548 // Pass a proper AutomationContext (the engine never read the former
24982549 // `triggerData` envelope).
24992550 const result : any = await automation . execute ( action . target , {
@@ -3568,6 +3619,95 @@ export function apiForwardSubjectRecord(
35683619 return recordId ? { id : recordId } : { } ;
35693620}
35703621
3622+ /**
3623+ * The row-slot keys a start hands a run, each with the value the run will
3624+ * receive under it — what {@link refuseUnreadableFlowStartRecords} loads.
3625+ */
3626+ export type FlowStartRecordKeys = ReadonlyArray < readonly [ key : string , value : unknown ] > ;
3627+
3628+ /**
3629+ * Flow start admission's THIRD question, the row a start addresses
3630+ * (`flow-start-admission.ts`), as the ONE function every door that starts a
3631+ * flow by name calls after `flowStartRefusal` and before `execute()`.
3632+ *
3633+ * ## The rule
3634+ *
3635+ * Every record key a door hands the run in a row slot the door defines
3636+ * (`recordId`, the `<object>Id` alias, an action's declared `recordIdParam`)
3637+ * is loaded first, in the CALLER's own scope, through
3638+ * {@link loadActionSubjectRecord}, and a key that scope does not deliver is
3639+ * refused by {@link refuseDeniedSubjectLoad}: the action door's own
3640+ * `404 RECORD_NOT_FOUND`, one envelope for a row the caller cannot read and an
3641+ * id that names nothing, so no door is an existence oracle the action door
3642+ * declines to be. Before `execute()`, so a refused start persists no run.
3643+ *
3644+ * It holds for every run, as the action door's load does: a persisted run is
3645+ * built only on a row the caller has shown it can read. A `runAs: 'system'`
3646+ * run is where the rule bites, because its nodes read and write past the
3647+ * caller's own access.
3648+ *
3649+ * ## The object a key is loaded from
3650+ *
3651+ * `objectName` is the object the start names: the action's own object at the
3652+ * action door, the request's `objectName` (or `object`) at the trigger routes
3653+ * and a declared endpoint, the object the door hands the run as `object` and
3654+ * derives the `<object>Id` alias from. A start whose keys come with no object
3655+ * to load them from (none named, or an object-less action key) cannot be
3656+ * judged:
3657+ *
3658+ * - an ELEVATED run ({@link flowRunsElevated}) is refused
3659+ * `400 VALIDATION_FAILED` naming `objectName`, the one thing the caller
3660+ * adds to be judged;
3661+ * - a user run starts as before: it reads in the caller's own scope.
3662+ *
3663+ * A key whose value is not one record id (a string or a number) is never
3664+ * loaded and is refused `400 VALIDATION_FAILED` naming the key: only an id
3665+ * names one row.
3666+ *
3667+ * ## What stays as it was
3668+ *
3669+ * - **The system principal** (`isSystem`, never set on inbound HTTP) starts
3670+ * every flow on every row, as the two admission rules let it.
3671+ * - **A start that names no record key** loads nothing.
3672+ * - **A key the door already loaded** is not passed here: the action door's
3673+ * subject load judged the routed id, and its tests stay as they are.
3674+ *
3675+ * `readRecord` is the door's own caller-scope read, the `callData('get', …)`
3676+ * its subject load uses, so there is no second loader.
3677+ */
3678+ export async function refuseUnreadableFlowStartRecords (
3679+ automation : unknown ,
3680+ flowName : string ,
3681+ start : { objectName : unknown ; recordKeys : FlowStartRecordKeys } ,
3682+ executionContext : { isSystem ?: boolean } | null | undefined ,
3683+ readRecord : ( objectName : string , recordId : string ) => Promise < unknown > ,
3684+ ) : Promise < void > {
3685+ if ( executionContext ?. isSystem === true ) return ;
3686+ const named = start . recordKeys . filter ( ( [ , value ] ) => value !== undefined && value !== null && value !== '' ) ;
3687+ if ( named . length === 0 ) return ;
3688+ const objectName = typeof start . objectName === 'string' && start . objectName !== '' && ! isObjectLessActionKey ( start . objectName )
3689+ ? start . objectName
3690+ : undefined ;
3691+ if ( objectName === undefined ) {
3692+ if ( ! ( await flowRunsElevated ( automation , flowName ) ) ) return ;
3693+ const message = 'A start that names a record must also name the record\'s object (`objectName`), '
3694+ + 'so the caller\'s access to that record can be checked before the flow starts.' ;
3695+ throw validationFailure ( message , [ { field : 'objectName' , code : 'required' , message } ] ) ;
3696+ }
3697+ const loaded = new Set < string > ( ) ;
3698+ for ( const [ key , value ] of named ) {
3699+ if ( typeof value !== 'string' && typeof value !== 'number' ) {
3700+ const message = `The record key \`${ key } \` must name one record by its id.` ;
3701+ throw validationFailure ( message , [ { field : key , code : 'invalid_type' , message } ] ) ;
3702+ }
3703+ const recordId = String ( value ) ;
3704+ if ( loaded . has ( recordId ) ) continue ;
3705+ const subject = await loadActionSubjectRecord ( objectName , recordId , ( ) => readRecord ( objectName , recordId ) ) ;
3706+ refuseDeniedSubjectLoad ( objectName , recordId , subject ) ;
3707+ loaded . add ( recordId ) ;
3708+ }
3709+ }
3710+
35713711/**
35723712 * [#15079] The prior/next value pair a successful declarative update hands back
35733713 * when the action declares `undoable: true` — the anchor that key never had.
0 commit comments