Skip to content

fix(api): refuse bad post writes instead of answering 500 or returning the post - #1185

Merged
mortondev merged 2 commits into
mainfrom
fix/rest-post-writes
Nov 20, 2026
Merged

mortondev merged 2 commits into
mainfrom
fix/rest-post-writes

Conversation

@mortondev

Copy link
Copy Markdown
Member

What this fixes

Six REST post writes that answered 500, wrote what they should have refused, or handed a post to a key that cannot read it. Each was found by the product e2e suite and has a quarantined test on #1167.

Bug Before After
R-026 PATCH /api/v1/posts/:id with {} (or only unknown fields) checked no permission or scope and returned the full post, so a read:chat key could read any post Every PATCH needs post.view_private (the answer carries the post) on top of each field's permission. A body with no updatable field is refused: 403 without the feedback write scope, otherwise 400
R-027 Tag replacement deleted every assignment, then the insert of an unknown or repeated tag failed outside a transaction: 500 and the post lost its tags Tags are checked before anything is written; the post row update and the tag delete+insert run in one transaction
R-012 An unknown or repeated tag id on create answered 500 Unknown or deleted tag is 404 PostTag not found; a repeated id is 400. Both are refused before the post is written
R-013 A soft-deleted status was accepted on create and update Deleted statuses are filtered out: 404 Status not found
R-028 PATCH edited a soft-deleted post and fired status events A deleted post is 404 Post not found on update; nothing is written or announced
R-041 A proxy vote for an unknown voter hit the principal foreign key: 500 addVoteOnBehalf checks the voter in its CTE: 404 User not found. Any existing principal, including another key's service principal, is still accepted

Behaviour changes worth a look

  • Repeated tag ids are refused (400), not deduped. The quarantined tests R-012 and R-027 send [a, a] and expect 400/404 with nothing written, so dedupe would keep them red.
  • Every post PATCH now needs post.view_private. No system role has a post write permission without it; a custom role that does now gets 403 instead of the post.
  • A deleted tag the post already carries may be re-sent. Tag soft-delete keeps existing assignments, so re-sending a post's current tags (as the admin UI does) still works; only newly added deleted tags are 404.
  • The deleted-post, deleted-status, tag and unknown-voter refusals live in the service layer, so the admin server functions, MCP triage_post / proxy votes and integration votes get them too.

Quarantined e2e tests on #1167 this fixes

Lift after merge:

  • rest-posts.spec.ts: API-R-026 a PATCH with no recognised field is refused to a key that cannot read posts
  • rest-posts.spec.ts: API-R-027 PATCH tags with an unknown tag id is refused and keeps the post's tags
  • rest-posts.spec.ts: API-R-012 an unknown or repeated tag id on create is refused without leaving a post
  • rest-posts.spec.ts: API-R-013 a soft-deleted status is not found on create or update
  • rest-posts.spec.ts: API-R-028 a deleted post cannot be edited
  • rest-votes.spec.ts: API-R-041 a proxy vote for an unknown voter is a not-found refusal, not a server error (the quarantined one; "API-R-041 proxy vote validation refusals" is already live)

Tests

Each new test was run red against origin/main first, then green.

  • post-service-writes.db.test.ts (new, real Postgres): unknown/repeated/deleted tags on create and update, nothing written when tags are refused alongside a title change, re-sending a deleted tag the post carries, deleted status on create and update, deleted post on update with no status event, unknown voter on proxy vote, service principal accepted as voter
  • $postId.patch.test.ts (new): empty and unknown-field bodies refused to a read:chat key with no post in the answer, read-only key refused, writer gets 400, a caller without post.view_private refused, field permission still named first (R-025's messages unchanged), happy path
  • post-service.test.ts: tag replacement goes through the transaction handle, never the bare db
  • Existing mocked tests updated for the transaction and the voter check (post-service, post.service-mentions, post-voting)

…r leaking the post

- PATCH /posts/:id needs post.view_private on every call and refuses a body with no updatable field (400), so a key that cannot read posts never gets one back
- a repeated tag id is a 400 and an unknown or deleted tag a 404 on create and update, checked before anything is written
- updatePost replaces tags and the post row in one transaction, so a failed tag insert never strips the post's tags
- a deleted status is not found on create and update; a deleted post is not found on update
- a proxy vote for an unknown voter is a 404, not a foreign-key 500
@mortondev
mortondev force-pushed the fix/rest-post-writes branch from d08f575 to 86014c6 Compare November 20, 2026 11:55
@mortondev
mortondev merged commit 1fed03f into main Nov 20, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant