Skip to content

Commit 6c2c615

Browse files
committed
fix(spec,runtime,cli,app-shell): a hook body is not granted email.send at parse, at bind or in the picker
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwV8gsjrvCXCqKoaiG
1 parent 5ce459b commit 6c2c615

16 files changed

Lines changed: 361 additions & 68 deletions

File tree

‎.changeset/body-mail-activity-doors.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ Metadata-protocol: added
2020

2121
<!-- adr-0087: not-required (no-migration-prescription) no key, export or field is removed and no stored body changes shape; the change is which sender delivers a row an app inserted, and moving that app to the mail door is a code change no conversion can make for it -->
2222

23-
- **`@objectstack/spec`:** two new body capabilities, `email.send` and `activity.append`, and their inputs `BodyMailSendInputSchema` and `BodyActivityAppendInputSchema` (`@objectstack/spec/data`). The sender, the sending user, the organization, the actor and the origin are stamped by the platform and are not inputs: a key outside either shape is refused. Every string either input takes has an upper bound, and the limits that read across fields (`BODY_MAIL_RECIPIENT_LIMIT`, `BODY_MAIL_ADDRESS_LIST_LIMIT`, `BODY_MAIL_ADDRESS_LIMIT`, `BODY_ACTIVITY_METADATA_LIMIT`) are stated in each schema's description. `SYS_ACTIVITY_PLATFORM_TYPES` names the timeline types only the platform records. `SendEmailInput.requireLogRow` (`@objectstack/spec/contracts`) asks the mail service to send a message only if its log records it.
24-
- **`@objectstack/runtime`:** an action or a job body that declares the capability calls `ctx.email.send({ to, subject, text })`, and a hook, action or job body `ctx.activity.append({ object, recordId, type, summary })`. A message is sent from the configured sender, as the calling user, under the caller's organization; a timeline entry is written with the calling user as its actor and the package and body as its origin. A record the call points at must be one the caller can read, in the caller's own scope: for a hook, the user of the write that fired it, never the system. A message names at most 50 recipients, each recipient list and `replyTo` fits the mail log as recorded, a message is sent only if the mail log records it, and an organization's mail is held to `OS_EMAIL_APP_SEND_HOURLY_LIMIT` messages an hour (default 200) before the door refuses another. A message cannot be taken back, so a hook body cannot send mail (`403 PERMISSION_DENIED`); an action or a job body sends it, and is refused with `409 RESOURCE_CONFLICT` while it runs inside a transaction. A timeline entry commits or rolls back with the write that runs the body. A door call inside the body's own `ctx.api.transaction` is refused with `409 RESOURCE_CONFLICT`. An entry's origin names the package that holds the body. A body with no calling user (a job) sends and writes with no user recorded.
25-
- **`@objectstack/cli`:** the build infers both capabilities from a handler's calls, under a renamed or destructured context too. `ExtractedBody.capabilities` on `@objectstack/cli/hook-body` may now hold `'email.send'` and `'activity.append'`.
23+
- **`@objectstack/spec`:** two new body capabilities, `email.send` and `activity.append`, and their inputs `BodyMailSendInputSchema` and `BodyActivityAppendInputSchema` (`@objectstack/spec/data`). The sender, the sending user, the organization, the actor and the origin are stamped by the platform and are not inputs: a key outside either shape is refused. Every string either input takes has an upper bound, and the limits that read across fields (`BODY_MAIL_RECIPIENT_LIMIT`, `BODY_MAIL_ADDRESS_LIST_LIMIT`, `BODY_MAIL_ADDRESS_LIMIT`, `BODY_ACTIVITY_METADATA_LIMIT`) are stated in each schema's description. `SYS_ACTIVITY_PLATFORM_TYPES` names the timeline types only the platform records. `SendEmailInput.requireLogRow` (`@objectstack/spec/contracts`) asks the mail service to send a message only if its log records it. A hook whose body is granted `email.send` is refused at parse (`HookSchema`): a hook body sends no mail.
24+
- **`@objectstack/runtime`:** an action or a job body that declares the capability calls `ctx.email.send({ to, subject, text })`, and a hook, action or job body `ctx.activity.append({ object, recordId, type, summary })`. A message is sent from the configured sender, as the calling user, under the caller's organization; a timeline entry is written with the calling user as its actor and the package and body as its origin. A record the call points at must be one the caller can read, in the caller's own scope: for a hook, the user of the write that fired it, never the system. A message names at most 50 recipients, each recipient list and `replyTo` fits the mail log as recorded, a message is sent only if the mail log records it, and an organization's mail is held to `OS_EMAIL_APP_SEND_HOURLY_LIMIT` messages an hour (default 200) before the door refuses another. A message cannot be taken back, so a hook body cannot send mail: a hook body granted `email.send` is not bound, and a call is refused (`403 PERMISSION_DENIED`); an action or a job body sends it, and is refused with `409 RESOURCE_CONFLICT` while it runs inside a transaction. A timeline entry commits or rolls back with the write that runs the body. A door call inside the body's own `ctx.api.transaction` is refused with `409 RESOURCE_CONFLICT`. An entry's origin names the package that holds the body. A body with no calling user (a job) sends and writes with no user recorded.
25+
- **`@objectstack/cli`:** the build infers both capabilities from a handler's calls, under a renamed or destructured context too. `ExtractedBody.capabilities` on `@objectstack/cli/hook-body` may now hold `'email.send'` and `'activity.append'`. A hook handler that calls `ctx.email.send` fails the build, which refuses the inferred grant.
2626
- **`@objectstack/objectql`:** `seedHookUserGrants` and `seedHookUnreadMembership`, the second step of a `runAs: 'user'` derivation and its fail-safe, are exported beside `deriveHookApi`. `openTransactionKind` answers whether a write runs inside a transaction that a write beside it joins (`'joined'`), one it cannot reach (`'separate'`), or none.
2727
- **`@objectstack/core`:** the bounds a caller-sent message is held to (the sending user, the organization, the configured sender) are one function, `boundCallerMail`, shared by the REST mail door and the body door.
2828
- **`@objectstack/rest`:** the REST mail door reads its bounds from `boundCallerMail`; its answers are unchanged.
2929
- **`@objectstack/plugin-email`:** a mail-log row the mail service did not write is delivered from the configured sender, with no sender header of its own (see BREAKING above). `send()` honours `requireLogRow`: when the row cannot be written, it refuses with `503 SERVICE_UNAVAILABLE` and delivers nothing.
3030
- **`@objectstack/plugin-audit`:** the activity feed owns the door's writer, `appendAppActivity` on the `audit` service. An app's entry may not use a platform-only type or a platform metadata key, and may not be about an object the feed does not show or an append-only object. `sys_activity` gains a read-only `origin` column, empty on an entry not written through the activity door.
3131
- **`@objectstack/plugin-security`:** inserting into an append-only object from app automation that runs as system is deprecated, and says so once per kind and object, naming the two doors.
32-
- **`@object-ui/app-shell`:** the script capability picker lists both capabilities with their calls.
32+
- **`@object-ui/app-shell`:** the script capability picker lists both capabilities with their calls; on a hook it does not offer `email.send`, and flags a stored grant.

‎content/docs/automation/hook-bodies.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ The script sees only what the surrounding `ctx` object exposes:
105105
| `ctx.title('<lookup field>')` | The related record's title, through a lookup / master_detail / user / tree column. Costs one `findOne` (plus the lookups that record's own title reads through). | `api.read` |
106106
| `ctx.crypto.randomUUID()` | UUID generation. | `crypto.uuid` |
107107
| `ctx.log.{info,warn,error}` | Structured logging. | `log` |
108-
| `ctx.email.send({ to, subject, text })` | Sends mail through the mail service, from the configured sender, as the calling user and under the caller's organization. An action or a job body only: a hook body cannot send mail, because a message cannot be taken back if the write that runs the hook rolls back. Send mail on a record change from an action, a job or a flow. See [Email service](/docs/kernel/runtime-services/email-service#from-a-script-body). | `email.send` |
108+
| `ctx.email.send({ to, subject, text })` | Sends mail through the mail service, from the configured sender, as the calling user and under the caller's organization. An action or a job body only: a hook body cannot send mail, because a message cannot be taken back if the write that runs the hook rolls back. A hook whose body is granted `email.send` is refused when it is saved, validated, built or bound, and the Console does not offer the grant on a hook. Send mail on a record change from an action, a job or a flow. See [Email service](/docs/kernel/runtime-services/email-service#from-a-script-body). | `email.send` |
109109
| `ctx.activity.append({ object, recordId, type, summary })` | Adds an entry to a record's activity timeline. The platform stamps the calling user as the actor and the package and body as the entry's `origin`. `type` may not be one only the platform records (`SYS_ACTIVITY_PLATFORM_TYPES`), the record must be one the caller can read, and `metadata` takes at most 8,000 characters as JSON. The entry commits or rolls back with the write that runs the body. Refused inside the body's own `ctx.api.transaction`. | `activity.append` |
110110
| `ctx.connector(name).<method>(...)` _(planned)_ | Outbound HTTP / SaaS calls. **Not yet wired into the sandbox** — ships with the separate Connector spec. | (separate Connector spec) |
111111

‎content/docs/kernel/runtime-services/email-service.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ An action or a job body sends mail with `ctx.email.send(input)` when it declares
4040
- A message names at most 50 recipients across `to`, `cc` and `bcc`. Each of the three takes at most 4,000 characters as the mail log records it (every recipient as `"Name" <address>`, joined by `, `), and a `replyTo` at most 320. The subject takes at most 998 characters, and `text` and `html` at most 1,000,000 each. A message over any of these is refused with `400 VALIDATION_ERROR`.
4141
- While the organization's mail log records `OS_EMAIL_APP_SEND_HOURLY_LIMIT` messages (default 200) in the last hour, the call is refused with `429 RATE_LIMIT_EXCEEDED`. A caller with no organization is counted against the whole log. The count is read before the send, so two calls racing for the last message of the hour can both be sent.
4242
- The message is sent only if the mail log records it. When the row cannot be written, the call is refused with `503 SERVICE_UNAVAILABLE` and nothing is sent.
43-
- A message cannot be taken back, so a hook body cannot send mail: the write that runs the hook may still roll back. A call from a hook body is refused with `403 PERMISSION_DENIED`. Send mail on a record change from an action, a job or a flow.
43+
- A message cannot be taken back, so a hook body cannot send mail: the write that runs the hook may still roll back. A hook whose body is granted `email.send` is refused when it is saved, validated, built or bound, and a call from a hook body is refused with `403 PERMISSION_DENIED`. Send mail on a record change from an action, a job or a flow.
4444
- A call made while an action or a job runs inside a transaction, or inside the body's own `ctx.api.transaction`, is refused with `409 RESOURCE_CONFLICT`.
4545
- A job body has no calling user, so its message is recorded with no `sentBy`.
4646

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#32004] A hook body sends no mail, so the build refuses a hook whose handler
5+
* calls `ctx.email.send`: lowering infers the `email.send` grant from the call
6+
* (`extractLoweredBody`), and the hook parse every build runs after lowering
7+
* (`HookSchema`) refuses that grant, naming it. An action's handler that makes
8+
* the same call lowers to a body the body schema takes.
9+
*/
10+
11+
import { describe, it, expect } from 'vitest';
12+
import { HookBodySchema, HookSchema } from '@objectstack/spec/data';
13+
import { lowerCallables } from '../src/utils/lower-callables.js';
14+
15+
describe('the build refuses a hook whose handler sends mail', () => {
16+
it('lowers the handler to a body granted email.send, which the hook parse refuses', () => {
17+
const out = lowerCallables({
18+
hooks: [
19+
{
20+
name: 'welcome_contact',
21+
object: 'crm_contact',
22+
events: ['afterInsert'],
23+
handler: async (ctx: any) => {
24+
await ctx.email.send({ to: 'pat@example.test', subject: 'Welcome', text: 'Hello' });
25+
},
26+
},
27+
],
28+
});
29+
const lowered = (out.lowered.hooks as any[])[0];
30+
expect(lowered.body.capabilities).toContain('email.send');
31+
32+
const r = HookSchema.safeParse(lowered);
33+
expect(r.success).toBe(false);
34+
const issues = r.success ? [] : r.error.issues;
35+
expect(issues.map((i) => i.path.join('.'))).toEqual([`body.capabilities.${lowered.body.capabilities.indexOf('email.send')}`]);
36+
expect(issues[0].message).toContain('A hook body cannot be granted `email.send`');
37+
});
38+
39+
it('lowers a hook handler that only adds a timeline entry to a body the hook parse takes', () => {
40+
const out = lowerCallables({
41+
hooks: [
42+
{
43+
name: 'note_contact',
44+
object: 'crm_contact',
45+
events: ['afterInsert'],
46+
handler: async (ctx: any) => {
47+
await ctx.activity.append({ object: 'crm_contact', recordId: 'c1', type: 'completed', summary: 'Welcomed' });
48+
},
49+
},
50+
],
51+
});
52+
const lowered = (out.lowered.hooks as any[])[0];
53+
expect(lowered.body.capabilities).toEqual(['activity.append']);
54+
const r = HookSchema.safeParse(lowered);
55+
expect(r.success, JSON.stringify(r.error?.issues ?? [])).toBe(true);
56+
});
57+
58+
it('leaves the grant to the body an action takes', () => {
59+
expect(HookBodySchema.safeParse({ language: 'js', source: 'x', capabilities: ['email.send'] }).success).toBe(true);
60+
});
61+
});

‎packages/objectql/src/open-transaction.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
* explicitly, ADR-0067). A write through the engine would take another
1515
* connection, outside it.
1616
*
17-
* A handle whose unit has already closed counts as no transaction. Read
18-
* through the engine's own ambient store and unit ledger, so the answer is the
19-
* one the engine's write path acts on.
17+
* A handle of a unit the engine opened that has since closed counts as no
18+
* transaction. Read through the engine's own ambient store and unit ledger, so
19+
* the answer is the one the engine's write path acts on; those members are
20+
* private, and a real engine pins what is read off them
21+
* (`body-doors-transaction.integration.test.ts`, `@objectstack/runtime`).
2022
*/
2123
export type OpenTransactionKind = 'none' | 'joined' | 'separate';
2224

0 commit comments

Comments
 (0)