Skip to main content

Code reviews rule: Insecure direct object references (DOR)

Written by David Martin
Updated this week

Insecure direct object references (DOR)

Why is this an issue?

Insecure direct object references (IDOR) occur when an application provides direct access to objects based on user-supplied input without proper authorization checks. Attackers can manipulate these references to access records they should not be able to view or modify.

In Salesforce, this commonly occurs when code accepts a record ID from user input (URL parameters, form fields) and queries or modifies that record without verifying the user has permission to access it.

Examples

Example of incorrect code:

@AuraEnabled
public static Account getAccount(Id accountId) {
// Directly uses user-supplied ID without authorization check
return [SELECT Id, Name, Revenue__c FROM Account WHERE Id = :accountId];
}

Example of correct code:

@AuraEnabled
public static Account getAccount(Id accountId) {
// Query with sharing rules enforced
List<Account> accounts = [SELECT Id, Name, Revenue__c FROM Account WHERE Id = :accountId WITH USER_MODE];

if (accounts.isEmpty()) {
throw new AuraHandledException('Record not found or access denied');
}
return accounts[0];
}

How can I fix violations?

  1. Use WITH USER_MODE: Add WITH USER_MODE to SOQL queries to enforce sharing rules and field-level security.

  2. Use with sharing: Ensure your Apex class uses the with sharing keyword to enforce record-level sharing rules. Note that with sharing alone does not enforce field-level security or object-level permissions, and will not resolve this violation on its own.

  3. Validate access explicitly: As an additional security measure, use Schema.SObjectType.Account.isAccessible() and similar methods to check permissions before accessing records. Note that this practice alone will not resolve the violation; combine it with WITH USER_MODE or WITH SECURITY_ENFORCED.

  4. Avoid exposing internal IDs: Where possible, use external IDs or other non-guessable identifiers.

When should I disable this rule?

You may dismiss specific violations when the code is intentionally running in system context for legitimate administrative purposes, and access is controlled through other means (e.g., custom permissions, profile checks).

Resources

Did this answer your question?