|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// Flow `runAs` identity-enforcement fixture — the live, revert-provable #1888 gate. |
| 4 | +// |
| 5 | +// `flow.runAs` declares the execution identity for a run's data operations: |
| 6 | +// • `system` → elevated, RLS-bypassing system principal (full access), |
| 7 | +// • `user` → the triggering user (RLS-respecting; cannot exceed their grants). |
| 8 | +// The bug (#1888): the engine IGNORED `runAs`. CRUD nodes passed no identity to |
| 9 | +// ObjectQL at all, so the security middleware was skipped entirely — every flow |
| 10 | +// ran effectively elevated regardless of `runAs`. A `runAs:'user'` flow did NOT |
| 11 | +// de-elevate (a privilege-boundary surprise), and `runAs:'system'` did not |
| 12 | +// *explicitly* elevate (it merely happened to be unscoped too). |
| 13 | +// |
| 14 | +// This fixture reproduces the boundary with ZERO dependence on org-scoping, |
| 15 | +// mirroring rls-owner-fixture: one object `runas_note` whose member permission |
| 16 | +// set carries an OWNER policy keyed on `created_by` (survives single-tenant |
| 17 | +// stripping because the predicate references `current_user.id`). A fresh member |
| 18 | +// therefore genuinely CANNOT read or write a note the admin created. |
| 19 | +// |
| 20 | +// Four flows over that object, each triggered by the RESTRICTED member, prove |
| 21 | +// BOTH directions of the fix: |
| 22 | +// • runas_system_touch / runas_system_read (runAs:'system') — elevate: the |
| 23 | +// member-triggered run WRITES / READS the admin's note it cannot itself touch. |
| 24 | +// • runas_user_touch / runas_user_read (runAs:'user') — de-elevate: the |
| 25 | +// same run is RLS-denied on the admin's note (write lands nowhere; read empty). |
| 26 | +// |
| 27 | +// Before the fix the user-mode flows wrongly succeed (security skipped) → RED. |
| 28 | +// After the fix they are correctly denied while system-mode still succeeds → GREEN. |
| 29 | + |
| 30 | +import { defineStack } from '@objectstack/spec'; |
| 31 | +import { ObjectSchema, Field } from '@objectstack/spec/data'; |
| 32 | +import { PermissionSetSchema, RLS, type PermissionSet } from '@objectstack/spec/security'; |
| 33 | +import { SecurityPlugin, securityDefaultPermissionSets } from '@objectstack/plugin-security'; |
| 34 | + |
| 35 | +/** The one object under test: an owner-scoped note (isolated via `created_by`). */ |
| 36 | +export const RunAsNote = ObjectSchema.create({ |
| 37 | + name: 'runas_note', |
| 38 | + label: 'RunAs Note', |
| 39 | + pluralLabel: 'RunAs Notes', |
| 40 | + fields: { |
| 41 | + name: Field.text({ label: 'Name', required: true }), |
| 42 | + status: Field.text({ label: 'Status' }), |
| 43 | + }, |
| 44 | +}); |
| 45 | + |
| 46 | +/** |
| 47 | + * `runas_<mode>_touch` — start → update_record → end. Sets `status` on the note |
| 48 | + * whose id is passed as the `noteId` input variable. The two variants differ |
| 49 | + * ONLY in `runAs`, isolating the identity switch as the single variable. |
| 50 | + */ |
| 51 | +function touchFlow(name: string, runAs: 'system' | 'user', stamp: string) { |
| 52 | + return { |
| 53 | + name, |
| 54 | + label: `RunAs ${runAs} touch`, |
| 55 | + type: 'autolaunched', |
| 56 | + runAs, |
| 57 | + variables: [{ name: 'noteId', type: 'text', isInput: true }], |
| 58 | + nodes: [ |
| 59 | + { id: 'start', type: 'start', label: 'Start' }, |
| 60 | + { |
| 61 | + id: 'touch', |
| 62 | + type: 'update_record', |
| 63 | + label: 'Touch note', |
| 64 | + config: { objectName: 'runas_note', filter: { id: '{noteId}' }, fields: { status: stamp } }, |
| 65 | + }, |
| 66 | + { id: 'end', type: 'end', label: 'End' }, |
| 67 | + ], |
| 68 | + edges: [ |
| 69 | + { id: 'e1', source: 'start', target: 'touch' }, |
| 70 | + { id: 'e2', source: 'touch', target: 'end' }, |
| 71 | + ], |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * `runas_<mode>_read` — start → get_record → end. Reads the note by id into the |
| 77 | + * `found` output variable, surfaced on the trigger response as |
| 78 | + * `data.output.found`. Under `system` the elevated read returns the record; |
| 79 | + * under `user` the RLS-scoped read returns null. |
| 80 | + */ |
| 81 | +function readFlow(name: string, runAs: 'system' | 'user') { |
| 82 | + return { |
| 83 | + name, |
| 84 | + label: `RunAs ${runAs} read`, |
| 85 | + type: 'autolaunched', |
| 86 | + runAs, |
| 87 | + variables: [ |
| 88 | + { name: 'noteId', type: 'text', isInput: true }, |
| 89 | + { name: 'found', type: 'text', isOutput: true }, |
| 90 | + ], |
| 91 | + nodes: [ |
| 92 | + { id: 'start', type: 'start', label: 'Start' }, |
| 93 | + { |
| 94 | + id: 'get', |
| 95 | + type: 'get_record', |
| 96 | + label: 'Get note', |
| 97 | + config: { objectName: 'runas_note', filter: { id: '{noteId}' }, outputVariable: 'found' }, |
| 98 | + }, |
| 99 | + { id: 'end', type: 'end', label: 'End' }, |
| 100 | + ], |
| 101 | + edges: [ |
| 102 | + { id: 'e1', source: 'start', target: 'get' }, |
| 103 | + { id: 'e2', source: 'get', target: 'end' }, |
| 104 | + ], |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +export const runasSystemTouch = touchFlow('runas_system_touch', 'system', 'touched-system'); |
| 109 | +export const runasUserTouch = touchFlow('runas_user_touch', 'user', 'touched-user'); |
| 110 | +export const runasSystemRead = readFlow('runas_system_read', 'system'); |
| 111 | +export const runasUserRead = readFlow('runas_user_read', 'user'); |
| 112 | + |
| 113 | +/** A minimal, self-contained app config the dogfood harness can boot. */ |
| 114 | +export const runasFixtureStack = defineStack({ |
| 115 | + manifest: { |
| 116 | + id: 'com.dogfood.runas_fixture', |
| 117 | + namespace: 'runas', |
| 118 | + version: '0.0.0', |
| 119 | + type: 'app', |
| 120 | + name: 'Flow runAs Fixture', |
| 121 | + description: 'Owner-isolated single-object app exercising flow.runAs identity enforcement (#1888).', |
| 122 | + }, |
| 123 | + objects: [RunAsNote], |
| 124 | + flows: [runasSystemTouch, runasUserTouch, runasSystemRead, runasUserRead], |
| 125 | +}); |
| 126 | + |
| 127 | +/** |
| 128 | + * The fallback permission set a fresh member resolves to: full CRUD on |
| 129 | + * `runas_note` (so the request reaches the RLS layer, not an RBAC wall) plus an |
| 130 | + * owner RLS policy on ALL operations keyed on `created_by`. A member can read |
| 131 | + * and write their OWN notes, but neither read nor write the admin's — the exact |
| 132 | + * cross-owner isolation the runAs proof needs on both directions. |
| 133 | + */ |
| 134 | +const FIXTURE_MEMBER_SET = 'runas_fixture_member'; |
| 135 | + |
| 136 | +export const runasMemberSet: PermissionSet = PermissionSetSchema.parse({ |
| 137 | + name: FIXTURE_MEMBER_SET, |
| 138 | + label: 'RunAs Fixture Member — owner-scoped (all ops)', |
| 139 | + isProfile: true, |
| 140 | + objects: { |
| 141 | + runas_note: { allowRead: true, allowCreate: true, allowEdit: true, allowDelete: true }, |
| 142 | + }, |
| 143 | + rowLevelSecurity: [RLS.ownerPolicy('runas_note', 'created_by')], |
| 144 | +}); |
| 145 | + |
| 146 | +/** |
| 147 | + * Build a SecurityPlugin whose fallback (for a fresh, grant-less member) is the |
| 148 | + * owner-scoped fixture set, layered on the real platform defaults so the seeded |
| 149 | + * admin keeps `admin_full_access` (and can create the note + read everything). |
| 150 | + */ |
| 151 | +export function runasFixtureSecurity(): SecurityPlugin { |
| 152 | + return new SecurityPlugin({ |
| 153 | + defaultPermissionSets: [...securityDefaultPermissionSets, runasMemberSet], |
| 154 | + fallbackPermissionSet: runasMemberSet.name, |
| 155 | + }); |
| 156 | +} |
0 commit comments