Security Vulnerabilities
In the realm of Salesforce, where data integrity and security are paramount, it is imperative to address and fortify against common vulnerabilities. This post serves as an illuminating exploration into prevalent security risks such as SOQL injection, Cross-Site Scripting (XSS), Cross-Site Request Forgery (CSRF) and others that may compromise the robustness of applications within the Salesforce platform.
SOQL Injection
SOQL injection is a technique that is used to inject values to manipulate the construction and outcome of a dynamic SOQL query.
SOQL injection can potentially occur when user-supplied input is used to construct a dynamic SOQL statement.
The user input can modify the intended SOQL statement and result in unintended or harmful results.
Techniques to Prevent SOQL Injection
String exampleString = '\' Hello World\'';
System.debug(exampleString );
String escapedStr = String.escapeSingleQuotes(exampleString );
System.debug(escapedStr);
Cross-Site Request Forgery
Cross-site request forgery (CSRF) performs an action via a URL that takes advantage of the active session of anauthenticated user.
An attacker includes a URL on their site that performs an action on a second site.
If the user is still authenticated to the second site, the action may be successful, if there is no protection.
Considerations regarding protection from CSRF
Salesforce implements built-in anti-CSRF tokensin all its standard controllers and methods, but Custom controllers can become vulnerable to CSRF attacks.
Cross-Site Request Forgery Example
Cross-Site Scripting
Cross-site scripting is when malicious content such as JavaScript / VBScript / HTML is inserted into a web page and executed.
The script can take advantage of the user's session and use it to submit transactions, read data, or alter the page using HTML / CSS.
Considerations regarding protection from XSS
Controlling Data Access
Apex class executes in system mode and may expose sensitive data to unintended users.
Using the 'with sharing' keyword ensures that the permissions of the currently logged in user are used.
The 'inherited sharing' keyword can be specified on an Apex class to allow the class to run in the sharing mode of the class that called it.
The Apex class that uses the 'inherited sharing' keyword runs as 'with sharing' when used as a Visualforce page controller, Apex REST service, or an entry point to an Apex transaction.
Recommended by LinkedIn
@AuraEnabled Apex classes used by Aura components or Lightning web components that do not specify "with sharing" or "without sharing" will default to "with sharing" to ensure that Lightning components are secure by default.
The WITH SECURITY ENFORCED clause, which enforces field and object level security permissions, can be added to a SOQL statement which will cause the query to throw a System.QueryException if the current user does not have access to a field or object that is referenced in the SOQL statement.
The Security.stripInaccessible Apex method can be used to remove fields from SOQL query results that the current user does not have access to and avoid exceptions when a DML operation is performed.
Enforcing Object and Field Permissions
Object-level and field-level permissions can be enforced through code by explicitly using sObject and field describe result methods.
The following describes some of these methods:
OBJECT-LEVEL
FIELD-LEVEL
Secure Retrieval and Display of Third-Party Content
Visualforce provides methods to safely display third-party content on the page.
Visualforce pages can be displayed on trusted external domains using iframes by allowing it in Session Settings in Setup.
System/User Mode Operations
Salesforce allows explicitly specifying the running context when performing a SOQL query.
SOQL USER MODE
To explicitly perform a SOQL query in the context of the current user, the WITH USER_MODE clause is used. The field-level security settings, object permissions, and sharing rules are applied. For example:
List<Account> accs = [SELECT Id FROM Account WITH USER_MODE];
SOQL SYSTEM MODE
To explicitly perform a SOQL query in system context, the WITH SYSTEM_MODE clause is used. In this case, the field-level security settings and object permissions are ignored, and the sharing keyword defined on the Apex class determines whether sharing rules are applied. For example:
List<Account> allAccounts = [SELECT Id FROM Account WITH SYSTEM_MODE];
Salesforce also allows explicitly specifying the running context when performing a DML operation.
STATIC DML USER MODE
To explicitly execute a static DML statement in user mode, the “as user” keywords can be added to the statement. For example:
Account myAccount = new Account(Name='ACME');
insert as user myAccount;
STATIC DML SYSTEM MODE
To explicitly execute a static DML statement in system mode, the “as system” keywords can be added to the statement. For example:
List<Account> allAccounts = [SELECT Id FROM Account WITH SYSTEM_MODE];
update as system allAccounts;