Skip to content

Commit 6069a98

Browse files
committed
fix(comments): the resolution door takes the posting gate, and a comment with any reply is the one the delete door keeps
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L6bhgUH1jEKbDzYRc4nghq
1 parent 591de69 commit 6069a98

12 files changed

Lines changed: 205 additions & 31 deletions

File tree

‎.changeset/36186-36191-comment-doors.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ Metadata-protocol: none
1414

1515
Two server doors carry the maintainer's comment permission rulings past the platform's row floor (`owner_only_writes`, which lets a rank-and-file member update only the rows they created) and past the delete grant no every-member set may hold (ADR-0090 D5). Neither changes the floor, `sys_comment_moderation` or any permission set, and the data API answers exactly what it answered before.
1616

17-
- **Resolve and reopen: `PUT /api/v1/comments/:id/resolution`** with the body `{ "resolved": true }` or `{ "resolved": false }` and nothing else. Whoever can read the thread may (ruling record 7063285032, option A), external members who can read it included: the door reads the comment as the caller (a comment they cannot read answers `404 RECORD_NOT_FOUND`, as an unknown id does), refuses a reply (`400 VALIDATION_ERROR`) and then writes `is_resolved` alone, attributed to the caller. `resolved_by` names the caller. It answers `200` with `{ id, is_resolved, resolved_by, resolved_at }`. Any other query parameter or body is `400 VALIDATION_ERROR`; a signed-out request is `401`.
18-
- **Delete your own comment: `DELETE /api/v1/comments/:id`.** It removes a comment the caller wrote (`author_id`), with no delete grant needed, on the platform's shared own-row delete door. Another member's comment is `403 PERMISSION_DENIED` and stays, even for a record editor: moderating someone else's comment stays the data API's, behind the delete grant. A comment others replied to stays too, `409 DELETE_RESTRICTED`, because removing it would turn each reply into a thread of its own (ruling record 7069889899: no one deletes or rewrites someone else's comment); its author may still edit its text. It answers `200` with `{ id, deleted: true }`.
17+
- **Resolve and reopen: `PUT /api/v1/comments/:id/resolution`** with the body `{ "resolved": true }` or `{ "resolved": false }` and nothing else. Whoever can read the thread and may post a comment on it may, external members included (ruling record 7063285032, option A: anyone who can see the record, the same gate as posting a comment): the door reads the comment as the caller (a comment they cannot read answers `404 RECORD_NOT_FOUND`, as an unknown id does), refuses a caller who reads comments but may not post one (`403 PERMISSION_DENIED`), refuses a reply (`400 VALIDATION_ERROR`) and then writes `is_resolved` alone, attributed to the caller. `resolved_by` names the caller. It answers `200` with `{ id, is_resolved, resolved_by, resolved_at }`. Any other query parameter or body is `400 VALIDATION_ERROR`; a signed-out request is `401`.
18+
- **Delete your own comment: `DELETE /api/v1/comments/:id`.** It removes a comment the caller wrote (`author_id`), with no delete grant needed, on the platform's shared own-row delete door. Another member's comment is `403 PERMISSION_DENIED` and stays, even for a record editor: moderating someone else's comment stays the data API's, behind the delete grant. A comment that has replies stays too, whoever wrote them, `409 DELETE_RESTRICTED`, because removing it would turn each reply into a thread of its own (ruling record 7069889899: no one deletes or rewrites someone else's comment); its author may still edit its text. It answers `200` with `{ id, deleted: true }`.
1919
- **Unchanged:** comment text stays its author's alone; a member's data-API update of a colleague's comment, `is_resolved` included, and a member's data-API delete are still `403`; administrators keep the data API for resolving, editing and deleting anyone's comment.
2020

2121
The desktop comment panel follows the doors:
2222

23-
- ✓ is on every thread, the viewer's and anyone else's, and writes through the resolution door. It no longer asks the explain engine; that probe now gates only Edit on someone else's comment.
23+
- ✓ is on every thread, the viewer's and anyone else's, for a viewer holding the `sys_comment` create grant, and writes through the resolution door. It no longer asks the explain engine; that probe now gates only Edit on someone else's comment.
2424
- A viewer without the `sys_comment` delete grant (an ordinary member) is offered Delete on their own comments that have no reply, and it goes through the own-comment delete door; a viewer with the grant deletes through the data API as before, others' comments included where they may edit the record.
2525

2626
Corrections to two earlier notes of this release, which stay as written:
2727

2828
- A rank-and-file member now resolves and reopens a colleague's thread through the resolution door; this replaces the #36071 note's line "This is the one widening: an update that changes nothing but `is_resolved` is admitted without the author-or-record-editor rule", because the platform's row floor (`owner_only_writes`) runs first, so through the data API such a member still resolves only the threads they started and an administrator resolves any.
29-
- The desktop comment panel offers ✓ on every thread and writes it through the resolution door; this replaces the #36075 note's line "✓ is offered under the `sys_comment` update grant: on a thread the viewer started, and on anyone's only where the explain engine admits …", and its "Unchanged" line's "a deployment without the explain service shows ✓ and Edit on the viewer's own rows only" now holds for Edit alone.
29+
- The desktop comment panel offers ✓ on every thread to a viewer who may post a comment and writes it through the resolution door; this replaces the #36075 note's line "✓ is offered under the `sys_comment` update grant: on a thread the viewer started, and on anyone's only where the explain engine admits …", and its "Unchanged" line's "a deployment without the explain service shows ✓ and Edit on the viewer's own rows only" now holds for Edit alone.

‎content/docs/frontend/plugins/plugin-detail.mdx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,18 @@ the record drawer), and no longer inside the 动态 (Activity) tab
211211
and open in place. A comment's actions (Reply, an emoji, ✓ on a thread's
212212
first comment, and More with Edit and Delete) appear at its top right while
213213
the pointer is over it. Edit and Delete are on your own comments; Delete is
214-
not on a comment of yours that someone has replied to, unless you hold the
214+
not on a comment of yours that has replies, unless you hold the
215215
`sys_comment` delete grant (administrators do), because the platform keeps
216216
it for the replies under it. On other people's comments they appear only
217217
where the server would accept them: Delete for someone who holds the
218218
`sys_comment` delete grant and may edit the record, Edit when the server
219219
also lets you change a comment you did not write (today, administrators).
220220
- **Resolved threads**: ✓ marks a thread resolved and it leaves the list; the
221221
icon's number drops by one. It is on every thread, yours and other people's:
222-
whoever can read the record may resolve and reopen its threads, through the
223-
platform's resolution door (`PUT /api/v1/comments/:id/resolution`). The
222+
whoever can read the record and may comment on it may resolve and reopen its
223+
threads, through the platform's resolution door
224+
(`PUT /api/v1/comments/:id/resolution`); someone who can read the comments
225+
but not post one sees no ✓. The
224226
filter menu at the panel's top right holds 「显示已解决」, which shows resolved
225227
threads marked with who resolved them (their ✓ reopens them), and
226228
「只看我参与的」, the threads you started, replied in or are mentioned in. A
@@ -399,7 +401,7 @@ uses, and hands Edit over under the caller's update grant on `sys_comment`.
399401
Delete uses the data API under the caller's delete grant; a caller without it
400402
(an ordinary member) deletes through the platform's own-comment delete door
401403
(`DELETE /api/v1/comments/:id`) under the grant they comment with. That door
402-
removes only the caller's own comment, and not one others replied to, so the
404+
removes only the caller's own comment, and not one that has replies, so the
403405
host sets `ownCommentDeletesOnly` and the feed offers Delete on exactly those
404406
rows. Edit opens the text in place; Delete asks through the console's
405407
destructive-action confirm. The server still decides the write, and a refusal

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import {
6868
COMMENT_RESOLUTION_ROUTE,
6969
registerCommentDoorsRoutes,
7070
type CommentDoorsEngine,
71+
type CommentPostingGate,
7172
} from './comment-routes.js';
7273
import { admitMentionRecipients } from './mention-recipient-gate.js';
7374
import { installActivityReadVisibility } from './activity-read-visibility.js';
@@ -1114,6 +1115,7 @@ export class AuditPlugin implements Plugin {
11141115
registerCommentDoorsRoutes(http, {
11151116
getEngine: () => getEngine() as unknown as CommentDoorsEngine | undefined,
11161117
resolveCaller,
1118+
getSecurity: () => optional<CommentPostingGate>('security'),
11171119
});
11181120
ctx.logger.info(
11191121
`AuditPlugin: comment doors mounted at DELETE ${COMMENT_REACTION_REMOVE_ROUTE}, PUT ${COMMENT_RESOLUTION_ROUTE} ` +

‎packages/plugins/plugin-audit/src/comment-routes.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,62 @@ describe('the resolution door — answers given before the engine is asked', ()
154154
expect(answer.body?.data).toEqual({ id: 'c1', is_resolved: true, resolved_by: 'u_member', resolved_at: '2026-11-21T00:00:00.000Z' });
155155
});
156156
});
157+
158+
describe('the resolution door — the posting gate (step 6)', () => {
159+
const readableComment = () => ({
160+
findOne: vi.fn(async (o: string, q: any) => {
161+
assertEngineFindOnePredicate(o, q);
162+
return { id: 'c1' };
163+
}),
164+
update: vi.fn(async () => ({})),
165+
delete: vi.fn(),
166+
});
167+
168+
it('a caller who reads the comment but may not post one is 403 PERMISSION_DENIED, and nothing is written', async () => {
169+
const engine = readableComment();
170+
const canWriteObject = vi.fn(async () => false);
171+
const answer = await resolveCall(
172+
{ getEngine: () => engine as any, resolveCaller: async () => MEMBER, getSecurity: () => ({ canWriteObject }) },
173+
{ body: { resolved: true } },
174+
);
175+
expect(answer.status).toBe(403);
176+
expect(answer.body?.error?.code).toBe('PERMISSION_DENIED');
177+
expect(canWriteObject).toHaveBeenCalledWith('sys_comment', 'insert', MEMBER);
178+
expect(engine.update).not.toHaveBeenCalled();
179+
});
180+
181+
it('a posting check that throws refuses, as the check itself says a throw does', async () => {
182+
const engine = readableComment();
183+
const answer = await resolveCall(
184+
{
185+
getEngine: () => engine as any,
186+
resolveCaller: async () => MEMBER,
187+
getSecurity: () => ({ canWriteObject: async () => { throw new Error('store down'); } }),
188+
},
189+
{ body: { resolved: true } },
190+
);
191+
expect(answer.status).toBe(403);
192+
expect(engine.update).not.toHaveBeenCalled();
193+
});
194+
195+
it('an unreadable comment is still 404, asked before the posting gate', async () => {
196+
const engine = { ...readableComment(), findOne: vi.fn(async (o: string, q: any) => { assertEngineFindOnePredicate(o, q); return null; }) };
197+
const canWriteObject = vi.fn(async () => false);
198+
const answer = await resolveCall(
199+
{ getEngine: () => engine as any, resolveCaller: async () => MEMBER, getSecurity: () => ({ canWriteObject }) },
200+
{ body: { resolved: true } },
201+
);
202+
expect(answer.status).toBe(404);
203+
expect(canWriteObject).not.toHaveBeenCalled();
204+
});
205+
206+
it('a caller who may post resolves', async () => {
207+
const engine = readableComment();
208+
const answer = await resolveCall(
209+
{ getEngine: () => engine as any, resolveCaller: async () => MEMBER, getSecurity: () => ({ canWriteObject: async () => true }) },
210+
{ body: { resolved: true } },
211+
);
212+
expect(answer.status, JSON.stringify(answer.body)).toBe(200);
213+
expect(engine.update).toHaveBeenCalledTimes(1);
214+
});
215+
});

‎packages/plugins/plugin-audit/src/comment-routes.ts‎

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,17 @@ import {
7575
* 5. **The comment, read AS THE CALLER**: the object grant, the organization
7676
* wall, and the comment read gate (a comment is readable exactly when the
7777
* record its thread is on is). A comment the caller cannot read and an id
78-
* that names none get the same answer, `RECORD_NOT_FOUND` / `404`. This is
79-
* the whole of who may: whoever can read the thread may resolve it (ruling
80-
* record 7063285032, option A).
81-
* 6. **A reply** is refused, `VALIDATION_ERROR` / `400`: a thread is resolved
78+
* that names none get the same answer, `RECORD_NOT_FOUND` / `404`.
79+
* 6. **The posting gate**: the caller must be one who may post a comment
80+
* here, the security service's own object-level answer for a `sys_comment`
81+
* insert (`canWriteObject`). Ruling record 7063285032 (option A) names both
82+
* "anyone who can see the record" and "the same gate as posting a
83+
* comment"; the door takes the narrower reading, so a principal who reads
84+
* comments but may not post one (a read-only grant on its own) is refused,
85+
* `PERMISSION_DENIED` / `403`. Every stock member posts, so for them the
86+
* two readings agree. A deployment with no security service has no gate
87+
* on posting either, and this step admits there.
88+
* 7. **A reply** is refused, `VALIDATION_ERROR` / `400`: a thread is resolved
8289
* on its top-level comment (`refuseResolutionOnReply`, the data door's
8390
* own sentence).
8491
*
@@ -96,10 +103,14 @@ import {
96103
* "authors govern their own words". Another member's comment is refused,
97104
* `PERMISSION_DENIED` / `403`, even on a record the caller can edit: the
98105
* moderator's delete stays the data door's, behind an explicit delete grant.
99-
* - **A comment others replied under stays** (the door's step 7):
106+
* - **A comment that has replies stays** (the door's step 7), whoever wrote them:
100107
* `DELETE_RESTRICTED` / `409`. Removing it would rewrite the replies:
101108
* `parent_id` is a lookup that clears on delete, so each reply would become
102-
* a thread of its own. The author may still edit its text.
109+
* a thread of its own. The author may still edit its text. A known limit:
110+
* the reply check and the removal are two engine calls, and the data
111+
* engine offers no conditional delete to join them, so a reply posted in
112+
* the one request between them is promoted to a thread of its own, as it
113+
* is under an administrator's data-door delete today.
103114
* - **The removal** is a system delete attributed to the caller, so the
104115
* comment gates do not run a second time and the audit ledger credits the
105116
* caller.
@@ -134,7 +145,7 @@ async function refuseRemovalUnderReplies(
134145
});
135146
if (!reply) return;
136147
const err = new Error(
137-
`Comment ${String(row.id)} was not removed: other comments reply to it, and removing it would turn each reply ` +
148+
`Comment ${String(row.id)} was not removed: it has replies, and removing it would turn each reply ` +
138149
'into a thread of its own. Edit its text instead. The comment is unchanged.',
139150
) as Error & { code?: string; status?: number };
140151
err.code = 'DELETE_RESTRICTED';
@@ -162,6 +173,16 @@ export interface CommentDoorsRoutesDeps {
162173
getEngine(): CommentDoorsEngine | undefined;
163174
/** Resolve the caller of a request (`ownRowDeleteCallerResolver`); `undefined` names nobody. */
164175
resolveCaller: OwnRowDeleteRoutesDeps['resolveCaller'];
176+
/**
177+
* The security service, read per request for the resolution door's posting
178+
* gate (step 6); `undefined` when none is registered.
179+
*/
180+
getSecurity?(): CommentPostingGate | undefined;
181+
}
182+
183+
/** The slice of the security service the posting gate asks (plugin-security's `canWriteObject`). */
184+
export interface CommentPostingGate {
185+
canWriteObject?(object: string, operation: 'insert' | 'update', context?: unknown, data?: unknown): Promise<boolean>;
165186
}
166187

167188
/** Mount both comment doors on `http`. Nothing on `deps` is called until a request arrives. */
@@ -189,6 +210,7 @@ export async function resolveCommentThread(
189210
id: string,
190211
resolved: boolean,
191212
context: ExecutionContext,
213+
security?: CommentPostingGate,
192214
): Promise<CommentResolutionAnswer> {
193215
const callerId = context.userId;
194216
if (!callerId) throw new Error('resolveCommentThread: the caller context names no user');
@@ -200,7 +222,27 @@ export async function resolveCommentThread(
200222
| undefined;
201223
if (!row) throw recordNotFoundError(COMMENT_OBJECT, id, context);
202224

203-
// Step 6: a thread is resolved on its top-level comment.
225+
// Step 6: the posting gate. Asked after the read, so a comment the caller
226+
// cannot read still answers as an unknown id. A check that throws refuses.
227+
if (typeof security?.canWriteObject === 'function') {
228+
let mayPost = false;
229+
try {
230+
mayPost = (await security.canWriteObject(COMMENT_OBJECT, 'insert', context)) === true;
231+
} catch {
232+
mayPost = false;
233+
}
234+
if (!mayPost) {
235+
const err = new Error(
236+
`The thread of comment ${id} was not changed: resolving and reopening a thread takes the same access as ` +
237+
'posting a comment on it, and you can read this thread but not comment on it.',
238+
) as Error & { code?: string; status?: number };
239+
err.code = 'PERMISSION_DENIED';
240+
err.status = 403;
241+
throw err;
242+
}
243+
}
244+
245+
// Step 7: a thread is resolved on its top-level comment.
204246
refuseResolutionOnReply([row]);
205247

206248
const system = {
@@ -292,10 +334,10 @@ async function handleResolution(req: IHttpRequest, res: IHttpResponse, deps: Com
292334
return;
293335
}
294336

295-
// ⑤ ⑥ and the write.
337+
// ⑤ ⑥ ⑦ and the write.
296338
let answer: CommentResolutionAnswer;
297339
try {
298-
answer = await resolveCommentThread(engine, id, resolved, context);
340+
answer = await resolveCommentThread(engine, id, resolved, context, deps.getSecurity?.());
299341
} catch (err) {
300342
const thrown = resolveThrownHttpError(err, 500);
301343
if (thrown.status < 500) {

0 commit comments

Comments
 (0)