Sub topic 1
Why Apex Ignores User Permissions by Default
Prerequisites
This chapter assumes you can read basic Apex classes and write simple SOQL queries and DML statements. You should know what a profile and a permission set are. You should also know that object permissions (CRUD) and field-level security (FLS) control what a user can see and change in the Salesforce UI. You don't need any prior knowledge of user mode. This chapter explains the problem that user mode solves.
Apex runs in system mode
Apex code runs in system mode by default. In system mode, the code does not check the running user's object permissions or field-level security. If your code queries a field, the query returns it. This is true even when the user's profile hides that field.
The same applies to DML. If the code inserts a record, the platform does not check whether the user has Create permission on that object.
This is a design choice. Apex is often used for automation, such as triggers and batch jobs, where the code must work no matter who started it. But the same behavior becomes a security gap when the code handles requests from a user, for example in a Lightning component or an API endpoint.
A concrete example
Consider this class. It returns accounts and exposes the AnnualRevenue field.
public with sharing class RevenueController {
@AuraEnabled
public static List<Account> getTopAccounts() {
return [
SELECT Id, Name, AnnualRevenue
FROM Account
ORDER BY AnnualRevenue DESC
LIMIT 10
];
}
}Suppose a user's profile has no access to AnnualRevenue. The standard Account page hides that field from this user. But this query runs in system mode, so it still returns the revenue values. The Lightning component then displays them. The user sees data that the admin set up the org to hide.
Notice that the class is declared with sharing. That keyword does not prevent this problem, as the next section shows.
What `with sharing` does
The with sharing keyword controls record-level access. It makes the query respect sharing rules, role hierarchy, and organization-wide defaults. A user then sees only the rows they are allowed to see.
It does not control which objects or fields the user can access. The next table compares the two kinds of control.
| Question | Controlled by | Enforced by `with sharing`? |
|---|---|---|
| Which records can the user see? | Sharing rules, role hierarchy, organization-wide defaults | Yes |
| Can the user read or create this object? | Object permissions (CRUD) | No |
| Can the user read or edit this field? | Field-level security (FLS) | No |
In the example above, with sharing limits the rows to those the user can see. The AnnualRevenue values in those rows are still returned in full.
How the layers fit together
A request from a user passes through several checks. Apex in system mode skips two of them.
The diagram shows that no path through system mode checks CRUD or FLS. You must add those checks yourself or ask the platform to apply them.
Common errors
These mistakes are common when developers first learn this behavior.
- Assuming
with sharingis enough. It limits rows only. Hidden fields and inaccessible objects remain open. - Testing only as an administrator. Admins usually have access to everything, so the gap never shows up. Test with a restricted user.
- Trusting the UI to hide data. A component can hide a field, but the Apex method behind it can still be called with the data it returns. Anyone who calls the method directly receives whatever it returns.
- Assuming the platform will block the query. It will not. System mode produces no error, so the problem stays silent.
What comes next
Later chapters show how to close this gap. User mode makes a query or DML statement respect the running user's permissions. The next chapter separates sharing from CRUD and FLS in more detail, and the chapters after it show the syntax.
Exercise
A class is declared with sharing. It runs a SOQL query that selects a field the running user cannot read because of field-level security. Does the query return the field's value, and why?
Show answer
Yes, the query returns the value. Apex runs in system mode by default, and with sharing only enforces record-level sharing rules. It does not enforce object permissions or field-level security, so the hidden field is still returned.
Exercise
Why is testing a permission-related change only as a system administrator a poor check?
Show answer
Administrators typically have access to all objects and fields, so a missing permission check would not show up. A test with a restricted user is needed to see whether the code exposes data it should not.