You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Below is a summary of compliance checks for this PR:
Security Compliance
🔴
Unauthenticated state change
Description: The unsubscribe action relies on current_user and toggles notification levels without validating a signed token in the request, enabling anyone with the link to change a user's topic notification settings (CSRF/unauthenticated state change via GET). topics_controller.rb [98-116]
Description: Unsubscribe endpoints are exposed as GET routes without any authenticity protection or signed parameters, which facilitates CSRF or link-forgery to alter user notification settings. routes.rb [438-442]
Objective: To create a detailed and reliable record of critical system actions for security analysis and compliance.
Status: Missing audit log: The new per-topic unsubscribe action changes a user's topic notification level but does not log the action with user ID, timestamp, or outcome for auditability.
Generic: Robust Error Handling and Edge Case Management
Objective: Ensure comprehensive error handling that provides meaningful context and graceful degradation
Status: Nil handling risk: The unsubscribe action assumes a TopicUser record exists and that tu.notification_level is present, with no handling for nil cases or save failures beyond raising.
Generic: Security-First Input Validation and Data Handling
Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent vulnerabilities
Status: Authn/authz check: New unsubscribe routes expose a GET endpoint that changes user state, and the controller action does not visibly enforce authentication/authorization or CSRF protections in the diff.
Implement token-based authentication for unsubscribing
The current unsubscribe feature requires users to be logged in, creating a poor user experience. It should be changed to use a secure, single-use token in the unsubscribe URL to allow unsubscribing without a login.
# app/controllers/topics_controller.rbdefunsubscribe# Relies on a logged-in user via `current_user`@topic_view=TopicView.new(params[:topic_id],current_user)tu=TopicUser.find_by(user_id: current_user.id,topic_id: params[:topic_id])iftu.notification_level > TopicUser.notification_levels[:regular]tu.notification_level=TopicUser.notification_levels[:regular]elsetu.notification_level=TopicUser.notification_levels[:muted]endtu.save!perform_show_responseend
After:
# app/controllers/topics_controller.rbdefunsubscribe# Finds user via a secure token from params, no login requiredunsubscribe_key=UnsubscribeKey.find_by(key: params[:key])# Validate key and topic...user=unsubscribe_key.usertu=TopicUser.find_by(user_id: user.id,topic_id: params[:topic_id])# ... logic to change notification level ...tu.save!unsubscribe_key.destroy# Invalidate token after use# Render a simple confirmation pagerender"unsubscribe_confirmation"end
Suggestion importance[1-10]: 10
__
Why: This suggestion addresses a critical design flaw in the new feature, as requiring a login to unsubscribe from an email link creates significant user friction and renders the feature impractical for many users.
High
Possible issue
Handle nil when user record missing
To prevent a NoMethodError, handle cases where the TopicUser record is nil by using TopicUser.change, which gracefully creates or updates the record.
tu = TopicUser.find_by(user_id: current_user.id, topic_id: params[:topic_id])
-if tu.notification_level > TopicUser.notification_levels[:regular]- tu.notification_level = TopicUser.notification_levels[:regular]+new_level = if tu && tu.notification_level > TopicUser.notification_levels[:regular]+ TopicUser.notification_levels[:regular]
else
- tu.notification_level = TopicUser.notification_levels[:muted]+ TopicUser.notification_levels[:muted]
end
-tu.save!+TopicUser.change(current_user.id, params[:topic_id], notification_level: new_level)
Apply / Chat
Suggestion importance[1-10]: 8
__
Why: The suggestion correctly identifies a NoMethodError on a nil object, which would cause a server error for users without a pre-existing TopicUser record, and provides an idiomatic fix.
Medium
More
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
PR #2
PR Type
Enhancement
Description
Add per-topic unsubscribe functionality via email links
Implement topic unsubscribe route and controller action
Update email templates with topic-specific unsubscribe URL
Refactor code formatting and improve code quality
Diagram Walkthrough
File Walkthrough
11 files
Add unsubscribe action to topics controllerAdd unsubscribe_url method to topic modelAdd topic unsubscribe URL to email optionsSupport topic-specific unsubscribe links in emailsCreate topic unsubscribe controller componentCreate topic unsubscribe route handlerCreate topic unsubscribe view componentAdd conditional title rendering to dropdownAdd styles for topic unsubscribe pageCreate topic unsubscribe templateAdd unsubscribe link to email template2 files
Code formatting and style improvementsRefactor route with modern ES6 syntax2 files
Add unsubscribe routes for topicsRegister topic unsubscribe route mapping2 files
Add client-side unsubscribe localization stringsAdd server-side unsubscribe localization strings1 files
Update tests for unsubscribe URL parameter