Skip to content

fix(posts): scope post listings to the caller's pods; gate follows, likes and comments on the post's pod - #2185

Merged
lilyshen0722 merged 3 commits into
mainfrom
fix/post-pod-authz
Oct 14, 2026
Merged

lilyshen0722 merged 3 commits into
mainfrom
fix/post-pod-authz

Conversation

@lilyshen0722

@lilyshen0722 lilyshen0722 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Post routes now check the post's own pod, under three rules:

  • Listings (the feed and search with no podId, and followed threads) show global posts plus the caller's own pods. Admins get no bypass on these, per the CLAUDE.md rule that admins don't bypass membership on default listing surfaces.
  • Reads of one named pod or one post use DMService.canViewPod, admin bypass included. This is unchanged for GET /api/posts/:id and GET /api/posts?podId=.
  • Writes into a pod (comment, like/unlike) need membership via the shared write gate, callerHasPodWriteAccess.

Global posts (no pod) are open to any signed-in user throughout. Refusals are 403 with a JSON error.

What each endpoint now checks

Endpoint Check
GET /api/posts with no podId (the main feed) Global posts plus the caller's own pods. Posts from pods the caller isn't in no longer appear here, which is intended. This applies to the recent and hot sorts and to total.
GET /api/posts?podId=<id> Unchanged gate (canViewPod: 404 for a missing pod, 403 for one the caller can't view). The filter now uses the pod's ObjectId, so the hot sort returns the pod's posts; before, the query string matched nothing in the aggregate $match.
GET /api/posts/search With no podId, the same listing filter as the feed. A named podId goes through canViewPod, as on the feed. podId=global/none is unchanged.
GET /api/posts/following/threads The same listing filter. A followed thread drops out once the caller is no longer in its pod, or if the pod is deleted.
POST /api/posts/:id/follow canViewPod on the post's pod. Global posts stay open.
DELETE /api/posts/:id/follow No pod check. A user can always remove their own follow.
POST /api/posts/:id/like (like and unlike) Pod posts need membership (callerHasPodWriteAccess). Global posts are unchanged.
POST /api/posts/:id/comments Pod posts need membership (callerHasPodWriteAccess). The agent-mention enqueue and the thread_comment activity take their pod from the post, and the request-body podId is no longer read. On a global post the mention still falls back to a pod the commenter belongs to.
POST / DELETE /api/messages/:messageId/follow No code change. These already check the message's own pod, resolved on the server, with callerHasPodWriteAccess, for both human (req.userId) and agent (req.agentUser) callers. This PR adds tests that run that refusal path.
DELETE /api/posts/:id/comments/:commentId, PATCH /api/posts/:id/agent-comments No change. Both already allow only the comment's or post's author.

Input validation and rate limits

  • podId from the request (feed, search, create post) is checked with mongoose.Types.ObjectId.isValid and cast with new mongoose.Types.ObjectId(...) before it reaches Pod.findById or a Post filter. This is the sanitizer in docs/security-patterns.md. A malformed podId is a 400 { error: 'Invalid podId' }. global and none still work on reads.
  • category and each tag are coerced to strings before they reach a filter. An object in the query string (for example category[$ne]=x) is now matched as text rather than treated as an operator.
  • Rate limits in routes/posts.ts. There are two inline rateLimit({...}) limiters, built the same way as in routes/messages.ts.
    • Reads (GET /, /search, /following/threads, /:id): 240 per minute.
    • Writes (create, comment, follow, unfollow, like, delete post, delete comment, toggle agent comments): 60 per minute.
    • These are messages.ts's read and send budgets. 240 a minute is four reads a second sustained, which is well above feed scrolling, search-as-you-type and opening threads. 60 a minute is one write a second, far above anyone posting, commenting, following or liking by hand.
    • Each limiter is shared across its routes, so the budget is per token across the router.
    • Both are keyed on a hash of the caller's token (Authorization or x-auth-token, the two headers middleware/auth reads), not on IP. In production req.ip is a cloudflared pod for every caller.
    • Each limiter runs before auth on every route. The routes/posts.ts rows are removed from the rate-limit guard baseline.
    • The write limiter also covers create, delete post, delete comment and toggle agent comments, which weren't on the original list. main has the same rate-limiting alert on those routes, and it is one limiter for the whole file.

DMService.listPodIdsForListing(userId) is new and sits next to canViewPod. It returns the pods canViewPod admits minus the admin bypass: the user's memberships plus the agent-dm pods that share a member with one of them. The listing filter passes ObjectIds because the hot sort runs it through an aggregate $match, which Mongoose does not cast.

docs/development/BACKEND.md now describes these rules, and the comments row no longer lists a podId body field. In ApiDevPage.tsx, the posts list, get-by-id and search entries now say auth is required, which has been the case since those routes started requiring auth.

Known scaling note. The agent-dm fan-out query in listPodIdsForListing passes every co-member id of the user's pods to $in. For a user in a pod with thousands of members, that list is large. It is left as is for now.

Behaviour to be aware of. External social feed posts are stored in the pod of their integration (the "Global Social Feed" pod for global feeds). After this change, GET /api/posts?category=Social with no podId returns them only to members of that pod.

Tests

All tests run on memory Mongo with the real models and access services. Only the JWT check and the mention fan-out are stubbed. Suites were run individually on Node 22.

  • backend/__tests__/unit/routes/posts.podAccess.test.js (new, 42 tests):
    • Feed: a member sees their pod; a non-member is filtered on the recent sort, on the hot sort and in total; an admin gets only their own pods; an admin naming a pod still reads it; a non-member naming a pod gets 403; a named pod works on the hot sort; podId=global and podId=none return global posts only; a malformed podId or one sent as an object gets 400; a category sent as an object is matched as text.
    • Search: the same cases as the feed, plus a match that is only in a comment, a missing pod (404), and category and tags sent as objects matched as text.
    • Create post: a malformed podId gets 400 and nothing is created; a valid pod still works.
    • Followed threads: a member's thread is listed; a thread from a pod the caller left drops out; an admin gets no bypass.
    • Follow: a member is allowed; a non-member gets 403 with nothing recorded; global posts are open.
    • Unfollow: a member is allowed; a user no longer in the pod can remove their follow; global posts are open.
    • Like: a member can like and unlike; a non-member's like and unlike both get 403 and leave the count and likedBy unchanged; global posts are open.
    • Comments: a member is allowed, with mention and activity under the post's pod even when the body names another pod; a non-member gets 403 and no comment, mention or activity is written; a global post stays open and its activity never takes a pod from the body.
  • backend/__tests__/unit/controllers/threadStateController.podAccess.test.js (new, 6 tests): message follow and unfollow. A member is allowed; a non-member gets 403 with no row written or changed; an agent in the pod is allowed; an agent outside it gets 403.
  • backend/__tests__/services/dmService.agentDm.test.ts (+4 tests):
    • listPodIdsForListing equals canViewPod's set for non-admin members, fan-out readers and outsiders.
    • It includes a fan-out agent-dm and leaves out an agent-room and an unrelated agent-dm.
    • It returns an empty list for a user in no pod.
    • For an admin, canViewPod admits every pod while the listing returns only the pods they had before promotion.
  • backend/__tests__/unit/routes/posts.test.js (+13 tests): a presence test, not a timing test. Every read route answers with the 240 limiter's RateLimit-Limit header and every write route with the 60 limiter's. A request that auth refuses still carries the header, which shows the limiter runs first.
  • backend/__tests__/unit/routes/routeRateLimitGuard.test.js: passes with the 12 routes/posts.ts baseline rows removed.
  • frontend/src/components/ApiDevPage.test.tsx: passes unchanged.

Result: 6 backend suites with 108 tests and 1 frontend suite with 8 tests, all passing. npm run lint:ts reports 0 errors.

CodeQL on the head: "No new alerts in code changed by this pull request", and no open alerts on routes/posts.ts or controllers/postController.ts on the PR ref.

Mutation check. 26 single-site reverts against this head, each restored from a saved copy, and each turned its paired tests red while the suite still ran its full test count:

  • Every listing filter (feed, search, followed threads).
  • The ObjectId cast in the listing filter and the named-pod ObjectId.
  • Reintroducing an admin bypass into the listing set.
  • The named-pod refusals on the feed and on search.
  • The like, comment and follow gates, and the follow projection.
  • Re-adding a pod check to unfollow.
  • The mention and activity pod sources.
  • The agent-dm fan-out.
  • The existing message-follow gate.
  • The podId ObjectId guard and cast on the feed, search and create post.
  • The string coercion of category (feed and search) and of tags.
  • Removing the limiter from a route, moving it behind auth, and putting a write route on the read budget.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UPw8eqDcnT3Q6yPxVqpAC1

- GET /api/posts/search: with no podId, results are global posts plus
  the pods the caller can view, pushed into the query as one podId
  filter. A named podId takes the same canViewPod gate as
  GET /api/posts?podId= (404 missing pod, 403 not viewable).
- GET /api/posts/following/threads: drops threads whose pod the caller
  can no longer view.
- POST/DELETE /api/posts/:id/follow: require canViewPod on the post's
  pod. Global posts stay open.
- POST /api/posts/:id/comments: commenting on a pod post requires pod
  membership (callerHasPodWriteAccess). The mention and activity
  records take their pod from the post; the request-body podId is no
  longer read.
- DMService.listViewablePodIds: the set form of canViewPod (admin ->
  all pods; otherwise memberships plus the agent-dm fan-out), with a
  test asserting the two agree over one fixture.

POST/DELETE /api/messages/:messageId/follow already checked the
message's own pod with the shared write gate for both human and agent
callers; this adds executing coverage of that refusal path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UPw8eqDcnT3Q6yPxVqpAC1
… check the pod

- GET /api/posts with no podId now returns global posts plus the
  caller's own pods, the same filter search uses.
- Listing surfaces (feed and search with no podId, followed threads)
  use DMService.listPodIdsForListing: membership plus the agent-dm
  fan-out, with no admin bypass. A named podId and reads by id keep
  canViewPod, admin bypass included. The filter carries ObjectIds so
  the hot sort's aggregate $match applies it.
- GET /api/posts?podId=<id> filters on the pod's ObjectId, so the hot
  sort returns that pod's posts (the query string matched none).
- POST /api/posts/:id/like (like and unlike) on a pod post requires
  pod membership (callerHasPodWriteAccess). Global posts unchanged.
- DELETE /api/posts/:id/follow no longer checks pod access, so a user
  can always remove their own follow. Following still requires
  canViewPod.
- ApiDevPage: the posts list, get-by-id and search entries require
  auth.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UPw8eqDcnT3Q6yPxVqpAC1
@lilyshen0722 lilyshen0722 changed the title fix(posts): search, follows and comments check the post's pod fix(posts): scope post listings to the caller's pods; gate follows, likes and comments on the post's pod Oct 14, 2026
…t every posts route

- podId from the request (feed, search, create) is checked with
  mongoose.Types.ObjectId.isValid and cast to an ObjectId before it
  reaches a query (docs/security-patterns.md). A malformed podId is a
  400; 'global'/'none' still work on reads.
- category and each tag are coerced to strings before reaching a
  filter, so an object in the query string is matched as text.
- routes/posts.ts: inline read (240/min) and write (60/min) limiters,
  the same budgets as routes/messages.ts, keyed on a hash of the
  caller's token (Authorization or x-auth-token) rather than IP, and
  placed ahead of auth on every route. The posts.ts rows leave the
  rate-limit guard baseline.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UPw8eqDcnT3Q6yPxVqpAC1

@lilyshen0722 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review at 11f7b4e (eng lead, code and security gate; my reading of fac5fa2's logic carries forward, and this binds the PASS to the head that includes the CodeQL fixes).

Checked:

  • Listing scope: the feed (recent, hot, total) and search without a podId return global posts plus DMService.listPodIdsForListing, which is membership plus the §3.7 agent-dm fan-out with no admin bypass. A named podId and reads by id still go through canViewPod.
  • Writes and follows: like/unlike and comments require membership on pod posts, and the mention and activity pod comes from the post. Follow requires canViewPod, followed threads are re-filtered against current visibility on every read, and unfollow is always allowed.
  • Input: a query or body podId must pass ObjectId.isValid and is cast before any query (feed, search, create), with 'global' and 'none' kept. category and tags are coerced to strings. This is the canonical sanitizer in docs/security-patterns.md, and it closes main's js/sql-injection record on this file.
  • Rate limits: all 13 routes in routes/posts.ts carry an inline read (240/min) or write (60/min) limiter ahead of auth. The key is a hash of Authorization or x-auth-token, falling back to the Cloudflare-aware IP key. The per-process buckets match messages.ts; I accept that as Cofounder noted.
  • CI: CodeQL reports no new alerts and nothing was dismissed. All checks are green.
  • Tests at this head: 55 backend suites touching posts, DM visibility, thread state and the rate-limit guard pass (588 tests), and lint shows 0 errors on the changed files.

Product consequence, ruled acceptable by Cofounder: category=Social lists now reach only members of the Global Social Feed pod. I'll check at deploy that any curator agent still reading them is a member.

GATE: PASS

@lilyshen0722
lilyshen0722 added this pull request to the merge queue Oct 14, 2026
Merged via the queue into main with commit 4a8acb7 Oct 14, 2026
24 checks passed
samxu01 pushed a commit that referenced this pull request Oct 14, 2026
Picks up #2185. The only conflict was routeRateLimitGuard.baseline.json:
main's version is kept, minus the seven moltbot rows this branch retires.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UPw8eqDcnT3Q6yPxVqpAC1
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.

2 participants