Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .changeset/37815-body-capabilities-every-gate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
'@objectstack/lint': minor
'@objectstack/spec': patch
---

feat(lint)!: a hook body's gated calls, and an action body's calls to every gate besides `api.write`, are refused at publish when the body is not granted the token

Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: follow-up #38655

<!-- adr-0087: registered hook-and-action-bodies-calling-an-ungranted-gate-refused -->

**BREAKING** for the publish of an action, an object or a hook whose authored L2 body calls a member the QuickJS sandbox gates without that member's token in `body.capabilities`. Such a body saved and published before; the sandbox refused the call with `403 INSUFFICIENT_PRIVILEGES` every time the body reached it, so it never worked. It is refused now, at the runtime publish door and in `os validate` / `lint` / `compile` / `build`, with the token named. The schema is unchanged and stored metadata is not rewritten or refused at boot; the refusal comes at the next publish.

The gates are the sandbox runner's own, token by token, and the rule's list is pinned to the runner's source: the `ctx.api.object(…)` read leaves (`find`, `findOne`, `count`, `aggregate`) behind `api.read`, the write leaves behind `api.write`, `ctx.api.transaction(…)` behind `api.transaction`, `ctx.log.debug|info|warn|error(…)` behind `log`, `ctx.crypto.randomUUID()` behind `crypto.uuid`, `ctx.email.send(…)` behind `email.send`, `ctx.activity.append(…)` behind `activity.append`, and on a hook body `ctx.title(FIELD)` with an argument behind `api.read` (`ctx.title()` needs no token).

- `hook-body-capability-undeclared` (new): an authored L2 hook body that calls any of those gates without its token. A hook body is never granted `email.send`, so its `ctx.email.send(…)` call is refused with the remedy that works: remove the call and send the mail from an action, a job or a flow. Only a body the author wrote is judged: a hook body `os build` / `os verify` lowered from an inline `handler` is not, since its grant is the build's inference (the lowered reading is #38653). Runs on the runtime `hook` door.
- `action-body-capability-undeclared` (new): an authored L2 action body that calls a gate other than the writes without its token. An action body `os build` / `os verify` lowered from an inline `target` function is not judged for these tokens, since its grant is the build's inference (the lowered reading is #38653); its writes are still judged under `action-body-write-capability-undeclared`. Runs on the runtime `action` and `object` doors.
- `action-body-write-capability-undeclared` is unchanged: an action body's write without `api.write` keeps its id and its words.

## FROM → TO

- Authored hook body (arm a). FROM: `hooks: [{ …, body: { language: 'js', source: "await ctx.api.object('task').update({ id, done: true })", capabilities: [] } }]` saved and threw at every run. TO: declare the token, `capabilities: ['api.write']`.
- Authored action body, other tokens (arm b). FROM: `body: { language: 'js', source: 'await ctx.api.transaction(async () => { … })', capabilities: ['api.write'] }` saved and threw at every run. TO: declare the token, `capabilities: ['api.write', 'api.transaction']` (likewise `log`, `crypto.uuid`, `email.send`, `activity.append`, `api.read` for the call the finding names).
- Authored hook body calling `ctx.email.send(…)`. FROM: saved and threw at every run. TO: remove the call and send the mail from an action, a job or a flow.

A hook whose body was lowered from an inline `handler` is unchanged: FROM and TO alike, it is not judged by this rule. So is an action whose body was lowered from an inline `target` function, for every token but `api.write`: FROM, `actions: [{ …, target: async (ctx) => { await ctx.api.transaction(…) } }]` built clean; TO, it still builds clean, and its writes are judged as before.

`os explain hook-body-capability-undeclared` and `os explain action-body-capability-undeclared` carry an entry for each. `@objectstack/lint` exports the two rule id constants, `ACTION_BODY_CAPABILITY_UNDECLARED` and `HOOK_BODY_CAPABILITY_UNDECLARED`. `@objectstack/spec` adds only the upgrade-guide entry `hook-and-action-bodies-calling-an-ungranted-gate-refused`.
2 changes: 2 additions & 0 deletions content/docs/deployment/validating-metadata.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,9 @@ orthogonal to both, and no cell here can carry it; it is written out in
| Script action bodies compile in the runtime's wrapper — an L2 action's `body.source` that does not compile as the body of `(async (input, ctx) => { … })`, most often `const input = …` redeclaring a parameter; an object write runs it over its inline `actions[]`, so an object write cannot republish a refused body (`action-body-does-not-compile`, #34884) | ✓ | ✓ | ✓ | ✓ᵃᶜᵒ |
| Script action bodies hold the grant they call — an L2 action body that calls a `ctx.api` write (`insert`, `update`, `updateById`, `delete`, `updateMany`, `deleteMany`, `upsert`) without `api.write` in its `body.capabilities`, which the sandbox refuses at every call (`action-body-write-capability-undeclared`, #35143) | ✓ | ✓ | ✓ | ✓ᵃᶜᵒ |
| Seed record keys — a seed record key, or a seed `columns` key, that names no field of the dataset's object, no system column injected on it and none of `id` / `created_at` / `updated_at`; the engine's insert door refuses such a record and the seed load loads the rest (`seed-record-unknown-field`, #35143) | ✓ | ✓ | ✓ | ✓ˢ |
| Script action bodies hold every other grant they call — an authored L2 action body (one `os build` lowered from a `target` function is judged for its writes only) that calls a member the sandbox gates behind a token other than `api.write` without that token in its `body.capabilities`: a `ctx.api.object(…)` read leaf (`api.read`), `ctx.api.transaction(…)` (`api.transaction`), `ctx.log.*` (`log`), `ctx.crypto.randomUUID()` (`crypto.uuid`), `ctx.email.send(…)` (`email.send`) or `ctx.activity.append(…)` (`activity.append`), each refused by the sandbox at every call (`action-body-capability-undeclared`, #37815) | ✓ | ✓ | ✓ | ✓ᵃᶜᵒ |
| Hook and job bodies compile in the runtime's wrapper — an L2 hook or job `body.source` that does not compile as the body of `(async (ctx) => { … })`, most often `const ctx = …` redeclaring the parameter or one name declared twice; no door publishes a job, so the publish column is the `hook` write alone (`hook-body-does-not-compile`, `job-body-does-not-compile`, #34987) | ✓ | ✓ | ✓ | ✓ʰ |
| Hook bodies hold the grant they call — an authored L2 hook body (one `os build` lowered from a `handler` is not judged) that calls a member the sandbox gates without its token in `body.capabilities`: a `ctx.api.object(…)` read or write leaf, `ctx.api.transaction(…)`, `ctx.log.*`, `ctx.crypto.randomUUID()`, `ctx.activity.append(…)` or the related-record form `ctx.title(FIELD)`, each refused by the sandbox at every call; and any `ctx.email.send(…)`, which a hook body is never granted (`hook-body-capability-undeclared`, #37815) | ✓ | ✓ | ✓ | ✓ʰ |
| Approval-node approvers (ADR-0090 D3) | ✓ | ✓ | ✓ | ✓ᶠ |
| Security posture (ADR-0090 — e.g. every custom object declares `sharingModel`) | ✓ | ✓ | ✓ | ✓ˢᵖᵉᵇᵒ |
| Security vocabulary freeze (ADR-0090 D3 — the reserved word, replaced by `permission_set` / `position` / `business_unit`) | ✓ | ✓ | ✓ | ✓ᵒᵖᵉᵇᵖᵒᵃᵒˣ |
Expand Down
7 changes: 5 additions & 2 deletions packages/lint/src/authoring-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3400,8 +3400,11 @@ export const AUTHORING_RULES: readonly AuthoringRule[] = [
commands: ALL,
source: 'packages/lint/src/validate-action-body-capabilities.ts',
surfaces: CLI_AND_RUNTIME,
runtimeTypes: ['action', 'object'],
run: (stack) => validateActionBodyCapabilities(stack),
// [#37815] `hook`: the same judgement over an L2 hook body, every token
// the sandbox gates (`hook-body-capability-undeclared`); no object write
// carries a hook.
runtimeTypes: ['action', 'object', 'hook'],
run: (stack, ctx) => validateActionBodyCapabilities(stack, ctx),
},
// The same compile check over an L2 hook body and an L2 job body, in the
// wrapper the runtime runs both in (`(async (ctx) => { SOURCE })`, built from
Expand Down
6 changes: 5 additions & 1 deletion packages/lint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,11 @@ export {
// `os explain` can carry an entry for each (the explanation table holds an
// entry only for an id the barrel exports). The rules themselves run through
// the registry and are not exported.
export { ACTION_BODY_WRITE_CAPABILITY_UNDECLARED } from './validate-action-body-capabilities.js';
export {
ACTION_BODY_WRITE_CAPABILITY_UNDECLARED,
ACTION_BODY_CAPABILITY_UNDECLARED,
HOOK_BODY_CAPABILITY_UNDECLARED,
} from './validate-action-body-capabilities.js';
export { SEED_RECORD_UNKNOWN_FIELD } from './validate-seed-record-keys.js';
// [#35143] The Views, dashboards and app shell family's app-shell ids, all
// advisory, exported for `os explain` as the two above; the rules run through
Expand Down
56 changes: 55 additions & 1 deletion packages/lint/src/rule-explanations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,9 @@ const ACTION_BODY_WRITE_UNKNOWN_FIELD_EXPLANATION: RuleExplanation = {
covers: 'why an undeclared field write is refused',
paragraphs: [
'An action body persists records only through `ctx.api`: ' +
'`ctx.api.object(NAME).insert({ … })`, or `.update({ id, … })` for one record. `ctx.api` is a scoped handle on ' +
'`ctx.api.object(NAME).insert({ … })`, `.update({ id, … })` or `.updateById(id, { … })` for one record, ' +
'`.upsert(…)` or `.updateMany(…)`, the leaves the sandbox installs that carry fields. `ctx.api` is a ' +
'scoped handle on ' +
'the running engine, so the payload arrives as an ordinary CALLER write. An action\'s `ctx.input` ' +
'is its params bag, validated against the action\'s own `params`, so it is never resolved against ' +
'fields.',
Expand Down Expand Up @@ -1522,6 +1524,56 @@ const ACTION_BODY_WRITE_CAPABILITY_UNDECLARED_EXPLANATION: RuleExplanation = {
],
};

/**
* [#37815] The two ids beside `action-body-write-capability-undeclared`: every
* other sandbox gate on an action body, and every gate on a hook body. One
* paragraph set, worded per kind.
*/
const BODY_GATES_PARAGRAPH =
'The sandbox gates these calls, each behind its own token: the `ctx.api.object(…)` read leaves (`find`, ' +
'`findOne`, `count`, `aggregate`) behind `api.read`, the write leaves behind `api.write`, ' +
'`ctx.api.transaction(…)` behind `api.transaction`, `ctx.log.debug|info|warn|error(…)` behind `log`, ' +
'`ctx.crypto.randomUUID()` behind `crypto.uuid`, `ctx.email.send(…)` behind `email.send` and ' +
'`ctx.activity.append(…)` behind `activity.append`. A call the body was not granted throws at the call site ' +
'with `403 INSUFFICIENT_PRIVILEGES`.';
const BODY_GATES_READING =
'The rule reads the body\'s source with comments, strings and template literals blanked, and finds a gated ' +
'call on the `ctx` member itself (with `?.` allowed), on a local bound once to it (`const log = ctx.log`, or ' +
'`log` destructured from `ctx`), or, for the `ctx.api.object(…)` leaves, on a handle bound once to ' +
'`.object(…)`. It does not parse or run the body: the runtime save door may load no parser. A call spelled ' +
'another way is not found, which leaves the author where the sandbox would.';

const ACTION_BODY_CAPABILITY_UNDECLARED_EXPLANATION: RuleExplanation = {
rule: 'action-body-capability-undeclared',
covers: 'the grant an action body\'s other gated calls need',
paragraphs: [
'A script action body runs in the QuickJS sandbox with only the capabilities its `body.capabilities` ' +
'lists (none by default). ' + BODY_GATES_PARAGRAPH + ' The action answers that refusal every time it ' +
'reaches the call. A write without `api.write` has its own id, `action-body-write-capability-undeclared`.',
BODY_GATES_READING,
'To fix it, add the token the message names to the action\'s `body.capabilities` (Studio\'s action ' +
'inspector draws the list), or remove the call. Only a body the author wrote is judged: an action body ' +
'the build lowered from an inline `target` function is not, since its grant is the build\'s inference ' +
'(its writes are still judged under `action-body-write-capability-undeclared`).',
],
};

const HOOK_BODY_CAPABILITY_UNDECLARED_EXPLANATION: RuleExplanation = {
rule: 'hook-body-capability-undeclared',
covers: 'the grant a hook body\'s gated calls need',
paragraphs: [
'A hook body runs in the same QuickJS sandbox as an action body, with only the capabilities its ' +
'`body.capabilities` lists (none by default). ' + BODY_GATES_PARAGRAPH + ' A hook body also has ' +
'`ctx.title(FIELD)`, which reads the related record and needs `api.read`; `ctx.title()` with no argument ' +
'needs no token. Under the hook\'s default `onError: \'abort\'` the refusal aborts the write that ran it.',
BODY_GATES_READING,
'To fix it, add the token the message names to the hook\'s `body.capabilities`, or remove the call. A hook ' +
'body is never granted `email.send`, because a message cannot be taken back if the write that runs the ' +
'hook rolls back: remove the `ctx.email.send` call and send the mail from an action, a job or a flow. ' +
'Only a body the author wrote is judged: a hook body the build lowered from an inline `handler` is not.',
],
};

const SEED_RECORD_UNKNOWN_FIELD_EXPLANATION: RuleExplanation = {
rule: 'seed-record-unknown-field',
covers: 'which keys a seed record may carry',
Expand Down Expand Up @@ -7335,6 +7387,8 @@ export const RULE_EXPLANATIONS: Readonly<Record<string, RuleExplanation>> = Obje
[ACTION_BODY_SOURCE_UNPARSEABLE_EXPLANATION.rule]: ACTION_BODY_SOURCE_UNPARSEABLE_EXPLANATION,
[ACTION_BODY_DOES_NOT_COMPILE_EXPLANATION.rule]: ACTION_BODY_DOES_NOT_COMPILE_EXPLANATION,
[ACTION_BODY_WRITE_CAPABILITY_UNDECLARED_EXPLANATION.rule]: ACTION_BODY_WRITE_CAPABILITY_UNDECLARED_EXPLANATION,
[ACTION_BODY_CAPABILITY_UNDECLARED_EXPLANATION.rule]: ACTION_BODY_CAPABILITY_UNDECLARED_EXPLANATION,
[HOOK_BODY_CAPABILITY_UNDECLARED_EXPLANATION.rule]: HOOK_BODY_CAPABILITY_UNDECLARED_EXPLANATION,
[SEED_RECORD_UNKNOWN_FIELD_EXPLANATION.rule]: SEED_RECORD_UNKNOWN_FIELD_EXPLANATION,
[LIST_VIEW_KANBAN_GROUP_FIELD_UNCATEGORICAL_EXPLANATION.rule]: LIST_VIEW_KANBAN_GROUP_FIELD_UNCATEGORICAL_EXPLANATION,
[LIST_VIEW_FILTER_FIELD_VIRTUAL_EXPLANATION.rule]: LIST_VIEW_FILTER_FIELD_VIRTUAL_EXPLANATION,
Expand Down
Loading
Loading