
Introduction
In Salesforce development, writing secure and efficient code is crucial to protect against vulnerabilities like SOQL (Salesforce Object Query Language) injection. SOQL injection occurs when malicious data is injected into a query, potentially leading to unauthorized access or alteration of data. One of the simplest and most effective ways to safeguard against such vulnerabilities is by using the String.escapeSingleQuotes() method to escape user input.
In this blog, we will explore how to prevent SOQL injection using escapeSingleQuotes(), compare unsafe and safe methods for handling user input in queries, and show you how to run the code to observe the difference.
Understanding the Code
Let's break down the code example provided:
GetAccountsByName.apxcpublic class GetAccountsByName { public static List<Account> getAccountsByNameUnsafe(String userInput) { String query = 'SELECT Id, Name FROM Account WHERE Name = \'' + userInput + '\''; System.debug('Unsafe query: ' + query); return Database.query(query); // Unsafe! } public static List<Account> getAccountsByNameSafe(String userInput) { String safeInput = String.escapeSingleQuotes(userInput); String query = 'SELECT Id, Name FROM Account WHERE Name = \'' + safeInput + '\''; System.debug('Safe query: ' + query); return Database.query(query); // Safe! } }
Explanation of Methods
1. Unsafe Method (getAccountsByNameUnsafe):
The method accepts a String userInput and directly injects it into the SOQL query without any sanitization.
The resulting query can be manipulated by a user who enters malicious data, allowing for SOQL injection.
Example of dangerous user input: \' OR Name != \'
If an attacker passes the above input, the query becomes:
SELECT Id, Name FROM Account WHERE Name = '' OR Name != ''
This could lead to unintended behavior, such as retrieving all accounts or bypassing conditions.
2. Safe Method (getAccountsByNameSafe):
This method first escapes any single quotes in the user input by using the String.escapeSingleQuotes() function.
Escaping single quotes helps prevent any injected SQL code from altering the structure of the query, ensuring that the user input is treated as a string literal, not executable code.
Example of safe user input: \' OR Name != \'
The query becomes:
SELECT Id, Name FROM Account WHERE Name = '\' OR Name != \''
In this case, the single quote characters are safely escaped, and the query will function as expected without allowing injection.
The escapeSingleQuotes() Method
The String.escapeSingleQuotes() method in Apex is used to escape single quotes in a string. It's a simple yet powerful tool that ensures user input, which may contain special characters, does not interfere with the syntax of SOQL queries.
Here’s how it works:
If the input string contains a single quote ('), the method adds an extra backslash () before it, which tells the system to treat the quote as part of the string instead of a delimiter.
This is a basic security measure that helps prevent SOQL injection attacks. Without it, malicious users could manipulate the query by injecting special characters like single quotes or SQL operators.
How to Run the Code
To test the code and observe the difference between unsafe and safe methods, follow these steps:
1. Prepare User Input: Let's use the same potentially malicious input to test both methods.
Unsafe input
String userInput1 = '\' OR Name != \'';