Skip to content
Merged
14 changes: 14 additions & 0 deletions .changeset/24343-8b0f-grant-on-first-membership.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@objectstack/plugin-auth": patch
---

fix(plugin-auth): the self-registration grant settles on the registrant's first membership, owned by that organization (ADR-0131 D3)

Clause-②: no

The declared self-registration permission set (`auth.audience.selfRegistrationPermissionSet`) is an assignment, and an assignment is an organization row (ADR-0131 D3). It is now written with the registrant's first membership and owned by that membership's organization, under every posture. It is never written without an organization.

- **`single`: unchanged.** The membership reconciler binds the Default Organization in the same `user.create.after` hook, so a sign-up still ends with one grant, owned by the Default Organization.
- **A wall (`group`, `isolated`).** A self-registrant belongs to no organization at sign-up, so the sign-up writes no `sys_user_permission_set` row. It used to write one with no organization. The grant is held as a pending claim in `sys_verification`, which is deployment-level and survives a restart. The first `sys_member` row written for the user lands exactly one grant, owned by that organization. That row can come from creating an organization (the creator becomes its owner), accepting an invitation, being added by an administrator, or any other membership writer. A later membership writes no second grant.
- **The claim waits 365 days.** A registrant who first joins an organization after that holds only the deployment's baseline. An engine with no middleware seam cannot settle a claim, so there a registrant with no organization is reported ungranted at `error` at sign-up instead.
- Grants already written without an organization by earlier walled sign-ups are not touched here.
50 changes: 25 additions & 25 deletions content/docs/permissions/tenant-audit-census.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ what moved this page's population from 225 to 227; nothing about the two sites
changed, only whether this instrument could see them.

**The expensive failure direction is a keyword.** Sites whose receiver the author
typed `any` have no type to read, and there are 38 of them — just under a fifth
typed `any` have no type to read, and there are 40 of them — just under a fifth
of the population, concentrated in exactly the seed and bootstrap paths this
control exists for. Scoring an unreadable receiver as "not an engine" would have
dropped every one of them silently, with a clean exit and a smaller number that
Expand Down Expand Up @@ -122,7 +122,7 @@ are reported as `undecidable` rather than assumed either way.

The same holds twice over for the context. An options argument spelled as a
literal can be read; one spelled `options`, `{ ...opts }`, or handed through a
forwarding shim cannot, and **53 of the 229 sites are spelled that way**. A
forwarding shim cannot, and **52 of the 231 sites are spelled that way**. A
context resolved from an inline literal or a local `const` can be tested for
`isSystem`; one arriving from a helper call cannot.

Expand Down Expand Up @@ -150,8 +150,8 @@ now **0**: nothing on this surface threads a context that provably lacks the fla

**"No tenant context" counted sites it had not read.** An options argument the
walker could not parse was folded into the same bucket as one it had read and
found empty. That published **61 sites "carrying no tenant context at all"**
when 8 said so and 53 were simply unread — an over-claim in the *alarming*
found empty. That published **60 sites "carrying no tenant context at all"**
when 8 said so and 52 were simply unread — an over-claim in the *alarming*
direction, on the very figure this page tells other cards to cite. `carries` is
now three-valued, and an unreadable argument can never contribute to the
provable count.
Expand Down Expand Up @@ -187,10 +187,10 @@ reproduce them. Where it disagrees, it disagrees on the page:

| carried figure | where it survives | this census |
| :--- | :--- | ---: |
| 175 write call sites | quoted in the merged changeset | **229** |
| 24 carrying no tenant context | quoted in the merged changeset | **2** provable and tenancy-enabled; **32** more whose options argument is unreadable |
| 127 of 175 statically decidable, 48 runtime-parameter-name sites | restated on the `isSystem`-scoping card | **158 of 229** decidable, **71** undecidable |
| 135 (77%) silenced by the `isSystem` guard before the posture gate | the lost issue body — **no surviving corroboration** | **not reproduced**: 125 decidably elevated, 0 decidably not, 96 undecidable |
| 175 write call sites | quoted in the merged changeset | **231** |
| 24 carrying no tenant context | quoted in the merged changeset | **2** provable and tenancy-enabled; **31** more whose options argument is unreadable |
| 127 of 175 statically decidable, 48 runtime-parameter-name sites | restated on the `isSystem`-scoping card | **160 of 231** decidable, **71** undecidable |
| 135 (77%) silenced by the `isSystem` guard before the posture gate | the lost issue body — **no surviving corroboration** | **not reproduced**: 128 decidably elevated, 0 decidably not, 95 undecidable |
| 141 and 132, two independent re-derivations | the card that filed this work | — |

**The differences are not reconciled, and deliberately so.** The old census's
Expand All @@ -200,21 +200,21 @@ be stated is what this instrument counts, which is written above and re-runnable
at any commit.

Two structural facts do plausibly widen this reading against any hand or regex
one, and both are counted in the generated tables below: the 38 sites reached
one, and both are counted in the generated tables below: the 40 sites reached
through an erased (`any`) receiver, and the 52 that name their object through a
`const` rather than inline. An instrument that read either the way a person does
would report a smaller number and would not say so.

The fourth row is the one worth flagging to anyone citing it. **The 135 / 77%
figure has no surviving corroboration anywhere in the tree.** This census reads
125 of 229 (55%) as decidably elevated, with 96 more whose elevation is a
128 of 231 (55%) as decidably elevated, with 95 more whose elevation is a
run-time fact — so the claim is neither confirmed nor refuted, and the honest
answer is that a static reading cannot settle it.

⇒ **Cite `2 / 229`, and say what it is**: the sites whose options argument was
⇒ **Cite `2 / 231`, and say what it is**: the sites whose options argument was
READ and holds no tenant context, against a decidably tenancy-enabled object.
That is the control's provable yield surface. ⛔ Do not cite it as "the sites
without tenant context" — **32 further sites** have an options argument this
without tenant context" — **31 further sites** have an options argument this
cannot read, and they are neither in nor out.

{/* BEGIN GENERATED: tenant-audit-census (scripts/tenant-audit-census.mjs) — DO NOT EDIT */}
Expand All @@ -223,28 +223,28 @@ cannot read, and they are neither in nor out.

| what | count |
| :--- | ---: |
| write call sites on the application surface | **229** |
| …whose object name is statically decidable | 158 |
| write call sites on the application surface | **231** |
| …whose object name is statically decidable | 160 |
| …whose object name is chosen at run time | 71 |
| …against an object with tenancy ENABLED | 157 |
| …against an object with tenancy ENABLED | 159 |
| …against an object that declares tenancy off | 1 |
| threading a tenant context | 168 |
| threading a tenant context | 171 |
| PROVABLY carrying none (options read, no context key) | **8** |
| …of those, against a decidably tenancy-enabled object | **2** |
| options argument UNREADABLE — may or may not carry one | 53 |
| …of those, against a decidably tenancy-enabled object | 32 |
| threading a decidably ELEVATED (`isSystem`) context | 125 |
| options argument UNREADABLE — may or may not carry one | 52 |
| …of those, against a decidably tenancy-enabled object | 31 |
| threading a decidably ELEVATED (`isSystem`) context | 128 |
| threading a context that is decidably NOT elevated | 0 |
| threading a context whose elevation is a run-time fact | 96 |
| threading a context whose elevation is a run-time fact | 95 |

| how the instrument reached the site | count |
| :--- | ---: |
| receiver carried a readable engine type | 191 |
| receiver erased, placed by the object NAME | 28 |
| receiver erased, placed by the object NAME | 30 |
| receiver erased, placed by an `object: string` PARAMETER | 5 |
| receiver erased, placed by an `UNTYPED_RECEIVERS` row | 5 |

| object name spelled inline | 106 |
| object name spelled inline | 108 |
| object name spelled through a `const` | 52 |
| object name is an `object: string` parameter | 7 |
| object name is some other run-time expression | 64 |
Expand Down Expand Up @@ -297,13 +297,13 @@ holds still. They are required to be HERE and to say WHEN they were true;
their values are not compared. The reasoning, and the measurement behind it,
are in `scripts/check-tenant-audit-census.mjs`.

Measured on 2026-10-16 at `22b217924`.
Measured on 2026-10-19 at `2623d6d6c`.

| corpus scale (not enforced) | count |
| :--- | ---: |
| tracked non-test sources scanned | 657 |
| tracked non-test sources scanned | 670 |
| engine-shaped types recognised | 79 |
| declared objects in the registry | 116 |
| same-named calls subtracted as non-engine | 165 |
| same-named calls subtracted as non-engine | 166 |

{/* END GENERATED: tenant-audit-census */}
26 changes: 14 additions & 12 deletions docs/audits/2026-08-tenant-audit-write-call-sites.counts.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ silent, and `node scripts/tenant-audit-census.mjs --write` is the resolution.

| Measure | Value |
|---|---:|
| Write call sites | 229 |
| Object name statically decidable | 158 |
| Write call sites | 231 |
| Object name statically decidable | 160 |
| Object name chosen at run time | 71 |
| Against a tenancy-enabled object | 157 |
| Against a tenancy-enabled object | 159 |
| Against an object declaring tenancy off | 1 |
| Threading a tenant context | 168 |
| Threading a tenant context | 171 |
| Provably carrying none | 8 |
| …and decidably tenancy-enabled | 2 |
| Options argument unreadable | 53 |
| …and decidably tenancy-enabled | 32 |
| Threading a decidably elevated context | 125 |
| Options argument unreadable | 52 |
| …and decidably tenancy-enabled | 31 |
| Threading a decidably elevated context | 128 |
| Threading a decidably non-elevated context | 0 |
| Threading a context of undecidable elevation | 96 |
| Threading a context of undecidable elevation | 95 |

## Subtractions the census could NOT defend — enforced

Expand Down Expand Up @@ -90,14 +90,14 @@ holds still. They are required to be HERE and to say WHEN they were true;
their values are not compared. The reasoning, and the measurement behind it,
are in `scripts/check-tenant-audit-census.mjs`.

Measured on 2026-10-16 at `22b217924`.
Measured on 2026-10-19 at `2623d6d6c`.

| corpus scale (not enforced) | count |
| :--- | ---: |
| tracked non-test sources scanned | 657 |
| tracked non-test sources scanned | 670 |
| engine-shaped types recognised | 79 |
| declared objects in the registry | 116 |
| same-named calls subtracted as non-engine | 165 |
| same-named calls subtracted as non-engine | 166 |

## Every site

Expand Down Expand Up @@ -133,7 +133,9 @@ Measured on 2026-10-16 at `22b217924`.
| `packages/plugins/plugin-auth/src/auth-manager.ts` | `update` | `sys_session` | enabled | options unreadable | 3 |
| `packages/plugins/plugin-auth/src/auth-manager.ts` | `update` | `sys_two_factor` | enabled | options unreadable | 1 |
| `packages/plugins/plugin-auth/src/auth-manager.ts` | `update` | `sys_user` | enabled | options unreadable | 8 |
| `packages/plugins/plugin-auth/src/auth-manager.ts` | `insert` | `sys_user_permission_set` | enabled | options unreadable | 1 |
| `packages/plugins/plugin-auth/src/auth-manager.ts` | `insert` | `sys_user_permission_set` | enabled | elevated | 1 |
| `packages/plugins/plugin-auth/src/auth-manager.ts` | `delete` | `sys_verification` | enabled | elevated | 1 |
| `packages/plugins/plugin-auth/src/auth-manager.ts` | `insert` | `sys_verification` | enabled | elevated | 1 |
| `packages/plugins/plugin-auth/src/auth-plugin.ts` | `update` | `sys_oauth_application` | enabled | PROVABLY NONE | 1 |
| `packages/plugins/plugin-auth/src/auth-plugin.ts` | `update` | `sys_user` | enabled | elevated | 1 |
| `packages/plugins/plugin-auth/src/auth-plugin.ts` | `update` | `SystemObjectName.USER` | undecidable | elevated | 1 |
Expand Down
16 changes: 16 additions & 0 deletions packages/plugins/plugin-auth/src/audience-posture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ const PASSWORD = 'S3cure!Passw0rd-4785';
const bindRowCatalog = (engine: any) =>
bindTestSecurityCatalog(engine, () => catalogFromTables({ sys_permission_set: engine.tables.get('sys_permission_set') }));

/**
* [ADR-0131 D3] The self-registration grant is written WITH a membership. The
* end-of-chain cases that assert the grant lands at sign-up run `single`: the
* reconciler binds the Default Organization in the same `user.create.after`
* hook, and the grant is owned by it. The walled half (no membership yet, the
* grant held as a claim until the first one) is pinned in
* `self-registration-grant-first-membership.test.ts`.
*/
const SINGLE_TENANCY = () => ({ defaultOrgId: async () => 'org_default' });

const makeManager = (engine: any, config: Record<string, unknown> = {}) =>
new AuthManager({
secret: SECRET,
Expand Down Expand Up @@ -731,6 +741,7 @@ describe('end of the chain: better-auth pipeline over the memory engine (#11739)
seedExistingUser(engine);
seedPermissionSet(engine, 'portal_user');
const manager = makeManager(engine, {
getTenancy: SINGLE_TENANCY,
audience: {
posture: 'email_domain',
allowedEmailDomains: ['acme.com'],
Expand All @@ -747,6 +758,8 @@ describe('end of the chain: better-auth pipeline over the memory engine (#11739)
expect(grants[0].permission_set).toBe('portal_user');
const created = (engine.tables.get('sys_user') ?? []).find((u: any) => u.email === 'user+tag@acme.com');
expect(grants[0].user_id).toBe(created?.id);
// ADR-0131 D3: owned by the membership it travels with.
expect(grants[0].organization_id).toBe('org_default');
});

const refused = await signUp(manager, 'user@mail.acme.com'); // subdomain: NOT implied
Expand All @@ -764,6 +777,7 @@ describe('end of the chain: better-auth pipeline over the memory engine (#11739)
const engine = createMemoryEngine();
seedExistingUser(engine);
const manager = makeManager(engine, {
getTenancy: SINGLE_TENANCY,
audience: { posture: 'open', selfRegistrationPermissionSet: 'portal_user' },
});
bindTestSecurityCatalog(engine, { permissions: [{ name: 'portal_user' }] });
Expand Down Expand Up @@ -838,6 +852,7 @@ describe('end of the chain: better-auth pipeline over the memory engine (#11739)
seedExistingUser(engine);
seedPermissionSet(engine, 'member_default', { active: false });
const manager = makeManager(engine, {
getTenancy: SINGLE_TENANCY,
audience: { posture: 'open', selfRegistrationPermissionSet: 'member_default' },
});
const res = await signUp(manager, 'anyone@anywhere.com');
Expand All @@ -852,6 +867,7 @@ describe('end of the chain: better-auth pipeline over the memory engine (#11739)
seedExistingUser(engine);
seedPermissionSet(engine, 'member_default');
const manager = makeManager(engine, {
getTenancy: SINGLE_TENANCY,
audience: { posture: 'open', selfRegistrationPermissionSet: 'member_default' },
});
const res = await signUp(manager, 'anyone@anywhere.com');
Expand Down
Loading
Loading