-
Notifications
You must be signed in to change notification settings - Fork 4
global logging for PermissionDenied DRF errors #904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughRemoved a custom OpportunityViewSetPermission from commcare_connect/opportunity/api/views.py and reverted OpportunityViewSet to use DRF's IsAuthenticated. Added commcare_connect/utils/exceptions.drf_permission_denied_handler to log PermissionDenied events (user id, auth method, request URL, sanitized headers) and delegate to DRF's default handler. Wired that handler into REST_FRAMEWORK via the EXCEPTION_HANDLER setting in config/settings/base.py. Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
commcare_connect/opportunity/api/views.pycommcare_connect/utils/exceptions.pyconfig/settings/base.py
🧰 Additional context used
🪛 Ruff (0.14.10)
commcare_connect/opportunity/api/views.py
35-35: Mutable class attributes should be annotated with typing.ClassVar
(RUF012)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: pytest
🔇 Additional comments (2)
commcare_connect/opportunity/api/views.py (1)
33-38: LGTM!Simplifying to use
IsAuthenticateddirectly is appropriate now that the custom logging logic has been centralized in the exception handler. The static analysis hint aboutClassVarcan be safely ignored here—this is standard DRF convention forpermission_classes.config/settings/base.py (1)
321-321: Configuration is correct.The
EXCEPTION_HANDLERsetting correctly points to the new centralized handler. Ensure the issues flagged incommcare_connect/utils/exceptions.py(missing return for non-PermissionDenied exceptions, sensitive header logging) are addressed before merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
commcare_connect/utils/exceptions.py (1)
23-23: Minor: Redundant parentheses inisinstancecheck.The tuple wrapper around a single type is unnecessary.
🔎 Suggested fix
- if isinstance(exc, (PermissionDenied)) and request is not None: + if isinstance(exc, PermissionDenied) and request is not None:
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
commcare_connect/utils/exceptions.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: pytest
🔇 Additional comments (3)
commcare_connect/utils/exceptions.py (3)
1-6: LGTM!Imports are minimal and appropriate. Logger setup follows Python best practices using
__name__.
10-19: Header sanitization addresses the security concern.This correctly redacts sensitive headers before logging, fixing the previously flagged security issue. The substring matching approach is reasonable for catching variations like
X-Api-KeyorAuth-Token.Note: The substring
"key"may be slightly broad (matching headers likeX-Monkey-Header), but erring on the side of over-redaction is safer for security.
21-39: Critical fix applied: All exceptions now delegate correctly.The return statement on line 39 is now outside the
ifblock, ensuring all exception types (not justPermissionDenied) are properly handled by DRF's default handler. This fixes the previously flagged critical issue.The safe attribute access patterns (
getattr,hasattr) are well implemented.
Product Description
https://dimagi.atlassian.net/browse/CI-430
Followup on #878 to enable such logging for all API requests where permission is denied.
Technical Summary
Safety Assurance
Safety story
Automated test coverage
Tested flow locally to see it gets logged.
Tested flow locally to see logs.
QA Plan
NA
Labels & Review