Sub topic 2
Sharing is not the same as CRUD and FLS
This chapter assumes you can read basic Apex and SOQL, know what profiles and permission sets are, and have seen the terms CRUD and field-level security (FLS) in Setup. It separates two security layers that developers often treat as one. The next chapter builds on this when it describes what user mode enforces.
Two questions, two mechanisms
Salesforce answers two different questions when a user touches data.
- Which records can this user see or change? Sharing controls this. It covers organization-wide defaults, the role hierarchy, sharing rules, manual shares, teams, and record ownership.
- Which objects and fields can this user work with at all? Object permissions (CRUD) and field-level security (FLS) control this. They come from profiles and permission sets.
The two layers are independent. A user can have access to a record through sharing and still be blocked from the object or from one of its fields. A user can also have full CRUD on Account and still not see a particular account, because sharing hides it.
| Layer | Question it answers | Configured with | Granularity |
|---|---|---|---|
| Sharing | Can the user access this specific record? | Org-wide defaults, roles, sharing rules, teams, manual shares | Row |
| Object permissions (CRUD) | Can the user create, read, edit, or delete records of this object? | Profiles, permission sets | Object |
| Field-level security (FLS) | Can the user read or edit this field? | Profiles, permission sets | Column |
A useful picture is a spreadsheet. Sharing decides which rows you get. CRUD decides whether you may open the sheet. FLS decides which columns you see.
What the sharing keywords do
Apex classes declare how they treat sharing. There are three keywords.
with sharingmakes the class respect the running user's sharing rules for SOQL and DML on records.without sharingmakes the class ignore sharing rules.inherited sharingmakes the class use the sharing mode of whatever called it. If it is the entry point, it behaves aswith sharing.
If you write no keyword, the behavior depends on how the class is called. A class with no declaration runs with the sharing mode of its caller when it is called from another class. A class that is an entry point (for example, an Apex REST endpoint or a trigger) runs without sharing. Because that default is easy to misjudge, declare the keyword explicitly on every class.
Now the key point. These keywords affect rows only. They do not check object permissions, and they do not check FLS.
A class that looks safe and is not
Consider this class.
public with sharing class AccountSalaryReader {
public static List<Account> getAccounts() {
return [
SELECT Id, Name, AnnualRevenue
FROM Account
LIMIT 50
];
}
}The class is declared with sharing, and many developers read that as "this respects the user's permissions". It does less than that.
- The query returns only accounts the user can see through sharing. That part works.
- The query returns
AnnualRevenueeven if FLS hides that field from the user's profile. - The query runs even if the user has no Read permission on Account at all.
The code runs in system mode, which the previous chapter introduced. In system mode, Apex has the access of an administrator with respect to object permissions and FLS. Only the sharing keyword narrows the rows.
Compare this with a Lightning page or a list view. Those standard features enforce CRUD and FLS automatically. If a custom Apex controller feeds a page and skips enforcement, the page can show data that the standard UI would hide. That gap is the usual source of security review findings.
Where the layers meet
The next table shows what each setup does with a query on an object. Assume the user has sharing access to some accounts but not others.
| Class declaration | Query form | Rows filtered by sharing | Object permission checked | FLS checked |
|---|---|---|---|---|
| `with sharing` | Plain SOQL | Yes | No | No |
| `without sharing` | Plain SOQL | No | No | No |
| `with sharing` | `WITH USER_MODE` | Yes | Yes | Yes |
| `without sharing` | `WITH USER_MODE` | Yes | Yes | Yes |
The last row needs explanation. User mode enforces sharing on its own, regardless of the class keyword, for the operation it applies to. So a WITH USER_MODE query inside a without sharing class still honors sharing for that query. The next chapters cover this in detail. For now, remember that the sharing keyword is a class-level setting for plain operations, and that user mode is a per-operation setting that covers all three layers.
Experiencing the difference
You can see the gap with a user who has restricted FLS. Suppose a profile has Read access to Account but the AnnualRevenue field is hidden. Run this as that user through an anonymous Apex session or a test with System.runAs.
public with sharing class FlsGapDemo {
public static void run() {
List<Account> accounts = [SELECT Id, Name, AnnualRevenue FROM Account LIMIT 5];
for (Account a : accounts) {
// AnnualRevenue is populated even though FLS hides it from this user.
System.debug(a.Name + ' revenue: ' + a.AnnualRevenue);
}
Boolean readable = Schema.SObjectType.Account.fields.AnnualRevenue.isAccessible();
System.debug('User can read AnnualRevenue: ' + readable);
}
}The loop prints real revenue values, and the last line prints false. The data and the permission disagree, because the query never consulted FLS. The describe call at the end shows what the permission actually is. Checking permissions by hand with describe calls is one way to close the gap, but it requires a check for every object and field you touch. User mode does the checking for you.
The same gap on writes
The gap exists for DML too. A with sharing class that runs insert or update in system mode will do the following.
- Respect sharing for updates and deletes. A user cannot edit a record they only have read access to through sharing.
- Skip the object Create, Edit, and Delete permissions.
- Skip field-level edit checks, so the code can write to a field the user can only read.
public with sharing class AccountUpdater {
public static void raiseRevenue(Id accountId) {
Account a = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :accountId];
a.AnnualRevenue = 1000000;
update a; // Succeeds even if the user's FLS makes AnnualRevenue read-only.
}
}The update succeeds when the user can edit that account record through sharing, even if the profile marks AnnualRevenue as read-only. The record-level rule is applied. The field-level rule is not.
Common mistakes
These are the errors this chapter's distinction is meant to prevent.
- Treating
with sharingas a complete security measure. It filters rows. It does not check CRUD or FLS. - Assuming a
without sharingclass is the only risk. Awith sharingclass can still expose hidden fields. - Assuming the Apex code and the UI share the same checks. Standard pages enforce CRUD and FLS. Your Apex does not, unless you add enforcement.
- Forgetting that inner classes and called classes matter. A
with sharingclass that calls awithout sharingclass runs the called code without sharing. Sharing is set per class, and an inner class does not inherit the outer class's keyword. - Reading describe results as enforcement. A describe check informs your code. It does not stop a query by itself.
Why this matters for what comes next
The remaining chapters cover tools that close the CRUD and FLS gap. Each one applies the same split you just learned.
- User mode on queries and DML enforces all three layers in one place.
WITH SECURITY_ENFORCED,Security.stripInaccessible, and describe checks enforce CRUD and FLS only, and they do it with different failure behavior.- The sharing keywords stay useful for setting the default row behavior of a class.
Keep the mental model of rows, objects, and columns. When you review a class, ask three questions: which rows does it return, is the object permission checked, and is every returned field checked.
Exercise
A class is declared public with sharing class ReportService and contains the query [SELECT Id, Name, Phone FROM Contact]. A user has Read permission on Contact and can see some contact records through sharing. The Phone field is hidden from that user by FLS. What does the query return for that user, and why?
Show answer
The query returns only the contact records the user can see through sharing, because with sharing filters rows. It still returns the Phone value on those records, because the query runs in system mode and with sharing does not check FLS. To enforce FLS you would add user mode, for example WITH USER_MODE, which is covered in later chapters.
Exercise
Name the mechanism that controls each of these: (a) whether a user can open any Opportunity records at all, (b) whether a user can see a specific Opportunity owned by a colleague, (c) whether a user can see the Amount field.
Show answer
(a) Object permissions (CRUD) from the profile or permission sets. (b) Sharing, through org-wide defaults, the role hierarchy, sharing rules, teams, or manual shares. (c) Field-level security (FLS) from the profile or permission sets.