Apex runs in system mode by default, so it ignores the user's object and field permissions. Declare classes with sharing so record level access is enforced, or inherited sharing to follow the caller. For object and field permissions, run queries and DML in user mode, or remove inaccessible fields with Security.stripInaccessible before returning data. Treat any class exposed to Lightning components as a security boundary and test it as a restricted user.
Why interviewers ask this
Security mistakes in Apex expose data to users who should never see it, and they are a common finding in security reviews. The interviewer wants the distinction between record level sharing and object and field permissions, since with sharing only covers the first. They listen for user mode operations, stripInaccessible, and treating AuraEnabled methods as public endpoints. Testing with System.runAs as a limited user shows you verify rather than assume.
How to structure your answer
- Explain that Apex runs in system mode by default.
- Cover the sharing keywords for record level access.
- Cover object and field level enforcement options.
- Explain where to apply them and how you test.
Example answer
The starting point is that Apex runs in system mode, so by default my code can see and change data the running user could not. There are two layers. Record level access is controlled by the sharing keywords. I declare classes with sharing so queries only return records the user can see, use inherited sharing for utility classes so they follow whoever called them, and use without sharing only deliberately, in small, commented classes. The second layer is object and field permissions, which with sharing does not touch. For that I run queries and DML in user mode, so the platform enforces the user's permissions, or use stripInaccessible on records before returning them. The classes I worry about most are those exposed to Lightning components through AuraEnabled methods, because any authenticated user can call them, so I treat them like public API endpoints. And I test them properly, using System.runAs with a user on a minimal permission set and asserting they cannot see or edit what they should not.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- What is the difference between with sharing and inherited sharing?
- When is without sharing justified?
- How would you test that a user cannot see a restricted field?
Related salesforce developer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions