fix: add post permission checks to comment interaction endpoints#4912
fix: add post permission checks to comment interaction endpoints#4912lsabor wants to merge 1 commit into
Conversation
Closes several secondary permission gaps where users could interact with comments on posts they don't have access to: - comment_toggle_cmm_view, comment_report_api_view: add can_view check - comments_of_week_view: filter entries to posts visible to the user - key_factor_vote_view, comment_suggested_key_factors_view: add can_view check Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughTwo comment view modules ( ChangesAuthorization enforcement on comment endpoints
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
comments/views/common.py (1)
244-258:⚠️ Potential issue | 🟠 MajorGuard against
Nonevalue inon_postbefore the permission check.The
on_postfield in the Comment model is defined withnull=True, allowing it to beNone. However,get_post_permission_for_user(post)accessespost.id(line 355 in posts/services/common.py), which will crash ifpostisNone. The existing guard at line 255 comes too late—it should be moved before the permission check.Proposed fix
def comment_report_api_view(request, pk=int): comment = get_object_or_404(Comment, pk=pk) post = comment.on_post + if not post: + return Response( + {"error": "Comment is not associated with a post"}, + status=status.HTTP_400_BAD_REQUEST, + ) + permission = get_post_permission_for_user(post, user=request.user) ObjectPermission.can_view(permission, raise_exception=True)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@comments/views/common.py` around lines 244 - 258, The comment_report_api_view function retrieves post from comment.on_post which can be None due to null=True in the model definition, but then immediately passes this potentially None value to get_post_permission_for_user which will crash when accessing post.id. Move the guard check for None post value before the permission check call, raising an appropriate 404 exception if post is None, rather than waiting until after the permission validation to check if post exists.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@comments/views/common.py`:
- Around line 244-258: The comment_report_api_view function retrieves post from
comment.on_post which can be None due to null=True in the model definition, but
then immediately passes this potentially None value to
get_post_permission_for_user which will crash when accessing post.id. Move the
guard check for None post value before the permission check call, raising an
appropriate 404 exception if post is None, rather than waiting until after the
permission validation to check if post exists.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 96c62404-c0f5-44ee-9c06-cd21528f0edb
📒 Files selected for processing (2)
comments/views/common.pycomments/views/key_factors.py
🚀 Preview EnvironmentYour preview environment is ready!
Details
ℹ️ Preview Environment InfoIsolation:
Limitations:
Cleanup:
|
Closes several secondary permission gaps where users could interact with comments on posts they don't have access to:
Summary by CodeRabbit