diff --git a/.changeset/33584-member-default-comment-reactions.md b/.changeset/33584-member-default-comment-reactions.md new file mode 100644 index 00000000000..0669eabb99e --- /dev/null +++ b/.changeset/33584-member-default-comment-reactions.md @@ -0,0 +1,14 @@ +--- +'@objectstack/plugin-security': patch +--- + +fix(plugin-security): `member_default` grants read and create on `sys_comment_reaction`, so a stock member can react to the comments they can read + +Clause-②: yes (widening) +Metadata-protocol: none + +The record page offers every member a reaction control on each comment, but no shipped permission set named `sys_comment_reaction`, so a member with only the platform baseline was refused (`403`) both adding a reaction (`POST /api/v1/data/sys_comment_reaction`) and listing them. `member_default` now carries `sys_comment_reaction` with read and create only. No row policy is added, and edit and delete stay off. + +Which rows a member reaches is unchanged and is still `@objectstack/plugin-audit`'s comment gate: reactions are listed and read only on comments the caller can read, a reaction on any other comment is refused (`403 RECORD_NOT_ACCESSIBLE`), and `user_id` is taken from the session. A member removes only their own reaction, through `DELETE /api/v1/comments/reactions/:id`. Another member's reaction is refused there (`403`) and on the data door, which needs a delete bit the baseline does not carry. + +`external_default` is unchanged: a portal user still needs an explicit grant to react. diff --git a/packages/plugins/plugin-security/src/comment-reaction-own-record-floor.test.ts b/packages/plugins/plugin-security/src/comment-reaction-own-record-floor.test.ts index 9cb68c4866c..dd6af79931a 100644 --- a/packages/plugins/plugin-security/src/comment-reaction-own-record-floor.test.ts +++ b/packages/plugins/plugin-security/src/comment-reaction-own-record-floor.test.ts @@ -32,13 +32,16 @@ * * ## Who grants the bits * - * The object-level create / read / delete bits come from an ordinary - * position-distributed set an application ships (as an app grants the - * `sys_comment` delete bit; the baseline grants comments' read, create and - * edit, never delete), never from the `everyone`-bound baseline, which - * ADR-0090 D5 keeps free of delete bits. Which shipped set — if any — carries - * them is an open decision on #22566; the floor pinned here holds whatever set - * hands the delete bit out. + * Read and create on reactions are the baseline's: `member_default` carries + * them (maintainer decision on #33584, option A, "members may react by + * default"), as it carries comments' read, create and edit. The delete bit is + * never the baseline's: ADR-0090 D5 keeps the `everyone`-bound baseline free + * of delete bits, so a member's unreact is plugin-audit's own-row door + * (`comment-reaction-unreact-door.integration.test.ts`). On the generic data + * door the delete bit comes from an ordinary position-distributed set an + * application ships (as an app grants the `sys_comment` delete bit) — the + * `qa_comment_reactor` set below — and the floor pinned here holds whatever + * set hands that bit out. */ import { describe, it, expect, afterEach } from 'vitest'; diff --git a/packages/plugins/plugin-security/src/objects/default-permission-sets.test.ts b/packages/plugins/plugin-security/src/objects/default-permission-sets.test.ts index 2e968f73b09..33221adff28 100644 --- a/packages/plugins/plugin-security/src/objects/default-permission-sets.test.ts +++ b/packages/plugins/plugin-security/src/objects/default-permission-sets.test.ts @@ -371,6 +371,35 @@ describe('sys_comment_reaction delete is ownership-shaped (#22566)', () => { }); }); +/** + * [#33584] Maintainer decision, option A ("members may react by default"): + * `member_default` names `sys_comment_reaction` read + create, and nothing + * more — no edit (the object serves no update), no delete (ADR-0090 D5; unreact + * is plugin-audit's own-row door). Which rows stays plugin-audit's comment + * gate. The decision covers `member_default` only, so `external_default` is + * the CONTROL: a portal user's answer is unchanged. + */ +describe('member_default lets a member react to comments; external_default does not (#33584)', () => { + it('member_default carries exactly read + create on sys_comment_reaction', () => { + // The parsed entry, so the schema's materialized `false`s are pinned too: + // no transfer and no view-all / modify-all bypass rides on the grant. + expect(setByName('member_default').objects.sys_comment_reaction).toEqual({ + allowRead: true, + allowCreate: true, + allowEdit: false, + allowDelete: false, + allowTransfer: false, + viewAllRecords: false, + modifyAllRecords: false, + }); + }); + + it('CONTROL: external_default names no sys_comment_reaction grant', () => { + expect(setByName('external_default')).toBeTruthy(); + expect(setByName('external_default').objects).not.toHaveProperty('sys_comment_reaction'); + }); +}); + /** * [#11965 / #11663 Choice 6A] platform-admin re-anchor, L1 behaviour-neutrality * pin. diff --git a/packages/plugins/plugin-security/src/objects/default-permission-sets.ts b/packages/plugins/plugin-security/src/objects/default-permission-sets.ts index 108f5bd254e..de71003fa3a 100644 --- a/packages/plugins/plugin-security/src/objects/default-permission-sets.ts +++ b/packages/plugins/plugin-security/src/objects/default-permission-sets.ts @@ -853,6 +853,23 @@ const baseDefaultPermissionSets: PermissionSet[] = [ // the same two gates. sys_comment: { allowRead: true, allowCreate: true, allowEdit: true, allowDelete: false }, sys_activity: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false }, + // [#33584 — maintainer decision 2026-11-15, option A: members may react + // by default] A member reacts to the comments they can read. Before this + // line no shipped set named `sys_comment_reaction`, so a stock member was + // refused (`403`) the reaction control the record page offers them. + // + // Same shape as `sys_comment` above: this opens the OBJECT, and ⛔ no row + // policy is added (the reaction tripwire in default-permission-sets.test.ts + // forbids one). WHICH ROWS is plugin-audit's comment gate + // (`comment-access-hooks.ts`): a reaction is listed and read only on a + // comment the caller can read, and an insert on any other comment is + // refused, with `user_id` stamped from the session. + // + // ⛔ No `allowEdit` (the object serves no update) and no `allowDelete` + // (the anchor reason above). Unreact is plugin-audit's own-row door + // (`DELETE /api/v1/comments/reactions/:id`), which removes only a row + // whose `created_by` is the caller. `external_default` gets no such line. + sys_comment_reaction: { allowRead: true, allowCreate: true, allowEdit: false, allowDelete: false }, // [#32442 — maintainer ruling 6849636735, 1B] The approval requests a // member takes part in, on the data door and the MCP read tools, as the // approvals inbox already serves them. Before this line a member held no diff --git a/packages/qa/dogfood/test/comment-reactions-and-edit-stamps.dogfood.test.ts b/packages/qa/dogfood/test/comment-reactions-and-edit-stamps.dogfood.test.ts index 604d79ba67a..ece0d0596d1 100644 --- a/packages/qa/dogfood/test/comment-reactions-and-edit-stamps.dogfood.test.ts +++ b/packages/qa/dogfood/test/comment-reactions-and-edit-stamps.dogfood.test.ts @@ -34,7 +34,7 @@ import { AuditPlugin } from '@objectstack/plugin-audit'; import { SecurityPlugin, securityDefaultPermissionSets } from '@objectstack/plugin-security'; import { PermissionSetSchema } from '@objectstack/spec/security'; import { commentsFixtureStack, cmtFixtureBaselineSet, commentManagerSet } from './fixtures/comments-fixture.js'; -import { assertArmed, principalArmed } from './armed.js'; +import { armedWhen, assertArmed, principalArmed, resolveAuthzFor } from './armed.js'; import { catalogPermissionSet } from './catalog-permission-set.js'; const SYS = { isSystem: true } as const; @@ -190,3 +190,203 @@ describe('records-forms.comment-reactions-and-edit-stamps — over the REST data expect(new Date(after.edited_at).getUTCFullYear()).toBeGreaterThan(2000); }); }); + +// ── A stock member reacts by default (#33584) ─────────────────────────────── +// +// Maintainer decision on #33584, option A ("members may react by default"): +// `member_default` names `sys_comment_reaction` read + create. Pinned here at +// the REST doors a stock member reaches, on the same fixture app, with NO +// reaction grant from the app: the app's own baseline (`rxs_app_member`, its +// `isDefault` posture) reads its records and nothing else, so every reaction +// bit the members hold is the platform baseline's. The suite above holds an app +// grant with the delete bit (`rx_reactor`) and measures the floor; this one +// holds none and measures the default. +// +// - a member reacts to a comment they can read (201) and lists its reactions; +// - they unreact their own (the own-row door, `DELETE +// /api/v1/comments/reactions/:id`: the baseline carries no delete bit); +// - they cannot remove another member's reaction, on that door (403) or the +// data door (no delete bit), and the row stays; +// - CONTROL: an unfiltered list carries the readable comment's reactions and +// not the unreadable comment's; +// - CONTROL: on a comment they cannot read, a reaction is neither listed, +// read by id, removed, nor inserted (403 RECORD_NOT_ACCESSIBLE). + +/** The app's every-member baseline: its own records, read-only — no comment or reaction bit. */ +const appMemberSet = PermissionSetSchema.parse({ + name: 'rxs_app_member', + label: 'Reactions fixture — app member baseline', + objects: { + cmt_open: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false }, + cmt_private: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false }, + }, +}); + +describe('a stock member reacts to the comments they can read — member_default only (#33584)', () => { + let stack: VerifyStack; + let ql: any; + let adminTok = ''; + let aliceTok = ''; + let bobTok = ''; + let aliceId = ''; + let bobId = ''; + let commentId = ''; + let privateCommentId = ''; + let privateReactionId = ''; + + const call = async (token: string, method: string, path: string, body?: unknown): Promise => { + const res = await stack.apiAs(token, method, path, body); + return { status: res.status, json: await res.json().catch(() => ({})) }; + }; + const uid = async (email: string) => String((await ql.findOne('sys_user', { where: { email }, context: SYS }))?.id); + const reactionRows = (where: Record) => ql.find(REACTION, { where, context: SYS }); + /** A list answer's rows — the shape asserted, so an empty CONTROL never reads an unrecognised answer as no rows. */ + const listed = (json: any): any[] => { + expect(Array.isArray(json?.records), `a list answer carries records[]: ${JSON.stringify(json)}`).toBe(true); + return json.records; + }; + const react = (token: string, comment: string, emoji: string) => call(token, 'POST', `/data/${REACTION}`, { comment_id: comment, emoji }); + const reactionId = async (where: Record) => String((await reactionRows(where))[0]?.id); + + beforeAll(async () => { + stack = await bootStack(commentsFixtureStack as never, { + security: new SecurityPlugin({ + defaultPermissionSets: [...securityDefaultPermissionSets, appMemberSet], + // The app's `isDefault` posture: its baseline COMPOSED with the + // platform's `member_default` (ADR-0090 D5), as a showcase member holds. + fallbackPermissionSet: appMemberSet.name, + }), + extraPlugins: [new AuditPlugin()], + orgContext: true, + }); + adminTok = await stack.signIn(); + aliceTok = await stack.signUp('rxs-alice@verify.test'); + bobTok = await stack.signUp('rxs-bob@verify.test'); + ql = await stack.kernel.getServiceAsync('objectql'); + const adminId = await uid('admin@objectos.ai'); + aliceId = await uid('rxs-alice@verify.test'); + bobId = await uid('rxs-bob@verify.test'); + + // Stock: each member holds `member_default`, and no other set they hold — + // the app's baseline, or a set another package composes in — names + // reactions or a wildcard, so no reaction bit comes from anywhere else. + const otherReactionGrants = async (token: string) => { + const held = (await resolveAuthzFor(stack, token)).permissions; + const named: string[] = []; + for (const name of held) { + if (name === 'member_default') continue; + // A set the catalog cannot serve is unread, so it counts against arming. + const definition = await catalogPermissionSet(ql, name); + const objects = definition?.objects ?? {}; + if (!definition || REACTION in objects || '*' in objects) named.push(name); + } + return { held, named }; + }; + await assertArmed( + [['alice', aliceTok], ['bob', bobTok]].map(([who, token]) => + armedWhen({ + control: `${who} holds member_default, and no other set they hold names ${REACTION} or a wildcard`, + disarmedBy: 'another set naming reactions (an app or fixture grant) would make a 201 its answer, not the default\u2019s', + observe: () => otherReactionGrants(token), + armed: ({ held, named }) => held.includes('member_default') && named.length === 0, + }), + ), + ); + + // A record both members read (public_read_write), with one comment by the admin. + const open = await call(adminTok, 'POST', '/data/cmt_open', { name: 'stock reactions record' }); + expect(open.status, JSON.stringify(open.json)).toBeLessThan(300); + const openId = String(open.json.id ?? open.json.record?.id); + const posted = await call(adminTok, 'POST', '/data/sys_comment', { thread_id: `cmt_open:${openId}`, body: 'admin comment' }); + expect(posted.status, JSON.stringify(posted.json)).toBeLessThan(300); + commentId = String((await ql.findOne('sys_comment', { where: { thread_id: `cmt_open:${openId}` }, context: SYS })).id); + + // A comment on a record no member reads (cmt_private, owner-only), with the admin's reaction on it. + const privateId = String((await ql.insert('cmt_private', { name: 'admin only', owner_id: adminId }, { context: SYS })).id); + const privatePost = await call(adminTok, 'POST', '/data/sys_comment', { thread_id: `cmt_private:${privateId}`, body: 'private comment' }); + expect(privatePost.status, JSON.stringify(privatePost.json)).toBeLessThan(300); + privateCommentId = String((await ql.findOne('sys_comment', { where: { thread_id: `cmt_private:${privateId}` }, context: SYS })).id); + const adminReaction = await react(adminTok, privateCommentId, '👀'); + expect(adminReaction.status, JSON.stringify(adminReaction.json)).toBe(201); + privateReactionId = await reactionId({ comment_id: privateCommentId }); + }, 180_000); + + afterAll(async () => { + await stack?.stop(); + }); + + it('a member reacts to a comment they can read (201, user_id is the caller) and lists its reactions', async () => { + const created = await react(aliceTok, commentId, '👍'); + expect(created.status, JSON.stringify(created.json)).toBe(201); + expect((await reactionRows({ comment_id: commentId, emoji: '👍' })).map((r: any) => r.user_id)).toEqual([aliceId]); + + const bobs = await react(bobTok, commentId, '🎉'); + expect(bobs.status, JSON.stringify(bobs.json)).toBe(201); + + const filter = encodeURIComponent(JSON.stringify(['comment_id', '=', commentId])); + const list = await call(aliceTok, 'GET', `/data/${REACTION}?$filter=${filter}`); + expect(list.status, JSON.stringify(list.json)).toBe(200); + expect(listed(list.json).map((r: any) => r.user_id).sort()).toEqual([aliceId, bobId].sort()); + }); + + it('a member unreacts their own reaction through the own-row door', async () => { + const created = await react(aliceTok, commentId, '🚀'); + expect(created.status, JSON.stringify(created.json)).toBe(201); + const mineId = await reactionId({ comment_id: commentId, emoji: '🚀' }); + + const removed = await call(aliceTok, 'DELETE', `/comments/reactions/${mineId}`); + expect(removed.status, JSON.stringify(removed.json)).toBe(200); + expect(await reactionRows({ id: mineId })).toHaveLength(0); + }); + + it('a member cannot remove another member’s reaction — on the own-row door or the data door — and the row stays', async () => { + const theirs = await react(bobTok, commentId, '❤️'); + expect(theirs.status, JSON.stringify(theirs.json)).toBe(201); + const theirsId = await reactionId({ comment_id: commentId, emoji: '❤️' }); + + const door = await call(aliceTok, 'DELETE', `/comments/reactions/${theirsId}`); + expect(door.status, JSON.stringify(door.json)).toBe(403); + expect(door.json.error?.code).toBe('PERMISSION_DENIED'); + + // The baseline carries no delete bit, so the data door refuses before the floor. + const data = await call(aliceTok, 'DELETE', `/data/${REACTION}/${theirsId}`); + expect(data.status, JSON.stringify(data.json)).toBe(403); + expect(data.json.code).toBe('PERMISSION_DENIED'); + + expect(await reactionRows({ id: theirsId })).toHaveLength(1); + }); + + it('CONTROL: an unfiltered list answers the readable comment\u2019s reactions and omits the unreadable comment\u2019s', async () => { + const visible = await react(aliceTok, commentId, '👏'); + expect(visible.status, JSON.stringify(visible.json)).toBe(201); + const visibleId = await reactionId({ comment_id: commentId, emoji: '👏' }); + + const list = await call(aliceTok, 'GET', `/data/${REACTION}`); + expect(list.status, JSON.stringify(list.json)).toBe(200); + const ids = listed(list.json).map((r: any) => String(r.id)); + // The readable side is the witness that the answer carries rows at all. + expect(ids).toContain(visibleId); + expect(ids).not.toContain(privateReactionId); + // Ground truth past every scope: the hidden row exists. + expect(await reactionRows({ id: privateReactionId })).toHaveLength(1); + }); + + it('CONTROL: on a comment the member cannot read, a reaction is neither listed, read, removed nor inserted', async () => { + const filter = encodeURIComponent(JSON.stringify(['comment_id', '=', privateCommentId])); + const list = await call(aliceTok, 'GET', `/data/${REACTION}?$filter=${filter}`); + expect(list.status, JSON.stringify(list.json)).toBe(200); + expect(listed(list.json)).toEqual([]); + + const byId = await call(aliceTok, 'GET', `/data/${REACTION}/${privateReactionId}`); + expect(byId.status, JSON.stringify(byId.json)).toBe(404); + + const door = await call(aliceTok, 'DELETE', `/comments/reactions/${privateReactionId}`); + expect(door.status, JSON.stringify(door.json)).toBe(404); + expect(await reactionRows({ id: privateReactionId })).toHaveLength(1); + + const denied = await react(aliceTok, privateCommentId, '🎉'); + expect(denied.status, JSON.stringify(denied.json)).toBe(403); + expect(denied.json.code).toBe('RECORD_NOT_ACCESSIBLE'); + expect(await reactionRows({ comment_id: privateCommentId, user_id: aliceId })).toHaveLength(0); + }); +});