Sub topic 11
Enforcing Sharing, CRUD and Field-Level Security with User Mode
What this chapter assumes
You can read basic Apex and write static SOQL. You know that Salesforce controls record access with sharing rules, object permissions (CRUD) and field-level security (FLS). You have seen bind variables and allowlists in the earlier chapters.
Injection defenses do not check permissions
Bind variables stop an attacker from changing the structure of a query. They do not decide which records or fields the running user is allowed to read.
By default, Database.query runs in system mode. System mode ignores the user's object and field permissions. Sharing rules follow the class declaration: with sharing enforces them, while without sharing skips them. An allowlisted field such as Industry can still be one the user has no permission to see. In system mode the query returns it anyway.
| Check | System mode (default) | User mode |
|---|---|---|
| Sharing rules | Depends on the class keyword | Always enforced |
| Object permissions (CRUD) | Not enforced | Enforced |
| Field-level security | Not enforced | Enforced |
Passing an access level
Dynamic query methods accept an AccessLevel argument. AccessLevel.USER_MODE runs the query with the permissions of the current user.
public with sharing class AccountReader {
public static List<Account> byIndustry(String industry) {
Map<String, Object> binds = new Map<String, Object>{ 'industry' => industry };
try {
return Database.queryWithBinds(
'SELECT Id, Name, Industry FROM Account WHERE Industry = :industry LIMIT 50',
binds,
AccessLevel.USER_MODE
);
} catch (QueryException e) {
throw new AuraHandledException('You do not have access to the requested data.');
}
}
}The same argument works on the other dynamic methods:
Database.query(soql, AccessLevel.USER_MODE)for a query string without a bind map.Database.countQueryWithBinds(soql, binds, AccessLevel.USER_MODE)for counts.
You can also put the mode in the query text with WITH USER_MODE:
List<Account> accounts = Database.query(
'SELECT Id, Name FROM Account WITH USER_MODE LIMIT 50'
);What user mode does when access is missing
The two kinds of restriction behave differently.
- Sharing: records the user cannot see are left out of the result. No error is raised.
- CRUD and FLS: if the query touches an object or field the user cannot read, Apex throws a
QueryException.
User mode checks every field in the query, including fields in WHERE and ORDER BY. This matters for dynamic queries. An attacker cannot learn about a hidden field by filtering on it, because the query fails.
Catch QueryException where you can respond usefully. Return a generic message to the caller and do not echo the raw exception text, because it names the field or object involved.
Layering with the other defenses
User mode is a second layer, not a replacement for binds and allowlists.
- Bind variables keep user input as data.
- Allowlists and describe checks limit which identifiers can enter the query text.
- User mode limits what the query may read, even if the first two layers miss something.
Two related points:
Schema.SObjectField.getDescribe().isAccessible(), used in the earlier describe example, checks one field. User mode checks the whole query, including sharing, so prefer it for the final execution.- The same idea applies to DML.
Database.insert(records, AccessLevel.USER_MODE)enforces CRUD and FLS on writes.
Common mistakes
- Marking the class
with sharingand assuming FLS is enforced. The keyword covers record sharing only. - Forgetting the access level on
countQuery-style calls, so the count leaks the number of records the user cannot see. - Catching a broad
Exceptionand hiding the failure. A missing permission then looks like an empty result. - Using
without sharingon a helper class that builds dynamic queries. PassingUSER_MODEstill enforces sharing in that case, but the class keyword hides the intent. Preferwith sharing.
Exercise
A class declared with sharing runs Database.query('SELECT Id, Salary__c FROM Employee__c'). The running user has no FLS read access to Salary__c. What happens, and what change makes the query respect that restriction?
Show answer
The query succeeds and returns Salary__c, because with sharing enforces record sharing only and the query runs in system mode, which ignores FLS. Pass AccessLevel.USER_MODE (or add WITH USER_MODE to the query). The query then throws a QueryException because the user cannot read Salary__c.