How should one prevent soql injection when using dynamic soql?
Click on the arrows to vote for the correct answer
A. B. C. D.D.
When using dynamic SOQL, it is important to take precautions to prevent SOQL injection attacks. SOQL injection occurs when an attacker is able to inject unexpected and potentially harmful code into a SOQL statement, which can lead to data theft or destruction.
To prevent SOQL injection when using dynamic SOQL, the best practice is to utilize the String.escapeSingleQuotes(string) method. This method properly escapes any single quotes in the string, preventing an attacker from being able to inject additional code.
For example, consider the following dynamic SOQL statement:
javascriptString query = 'SELECT Name FROM Account WHERE Industry = \'' + searchTerm + '\''; List<Account> accounts = Database.query(query);
In this case, if the searchTerm variable contained a single quote, an attacker could inject additional code into the statement. To prevent this, we can modify the statement to use the escapeSingleQuotes method:
javascriptString query = 'SELECT Name FROM Account WHERE Industry = \'' + String.escapeSingleQuotes(searchTerm) + '\''; List<Account> accounts = Database.query(query);
This ensures that any single quotes in the searchTerm variable are properly escaped, and prevents the injection of additional code.
Option A, XMLStreamWriter and XMLStreamReader, are not related to preventing SOQL injection when using dynamic SOQL. These classes are used for reading and writing XML data.
Option B, Messaging.InboundEmailHandler, is a class used to handle inbound email messages and is not related to preventing SOQL injection.
Option C, the "with sharing" keyword, is used to enforce sharing rules when querying data. While it is important to use this keyword when appropriate, it is not related to preventing SOQL injection when using dynamic SOQL.