Sub topic 10
Why Apex Does Not Enforce FLS by Default
This chapter assumes you know what field-level security (FLS) is, how profiles and permission sets grant Read and Edit on fields, and basic Apex and SOQL syntax. It explains why Apex ignores FLS unless you ask it to enforce it. The next chapters cover the tools that enforce it.
System mode: the default
Apex normally runs in system mode. In this mode the code has access to every object and field, whatever the running user's profile or permission sets say. Salesforce designed it this way so that triggers, batch jobs, and automation can do their work without every user needing access to every field they touch.
Sharing rules are a separate matter. The with sharing keyword controls record visibility. It does not control field access. A with sharing class still reads and writes fields the user cannot see.
What this looks like in practice
Suppose Internal_Notes__c is hidden from a user's profile. This code returns the field's value to that user anyway:
public with sharing class AccountService {
@AuraEnabled
public static List<Account> getAccounts() {
return [SELECT Id, Name, Internal_Notes__c FROM Account LIMIT 10];
}
}The class uses with sharing, but the query runs in system mode. Every returned record includes Internal_Notes__c. If a Lightning component calls this method, the hidden value reaches the browser.
public with sharing class AccountEditor {
public static void setNotes(Id accountId, String notes) {
Account acc = new Account(Id = accountId, Internal_Notes__c = notes);
update acc;
}
}The update succeeds even if the user has no edit access to the field.
What is and is not enforced
| Feature | Respects FLS automatically? |
|---|---|
| Standard UI pages, reports, list views | Yes |
| Standard APIs such as REST and SOAP | Yes |
| Apex SOQL and DML | No |
| Apex `with sharing` | No (record sharing only) |
Why this is a security risk
Any Apex that returns data to a user, or accepts input from one, can leak or overwrite fields the user should not touch. This applies to @AuraEnabled methods, @RestResource classes, Visualforce controllers, and invocable actions. Security scanners and the AppExchange security review flag unchecked queries and DML for this reason.
Some Apex should stay in system mode. An example is a trigger that stamps an internal field no user should edit. The rule is to choose system mode on purpose. Enforce FLS wherever code acts on behalf of a user.
Ways to enforce FLS
Apex gives you three approaches, each covered in the following chapters:
- User-mode queries and DML with
WITH USER_MODEandAccessLevel.USER_MODE. Security.stripInaccessibleto remove fields the user cannot access.- Schema describe checks such as
isAccessible()andisUpdateable().
Exercise
A class is declared with sharing and runs SELECT Id, Internal_Notes__c FROM Account. The running user cannot read Internal_Notes__c. Does the query return the field, and why?
Show answer
Yes, it returns the field. with sharing only enforces record-level sharing rules. Apex SOQL runs in system mode, which ignores FLS unless you use WITH USER_MODE, Security.stripInaccessible, or a describe check.