From 7a26d986e762c1034f571be85f187f09ac0e2064 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 3 Dec 2026 14:44:03 +0000 Subject: [PATCH 1/5] fix(plugin-sharing): refuse an unresolved user recipient on the sys_sharing_rule /data door The Setup sharing-rule form writes through POST /api/v1/data/sys_sharing_rule, which never reaches defineRule, so a rule whose `user` recipient named no user (or no member of the rule's organization) was stored and granted no one access. bindRuleRecipientGuard refuses that insert, and an update that moves a rule onto such a recipient, with defineRule's own verdict and sentence (SharingService.userRecipientRefusal), VALIDATION_FAILED under recipient_id. System writes and updates that keep the stored recipient are not judged. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01Utp6CZVKCmRkE5bj7Rwba3 --- .../38254-sharing-rule-data-door-recipient.md | 14 + .../plugins/plugin-sharing/src/rule-hooks.ts | 69 +++++ .../plugin-sharing/src/sharing-plugin.ts | 6 + .../src/sharing-rule-recipient-guard.test.ts | 241 ++++++++++++++++++ 4 files changed, 330 insertions(+) create mode 100644 .changeset/38254-sharing-rule-data-door-recipient.md create mode 100644 packages/plugins/plugin-sharing/src/sharing-rule-recipient-guard.test.ts diff --git a/.changeset/38254-sharing-rule-data-door-recipient.md b/.changeset/38254-sharing-rule-data-door-recipient.md new file mode 100644 index 00000000000..a0b2b3b9f48 --- /dev/null +++ b/.changeset/38254-sharing-rule-data-door-recipient.md @@ -0,0 +1,14 @@ +--- +'@objectstack/plugin-sharing': patch +--- + +fix(plugin-sharing): a sharing rule saved through the `/data` door (the Setup sharing-rule form) is refused when its `user` recipient names no user the rule could share with + +Clause-②: no (narrowing) +Metadata-protocol: none + +Who sees it: an administrator saving a sharing rule in Setup, or any caller of `POST`/`PATCH /api/v1/data/sys_sharing_rule`. + +- **Before:** a rule whose `user` recipient named no user (or, on a multi-organization deployment, no member of the rule's organization) was stored. It granted no one access, and the form said it saved. +- **Now:** the insert, and an update that moves a rule onto such a recipient, answer `400 VALIDATION_FAILED` under `recipient_id`, with the sentence `POST /api/v1/sharing/rules` already answered for the same recipient. Nothing is stored. An id that does not exist and a user of another organization get the same answer. +- An update that keeps the stored recipient is not judged, so a rule whose user has since left can still be switched off or edited. System writes are not judged. diff --git a/packages/plugins/plugin-sharing/src/rule-hooks.ts b/packages/plugins/plugin-sharing/src/rule-hooks.ts index 2720d62623b..50e8bd83995 100644 --- a/packages/plugins/plugin-sharing/src/rule-hooks.ts +++ b/packages/plugins/plugin-sharing/src/rule-hooks.ts @@ -4,6 +4,7 @@ import type { SharingRuleService } from './sharing-rule-service.js'; import type { SharingRuleRow } from '@objectstack/spec/contracts'; import { isMatchAllCriteria, SharingCriteriaValidationError, SharingRuleObjectNotEnforcedError } from './rule-criteria.js'; import type { SharingService } from './sharing-service.js'; +import { validationFailure } from '@objectstack/types'; import { RULE_RECOMPUTE_ROW_CAP, RuleRegrantQueue, @@ -33,6 +34,9 @@ export const RULE_CRITERIA_GUARD_PACKAGE = 'plugin-sharing:rule-criteria-guard'; /** Package id of {@link bindRuleObjectEnforcementGuard}'s hooks. */ export const RULE_OBJECT_ENFORCEMENT_GUARD_PACKAGE = 'plugin-sharing:rule-object-enforcement-guard'; +/** Package id of {@link bindRuleRecipientGuard}'s hooks. */ +export const RULE_RECIPIENT_GUARD_PACKAGE = 'plugin-sharing:rule-recipient-guard'; + /* * RETIRED — the `isSystem` materialisation skip, and with it the INFO line that * announced it (`SYSTEM_WRITE_SKIP_NOTICE`, #6783 / #4707 demand 3). @@ -427,3 +431,68 @@ export function bindRuleObjectEnforcementGuard( engine.registerHook('beforeInsert', guard(true), opts); engine.registerHook('beforeUpdate', guard(false), opts); } + +/** + * [#38254] Refuse a `sys_sharing_rule` write whose `user` recipient names no + * user the rule could share with. + * + * `SharingRuleService.defineRule` refuses such a recipient (#33912), but the + * Setup sharing-rule form writes through the `/data` door, a plain write on + * this table that never reaches that method. Before this guard that door + * stored the rule: it granted no one access, and the author was told it + * saved. The verdict and the sentence are `defineRule`'s own + * ({@link SharingService.userRecipientRefusal}, subject `rule`), so the two + * doors cannot disagree, and the refusal is `VALIDATION_FAILED` (400) under + * `recipient_id`. + * + * Judged in the rule's organization: on an update the stored row's, on an + * insert the row's own `organization_id` or else the caller's active + * organization (the one the engine stamps on the row). An id that names no + * user and a user of another organization are answered alike, so the door + * is no oracle for who exists in another tenant. With no organization under + * a wall the verdict stands aside, as it does for `defineRule`: the engine + * refuses that insert for every recipient alike. + * + * An insert with a `user` recipient is judged. An update is judged only when + * it leaves a `user` recipient that differs from the pre-image (type or id), + * so a rule whose user has since left can still be switched off or edited + * otherwise. System writes pass, as with the object guard: `defineRule` + * judges its own non-system callers, and the remaining system writers are + * the platform's own machinery. + */ +export function bindRuleRecipientGuard( + engine: MinimalEngine, + sharing: Pick, +): void { + if (typeof engine.registerHook !== 'function') return; + if (typeof engine.unregisterHooksByPackage === 'function') { + engine.unregisterHooksByPackage(RULE_RECIPIENT_GUARD_PACKAGE); + } + const present = (v: unknown): string | null => (typeof v === 'string' && v.length > 0 ? v : null); + const guard = (insert: boolean) => async (ctx: any) => { + if ((ctx?.session as any)?.isSystem) return; + const data = ctx?.input?.data; + if (!data || typeof data !== 'object' || Array.isArray(data)) return; + const previous = !insert && ctx?.previous && typeof ctx.previous === 'object' + ? ctx.previous as Record + : undefined; + const has = (key: string) => Object.prototype.hasOwnProperty.call(data, key); + if (!insert && !has('recipient_type') && !has('recipient_id')) return; + const recipientType = has('recipient_type') ? data.recipient_type : previous?.recipient_type; + if (recipientType !== 'user') return; + const recipientId = present(has('recipient_id') ? data.recipient_id : previous?.recipient_id); + // A missing id is the record validator's to answer (`required`), not this guard's. + if (!recipientId) return; + if (previous && previous.recipient_type === 'user' && previous.recipient_id === recipientId) return; + const organizationId = present(previous?.organization_id) + ?? present(data.organization_id) + ?? present(ctx?.session?.organizationId); + const refusal = await sharing.userRecipientRefusal(recipientId, organizationId, 'rule'); + if (refusal) { + throw validationFailure(refusal, [{ field: 'recipient_id', code: 'invalid_value', message: refusal }]); + } + }; + const opts = { object: 'sys_sharing_rule', packageId: RULE_RECIPIENT_GUARD_PACKAGE, priority: 100 }; + engine.registerHook('beforeInsert', guard(true), opts); + engine.registerHook('beforeUpdate', guard(false), opts); +} diff --git a/packages/plugins/plugin-sharing/src/sharing-plugin.ts b/packages/plugins/plugin-sharing/src/sharing-plugin.ts index 5ce5cba51d9..78f4f15db0b 100644 --- a/packages/plugins/plugin-sharing/src/sharing-plugin.ts +++ b/packages/plugins/plugin-sharing/src/sharing-plugin.ts @@ -85,6 +85,7 @@ import { rebindRuleHooks, bindRuleCriteriaGuard, bindRuleObjectEnforcementGuard, + bindRuleRecipientGuard, RULE_REBIND_TRIGGER_PACKAGE, } from './rule-hooks.js'; import { bindRuleProvenanceStamp, unbindRuleProvenanceStamp } from './sharing-rule-provenance.js'; @@ -1080,6 +1081,11 @@ export class SharingServicePlugin implements Plugin { // under record-sharing enforcement: it would save and share nothing. if (this.service) bindRuleObjectEnforcementGuard(engine, this.service); + // [#38254] …and so must the refusal of a `user` recipient that + // names no user the rule could share with (`defineRule`'s own + // verdict, #33912): it would save and grant no one access. + if (this.service) bindRuleRecipientGuard(engine, this.service); + // [ADR-0131 D6 regime C] …and so must the refusal of a row, new or // renamed, that takes a declared rule's name: `defineRule` makes it // for its own callers. A declared rule is customised by a clone diff --git a/packages/plugins/plugin-sharing/src/sharing-rule-recipient-guard.test.ts b/packages/plugins/plugin-sharing/src/sharing-rule-recipient-guard.test.ts new file mode 100644 index 00000000000..c5fedce04de --- /dev/null +++ b/packages/plugins/plugin-sharing/src/sharing-rule-recipient-guard.test.ts @@ -0,0 +1,241 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#38254] A sharing rule written through the `/data` door (the Setup + * sharing-rule form) is refused when its `user` recipient names no user the + * rule could share with — the refusal `defineRule` already made (#33912). + * + * Before: the `sys_sharing_rule` guards judged `object_name` and the criteria + * only, so `POST /api/v1/data/sys_sharing_rule` stored a rule whose recipient + * resolved to no user (or, behind a wall, to no member of the rule's + * organization). The rule granted no one access, and the author was told it + * saved. + * + * Now: `bindRuleRecipientGuard` refuses that insert, and an update that moves + * a rule onto such a recipient, with `VALIDATION_FAILED` under `recipient_id` + * and `defineRule`'s own sentence (`SharingService.userRecipientRefusal`, + * subject `rule`). An unknown id and a user of another organization get the + * same answer. The system context, and an update that keeps the stored + * recipient, are not judged. + * + * Real `ObjectQL` + `SqlDriver` (better-sqlite3 `:memory:`), so the hook runs + * on the engine's own write path and `ctx.previous` is the engine's pre-image. + * Each refusal sits beside a control that changes one value. + */ + +import { describe, it, expect, afterEach } from 'vitest'; +import { ObjectQL } from '@objectstack/objectql'; +import { SqlDriver } from '@objectstack/driver-sql'; +import type { ExecutionContext } from '@objectstack/spec/kernel'; + +import { SharingService } from './sharing-service.js'; +import { SharingRuleService } from './sharing-rule-service.js'; +import { bindRuleRecipientGuard } from './rule-hooks.js'; + +const OBJECT = 'os38254_deal'; + +type Fields = Record>; +const text = (name: string, extra: Record = {}) => ({ type: 'text', name, label: name, ...extra }); +const fieldsOf = (...names: string[]): Fields => { + const out: Fields = { id: text('id', { primary: true }) }; + for (const n of names) out[n] = text(n); + return out; +}; + +const DEAL_FIELDS = fieldsOf('stage', 'owner_id', 'organization_id'); +const RULE_FIELDS: Fields = { + ...fieldsOf( + 'organization_id', 'name', 'label', 'description', 'object_name', 'criteria_json', + 'recipient_type', 'recipient_id', 'access_level', 'managed_by', 'created_at', 'updated_at', + ), + active: { type: 'boolean', name: 'active', label: 'Active' }, + customized: { type: 'boolean', name: 'customized', label: 'Customized' }, +}; +const USER_FIELDS = fieldsOf('name'); +const MEMBER_FIELDS = fieldsOf('user_id', 'organization_id', 'role'); + +const ORG_A = 'org_a'; +const ORG_B = 'org_b'; +/** A member of ORG_A: the control recipient. */ +const MEMBER_A = 'u_member_a'; +/** An existing user who is a member of ORG_B only. */ +const MEMBER_B = 'u_member_b'; +/** No `sys_user`, no `sys_member`. */ +const UNKNOWN = 'nobody_xyz'; + +const ORG_A_ADMIN = { + userId: 'u_admin', + tenantId: ORG_A, + positions: [], + permissions: [], + systemPermissions: ['manage_sharing'], +} as unknown as ExecutionContext; +const SYSTEM_A = { isSystem: true, tenantId: ORG_A, positions: [], permissions: [] } as unknown as ExecutionContext; + +const RULE_REFUSAL = (id: string) => + `recipientId ${JSON.stringify(id)} does not name a user this rule can share with, ` + + 'so the rule would grant no one access. Name an existing user ' + + "(on a multi-organization deployment, a member of the rule's organization)."; + +const open: SqlDriver[] = []; +afterEach(async () => { + for (const d of open.splice(0)) { + try { await d.disconnect(); } catch { /* noop */ } + } +}); + +async function boot(posture: 'single' | 'isolated') { + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); + open.push(driver); + const ql = new ObjectQL(); + ql.registerDriver(driver as never, true); + await ql.init(); + ql.setTenancyPostureProvider(() => posture); + ql.registerObject({ name: OBJECT, label: 'Deal', sharingModel: 'private', fields: DEAL_FIELDS } as never); + ql.registerObject({ name: 'sys_sharing_rule', label: 'Sharing Rule', isSystem: true, fields: RULE_FIELDS } as never); + ql.registerObject({ name: 'sys_user', label: 'User', isSystem: true, systemFields: { tenant: false }, fields: USER_FIELDS } as never); + ql.registerObject({ name: 'sys_member', label: 'Member', isSystem: true, fields: MEMBER_FIELDS } as never); + await driver.initObjects([ + { name: OBJECT, fields: DEAL_FIELDS }, + { name: 'sys_sharing_rule', fields: RULE_FIELDS }, + { name: 'sys_user', fields: USER_FIELDS }, + { name: 'sys_member', fields: MEMBER_FIELDS }, + ] as never); + + // Seeded through the driver: the fixture is the DATA at rest. + for (const id of [MEMBER_A, MEMBER_B, 'u_admin']) await driver.create('sys_user', { id, name: id } as never); + await driver.create('sys_member', { id: 'm_a', user_id: MEMBER_A, organization_id: ORG_A, role: 'member' } as never); + await driver.create('sys_member', { id: 'm_b', user_id: MEMBER_B, organization_id: ORG_B, role: 'member' } as never); + + const sharing = new SharingService({ engine: ql as never, tenancy: () => ({ posture }) }); + const rules = new SharingRuleService({ engine: ql as never, sharing }); + bindRuleRecipientGuard(ql as never, sharing); + return { + ql, + rules, + storedRules: async () => + (await driver.find('sys_sharing_rule', {} as never)) as unknown as Array<{ id: string; recipient_id: string; active: unknown }>, + /** A stored rule written straight to the driver: a row saved before this guard. */ + seedRule: (id: string, recipientId: string, recipientType = 'user') => + driver.create('sys_sharing_rule', { ...ruleRow(id, recipientId), recipient_type: recipientType } as never), + }; +} + +const ruleRow = (name: string, recipientId: string) => ({ + id: `srule_${name}`, + organization_id: ORG_A, + name, + label: name, + object_name: OBJECT, + criteria_json: '{"stage":"won"}', + recipient_type: 'user', + recipient_id: recipientId, + access_level: 'read', + active: true, + managed_by: 'admin', +}); + +/** The refusal a write answers, or `null` when it was admitted. */ +async function refusalOf(call: Promise): Promise<{ code: string; message: string; fields: unknown } | null> { + return call.then( + () => null, + (e: any) => ({ code: String(e?.code ?? '(no code)'), message: String(e?.message ?? e), fields: e?.fields }), + ); +} + +describe('[#38254] the /data door refuses a sharing rule whose user recipient does not resolve — walled posture', () => { + it('an insert naming no user is refused VALIDATION_FAILED under recipient_id, and nothing is stored', async () => { + const { ql, storedRules } = await boot('isolated'); + const refusal = await refusalOf(ql.insert('sys_sharing_rule', ruleRow('r_unknown', UNKNOWN), { context: ORG_A_ADMIN } as never)); + expect(refusal, 'the unknown recipient was stored').not.toBeNull(); + expect(refusal!.code).toBe('VALIDATION_FAILED'); + expect(refusal!.message).toBe(RULE_REFUSAL(UNKNOWN)); + expect(refusal!.fields).toEqual([{ field: 'recipient_id', code: 'invalid_value', message: RULE_REFUSAL(UNKNOWN) }]); + expect(await storedRules()).toEqual([]); + }); + + it('control: an insert naming a member of the rule\'s organization is stored', async () => { + const { ql, storedRules } = await boot('isolated'); + await ql.insert('sys_sharing_rule', ruleRow('r_member', MEMBER_A), { context: ORG_A_ADMIN } as never); + expect((await storedRules()).map((r) => r.recipient_id)).toEqual([MEMBER_A]); + }); + + it('a user of ANOTHER organization is answered exactly as an id that does not exist', async () => { + const { ql, storedRules } = await boot('isolated'); + const foreign = await refusalOf(ql.insert('sys_sharing_rule', ruleRow('r_foreign', MEMBER_B), { context: ORG_A_ADMIN } as never)); + const unknown = await refusalOf(ql.insert('sys_sharing_rule', ruleRow('r_unknown', UNKNOWN), { context: ORG_A_ADMIN } as never)); + expect(foreign, 'the other-organization user was stored').not.toBeNull(); + expect(foreign!.code).toBe(unknown!.code); + // The same sentence, the id aside: nothing tells the caller MEMBER_B exists. + expect(foreign!.message.replace(MEMBER_B, '')).toBe(unknown!.message.replace(UNKNOWN, '')); + expect(await storedRules()).toEqual([]); + }); + + it('the answer is defineRule\'s own: same code and sentence at both doors', async () => { + const { ql, rules } = await boot('isolated'); + const viaData = await refusalOf(ql.insert('sys_sharing_rule', ruleRow('r_data', UNKNOWN), { context: ORG_A_ADMIN } as never)); + const viaService = await refusalOf(rules.defineRule({ + name: 'r_service', label: 'r_service', object: OBJECT, criteria: { stage: 'won' }, recipientType: 'user', recipientId: UNKNOWN, accessLevel: 'read', + } as never, ORG_A_ADMIN)); + expect(viaService!.message).toBe(`VALIDATION_FAILED: ${viaData!.message}`); + }); + + it('a non-user recipient is not judged by this guard', async () => { + const { ql, storedRules } = await boot('isolated'); + await ql.insert('sys_sharing_rule', { ...ruleRow('r_position', 'sales_rep'), recipient_type: 'position' }, { context: ORG_A_ADMIN } as never); + expect((await storedRules()).map((r) => r.recipient_id)).toEqual(['sales_rep']); + }); + + it('the system context is not judged', async () => { + const { ql, storedRules } = await boot('isolated'); + await ql.insert('sys_sharing_rule', ruleRow('r_system', UNKNOWN), { context: SYSTEM_A } as never); + expect((await storedRules()).map((r) => r.recipient_id)).toEqual([UNKNOWN]); + }); + + it('an update that moves a stored rule onto a user that does not resolve is refused, and the row keeps its recipient', async () => { + const { ql, storedRules, seedRule } = await boot('isolated'); + await seedRule('r_moved', MEMBER_A); + const refusal = await refusalOf(ql.update('sys_sharing_rule', { id: 'srule_r_moved', recipient_id: MEMBER_B }, { context: ORG_A_ADMIN } as never)); + expect(refusal?.code).toBe('VALIDATION_FAILED'); + expect(refusal?.message).toBe(RULE_REFUSAL(MEMBER_B)); + expect((await storedRules()).map((r) => r.recipient_id)).toEqual([MEMBER_A]); + }); + + it('an update that switches a stored rule from a position to an unresolved user is refused', async () => { + const { ql, seedRule } = await boot('isolated'); + await seedRule('r_retype', UNKNOWN, 'position'); + const refusal = await refusalOf(ql.update('sys_sharing_rule', { id: 'srule_r_retype', recipient_type: 'user' }, { context: ORG_A_ADMIN } as never)); + expect(refusal?.message).toBe(RULE_REFUSAL(UNKNOWN)); + }); + + it('control: an update that moves a stored rule onto a member is stored', async () => { + const { ql, storedRules, seedRule } = await boot('isolated'); + await seedRule('r_ok', UNKNOWN); + await ql.update('sys_sharing_rule', { id: 'srule_r_ok', recipient_id: MEMBER_A }, { context: ORG_A_ADMIN } as never); + expect((await storedRules()).map((r) => r.recipient_id)).toEqual([MEMBER_A]); + }); + + it('an update that keeps the stored recipient is not re-judged: a rule whose user has gone can still be switched off', async () => { + const { ql, storedRules, seedRule } = await boot('isolated'); + await seedRule('r_stale', UNKNOWN); + await ql.update('sys_sharing_rule', { id: 'srule_r_stale', active: false }, { context: ORG_A_ADMIN } as never); + await ql.update('sys_sharing_rule', { id: 'srule_r_stale', recipient_type: 'user', recipient_id: UNKNOWN }, { context: ORG_A_ADMIN } as never); + expect((await storedRules()).map((r) => r.recipient_id)).toEqual([UNKNOWN]); + }); +}); + +describe('[#38254] the /data door refuses a sharing rule whose user recipient does not resolve — single posture', () => { + it('an id that names no user is refused', async () => { + const { ql, storedRules } = await boot('single'); + const refusal = await refusalOf(ql.insert('sys_sharing_rule', ruleRow('r_unknown', UNKNOWN), { context: ORG_A_ADMIN } as never)); + expect(refusal?.code).toBe('VALIDATION_FAILED'); + expect(refusal?.message).toBe(RULE_REFUSAL(UNKNOWN)); + expect(await storedRules()).toEqual([]); + }); + + it('control: any existing user is stored, with no membership asked', async () => { + const { ql, storedRules } = await boot('single'); + await ql.insert('sys_sharing_rule', ruleRow('r_b', MEMBER_B), { context: ORG_A_ADMIN } as never); + expect((await storedRules()).map((r) => r.recipient_id)).toEqual([MEMBER_B]); + }); +}); From 3026979adb80bad5edf371aa9ffdf21c259f9507 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 3 Dec 2026 15:09:59 +0000 Subject: [PATCH 2/5] fix(plugin-sharing): clear the recipient guard's CI gates - changeset: answer the ADR-0087 question for the narrowing (not-required, no-migration-prescription). - system-context page: row 40 names bindRuleRecipientGuard, whose system-context early return is an elevation read; register regenerated. - runtime declared-name pin: author its rows to the dev admin's real user id, since the data door now refuses a user recipient that names no user. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01Utp6CZVKCmRkE5bj7Rwba3 --- .changeset/38254-sharing-rule-data-door-recipient.md | 2 ++ content/docs/permissions/system-context.mdx | 3 ++- .../sharing-rule-declared-name.data-door.pin.test.ts | 12 +++++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/.changeset/38254-sharing-rule-data-door-recipient.md b/.changeset/38254-sharing-rule-data-door-recipient.md index a0b2b3b9f48..a7651cb297a 100644 --- a/.changeset/38254-sharing-rule-data-door-recipient.md +++ b/.changeset/38254-sharing-rule-data-door-recipient.md @@ -7,6 +7,8 @@ fix(plugin-sharing): a sharing rule saved through the `/data` door (the Setup sh Clause-②: no (narrowing) Metadata-protocol: none + + Who sees it: an administrator saving a sharing rule in Setup, or any caller of `POST`/`PATCH /api/v1/data/sys_sharing_rule`. - **Before:** a rule whose `user` recipient named no user (or, on a multi-organization deployment, no member of the rule's organization) was stored. It granted no one access, and the form said it saved. diff --git a/content/docs/permissions/system-context.mdx b/content/docs/permissions/system-context.mdx index 5b4dcac1a3b..9c417afe767 100644 --- a/content/docs/permissions/system-context.mdx +++ b/content/docs/permissions/system-context.mdx @@ -180,7 +180,7 @@ One of the largest consumers of the flag; | 37 | `sys_record_share` reads are **not** self-scoped | Get: tenant-wide share listing without `manage_sharing` | `buildSharingMiddleware` | | 38 | Share-link policy `enabled` check bypassed; system callers re-enter under a system context | Get: link **creation** while the policy is off — resolution is **not** bypassed since #14033 (`publicSharing.enabled` is a standing policy held at every redemption): a link minted this way does not resolve until the block is enabled | `createLink`, `revokeLink`, `listLinks` | | 39 | Sharing-rule provenance stamp skipped | Lose: the row is not marked as an admin customization — seeder / `defineRule` / boot reconcilers are "the package door" | `bindRuleProvenanceStamp` | -| 40 | Sharing-rule service write + delete paths return early | Lose: the manage-rules gate on the service surface and the declared-name guard on `sys_sharing_rule` writes (a new row or a rename taking a declared rule's name, 409) — `defineRule` makes that check itself before its own system write, and the other system writers are the platform's machinery (the ownership ceremony, migrations); and the object-enforcement guard on the same writes (a rule whose object is not under record-sharing enforcement, 422 `SHARING_NOT_ENABLED`), which `defineRule` likewise makes for its non-system callers | `assertCanManageRules`, `bindDeclaredRuleNameGuard`, `bindRuleObjectEnforcementGuard`, `defineRule` | +| 40 | Sharing-rule service write + delete paths return early | Lose: the manage-rules gate on the service surface and the declared-name guard on `sys_sharing_rule` writes (a new row or a rename taking a declared rule's name, 409) — `defineRule` makes that check itself before its own system write, and the other system writers are the platform's machinery (the ownership ceremony, migrations); and the object-enforcement guard on the same writes (a rule whose object is not under record-sharing enforcement, 422 `SHARING_NOT_ENABLED`), which `defineRule` likewise makes for its non-system callers; and the recipient guard on the same writes (a `user` recipient that names no user the rule could share with, 400 `VALIDATION_FAILED`), which `defineRule` likewise makes for its non-system callers | `assertCanManageRules`, `bindDeclaredRuleNameGuard`, `bindRuleObjectEnforcementGuard`, `bindRuleRecipientGuard`, `defineRule` | ### 4. Approvals, attachments, comments, knowledge @@ -643,6 +643,7 @@ and CI refuses one that differs from what the tree produces. | `packages/plugins/plugin-security/src/system-write-guard.ts#isUserContextWrite` | 1 | 25 | | `packages/plugins/plugin-sharing/src/rule-hooks.ts#bindRuleHooks` | 1 | 31 | | `packages/plugins/plugin-sharing/src/rule-hooks.ts#bindRuleObjectEnforcementGuard` | 1 | 40 | +| `packages/plugins/plugin-sharing/src/rule-hooks.ts#bindRuleRecipientGuard` | 1 | 40 | | `packages/plugins/plugin-sharing/src/share-link-service.ts#createLink` | 3 | 38 | | `packages/plugins/plugin-sharing/src/share-link-service.ts#listLinks` | 1 | 38 | | `packages/plugins/plugin-sharing/src/share-link-service.ts#revokeLink` | 1 | 38 | diff --git a/packages/runtime/src/sharing-rule-declared-name.data-door.pin.test.ts b/packages/runtime/src/sharing-rule-declared-name.data-door.pin.test.ts index 7ff6e8e6f3c..949d6b947f3 100644 --- a/packages/runtime/src/sharing-rule-declared-name.data-door.pin.test.ts +++ b/packages/runtime/src/sharing-rule-declared-name.data-door.pin.test.ts @@ -96,6 +96,13 @@ const PIN_APP: any = { sharingRules: [declaredRule(DECLARED_CREATE, 'red'), declaredRule(DECLARED_RENAME, 'amber')], }; +/** + * The authored rows' recipient: the signed-in admin, a real user. Since #38254 + * the data door refuses a `user` recipient that names no user, so a made-up id + * would be refused before the declared-name guard this file pins is reached. + */ +let ownRecipient = ''; + /** A complete authored row, as Setup's New form posts it. */ const authoredRow = (name: string) => ({ name, @@ -103,7 +110,7 @@ const authoredRow = (name: string) => ({ object_name: 'pin_project', criteria_json: JSON.stringify({ status: 'red' }), recipient_type: 'user', - recipient_id: 'usr_own_recipient', + recipient_id: ownRecipient, access_level: 'read', }); @@ -166,6 +173,9 @@ beforeAll(async () => { app = httpServer.getRawApp(); engine = await kernel.getServiceAsync('objectql'); adminToken = await signIn(ADMIN); + const [admin] = await engine.find('sys_user', { where: { email: ADMIN.email }, context: { isSystem: true } }); + ownRecipient = String(admin?.id ?? ''); + if (!ownRecipient) throw new Error('pin precondition: the dev admin has no sys_user row'); }, BOOT_TIMEOUT); afterAll(async () => { From 26afba6f4b429fe554201a84f6c9a1e68bed33c1 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 3 Dec 2026 15:18:00 +0000 Subject: [PATCH 3/5] test(runtime): pay the declared-name pin's metadata load at collection Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01Utp6CZVKCmRkE5bj7Rwba3 --- .../src/sharing-rule-declared-name.data-door.pin.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/runtime/src/sharing-rule-declared-name.data-door.pin.test.ts b/packages/runtime/src/sharing-rule-declared-name.data-door.pin.test.ts index 949d6b947f3..f89af0b4b60 100644 --- a/packages/runtime/src/sharing-rule-declared-name.data-door.pin.test.ts +++ b/packages/runtime/src/sharing-rule-declared-name.data-door.pin.test.ts @@ -56,6 +56,8 @@ import { HttpStatusErrorCodeMap } from '@objectstack/spec/api'; import { AppPlugin } from './app-plugin.js'; import { DefaultDatasourcePlugin } from './default-datasource-plugin.js'; import { createDispatcherPlugin } from './dispatcher-plugin.js'; +// Paid during collection, not inside a clocked case: `app-plugin.ts` loads it lazily. +import '@objectstack/metadata'; const BOOT_TIMEOUT = 180_000; const ORIGIN = 'http://localhost:3000'; From 1b8a19562b6ef62c82ec0977fb4c40f565a5c2f6 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 3 Dec 2026 16:04:46 +0000 Subject: [PATCH 4/5] fix(plugin-sharing): judge a re-pointed rule in its new organization; pin the organization arms Review round 1 on the recipient guard: - the organization a write is judged in is the one it leaves the rule in: a supplied organization_id first, then the stored row's, then the caller's active organization; each arm has a pin that turns red without it; - the runtime door pin asserts the 400 VALIDATION_FAILED envelope; - the changeset carries the BREAKING line and the caller's fix; - the sharing-service and sharing-rules pages name the /data door refusal. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01Utp6CZVKCmRkE5bj7Rwba3 --- .../38254-sharing-rule-data-door-recipient.md | 7 ++- .../runtime-services/sharing-service.mdx | 2 +- content/docs/permissions/sharing-rules.mdx | 10 ++++ .../plugins/plugin-sharing/src/rule-hooks.ts | 12 +++-- .../src/sharing-rule-recipient-guard.test.ts | 50 ++++++++++++++++++- ...g-rule-declared-name.data-door.pin.test.ts | 14 ++++++ 6 files changed, 86 insertions(+), 9 deletions(-) diff --git a/.changeset/38254-sharing-rule-data-door-recipient.md b/.changeset/38254-sharing-rule-data-door-recipient.md index a7651cb297a..2e6e8bbc06b 100644 --- a/.changeset/38254-sharing-rule-data-door-recipient.md +++ b/.changeset/38254-sharing-rule-data-door-recipient.md @@ -9,8 +9,11 @@ Metadata-protocol: none -Who sees it: an administrator saving a sharing rule in Setup, or any caller of `POST`/`PATCH /api/v1/data/sys_sharing_rule`. +Who sees it: an administrator saving a sharing rule in Setup, and any non-system write of `sys_sharing_rule` through the engine: `POST`/`PATCH /api/v1/data/sys_sharing_rule`, an import, a batch insert, a predicate update, or a user-context automation. - **Before:** a rule whose `user` recipient named no user (or, on a multi-organization deployment, no member of the rule's organization) was stored. It granted no one access, and the form said it saved. -- **Now:** the insert, and an update that moves a rule onto such a recipient, answer `400 VALIDATION_FAILED` under `recipient_id`, with the sentence `POST /api/v1/sharing/rules` already answered for the same recipient. Nothing is stored. An id that does not exist and a user of another organization get the same answer. +- **Now:** the insert, and an update that moves a rule onto such a recipient, answer `400 VALIDATION_FAILED` with one `fields` entry under `recipient_id`, with the sentence `POST /api/v1/sharing/rules` already answered for the same recipient. Nothing is stored. An id that does not exist and a user of another organization get the same answer. The write is judged in the organization it leaves the rule in: an `organization_id` the write supplies, else the stored rule's, else the caller's active organization (the Setup form posts none). - An update that keeps the stored recipient is not judged, so a rule whose user has since left can still be switched off or edited. System writes are not judged. + +**BREAKING** for a caller that writes a `sys_sharing_rule` row outside the system context naming a `user` recipient id that does not exist or, on a multi-organization deployment, a user outside the rule's organization: it answered 2xx and stored a rule that granted nothing; it answers 400 `VALIDATION_FAILED` and stores nothing. +The fix for a caller: send the id of an existing user, on a multi-organization deployment a member of the rule's organization. diff --git a/content/docs/kernel/runtime-services/sharing-service.mdx b/content/docs/kernel/runtime-services/sharing-service.mdx index 07fc71498cb..e34faf9760c 100644 --- a/content/docs/kernel/runtime-services/sharing-service.mdx +++ b/content/docs/kernel/runtime-services/sharing-service.mdx @@ -105,7 +105,7 @@ mask AND-ed with object CRUD, not a fourth `access_level`. ## Typical Errors - `FORBIDDEN` (403) — a write denied by the `canEdit` gate. Thrown by the sharing engine middleware; `canEdit` itself returns `false` rather than throwing. -- `VALIDATION_FAILED` (400) — `grant`/`revoke` called without a required field (`object`, `recordId`, `recipientId`, or `shareId`), or `grant` with a non-`user` `recipientType` (only `user` rows are enforced by the gates). The refusal names the sharing-rule recipient that delivers each declared value: `group` → `team` (a rule spells it `team` since the ADR-0090 D3 rename), `position` → `position`, `unit_and_subordinates` → `unit_and_subordinates`. A non-system `grant` is also refused when its `user` recipient names no user the gates could ever match: on a deployment walled by organization (`isolated` / `group`) with the record's organization known, the recipient must be a member of that organization, and a user of another organization gets the same answer as an id that does not exist, so the door tells no caller who exists in another tenant; on `single` it must be an existing `sys_user`. Under a wall where the grant's organization does not resolve, the recipient is not judged: the engine refuses that insert for every recipient alike (`ERR_SYSTEM_WRITE_ORGANIZATION_REQUIRED`, 500). `defineRule` refuses a non-system `user` recipient the same way, in the rule's organization, when the rule is new or its recipient changes. The rule's organization is the caller's or, for a platform operator with no active organization updating a stored rule, the stored rule's own (the engine's organization rule refuses only inserts, so that update is judged here); a new rule written with no organization under a wall is not judged, and the engine refuses its insert. +- `VALIDATION_FAILED` (400) — `grant`/`revoke` called without a required field (`object`, `recordId`, `recipientId`, or `shareId`), or `grant` with a non-`user` `recipientType` (only `user` rows are enforced by the gates). The refusal names the sharing-rule recipient that delivers each declared value: `group` → `team` (a rule spells it `team` since the ADR-0090 D3 rename), `position` → `position`, `unit_and_subordinates` → `unit_and_subordinates`. A non-system `grant` is also refused when its `user` recipient names no user the gates could ever match: on a deployment walled by organization (`isolated` / `group`) with the record's organization known, the recipient must be a member of that organization, and a user of another organization gets the same answer as an id that does not exist, so the door tells no caller who exists in another tenant; on `single` it must be an existing `sys_user`. Under a wall where the grant's organization does not resolve, the recipient is not judged: the engine refuses that insert for every recipient alike (`ERR_SYSTEM_WRITE_ORGANIZATION_REQUIRED`, 500). `defineRule` refuses a non-system `user` recipient the same way, in the rule's organization, when the rule is new or its recipient changes. The rule's organization is the caller's or, for a platform operator with no active organization updating a stored rule, the stored rule's own (the engine's organization rule refuses only inserts, so that update is judged here); a new rule written with no organization under a wall is not judged, and the engine refuses its insert. A `sys_sharing_rule` write through the `/data` door (Setup's form) gets the same refusal and sentence, judged in the organization the write leaves the rule in: an `organization_id` the write supplies, else the stored rule's, else the caller's active organization; a write that keeps the stored recipient is not judged. - `PERMISSION_DENIED` (403) — the caller does not hold `canManageShares` on the record (ADR-0111 D1). On the sharing-**rule** surface (`ISharingRuleService`, declared in the same canonical source — `listRules` / `getRule` / `defineRule` / `deleteRule` / `evaluateRule`) the same code carries a second condition: the caller holds `manage_sharing` but their session resolves **no active organization**, and an org-scoped capability with no organization has no tenant whose rules it authorizes. System contexts and platform operators (`manage_platform_settings`, or the `platform_admin` position) are unaffected — see [Rule administration](/docs/permissions/sharing-rules). - `NOT_FOUND` (404) — the record is missing **or not visible to the caller** (indistinguishable by design), or a `revoke` share id does not exist / does not belong to the `scope` record. - `CONFLICT` (409) — `revoke` on a rule-materialised share (`source != 'manual'`); the next rule reconciliation would silently re-grant it. Deactivate or edit the sharing rule instead. diff --git a/content/docs/permissions/sharing-rules.mdx b/content/docs/permissions/sharing-rules.mdx index 3b7708dab84..e2af6f27b51 100644 --- a/content/docs/permissions/sharing-rules.mdx +++ b/content/docs/permissions/sharing-rules.mdx @@ -198,6 +198,16 @@ names the object and how to turn record sharing on for it. This holds for Setup' a rule onto such an object. A rule saved before this check keeps its row and can still be switched off. +A rule whose recipient is a `user` is refused when that id names no user the +rule could share with: on a multi-organization deployment, a user who is not a +member of the rule's organization; otherwise, an id no `sys_user` carries. It is +refused with `VALIDATION_FAILED` (400) under `recipient_id`, and a user of +another organization gets the same answer as an id that does not exist, so the +form tells no one who exists in another tenant. This holds for Setup's form and +the `/data` door as well as `POST /api/v1/sharing/rules`, and for an edit that +moves a rule onto such a user. A rule saved before this check keeps its row and +can still be switched off. + `externalSharingModel`, the external dial ADR-0090 D11 declared, was removed in `@objectstack/spec` 18 (ADR-0049 enforce-or-remove): while it existed no request resolved an external principal, so the value changed no answer. The schema diff --git a/packages/plugins/plugin-sharing/src/rule-hooks.ts b/packages/plugins/plugin-sharing/src/rule-hooks.ts index 50e8bd83995..ebc4731c5e0 100644 --- a/packages/plugins/plugin-sharing/src/rule-hooks.ts +++ b/packages/plugins/plugin-sharing/src/rule-hooks.ts @@ -445,9 +445,11 @@ export function bindRuleObjectEnforcementGuard( * doors cannot disagree, and the refusal is `VALIDATION_FAILED` (400) under * `recipient_id`. * - * Judged in the rule's organization: on an update the stored row's, on an - * insert the row's own `organization_id` or else the caller's active - * organization (the one the engine stamps on the row). An id that names no + * Judged in the organization the write leaves the rule in: the + * `organization_id` the write supplies (on an update, a re-point the tenant + * wall admitted), else the stored row's, else the caller's active + * organization (the one the engine stamps on a new row; the Setup form + * posts none). An id that names no * user and a user of another organization are answered alike, so the door * is no oracle for who exists in another tenant. With no organization under * a wall the verdict stands aside, as it does for `defineRule`: the engine @@ -484,8 +486,8 @@ export function bindRuleRecipientGuard( // A missing id is the record validator's to answer (`required`), not this guard's. if (!recipientId) return; if (previous && previous.recipient_type === 'user' && previous.recipient_id === recipientId) return; - const organizationId = present(previous?.organization_id) - ?? present(data.organization_id) + const organizationId = present(data.organization_id) + ?? present(previous?.organization_id) ?? present(ctx?.session?.organizationId); const refusal = await sharing.userRecipientRefusal(recipientId, organizationId, 'rule'); if (refusal) { diff --git a/packages/plugins/plugin-sharing/src/sharing-rule-recipient-guard.test.ts b/packages/plugins/plugin-sharing/src/sharing-rule-recipient-guard.test.ts index c5fedce04de..ce951b1722d 100644 --- a/packages/plugins/plugin-sharing/src/sharing-rule-recipient-guard.test.ts +++ b/packages/plugins/plugin-sharing/src/sharing-rule-recipient-guard.test.ts @@ -70,6 +70,10 @@ const ORG_A_ADMIN = { permissions: [], systemPermissions: ['manage_sharing'], } as unknown as ExecutionContext; +/** A caller with no active organization (a platform operator's shape): the session supplies none. */ +const NO_ORG_ADMIN = { + userId: 'u_admin', positions: [], permissions: [], systemPermissions: ['manage_sharing'], +} as unknown as ExecutionContext; const SYSTEM_A = { isSystem: true, tenantId: ORG_A, positions: [], permissions: [] } as unknown as ExecutionContext; const RULE_REFUSAL = (id: string) => @@ -84,7 +88,7 @@ afterEach(async () => { } }); -async function boot(posture: 'single' | 'isolated') { +async function boot(posture: 'single' | 'isolated' | 'group') { const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); open.push(driver); const ql = new ObjectQL(); @@ -239,3 +243,47 @@ describe('[#38254] the /data door refuses a sharing rule whose user recipient do expect((await storedRules()).map((r) => r.recipient_id)).toEqual([MEMBER_B]); }); }); + +describe('[#38254] the organization a write is judged in', () => { + it('an insert that carries no organization_id (the Setup form\'s shape) is judged in the caller\'s active organization', async () => { + const { ql, storedRules } = await boot('isolated'); + const { organization_id: _dropped, ...formRow } = ruleRow('r_form', UNKNOWN); + const unknown = await refusalOf(ql.insert('sys_sharing_rule', formRow, { context: ORG_A_ADMIN } as never)); + const { organization_id: _dropped2, ...foreignRow } = ruleRow('r_form_foreign', MEMBER_B); + const foreign = await refusalOf(ql.insert('sys_sharing_rule', foreignRow, { context: ORG_A_ADMIN } as never)); + expect(unknown?.message).toBe(RULE_REFUSAL(UNKNOWN)); + expect(foreign?.message).toBe(RULE_REFUSAL(MEMBER_B)); + expect(await storedRules()).toEqual([]); + // Control: a member of the caller's organization, same shape, is stored. + const { organization_id: _dropped3, ...memberRow } = ruleRow('r_form_member', MEMBER_A); + await ql.insert('sys_sharing_rule', memberRow, { context: ORG_A_ADMIN } as never); + expect((await storedRules()).map((r) => r.recipient_id)).toEqual([MEMBER_A]); + }); + + it('an update by a caller with no active organization is judged in the stored rule\'s organization', async () => { + const { ql, storedRules, seedRule } = await boot('isolated'); + await seedRule('r_cross', UNKNOWN); + // The session names no organization, so only the stored row's (org_a) can judge: MEMBER_B is not in it. + const refusal = await refusalOf(ql.update('sys_sharing_rule', { id: 'srule_r_cross', recipient_id: MEMBER_B }, { context: NO_ORG_ADMIN } as never)); + expect(refusal?.message).toBe(RULE_REFUSAL(MEMBER_B)); + // Control: a member of the rule's organization is stored. + await ql.update('sys_sharing_rule', { id: 'srule_r_cross', recipient_id: MEMBER_A }, { context: NO_ORG_ADMIN } as never); + expect((await storedRules()).map((r) => r.recipient_id)).toEqual([MEMBER_A]); + }); + + it('an update that re-points the rule to another organization is judged in the organization it moves to', async () => { + const { ql, storedRules, seedRule } = await boot('group'); + await seedRule('r_repoint', UNKNOWN); + // MEMBER_A is a member of org_a, the organization the rule is LEAVING, not of org_b: refused. + const refusal = await refusalOf(ql.update( + 'sys_sharing_rule', { id: 'srule_r_repoint', organization_id: ORG_B, recipient_id: MEMBER_A }, { context: ORG_A_ADMIN } as never, + )); + expect(refusal?.message).toBe(RULE_REFUSAL(MEMBER_A)); + expect((await storedRules()).map((r) => r.recipient_id)).toEqual([UNKNOWN]); + // Control: a member of org_b, the organization the rule moves to, is stored. + await ql.update( + 'sys_sharing_rule', { id: 'srule_r_repoint', organization_id: ORG_B, recipient_id: MEMBER_B }, { context: ORG_A_ADMIN } as never, + ); + expect((await storedRules()).map((r) => r.recipient_id)).toEqual([MEMBER_B]); + }); +}); diff --git a/packages/runtime/src/sharing-rule-declared-name.data-door.pin.test.ts b/packages/runtime/src/sharing-rule-declared-name.data-door.pin.test.ts index f89af0b4b60..df9fd150c3b 100644 --- a/packages/runtime/src/sharing-rule-declared-name.data-door.pin.test.ts +++ b/packages/runtime/src/sharing-rule-declared-name.data-door.pin.test.ts @@ -231,3 +231,17 @@ describe('[ADR-0131 D6 regime C] POST / PATCH /data/sys_sharing_rule onto a decl expect((await rowsNamed(OWN)).map((r) => r.id)).toEqual([own.id]); }); }); + +describe('[#38254] POST /data/sys_sharing_rule with a user recipient that names no user', () => { + it('answers 400 VALIDATION_FAILED under recipient_id at the HTTP door, and no row lands', async () => { + const name = 'pin_unknown_recipient'; + const res = await as(adminToken, 'POST', RULES, { ...authoredRow(name), recipient_id: 'usr_nobody_38254' }); + const payload = await readJson(res); + expect(res.status, JSON.stringify(payload)).toBe(400); + expect(payload?.code, JSON.stringify(payload)).toBe('VALIDATION_FAILED'); + // The `/data` door's validation envelope carries `fields` at the top level. + const fields = payload?.fields; + expect(fields?.[0]?.field, JSON.stringify(payload)).toBe('recipient_id'); + expect(await rowsNamed(name)).toEqual([]); + }); +}); From 7a2fcc660a8c824d8d87cc950735685275f45b69 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Dec 2026 00:02:28 +0000 Subject: [PATCH 5/5] fix(plugin-sharing): a recipient key carrying undefined is not a supplied value; scope the fields entry to the /data door in the docs Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01Utp6CZVKCmRkE5bj7Rwba3 --- content/docs/permissions/sharing-rules.mdx | 11 ++++++----- packages/plugins/plugin-sharing/src/rule-hooks.ts | 4 +++- .../src/sharing-rule-recipient-guard.test.ts | 10 ++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/content/docs/permissions/sharing-rules.mdx b/content/docs/permissions/sharing-rules.mdx index e2af6f27b51..763c233f020 100644 --- a/content/docs/permissions/sharing-rules.mdx +++ b/content/docs/permissions/sharing-rules.mdx @@ -201,11 +201,12 @@ can still be switched off. A rule whose recipient is a `user` is refused when that id names no user the rule could share with: on a multi-organization deployment, a user who is not a member of the rule's organization; otherwise, an id no `sys_user` carries. It is -refused with `VALIDATION_FAILED` (400) under `recipient_id`, and a user of -another organization gets the same answer as an id that does not exist, so the -form tells no one who exists in another tenant. This holds for Setup's form and -the `/data` door as well as `POST /api/v1/sharing/rules`, and for an edit that -moves a rule onto such a user. A rule saved before this check keeps its row and +refused with `VALIDATION_FAILED` (400), and a user of another organization gets +the same answer as an id that does not exist, so the form tells no one who +exists in another tenant. Setup's form and the `/data` door answer with a +`fields` entry under `recipient_id`; `POST /api/v1/sharing/rules` answers the +same code and sentence, without `fields`. This holds for an edit that moves a +rule onto such a user as well. A rule saved before this check keeps its row and can still be switched off. `externalSharingModel`, the external dial ADR-0090 D11 declared, was removed diff --git a/packages/plugins/plugin-sharing/src/rule-hooks.ts b/packages/plugins/plugin-sharing/src/rule-hooks.ts index ebc4731c5e0..fcff57659d4 100644 --- a/packages/plugins/plugin-sharing/src/rule-hooks.ts +++ b/packages/plugins/plugin-sharing/src/rule-hooks.ts @@ -478,7 +478,9 @@ export function bindRuleRecipientGuard( const previous = !insert && ctx?.previous && typeof ctx.previous === 'object' ? ctx.previous as Record : undefined; - const has = (key: string) => Object.prototype.hasOwnProperty.call(data, key); + // A key present with the value `undefined` leaves the column alone (the tenant wall reads it the same way), so + // it is not a supplied value: the stored one stands and is the one judged. + const has = (key: string) => Object.prototype.hasOwnProperty.call(data, key) && data[key] !== undefined; if (!insert && !has('recipient_type') && !has('recipient_id')) return; const recipientType = has('recipient_type') ? data.recipient_type : previous?.recipient_type; if (recipientType !== 'user') return; diff --git a/packages/plugins/plugin-sharing/src/sharing-rule-recipient-guard.test.ts b/packages/plugins/plugin-sharing/src/sharing-rule-recipient-guard.test.ts index ce951b1722d..540227e2042 100644 --- a/packages/plugins/plugin-sharing/src/sharing-rule-recipient-guard.test.ts +++ b/packages/plugins/plugin-sharing/src/sharing-rule-recipient-guard.test.ts @@ -212,6 +212,16 @@ describe('[#38254] the /data door refuses a sharing rule whose user recipient do expect(refusal?.message).toBe(RULE_REFUSAL(UNKNOWN)); }); + it('a recipient_type key carrying undefined leaves the stored type in force: the new id is judged', async () => { + const { ql, storedRules, seedRule } = await boot('isolated'); + await seedRule('r_undef', MEMBER_A); + const refusal = await refusalOf(ql.update( + 'sys_sharing_rule', { id: 'srule_r_undef', recipient_type: undefined, recipient_id: UNKNOWN }, { context: ORG_A_ADMIN } as never, + )); + expect(refusal?.message).toBe(RULE_REFUSAL(UNKNOWN)); + expect((await storedRules()).map((r) => r.recipient_id)).toEqual([MEMBER_A]); + }); + it('control: an update that moves a stored rule onto a member is stored', async () => { const { ql, storedRules, seedRule } = await boot('isolated'); await seedRule('r_ok', UNKNOWN);