Sub topic 10
String.escapeSingleQuotes: a last resort and its limits
This chapter assumes you can read basic Apex, know that Database.query runs a query string, and have seen a bind variable such as :name. Earlier chapters showed that bind variables are the main defense against SOQL injection. This chapter covers the fallback for cases where a bind variable is not possible.
What the method does
String.escapeSingleQuotes(s) adds a backslash before each single quote in s. In a SOQL string literal, \' means a quote character that belongs to the text, so the literal cannot end early.
public static List<Account> findByNameLastResort(String name) {
String safe = String.escapeSingleQuotes(name == null ? '' : name);
return Database.query('SELECT Id, Name FROM Account WHERE Name = \'' + safe + '\'');
}With the input ' OR Name != ', the escaped text stays inside the quotes. The query looks for an account whose name is that literal text, and it finds none.
Why it is only a last resort
The method solves exactly one problem: a quote breaking out of a quoted string. It leaves the query text built from user input, so any mistake in how you build that text can reopen the hole. A bind variable removes the problem differently. The value never becomes part of the query text.
The method also fails in several common situations.
| Situation | Does escapeSingleQuotes help? | Use instead |
|---|---|---|
| Value inside quotes in a WHERE clause | Yes | Bind variable (preferred) |
| Number, Id or date placed without quotes | No | Convert to a typed value, then bind |
| Field or object name | No | Allowlist or Schema describe |
| ORDER BY direction | No | Fixed set of values |
| LIKE wildcards `%` and `_` | No | Decide whether wildcards are allowed |
Failure case: unquoted values
A number has no quotes around it, so an attacker needs no quote character. Escaping changes nothing.
public static List<Account> bigAccounts(String minEmployees) {
// VULNERABLE: the value is not inside quotes, so there is nothing to escape.
String safe = String.escapeSingleQuotes(minEmployees);
return Database.query('SELECT Id, Name FROM Account WHERE NumberOfEmployees > ' + safe);
}
// Input: -1 OR Id != null
// Query: SELECT Id, Name FROM Account WHERE NumberOfEmployees > -1 OR Id != nullThe attacker's condition matches every account the user can see. The fix is to convert the input to the right type and bind it.
public static List<Account> bigAccounts(String minEmployees) {
Integer minCount = Integer.valueOf(minEmployees);
return Database.query('SELECT Id, Name FROM Account WHERE NumberOfEmployees > :minCount');
}Integer.valueOf throws a TypeException for text such as -1 OR Id != null, so the payload is rejected before any query runs.
Failure case: identifiers
Field names, object names and sort directions are not values, so escaping them is meaningless. An input such as Name DESC, Industry contains no quote and passes through unchanged. Check these against a fixed set or the Schema describe map, as covered in the previous chapters.
Failure case: LIKE wildcards
Escaping quotes does not touch % or _. A user who types % in a LIKE search matches every record. This is rarely a security hole on its own, but it can return far more rows than you intended, so decide whether wildcards are acceptable and add a LIMIT.
When it is acceptable
Use it only when all of these are true:
- The value is a string placed inside single quotes.
- A bind variable is not possible, for example because the query text comes from a framework that only accepts a finished string.
- You have considered whether LIKE wildcards need handling.
Even then, keep the escaping next to the concatenation so a later edit does not separate them.
Exercise
A developer writes 'SELECT Id FROM Contact WHERE Age__c > ' + String.escapeSingleQuotes(ageInput), where Age__c is a number field and ageInput is a String from a form. Why is this still injectable, and how would you fix it?
Show answer
The value is not inside quotes, so an attacker can type 0 OR Id != null without using a quote character, and escapeSingleQuotes leaves it unchanged. Convert the input with Integer ageValue = Integer.valueOf(ageInput); and use a bind variable in the query: 'SELECT Id FROM Contact WHERE Age__c > :ageValue'. Non-numeric input then throws a TypeException instead of reaching the query.