Skip to content

Commit 3c8ff67

Browse files
committed
fix(runtime): body doors read in the caller's own scope, bound every input, and stay out of a body's transaction
- A hook body's door reads a pointed-at record in the scope of the user whose write fired it. - Every string a door input takes is bounded; cross-field limits are stated in the published description. - A door call inside ctx.api.transaction is refused; an entry's origin names the package that holds the body. - A body's message is sent only if the mail log records it. - A mail-log row the mail service did not write is sent from the configured sender with no sender header of its own. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwV8gsjrvCXCqKoaiG
1 parent efbf75c commit 3c8ff67

28 files changed

Lines changed: 823 additions & 103 deletions

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
'@objectstack/spec': minor
33
'@objectstack/core': patch
4+
'@objectstack/objectql': patch
45
'@objectstack/runtime': patch
56
'@objectstack/cli': patch
67
'@objectstack/rest': patch
@@ -15,12 +16,17 @@ feat(runtime): a script body sends mail and adds a timeline entry through platfo
1516
Clause-②: yes (widening)
1617
Metadata-protocol: added
1718

18-
- **`@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. `SYS_ACTIVITY_PLATFORM_TYPES` names the timeline types only the platform records.
19-
- **`@objectstack/runtime`:** a hook, action or job body that declares the capability calls `ctx.email.send({ to, subject, text })` or `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. A message names at most 50 recipients, 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 body with no calling user (a job) sends and writes with no user recorded.
19+
**BREAKING** (`@objectstack/plugin-email`): a `queued` row an app inserts into the mail log (`sys_email`, `sys_identity_email`) is sent from the configured sender, with no `From`, `Sender` or `Return-Path` header of its own, whatever sender the row names; a deployment with no configured sender refuses such a row. Migration: FROM inserting a mail-log row that names its own sender TO `ctx.email.send({ to, subject, text })` in a body that declares `email.send`, which sends from the configured sender with the calling user named in its display name. A row inserted with no sender of its own is delivered as before.
20+
21+
<!-- 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 -->
22+
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`:** a hook, action or job body that declares the capability calls `ctx.email.send({ to, subject, text })` or `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 door call inside `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.
2025
- **`@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'`.
26+
- **`@objectstack/objectql`:** `seedHookUserGrants` and `seedHookUnreadMembership`, the second step of a `runAs: 'user'` derivation and its fail-safe, are exported beside `deriveHookApi`.
2127
- **`@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.
2228
- **`@objectstack/rest`:** the REST mail door reads its bounds from `boundCallerMail`; its answers are unchanged.
23-
- **`@objectstack/plugin-email`:** a mail-log row the mail service did not write is delivered from the configured sender.
24-
- **`@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 the platform recorded.
29+
- **`@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.
30+
- **`@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.
2531
- **`@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.
2632
- **`@object-ui/app-shell`:** the script capability picker lists both capabilities with their calls.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ 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. See [Email service](/docs/kernel/runtime-services/email-service#from-a-script-body). | `email.send` |
109-
| `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`), and the record must be one the caller can read. | `activity.append` |
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. Refused inside `ctx.api.transaction`. See [Email service](/docs/kernel/runtime-services/email-service#from-a-script-body). | `email.send` |
109+
| `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. Refused inside `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

112112
### Write provenance — `ctx.provenance`

‎content/docs/deployment/environment-variables.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Link previews read the same values. The console document served at `/_console/`
175175
| `OS_EMAIL_SMTP_SECURE` | boolean | `true` | Require TLS. On port `465` that means implicit TLS; on any other port a **required** STARTTLS upgrade (a server that will not upgrade fails the send instead of leaking credentials in the clear). `false` connects in the clear and upgrades only when STARTTLS is offered. |
176176
| `OS_EMAIL_SMTP_USER` | string | — | SMTP AUTH username. Omit for servers that accept unauthenticated relay. |
177177
| `OS_EMAIL_SMTP_PASSWORD` | string | — | SMTP AUTH password. |
178-
| `OS_EMAIL_APP_SEND_HOURLY_LIMIT` | number | `200` | The most messages an organization's mail log may record in one hour before a script body's `ctx.email.send` is refused with `429 RATE_LIMIT_EXCEEDED`. A whole number, `0` or more; any other value fails the call. See [Email service](/docs/kernel/runtime-services/email-service#from-a-script-body). |
178+
| `OS_EMAIL_APP_SEND_HOURLY_LIMIT` | number | `200` | The most messages an organization's mail log may record in one hour before a script body's `ctx.email.send` is refused with `429 RATE_LIMIT_EXCEEDED`. A whole number, `0` or more; any other value fails the call. The count is read before each send, so it bounds volume rather than holding an exact number. See [Email service](/docs/kernel/runtime-services/email-service#from-a-script-body). |
179179

180180
> SMTP delivery ships in `@objectstack/plugin-email` (ADR-0012) and is
181181
> implemented with `nodemailer`, imported lazily so deployments that never

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,20 @@ A hook, action or job body sends mail with `ctx.email.send(input)` when it decla
3636

3737
- The message is sent from the configured sender. Its display name names the calling user beside the organization, for example `Ann Lee via Acme`. Replies go to `replyTo`.
3838
- It is recorded as sent by the calling user and under the caller's organization, with the same bounds as the HTTP door. `from`, `sentBy`, `organizationId`, `headers` and `identityMail` are not inputs, and a call that names one is refused with `400 VALIDATION_ERROR`.
39-
- `related` must be a record the caller can read. Otherwise the call is refused with `404 RECORD_NOT_FOUND` and nothing is sent.
40-
- A message names at most 50 recipients across `to`, `cc` and `bcc`.
41-
- 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`.
39+
- `related` must be a record the caller can read, in the caller's own scope. Otherwise the call is refused with `404 RECORD_NOT_FOUND` and nothing is sent.
40+
- 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`.
41+
- 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.
42+
- 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 call made inside `ctx.api.transaction` is refused with `409 RESOURCE_CONFLICT`, because the door acts outside the body's transaction. Call it after the transaction commits.
4244
- A job body has no calling user, so its message is recorded with no `sentBy`.
4345

4446
The call answers `{ id, status }`. A deployment with no mail service answers `501 NOT_IMPLEMENTED`.
4547

46-
A row written into the mail log (`sys_email`, `sys_identity_email`) by anything other than the mail service is delivered from the configured sender, whatever its `from_address` said.
48+
A row written into the mail log (`sys_email`, `sys_identity_email`) at `queued` by anything other than the mail service is sent from the configured sender, with no `From`, `Sender` or `Return-Path` header of its own, whatever the row said. Where no sender is configured, the mail log refuses such a row.
49+
50+
<Callout type="warn">
51+
**Breaking for direct inserts.** Before this release, a `queued` row an app inserted into the mail log was sent from the `from_address` the row named. It is now sent from the configured sender. Send through `ctx.email.send`, which names the calling user in the sender's display name, instead of inserting the row.
52+
</Callout>
4753

4854
## Returns
4955

‎content/docs/references/data/hook-body-doors.mdx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const result = BodyActivityAppendInputSchema.parse(data);
3838

3939
## BodyActivityAppendInput
4040

41-
ctx.activity.append input: one entry a script body adds to a record timeline
41+
ctx.activity.append input: one entry a script body adds to a record timeline. metadata is at most 8000 characters as JSON.
4242

4343
### Properties
4444

@@ -50,7 +50,7 @@ ctx.activity.append input: one entry a script body adds to a record timeline
5050
| **summary** | `string` | ✅ | One-line summary |
5151
| **recordLabel** | `string` | optional | Display label of the record, as of the entry |
5252
| **source** | `{ object: string; id: string }` | optional | The record the entry points at for its detail |
53-
| **metadata** | `Record<string, any>` | optional | Display hints stored beside the entry |
53+
| **metadata** | `Record<string, any>` | optional | Display hints stored beside the entry, at most 8000 characters as JSON |
5454

5555
### Nested Shape: `BodyActivityAppendInput.source`
5656

@@ -78,7 +78,7 @@ A record the door reads in the caller's scope before it acts
7878

7979
## BodyMailSendInput
8080

81-
ctx.email.send input: a message a script body hands to the mail service
81+
ctx.email.send input: a message a script body hands to the mail service. At most 50 recipients across to, cc and bcc together; each of to, cc and bcc at most 4000 characters as recorded ("Name" `<address>`, joined by ", "); a replyTo at most 320 characters as recorded.
8282

8383
### Properties
8484

‎packages/objectql/src/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,11 @@ export {
373373
UnscopedHookApi,
374374
hookRunAs,
375375
deriveHookApi,
376+
// The second step of a `runAs: 'user'` derivation, and its fail-safe: the
377+
// runtime's body doors read a record in the hook's calling user's scope
378+
// through the same two steps the hook wrapper takes.
379+
seedHookUserGrants,
380+
seedHookUnreadMembership,
376381
} from './hook-run-as.js';
377382
export type { HookRunAs, HookRunAsRef, RunAsDerivableApi } from './hook-run-as.js';
378383
// Boot guard: thrown by `ObjectQL.init()` when a registered driver's connect()

‎packages/plugins/plugin-audit/src/objects/sys-activity.object.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ export const SysActivity = ObjectSchema.create({
375375
}),
376376

377377
// The activity door (`app-activity-door.ts`) stamps the app code that wrote
378-
// an entry here; the platform's own writers leave it empty, so a reader
379-
// tells an app's entry from a recorded one.
378+
// an entry here. Every other writer leaves it empty: the platform's own,
379+
// and, while it is still taken, an app's insert into this object.
380380
origin: Field.text({
381381
label: 'Origin',
382382
required: false,
@@ -385,7 +385,7 @@ export const SysActivity = ObjectSchema.create({
385385
maxLength: 500,
386386
description:
387387
'The app code that added this entry through the activity door, as "<package>/<kind>:<name>" '
388-
+ '(for example "com.acme.crm/action:send_email"). Empty on an entry the platform recorded.',
388+
+ '(for example "com.acme.crm/action:send_email"). Empty on an entry not written through the activity door.',
389389
group: 'Context',
390390
}),
391391

‎packages/plugins/plugin-audit/src/translations/en.objects.generated.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export const enObjects: NonNullable<TranslationData['objects']> = {
193193
},
194194
origin: {
195195
label: "Origin",
196-
help: "The app code that added this entry through the activity door, as \"<package>/<kind>:<name>\" (for example \"com.acme.crm/action:send_email\"). Empty on an entry the platform recorded."
196+
help: "The app code that added this entry through the activity door, as \"<package>/<kind>:<name>\" (for example \"com.acme.crm/action:send_email\"). Empty on an entry not written through the activity door."
197197
},
198198
changed_fields: {
199199
label: "Changed Fields",

‎packages/plugins/plugin-email/src/email-plugin.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,10 @@ export class EmailServicePlugin implements Plugin {
11141114
const fresh = Array.isArray(rows) ? rows[0] : (rows as any)?.data?.[0];
11151115
const target = fresh ?? row;
11161116
if (target.status !== 'queued' || target.message_id) return;
1117-
await svc.deliverPersistedRow(target, undefined, object);
1117+
// Sent from the configured sender, as the insert-time bound
1118+
// recorded it (`outbox-sender-bound.ts`), whatever the row
1119+
// says by the time it is read here.
1120+
await svc.deliverPersistedRow(target, { asConfiguredSender: true }, object);
11181121
} catch (err: any) {
11191122
// `error`, not `warn` (#5161): the insert returned, the row
11201123
// is there, everything looks normal — and the mail was NOT

0 commit comments

Comments
 (0)