@ultimat3/realtime 31.40.0 → 31.41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CLAUDE.md CHANGED
@@ -172,6 +172,7 @@ A rule marked `†` has its full text in [`docs/history/realtime-rules.md`](../.
172
172
  - **A channel is a DECLARATION, no primitive** (docs/history/primitive-factories.md):
173
173
  `channel(name, { params, policy, catchUp, records?, events? })`, deny by default; `policy`
174
174
  REQUIRED (`X_CHANNEL_DECLARATION_INVALID`; public: `allow('public')`). Presence: `events`.
175
+ - **A param naming a records entity's `$tenantColumn` is the subscriber's org** (`channel-tenant.ts`, before the policy). †
175
176
  - **A guard that FAILS on re-auth SUSPENDS the seat** (`guardFailures`): kept, silent until a pass.
176
177
  A `null` actor on a `row` channel and a tenancy refusal are DENIALS.
177
178
  - **A seat is decided for the actor ON the socket when the guard resolves** (`#settle`); a denied
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/realtime",
3
- "version": "31.40.0",
3
+ "version": "31.41.0",
4
4
  "description": "Three-tier realtime: channels, live queries, local-first sync — one protocol, one mutator shape",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -52,10 +52,10 @@
52
52
  "test": "bun test"
53
53
  },
54
54
  "dependencies": {
55
- "@ultimat3/core": "31.40.0",
56
- "@ultimat3/entity": "31.40.0",
57
- "@ultimat3/policy": "31.40.0",
58
- "@ultimat3/query": "31.40.0",
55
+ "@ultimat3/core": "31.41.0",
56
+ "@ultimat3/entity": "31.41.0",
57
+ "@ultimat3/policy": "31.41.0",
58
+ "@ultimat3/query": "31.41.0",
59
59
  "nats": "2.29.3"
60
60
  }
61
61
  }
@@ -1,17 +1,21 @@
1
- // A declared channel's policy, asked on subscribe: the params are the input and the row is what
2
- // the channel's own loader answered. Through `@ultimat3/query`'s `guardQuery`, the package's one
3
- // authz seam — the same call `policy-gate.ts` makes for a live query.
1
+ // A declared channel's subscribe decision: the tenant first (`channel-tenant.ts`), then the policy,
2
+ // with the params as input and the row the channel's own loader answered. Through
3
+ // `@ultimat3/query`'s `guardQuery`, the package's one authz seam — the call `policy-gate.ts` makes.
4
4
 
5
5
  import { type Actor, type Ctx, ctxOf, runWithContext } from '@ultimat3/core';
6
6
  import { guardQuery, QueryDeniedError } from '@ultimat3/query';
7
7
  import type { Channel } from './channel-decl';
8
+ import { assertChannelTenant } from './channel-tenant';
8
9
  import { isTenancyDenial, TopicForbiddenError } from './errors';
9
10
 
10
11
  /**
11
12
  * A denial is `X_TOPIC_FORBIDDEN`. A loader or a rule that RAISED is not a denial and leaves as it
12
13
  * came, so the caller can tell an outage from a decision — `onActorChange` suspends the topic on one.
13
14
  *
14
- * Two answers are denials before any rule runs, because neither is an outage:
15
+ * Three answers are denials before any rule runs, because none is an outage:
16
+ * - **a param naming a records entity's tenant column that is not the actor's org**
17
+ (`assertChannelTenant`): every frame kind — records, events, presence — rides the seat this
18
+ grants, so the tenant is the mechanism's to check, never a rule's to remember.
15
19
  * - **nobody, on a channel that decides on a row.** A loader runs AS the subscriber; with no actor
16
20
  * it would run as the node, and a row is not something nobody is entitled to.
17
21
  * - **a loader the tenant guard refused** (`TENANCY_DENIAL_CODES`): the actor has no org, or names
@@ -24,6 +28,7 @@ export async function authorizeChannel(
24
28
  topic: string,
25
29
  params: Readonly<Record<string, string>>,
26
30
  ): Promise<void> {
31
+ assertChannelTenant(channel, actor, topic, params, (who) => subscriberContext(ctx, who));
27
32
  const loader = channel.row;
28
33
  if (loader === undefined) return decide(channel, ctx, actor, topic, params, null);
29
34
  if (actor === null) {
@@ -0,0 +1,55 @@
1
+ // The tenant half of a channel subscribe: a param that names a records entity's tenant column must
2
+ // be the subscriber's own org. Decided by `@ultimat3/entity`'s own guard (`assertScoped`), so a
3
+ // socket is held to the rule an HTTP read is — never a second copy of it, and never left to a policy.
4
+
5
+ import { type Actor, type Ctx, isUltimateError, runWithContext } from '@ultimat3/core';
6
+ import { assertScoped, type QueryPlan } from '@ultimat3/entity';
7
+ import { tenantColumnOfTable } from './change-tenant';
8
+ import type { Channel } from './channel-decl';
9
+ import { isTenancyDenial, TopicForbiddenError } from './errors';
10
+
11
+ /**
12
+ * A row is routed to the topic its own params name (`channel-records.ts`), so a topic keyed by the
13
+ * tenant column IS one tenant's stream — of records, and of the events and presence that ride the
14
+ * same seat. Granting it on the policy alone handed org B's rows to any org-A member whose rule
15
+ * forgot to compare the org (`can('post:read')` does not).
16
+ *
17
+ * The verdict is `assertScoped`'s over the plan the subscribe amounts to — `<column> = <param>`
18
+ * under the subscriber's context: the actor's org or `X_TENANCY_ACTOR_MISMATCH`, no org at all
19
+ * `X_TENANCY_ACTOR_ORG_REQUIRED`, a proven `crossTenant()` scope passes. A refusal reaches the
20
+ * socket as `X_TOPIC_FORBIDDEN` naming that code, a denial the hub latches. Anything else the
21
+ * guard raised is not a decision and leaves as it came.
22
+ */
23
+ export function assertChannelTenant(
24
+ channel: Channel,
25
+ actor: Actor | null,
26
+ topic: string,
27
+ params: Readonly<Record<string, string>>,
28
+ contextFor: (actor: Actor) => Ctx,
29
+ ): void {
30
+ let ctx: Ctx | undefined;
31
+ for (const projection of channel.records) {
32
+ const column = tenantColumnOfTable(projection.table);
33
+ if (column === null || !channel.params.includes(column)) continue;
34
+ const refusal = (code: string): TopicForbiddenError =>
35
+ new TopicForbiddenError({
36
+ topic,
37
+ actorId: actor === null ? null : actor.id,
38
+ reason: `channel "${channel.name}" carries "${projection.type}" rows by its tenant column "${column}", and this actor's tenant is not the one the param names (${code})`,
39
+ });
40
+ if (actor === null) throw refusal('X_TENANCY_ACTOR_ORG_REQUIRED');
41
+ const plan: QueryPlan = {
42
+ entity: projection.type,
43
+ where: [{ column, op: 'eq', value: params[column] }],
44
+ orderBy: [],
45
+ limit: 1,
46
+ };
47
+ ctx ??= contextFor(actor);
48
+ try {
49
+ runWithContext(ctx, () => assertScoped(projection.type, column, 'subscribe', plan));
50
+ } catch (error) {
51
+ if (!isTenancyDenial(error) || !isUltimateError(error)) throw error;
52
+ throw refusal(error.code);
53
+ }
54
+ }
55
+ }