Sub topic 10
Why Apex Does Not Enforce FLS by Default
This chapter assumes you know Salesforce objects, fields, profiles, and permission sets. It also assumes you can read basic Apex and SOQL. You should already know what Read and Edit field access mean in Setup. Here you will learn why Apex code can ignore that access, which parts of security it does enforce, and the mistakes this causes.
The default: system mode
Apex runs in system mode by default. In system mode the code can read and write any object and field in the org. It ignores the running user's object permissions and field-level security (FLS). The code runs with the access of the platform itself, not with the access of the person who triggered it.
A simple example shows the effect. Suppose a user's profile has no Read access to Account.AnnualRevenue. This query still returns the value:
List<Account> accounts = [
SELECT Id, Name, AnnualRevenue
FROM Account
WHERE Industry = 'Technology'
];
System.debug(accounts[0].AnnualRevenue);The user never sees an error. The field value is in the result, and anything the code does with it, such as returning it to a component, can expose it.
Why Salesforce designed it this way
System mode is a deliberate choice. Apex often has to do work on behalf of users that the users could not do themselves. Common examples:
- A trigger that rolls up values from child records the user cannot see.
- A scheduled job or batch job that has no interactive user at all.
- An automation that stamps an internal field, such as an approval score, which no user should edit by hand.
- Integration code that must write to fields hidden from every profile.
If Apex enforced FLS automatically, these patterns would break whenever an admin tightened a field's access. Developers therefore have to decide, case by case, whether code should act as the user or as the system. The platform leaves that decision to you.
What Apex does and does not enforce
"System mode" is not the same as "no security at all." Different controls behave differently.
| Control | Enforced in default Apex? | Notes |
|---|---|---|
| Field-level security | No | Fields are readable and writable regardless of the user's FLS. |
| Object permissions (CRUD) | No | Code can query and modify objects the user cannot access. |
| Record sharing | Depends on the class keyword | `with sharing` enforces sharing rules. `without sharing` does not. |
| Validation rules and required fields | Yes | Data rules still apply to DML. |
The third row is a frequent source of confusion. The with sharing keyword on a class affects only record-level access, which controls which rows the user can see. It does not turn on FLS or object permissions. A class declared with sharing can still leak a field the user is not allowed to read.
public with sharing class AccountService {
@AuraEnabled(cacheable=true)
public static List<Account> getAccounts() {
// Sharing rules apply to which rows are returned.
// FLS does NOT apply. AnnualRevenue is returned for every row.
return [SELECT Id, Name, AnnualRevenue FROM Account LIMIT 50];
}
}The keywords with sharing, without sharing, and inherited sharing are covered by sharing rules. They are a separate mechanism from the user-mode features described next.
A picture of the access path
The diagram below shows what happens when a user action reaches Apex, and which checks are skipped in system mode.
Where the gap becomes a vulnerability
Because the platform does not stop you, the risk appears in code that returns data to a person. The usual routes are:
- An
@AuraEnabledmethod returns SObjects or wrapper objects to a Lightning component. The component can display or log any field in the response. - A Visualforce controller or an Apex REST resource serializes records to a page or caller.
- Code accepts a field name from the client and builds a dynamic query from it.
- An
updateruns on records that the client modified, including fields the user is not allowed to edit.
Writes have the same problem as reads. If a component sends back a record with a changed Internal_Notes__c value, and Apex runs update in system mode, the change is saved even when the user has no Edit access to that field.
public with sharing class AccountController {
@AuraEnabled
public static void saveAccount(Account acc) {
// Saves every field in the object, even fields the user cannot edit.
update acc;
}
}Edge cases and common mistakes
- Assuming the UI protects you. Standard record pages enforce FLS, but your Apex method is an independent entry point. A user or script can call it directly through the API or a custom component.
- Treating
with sharingas a complete fix. It controls rows only, as shown above. - Checking one field and returning many. If you check access to one field but return the whole record, the unchecked fields still leak.
- Forgetting indirect access. A formula field, or a relationship such as
Account.Owner.Emailin a query, reads data from another object. Make sure the access model you use covers those paths too. - Assuming tests will catch it. Tests usually run as an administrator. Without
System.runAsfor a restricted user, a test passes even when the code leaks data. Testing is covered in a later chapter. - Treating system mode as always wrong. Some code should run in system mode, such as a rollup trigger. The mistake is using it by accident, not by decision.
Deciding which mode a piece of code needs
Use a simple question: "Is this code acting for the person who triggered it, or for the system?" Then choose accordingly.
| Situation | Typical choice |
|---|---|
| Query or save in response to a user action in the UI | Enforce the user's access |
| Return data to a Lightning component or an external caller | Enforce the user's access |
| Trigger, batch, or scheduled job maintaining internal data | System mode, with a documented reason |
| Integration writing to restricted fields | System mode, with a restricted entry point |
When you do choose system mode, keep it narrow. Put the privileged work in a small, well-named method, and avoid returning its raw results to a user.
The next chapters cover the tools for enforcing access: user-mode SOQL and DML, Security.stripInaccessible, and schema describe checks. Each one lets you apply the user's field access at a specific point in the code.
Exercise
A class is declared public with sharing class ReportService. One of its @AuraEnabled methods runs [SELECT Id, Name, Internal_Notes__c FROM Account] and returns the result. A user whose profile has no Read access to Internal_Notes__c calls the method. Does the user receive the field value? Explain which control applies and which does not.
Show answer
Yes, the user receives the value. with sharing enforces record-level sharing rules, so only rows the user can see are returned. It does not enforce field-level security or object permissions. The query runs in system mode, so Internal_Notes__c is included in the result for those rows.
Exercise
Name two legitimate reasons Apex runs in system mode by default, and one risk that comes from this default.
Show answer
Legitimate reasons include triggers that roll up data from records the user cannot see, and batch or scheduled jobs that have no interactive user. Another is writing internal fields that no profile should edit. The risk is that code returning data or saving client-supplied records can expose or change fields the user has no permission to read or edit.