How to Prevent Security Vulnerabilities in Salesforce Development
Introduction
Salesforce acts as the foundation for numerous businesses, facilitating effective customer relationship management (CRM), simplified processes, and innovative connections. It also enables organizations to store confidential customer information, which also requires ensuring the platform's security. Developers and administrators can protect their Salesforce environments by being aware of vulnerabilities such as SOQL Injection, Data Access Control Issues, Third-Party Content Risks, Cross-Site Scripting (XSS), and Cross-Site Request Forgery (CSRF) and taking proactive measures.
This article discusses common security vulnerabilities in Salesforce development and effective methods to avoid them.
SOQL Injection
Salesforce Object Query Language Injection occurs when attackers manipulate query strings in SOQL to gain unauthorized access to data or manipulate the database. This often happens when unvalidated user inputs are directly included in dynamic SOQL queries.
Techniques to Prevent SOQL Injection:
- Use Static Queries: Wherever possible, use static queries rather than dynamic ones. Static queries are predefined and not influenced by user input.
- Escape User Inputs: If dynamic queries are unavoidable, use String.escapeSingleQuotes() to sanitize user inputs, preventing harmful characters from being executed in the query.
- Parameterized Queries: Leverage bind variables in Apex code to ensure user inputs are treated as data rather than executable code.
- Input Validation: Implement robust input validation to restrict the format, length, and type of input users can provide.
String queryString = 'SELECT Id FROM Account WHERE Name LIKE \'%' + name + '%\')';
Database.query(queryString);
//To fix this vulnerability, static queries and variable binding are used:
String queryName = '%' + name + '%';
queryResult = [SELECT Id FROM Account WHERE Name LIKE :queryName)];
Data Access Control Issues
Improperly configured access controls can lead to unauthorized data exposure. Users or integrations may gain access to sensitive records they shouldn't view or modify.
Techniques to Prevent Data Access Control Issues:
- Enforcing Object and Field Permissions: Configure appropriate field- and object-level security to restrict access based on user roles. Object-level and field-level permissions can be enforced through code by explicitly using sObject and field describe result methods:
OBJECT-LEVEL: Schema.sObjectType.Lead.isDeletable()
FIELD-LEVEL: Schema.sObjectType.Lead.fields.Company.isUpdateable()
- Sharing Rules and Role Hierarchies: Implement sharing rules to govern how data is distributed and accessed across users and teams. Role hierarchies should align with organizational structures.
- Use “With Sharing” Keyword: In Apex classes, enforce sharing rules by using the “with sharing” keyword to ensure that the code respects access permissions defined for the user.
- Audit Access Regularly: Periodically review profiles, permission sets, and sharing rules to ensure they align with current security policies.
- Secure Retrieval and Display of Third-Party Content: Visualforce provides methods to safely display third-party content on the page such as IMAGEPROXYURL: <apex:image value="{!IMAGEPROXYURL('http://somedomain.com/pic.png')}"/>. HTML static resources can be isolated on a separate domain using iframes to protect Visualforce content from untrusted sources.
Third-Party Content Issues
Integrations and embedded third-party content may introduce vulnerabilities such as data leakage or reliance on untrusted sources.
Techniques to Prevent Third-Party Content Issues:
- Verify Third-Party Vendors: Assess the security posture of third-party applications before integration. Review their compliance certifications, penetration test reports, and data handling policies.
- Securing Sensitive Data: Salesforce provides multiple options for securing sensitive data such as passwords, encryption keys, OAuth tokens, etc. Sensitive data can be stored using the declarative features: protected custom metadata types, protected custom settings, encrypted custom fields, named credentials. Data can be programmatically secured through encryption and decryption.
- Content Security Policy (CSP): Use Salesforce’s Content Security Policy settings to control which domains can interact with your Salesforce instance.
- Sandbox Testing: Test integrations in a sandbox environment before deploying to production. This helps identify potential security issues without risking live data.
Cross-Site Scripting (XSS)
XSS vulnerabilities occur when attackers inject malicious scripts into web pages viewed by other users. In Salesforce, this often targets Visualforce pages, Lightning components, or custom UI elements.
Techniques to Prevent Cross-Site Scripting (XSS):
- Use Standard Components: Prefer standard Lightning and Visualforce components whenever possible, as these are inherently secure. All standard Visualforce components, which start with <apex> have anti-XSS filters in place.
- Strict Input Validation: Only allow specific, expected input formats and lengths, especially for form fields. Note that labels of <apex:inputField> tags are automatically escaped for security.
- Escape User Inputs: Use Salesforce-provided encoding functions to sanitize user inputs displayed in UI elements.
- Output Filters: Salesforce has implemented filters that screen out harmful characters in most output methods as one of the anti-XSS defenses.
Cross-Site Request Forgery (CSRF)
CSRF attacks exploit authenticated users by tricking their browsers into executing unwanted actions on a web application. In Salesforce, this could mean unauthorized data changes or approvals.
Techniques to Prevent Cross-Site Request Forgery (CSRF):
- Use built-in protection: Salesforce provides built-in CSRF protection mechanisms for Visualforce and Lightning pages. Ensure these settings are activated in your org.
Setup → Session Settings → CSRF Protection
- Anti-CSRF Tokens: Salesforce implements built-in anti-CSRF tokens in all its standard controllers and methods
- Limit Session Duration: Shorten session expiration times to minimize the risk of session hijacking and CSRF attacks.
- Secure Cookies: Enable HTTPS for all Salesforce interactions to ensure session cookies are transmitted securely.
Conclusion
Ensuring the security of Salesforce development requires staying alert and taking a proactive stance in identifying and addressing potential risks. Vulnerabilities can lead to severe consequences if left unaddressed. By following recommended guidelines companies can establish strong protections against possible attacks. Check out other articles on our blog, where we share insights on Salesforce optimization, best practices, and the latest industry trends. Stay informed and stay secure.

