Sub topic 3
How SOQL injection works and what an attacker can change
This chapter assumes you can read Apex classes and static SOQL, and that you know Database.query builds a query from a string. It also assumes you know the basics of sharing and field-level security (FLS).
The root cause: input becomes query text
SOQL injection happens when untrusted input is concatenated into a query string. The platform parses the finished string. It cannot tell which characters you wrote and which the user typed.
public with sharing class AccountSearch {
// VULNERABLE: user input is concatenated into the query string.
public static List<Account> findByName(String name) {
String soql = 'SELECT Id, Name FROM Account WHERE Name = \'' + name + '\'';
return Database.query(soql);
}
}
// Input: ' OR Name != '
// Query: SELECT Id, Name FROM Account WHERE Name = '' OR Name != ''The developer meant the quotes to mark the edges of a value. The attacker's own quote closes the literal early, and the rest of the input becomes query syntax. The condition Name = '' OR Name != '' is true for almost every row, so the query returns every account the running user can see, not one named account.
What an attacker can change
Injection lets the attacker rewrite any part of the query that your code builds from their input. The typical changes are:
| Target | Example payload effect | Result |
|---|---|---|
| WHERE clause | Add `OR` with an always-true condition | Returns more records than intended |
| WHERE clause | Add `AND` with a subquery or a condition on another field | Tests guesses about hidden data, one yes/no answer at a time |
| LIMIT or ORDER BY | Remove, replace, or append clauses | Changes how many rows come back and in what order |
| Field list | Add fields to the SELECT, if input is placed there | Exposes fields you did not plan to return |
Input placed in an identifier position, such as a field name after ORDER BY, is also exploitable. Quotes are not needed there, so quote-based defenses do not help.
The limits of SOQL injection
SOQL is read-only. A single Database.query call runs one SELECT statement. It cannot run a second statement, so classic SQL attacks like ; DROP TABLE do not apply. It also cannot update or delete records.
Read-only access can still be serious. The attacker gets records the page was never meant to show, such as other customers' accounts or contacts. The attack is more dangerous if the query is also unconstrained in other ways:
Database.queryignores object permissions and FLS by default, because it runs in system mode. A user can then read objects and fields they have no permission to see.- A class declared
without sharingignores record-level sharing, so the attacker sees every record in the org. - A
with sharingclass limits results to records the user can see, but it still allows reading beyond the UI's intended filter.
Where the input comes from
Treat any value that a user can influence as untrusted. This includes:
- Parameters of
@AuraEnabledmethods and@RemoteActionmethods. - Visualforce page parameters and Experience Cloud URL parameters.
- Values stored earlier by a user, for example a record field that is later used to build a query.
- Inbound REST or SOAP request data.
Developers often trust values that come from a picklist in the UI. Methods exposed to Lightning components can be called directly, and the browser's controls can be bypassed. Validate on the server.
Spotting the pattern in code review
A query is suspect when all of these are true:
- It is passed to
Database.queryorDatabase.countQueryas a string. - The string is built with
+orString.format. - Some part of the string traces back to a method parameter, a URL parameter, or stored user data.
A query with a fixed string and a :variable bind does not match this pattern. The next chapters cover that fix and the harder cases, such as field names.
Exercise
Exercise
A method builds 'SELECT Id FROM Contact WHERE LastName = \'' + lastName + '\'' and runs it with Database.query. The caller passes ' OR LastName != '. What query is executed, and what does the attacker gain? Can the attacker delete records with this payload?
Show answer
The query becomes SELECT Id FROM Contact WHERE LastName = '' OR LastName != ''. The condition matches almost every contact, so the attacker gets all contacts the running user can see (and in system mode, regardless of FLS), not only one last name. The attacker cannot delete records. SOQL is read-only and the call runs a single SELECT, so the harm is unintended data exposure.