Sub topic 14
Pitfalls and review checklist
This chapter assumes you know the three sharing keywords, how sharing mode follows a call chain, and the difference between record-level sharing and object or field permissions. It collects the mistakes that show up most often in code review and gives you a checklist to apply.
Pitfalls
Omitting the keyword
A class with no sharing keyword does not have a mode of its own. When it is the entry point of a transaction, it runs without sharing. When another class calls it, it runs under the caller's mode. The result depends on who calls the class, and that can change when someone adds a new caller.
Declare a keyword on every top-level class. If you want the caller to decide, write inherited sharing so the intent is visible.
// Unclear: behavior depends on the caller or on the entry point
public class AccountLookup {
public static List<Account> all() {
return [SELECT Id, Name FROM Account];
}
}
// Clear: the caller decides, and an entry point defaults to with sharing
public inherited sharing class AccountLookup {
public static List<Account> all() {
return [SELECT Id, Name FROM Account];
}
}Assuming a with sharing caller protects the callee
Each method runs under the sharing mode of the class that defines it. A with sharing controller that calls a without sharing helper still reads every record inside the helper. Being "inside" a with sharing class does not make everything below it safe.
public with sharing class CaseController {
public static Integer countAllCases() {
// Runs CaseCounter.countAll under without sharing
return CaseCounter.countAll();
}
}
public without sharing class CaseCounter {
public static Integer countAll() {
return [SELECT COUNT() FROM Case];
}
}Widening access with without sharing
without sharing is sometimes needed, for example to check for duplicates across records a user cannot see. The risk is scope. A large without sharing class that exposes many methods gives every caller access to all of them. Keep these classes small, give them one purpose, and return only the data the caller needs, such as a count or a Boolean, instead of full records.
Treating sharing as complete security
Sharing keywords control only which records a query returns. They do not enforce object permissions or field-level security. A with sharing class can still return a field the user is not allowed to read. Add WITH USER_MODE to queries and AccessLevel.USER_MODE to DML when you need those checks.
Trusting inner classes
An inner class does not inherit the sharing mode of the class that contains it. Give each inner class its own keyword if it runs queries or DML.
Review checklist
Use these questions on every Apex class that touches data.
- Does every top-level class declare
with sharing,without sharing, orinherited sharing? - Is each
without sharingclass small, single-purpose, and commented with the reason it needs to bypass sharing? - Do entry points such as controllers, REST resources, and invocable methods use
with sharing? - Do shared utilities and data-access classes use
inherited sharing? - For each call into another class, did you check the callee's own keyword and not just the caller's?
- Do inner classes that query or modify data declare a keyword?
- Where users should only see fields they have permission to read, does the code use
WITH USER_MODEorAccessLevel.USER_MODE? - Do tests cover both a user who can see the records and a user who cannot?
Exercise
A with sharing class named OrderController calls a class named OrderStats, which has no sharing keyword. A user opens a page that runs OrderController. Later, a scheduled job calls OrderStats directly. In which sharing mode does OrderStats run in each case, and what is the fix?
Show answer
When called from OrderController, OrderStats runs with sharing because it takes the mode of its caller. When the scheduled job calls it directly, it is the entry point with no keyword, so it runs without sharing. The fix is to declare a keyword on OrderStats. Use inherited sharing if the caller should decide, which also makes an entry point run with sharing, or declare with sharing or without sharing explicitly if the class always needs one mode.
Exercise
A with sharing class runs [SELECT Id, Salary_c FROM Employeec]. The user has no read access to Salary_c. Does with sharing prevent the field from being returned, and how do you enforce the field permission?
Show answer
No. with sharing limits which records are returned, not which fields. Use WITH USERMODE in the query, as in [SELECT Id, Salaryc FROM Employeec WITH USERMODE], so field-level security is enforced. In user mode the query fails with an error when the user cannot read the field.