Home SalesforceApex Difference Between With Security and Without Security in Apex

Difference Between With Security and Without Security in Apex

by Dhanik Lal Sahni
Security in Apex

SOQL (Salesforce Object Query Language) is a powerful tool used for querying Salesforce data, but it can also be a point of security concern if not implemented correctly. A core way to secure your SOQL is to use the WITH SECURITY_ENFORCED clause. The above clause ensures that the current user’s FLS and OLS are intact. If you use WITH SECURITY_ENFORCED, the query will return only fields and rows that the user can access, preventing unauthorized data exposure.

Apart from writing the WITH SECURITY_ENFORCED developers can also write WITH USER_MODE clause. This allows for FLS, but provides a flexible way to control access at a data level and is a good way to approach this. It behaves like a WITH SECURITY_ENFORCED but in some cases, it can give you a point like when you want to implement FLS without the overhead of checking share rules.

Check out post – Enforce Object-level and Field-level permissions in Apex to understand with and without security in Apex.

Difference Between With Security and Without Security

The only difference between “with security” and “without security” in Apex is how your code respects user permissions and sharing rules.

With Security

When your SOQL queries use WITH SECURITY_ENFORCED, the code respects the permissions of the user. This means that only the fields and records to which the user has access will be returned. It is important are known to assure the foundational security policies are achieved and sensitive data is protected from an unapproved access.

List<Account> accounts = [SELECT Name, Email__c FROM Account WITH SECURITY_ENFORCED];
// Fails if the user lacks field or object access.

Without Security

Apex Executes in System Mode, which ignores the permissions of the running user. By defining a class with the without sharing keyword, it also goes against sharing rules, meaning the code can see all data without regard for the user’s access rights. Although this would help in admin task or getting some larger data but it is a serious security risk if not handled properly.

List<Account> accounts = [SELECT Name,Email__c FROM Account];
// Retrieves data regardless of user’s permissions.
AspectWith SecurityWithout Security
Field-Level SecurityEnforces FLS automaticallyIt does not enforce FLS; and may expose restricted data.
Object-Level SecurityEnforces OLS automaticallyIt does not enforce FLS; and may expose unauthorized objects
Error HandlingThrows an exception if access is restricted.Retrieves data even if the user lacks access.
Use CaseRecommended for user-facing scenarios or integrations.Suitable for backend operations where admin access is ensured.
Difference Between With Security and Without Security

Few Other Steps for secure SOQL

  • Bind Variables: Use bind variables for all your queries to avoid SOQL injection vulnerabilities.
  • Limit And Offset: Use LIMIT to reduce the number of records retrieved and save system performance
  • Field Accessibility Checks: Schema CheckfieldLevelSecurity: Use Schema.DescribeFieldResult to check FLS before querying sensitive fields.

Summary

SOQL Queries with WITH SECURITY_ENFORCED ensures that we adhere to Salesforce’s security model and prevent any sensitive data leakage maintaining trust. Never skip security unless necessary, such as backend processes specific to admins only.

Related Posts

Related Questions Posts

You may also like

Leave a Comment

Top 10 Flow Enhancement in Spring’25 Release Top 10 Salesforce Service Cloud Features Top 10 Best Practices for Lightning Flow Facts and Statistics for Salesforce’s Size and Market Share Top 5 Contract Management Salesforce Apps