Skip to main content

Best Practices to Avoid Security Vulnerabilities in Salesforce Development

About Us
Published by JET BI
21 November 2024
17

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.


Anzhelika Makarova
Junior Salesforce Developer
image
Expertise
Question to the expert
image

We have available resources to start working on your project within 5 business days

1 UX Designer

image

1 Admin

image

2 QA engineers

image

1 Consultant

image
Related Articles
All articles
image
How to Customize Salesforce Field Service Mobile for Your Technicians
Salesforce Field Service Mobile provides a standard technician experience that companies can extend to fit their own service processes. This article looks at areas where a custom mobile workflow can help: AI repair assistance, field documentation, external tools, back-office integrations, quotes and payments on site, barcode scanning and role-based mobile layouts. It also outlines four practical steps for planning a mobile workflow around the technician’s work.
21 October 2026
image
Salesforce Field Service Mobile: New Capabilities for Technicians
Salesforce Field Service Mobile continues to evolve with new capabilities for technicians working in the field. Explore how updates to job preparation, mobile forms, voice input, mapping, and Work Order views are changing the technician experience, and learn why configuring these capabilities around your service operations matters.
14 October 2026
image
Closing the Gap Between Field Service and Business Needs
Standard Salesforce Field Service configuration covers many service processes, but individual businesses can have different workflows, working conditions, scheduling rules, and operational requirements. This article looks at three areas where standard configuration can fall short: how technicians work, the conditions of field service operations, and business-specific processes. It also provides a practical checklist of signs that a Salesforce Field Service setup could benefit from customization.
07 October 2026