Securing Data in Apex Controllers
Enforce Sharing Rules
When you declare a class, it’s a best practice to specify with sharing to enforce sharing rules when a component uses the Apex controller.
public with sharing class SharingClass {
// Code here
}
An @AuraEnabled Apex class that doesn’t explicitly set with sharing or without sharing, or is defined with inherited sharing, uses a default or implicit value of with sharing. However, an Apex class that doesn’t explicitly set with sharing or without sharing inherits the value from the context in which it runs. So when a class without explicit sharing behavior is called by a class that sets one of the keywords, it operates with the sharing behavior of the calling class. To ensure that your class enforces sharing rules, set with sharing.
The with sharing keyword enforces record-level security. It doesn’t enforce object-level and field-level security. You must manually enforce object-level and field-level security separately in your Apex classes.
Enforce Object and Field Permissions (CRUD and FLS)
There are a few alternatives to enforce object-level and field-level permissions in your Apex code.
- Easiest enforcement using WITH USER_MODE
- To enforce object-level and field-level permissions, use the WITH USER_MODE clause for SOQL SELECT queries in Apex code, including subqueries and cross-object relationships.
- The WITH USER_MODE clause is ideal if you have minimal experience developing secure code and for applications that don’t require graceful degradation on permissions errors.
- This example queries fields on a custom expense object with an insecure
method, get_UNSAFE_Expenses(). Don't use
this
class!
// This class is an anti-pattern. public with sharing class UnsafeExpenseController { // ns refers to namespace; leave out ns__ if not needed // This method is vulnerable because it doesn't enforce FLS. @AuraEnabled public static List<ns__Expense__c> get_UNSAFE_Expenses() { return [SELECT Id, Name, ns__Amount__c, ns__Client__c, ns__Date__c, ns__Reimbursed__c, CreatedDate FROM ns__Expense__c]; } }
- This next example uses a secure method, getExpenses(), which uses the WITH USER_MODE clause to enforce object-level and field-level
permissions. Use this class instead of UnsafeExpenseController.
public with sharing class ExpenseController { // This method is recommended because it enforces FLS. @AuraEnabled public static List<ns__Expense__c> getExpenses() { // Query the object safely return [SELECT Id, Name, ns__Amount__c, ns__Client__c, ns__Date__c, ns__Reimbursed__c, CreatedDate FROM ns__Expense__c WITH USER_MODE]; } }
- For more details, see Enforce User Mode for Database Operations in the Apex Developer Guide.
- Graceful degradation with stripInaccessible()
- For more graceful degradation on permissions errors, use the stripInaccessible() method to enforce field- and object-level data protection. This method strips the fields and relationship fields from query and subquery results that the user can’t access. You can find out if any fields were stripped and throw an AuraHandledException with a custom error message, if desired.
- You can also use the method to remove inaccessible sObject fields before DML operations to avoid exceptions and to sanitize sObjects that have been deserialized from an untrusted source.
- This example updates ExpenseController to use stripInaccessible() instead of the WITH USER_MODE SOQL clause. The results
are the same but stripInaccessible()
gives you the opportunity to gracefully degrade instead of failing on an
access violation when using WITH
USER_MODE.
public with sharing class ExpenseControllerStripped { @AuraEnabled public static List<ns__Expense__c> getExpenses() { // Query the object but don't use WITH USER_MODE List<ns__Expense__c> expenses = [SELECT Id, Name, ns__Amount__c, ns__Client__c, ns__Date__c, ns__Reimbursed__c, CreatedDate FROM ns__Expense__c]; // Strip fields that are not readable SObjectAccessDecision decision = Security.stripInaccessible( AccessType.READABLE, expenses); // Throw an exception if any data was stripped if (!decision.getModifiedIndexes().isEmpty()) { throw new AuraHandledException('Data was stripped'); } return expenses; } }
- For more details and examples, see Enforce Security with the stripInaccessible Method in the Apex Developer Guide.
- Legacy code using DescribeSObjectResult and DescribeFieldResult methods
- Before the WITH USER_MODE clause and stripInaccessible() method were available, the only way to enforce object and field permissions was to check the current user’s access permission levels by calling the Schema.DescribeSObjectResult and Schema.DescribeFieldResult methods. Then, if a user has the necessary permissions, perform a specific DML operation or a query.
- For example, you can call the isAccessible, isCreateable, or isUpdateable methods of Schema.DescribeSObjectResult to verify whether the current user has read, create, or update access to an sObject, respectively. Similarly, Schema.DescribeFieldResult exposes access control methods that you can call to check the current user’s read, create, or update access for a field.
- This example uses the describe result methods. This approach requires many
more lines of boilerplate code so we recommend using the WITH USER_MODE clause or stripInaccessible() method
instead.
public with sharing class ExpenseControllerLegacy { @AuraEnabled public static List<ns__Expense__c> getExpenses() { String [] expenseAccessFields = new String [] {'Id', 'Name', 'ns__Amount__c', 'ns__Client__c', 'ns__Date__c', 'ns__Reimbursed__c', 'CreatedDate' }; // Obtain the field name/token map for the Expense object Map<String,Schema.SObjectField> m = Schema.SObjectType.ns__Expense__c.fields.getMap(); for (String fieldToCheck : expenseAccessFields) { // Call getDescribe to check if the user has access to view field if (!m.get(fieldToCheck).getDescribe().isAccessible()) { // Pass error to client throw new System.NoAccessException(); } } // Query the object safely return [SELECT Id, Name, ns__Amount__c, ns__Client__c, ns__Date__c, ns__Reimbursed__c, CreatedDate FROM ns__Expense__c]; } }