2021-12-13 16:18:55 +01:00
SOQL queries, just as SQL queries, are sensitive to injection attacks. An injection attack happens when a user-controlled value is inserted in a query without proper sanitization. This enables attackers to access sensitive information or even perform unauthorized data modification.
2021-04-28 16:49:39 +02:00
This rule raises an issue when one of the methods ``++Database.query++``, ``++Database.countQuery++`` is called with a string which was build by:
* calling ``++String.format++``
* concatenation (string + string)
* calling ``++String.replace++``, ``++String.replaceAll++``, ``++String.replaceFirst++``
AND the strings provided were not hardcoded nor sanitized by ``++string.escapeSingleQuotes++``
2021-04-28 18:08:03 +02:00
2021-04-28 16:49:39 +02:00
== Ask Yourself Whether
* The SOQL query is built using string formatting technics, such as concatenating variables.
* Some of the values are coming from an untrusted source and are not sanitized.
There is a risk if you answered yes to any of those questions.
2021-04-28 18:08:03 +02:00
2021-04-28 16:49:39 +02:00
== Recommended Secure Coding Practices
* Use static queries with bind variables whenever possible. This is the best way to prevent SOQL injections. Even when there is no injection possible it will at least make code review easier.
* If you really have to use dynamic queries, sanitize all values with while-listing, type-casting or ``++string.escapeSingleQuotes()++``. See the links below for examples.
2021-04-28 18:08:03 +02:00
2021-04-28 16:49:39 +02:00
== Sensitive Code Example
----
public class My {
public getContact(String firstname) {
String query = 'SELECT id FROM Contact WHERE firstname =\''+firstname+'\'';
return Database.execute(query); // Sensitive
}
}
----
2021-04-28 18:08:03 +02:00
2021-04-28 16:49:39 +02:00
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,apex]
2021-04-28 16:49:39 +02:00
----
public class My {
public getContactSafe(String firstname) {
String query = 'SELECT id FROM Contact WHERE firstname =\''+String.escapeSingleQuotes(firstname)+'\'';
return Database.execute(query); // Compliant
}
}
----
2021-04-28 18:08:03 +02:00
2021-04-28 16:49:39 +02:00
== See
2021-11-01 15:00:32 +01:00
* https://owasp.org/Top10/A03_2021-Injection/[OWASP Top 10 2021 Category A3] - Injection
2021-04-28 16:49:39 +02:00
* https://trailhead.salesforce.com/en/content/learn/modules/secure-serverside-development/mitigate-soql-injection[Prevent SOQL Injection in Your Code]
2022-07-08 13:58:56 +02:00
* https://owasp.org/www-project-top-ten/2017/A1_2017-Injection[OWASP Top 10 2017 Category A1] - Injection
2022-04-07 08:53:59 -05:00
* https://cwe.mitre.org/data/definitions/20[MITRE, CWE-20] - Improper Input Validation
2022-08-18 10:33:50 +02:00
* https://cwe.mitre.org/data/definitions/89[MITRE, CWE-89] - Improper Neutralization of Special Elements used in an SQL Command
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Make sure that formatting this SOQL query is safe here.
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]