FAQ-000203 - CSRF and DML Security Issues / General CSRF Prevention and Best Practices

Current Status:VALID_RESPONSEErrorUnable to AnswerSuggests Case

Current FAQ

Question
I fixed a CSRF vulnerability by replacing an automatic action on load with a user-initiated button, but this negatively impacted UX. Are there other secure alternatives?
Answer
To address CSRF vulnerabilities without negatively impacting user experience, here are some secure alternatives: 1. **Enable `confirmationTokenRequired`**: In Visualforce pages, this ensures GET requests require a CSRF token in the URL, preventing unauthorized access. You can configure this in your org's Visualforce page settings. 2. **Avoid State Changes on Load**: Refrain from performing state changes or DML operations during the instantiation of Apex controllers in Lightning or LWC. Instead, trigger these operations through user actions like button clicks. 3. **Token-Based Authentication for Redirection**: Implement user interaction for any redirection and validate redirection requests using token-based authentication. 4. **Secure Coding Practices**: Ensure high entropy for CSRF tokens and validate them before accepting requests. These measures can help mitigate CSRF risks while maintaining a smooth user experience.
Enhancing FAQ...

Enhancing FAQ with AI recommendations...

AI Recommended Enhancement

Related Security Rules (click to view)
ApexCSRFVfCsrf
Question
I fixed a CSRF vulnerability by replacing an automatic action on load with a user-initiated button, but this negatively impacted UX. Are there other secure alternatives?
Recommended Answer Update
To address CSRF vulnerabilities without negatively impacting user experience, here are some secure alternatives: 1. **Enable `confirmationTokenRequired`**: In Visualforce pages, this ensures GET requests require a CSRF token in the URL, preventing unauthorized access. You can configure this in your page's controller or page-level settings. 2. **Avoid State Changes on Load**: Don't perform state changes or DML operations during the instantiation of Apex controllers in Lightning or LWC. Instead, trigger these operations through user actions like button clicks. 3. **Token-Based Authentication for Redirection**: Implement user interaction for any redirection and validate redirection requests using token-based authentication. 4. **Secure Coding Practices**: Ensure high entropy for CSRF tokens and validate them before accepting requests. These measures can help mitigate CSRF risks while maintaining a smooth user experience.
Reasoning
The original FAQ content is accurate and comprehensive. I made minor clarifications to improve precision: changed 'Refrain from performing' to 'Don't perform' for clearer language, and clarified that `confirmationTokenRequired` can be configured 'in your page's controller or page-level settings' rather than the vague 'org's Visualforce page settings' which could be misleading about where this setting is actually configured. I selected ApexCSRF because this FAQ directly addresses CSRF vulnerabilities in Apex controllers, which is exactly what this rule detects - methods in Apex controllers that don't properly prevent CSRF attacks. The FAQ's recommendations about avoiding state changes on load and implementing proper user interactions directly relate to the patterns this rule identifies. I selected VfCsrf because the FAQ specifically mentions Visualforce pages and the `confirmationTokenRequired` setting, which is a key mechanism for CSRF protection in Visualforce that this rule validates. The FAQ's discussion of preventing unauthorized access through proper token handling in Visualforce pages directly corresponds to what this rule checks for.
Reasoning References