Close this search box.

Strengthening Salesforce: How to Protect Your Org from SOQL Injection

Red Argyle logo

It’s a sign of the times, on March 25th, 2024, the Federal Bureau of Investigation (FBI) and the Cybersecurity and Infrastructure Security Agency (CISA) have come together to issue a  “Security by Design” alert to eliminate SQL Injection vulnerabilities in software. This collaboration marks a notable milestone in cybersecurity awareness today, cybersecurity is an ever-evolving field, with new threats emerging as quickly as technology advances. 

Organizations of all sizes find themselves potential targets for cyberattacks, ranging from data breaches to sophisticated SQL Injection attacks. The stakes are high, as these threats not only compromise sensitive data but can also impact trust and business continuity.

In this article, I intend on taking you through what this alert is about, what SQL Injection means and how it applies to Salesforce, what your developers can start doing now, to help and lastly, stepping back and zooming out to a more strategic means to view what’s at the heart of this alert, ensuring a strong security posture for your entire Salesforce implementation, not just one facet of threat.

What Does The Alert Mean?

The core message of the release is straightforward in that security should be an integral part of the design process, not an afterthought. This principle is especially important in today’s tech landscape, where threats are becoming increasingly sophisticated. For Salesforce implementations, which often handle sensitive customer data and critical business processes, this alert is not just advice, it should be part of a blueprint for safeguarding the future. The platform’s security features are robust, but there is shared responsibility within cloud computing that means organizations must also take proactive steps to safeguard their Salesforce instances. It’s here that cybersecurity for Salesforce becomes a specialized field of its own, requiring expertise in both the platform’s unique capabilities and the broader principles of  defense.

This recent alert, focused in on SQL Injection vulnerability, carries specific relevance to Salesforce environments, despite their unique architecture and query language, SOQL (Salesforce Object Query Language).

Understanding SQL Injection in the Context of Salesforce

SQL Injection involves inserting or “injecting” malicious SQL statements into an entry field for execution, to manipulate a database in ways the developers did not intend.  Within the Salesforce ecosystem this translates to SOQL Injection. 

Custom development work in Salesforce introduces specific security considerations. Apex allows for the creation of complex business logic but also opens avenues for potential vulnerabilities, such as SOQL Injection. While SOQL is designed with safeguards to prevent traditional SQL Injection, SOQL Injection remains a potential threat when dynamic query strings are leveraged without proper precautions. Understanding and mitigating these risks are crucial for maintaining the integrity and security of the Salesforce platform.

In today’s landscape businesses no longer seek or desire but require flexibility and adaptability in their systems, dynamic SOQL allows for runtime construction of queries based on variable conditions and user inputs. This capability enhances customizability, supports agile response to changing business requirements, and improves data handling for complex or large datasets.

In Salesforce, SOQL Injection could allow attackers to construct queries that access, modify, or delete data they shouldn’t be able to, potentially leaking sensitive information or compromising data integrity. Given Salesforce’s operational role in many organizations, the impact of such an attack can be massive, affecting not just data security but also customer trust and compliance with data protection regulations.

What Can Salesforce Managers, Administrators and Developers do?

For Salesforce managers, administrators and developers, understanding the nuances of SOQL Injection and the broader implications of the alert is crucial. It underscores the need for a security-conscious development culture, where security considerations are part of the design and development process, and not an afterthought.

Protecting Salesforce environments against cyber threats, and understanding and implementing secure Apex coding practices is important. The “Apex in Action” section will open the conversation into safeguarding your custom code from SOQL Injection vulnerabilities. However, it’s important to recognize that securing Apex code, while essential, is just one piece of the puzzle.

Comprehensive protection extends beyond the code itself, encompassing an approach to security that includes everything from user education and access controls to regular security audits and adherence to best practices. This section should serve as a starting point to explore  all strategies required to secure your Salesforce environment fully.

Apex in Action

  • Always use bind variables in SOQL query strings. Apex will automatically sanitize these variables to prevent SQL Injection.
String name = 'a name';
List<Account> accounts = [SELECT Id FROM Account WHERE Name = :name];
  • Always use parameterized queries when writing dynamic SOQL statements. This makes sure that user inputs are treated as data, not executable code.
String searchQuery = 'Test';
searchQuery = String.escapeSingleQuotes(searchQuery);
String queryString = 'SELECT Id FROM Account WHERE Name LIKE \'%' + searchQuery + '%\'';
List<Account> accounts = Database.query(queryString);
  • Validate user inputs for type, length, and format, rejecting any inputs that don’t meet the predefined criteria. This step can prevent SOQL Injection and other potential security vulnerabilities.
public with sharing class ContactSearchController {

// Method to search contacts by name with input validation
public static List<Contact> searchContactsByName(String searchName) {
// Validate the search input for type, length, and format
if (String.isBlank(searchName)) {
throw new IllegalArgumentException('Search name cannot be empty.');

// Check length of the input to ensure it doesn't exceed 40 characters
if (searchName.length() > 40) {
throw new IllegalArgumentException('Search name must be 40 characters or fewer.');

// Regular expression to validate that the searchName contains only letters and spaces
Pattern validPattern = Pattern.compile('^[a-zA-Z\\s]+$');
Matcher matcher = validPattern.matcher(searchName);
if (!matcher.matches()) {
throw new IllegalArgumentException('Search name can only contain letters and spaces.');

// Constructing a dynamic query with the validated input
String query = 'SELECT Id, Name, Email FROM Contact WHERE Name LIKE :searchName';
searchName = '%' + searchName + '%'; // Modifying the searchName to use in SOQL LIKE operator
List<Contact> contacts = Database.query(query);

return contacts;
  • Limit the permissions of the Salesforce user accounts to the minimum necessary. Full stop. While I don’t have a coding example to share here, this is a necessary and core principle that we always ensure our clients follow, even in high trust, default to open company cultures.

Technically speaking, by integrating security measures from the very beginning of the design and development processes, Salesforce administrators and developers can significantly mitigate the risk of such attacks. In Salesforce environments, this means adopting best practices for secure coding and adopting security as a core tenet of code quality. This means ongoing education, and creating and maintaining a culture where security is everyone’s responsibility.

Beyond SOQL Injection….Navigating To a More Proactive Approach 

Navigating towards a proactive security stance requires a blend of strategic foresight and technical diligence. The shift from reacting to security incidents to preventing them proactively is not merely a change in tactics but a transformation in how security is perceived and implemented within your entire organization. This transition hinges on both overarching strategies and specific technical actions that reinforce the security posture of your Salesforce environment.

At the heart of a proactive approach is embedding security considerations into every phase of the development lifecycle, from initial design through to deployment and maintenance. It involves a commitment to ongoing education, ensuring that every team member, from developers to executives, understands their role in maintaining security. This culture ensures that security is not seen as a hindrance but as an integral component of code quality and operational excellence.

Transitioning to a proactive security posture is about marrying strategy with action. It’s about making security a foundational element of your organizational culture and technical practices. By adopting specific, actionable steps into your regular workflows, you position your organization to not only address current security challenges but also to anticipate and mitigate future risks. 

How Red Argyle can Support Your Journey

Given the complexities and evolving nature of cybersecurity threats, particularly in the Salesforce ecosystem, it’s crucial to have a partner that can navigate the intricacies of securing your environment effectively. 

At Red Argyle, we find that the best strategic first step in becoming more proactive is to establish where you’re starting from by conducting a full Salesforce Cybersecurity Audit.

Our expertise lies in comprehensive evaluation, solution development, and implementation of security measures for many organizations, even those with stringent security and compliance needs. Our approach is refined to swiftly conduct audits of Salesforce environments, proposing improvements that bolster security without disrupting user adoption or daily operations.

A Security Audit Project has the following goals:

  • We aim to deepen our client’s comprehension of their unique security requirements within their Salesforce environment. This understanding is crucial for tailoring security strategies that align with specific organizational needs.
  • To empower clients with the insights needed to proactively manage their cybersecurity posture, ensuring their Salesforce environment is both secure and compliant.
  • Development of an actionable roadmap. This tailored plan outlines clear steps for future projects to address identified security gaps, ensuring a strategic approach to enhancing your Salesforce environment’s security.

What We Offer

Policy and Procedure Review – We conduct thorough evaluations of your existing security policies and procedures, identifying and addressing gaps. This includes reviewing implementations of policies such as passwords, incident response processes, and staff onboarding and off-boarding protocols within Salesforce.

Technical Security Assessment – Utilizing a blend of standard and proprietary tools, we analyze your organization’s Salesforce configurations and security posture. Our unique methodologies involve extracting metadata to detect patterns and potential threats.

Penetration Testing – Our team employs advanced techniques to probe your Salesforce communities. This includes both unauthenticated and authenticated penetration testing to reveal vulnerabilities in data exposure, schema, and Apex.

Code and Endpoint Analysis – We conduct extensive reviews of Apex code and your endpoints. Our static code analysis spans across Apex, triggers, Visualforce pages, and components, uncovering patterns and vulnerabilities that need attention.

Customized Exploration – Recognizing that each organization’s Salesforce instance is unique, we tailor our approach to align with your specific needs. Our scans and analyses guide us through a journey of discovery, pinpointing issues and weaknesses within your org.

Comprehensive Reporting – Our final deliverable includes a detailed report of our findings and an executive summary. This report provides prioritized recommendations and, where necessary, a roadmap for implementing these changes.

In partnering with us, you gain more than just a service provider; you gain a collaborator dedicated to securing your Salesforce environment. Our holistic approach ensures that security is not just a technical requirement but a strategic advantage for your organization, enabling you to operate with confidence in today’s digital landscape.


Red Argyle logo
Red Argyle logo

Related Blog Posts