@AuraEnabled security is easy to misjudge because of how the code reads. In a Lightning web component, import getInvoices from '@salesforce/apex/InvoiceController.getInvoices' looks like an internal function call between two halves of one application. It is not. Every @AuraEnabled method is a server endpoint that accepts a JSON request, and the component is only one of the clients that can send it.
Salesforce's Lightning documentation says as much: treat each @AuraEnabled method as a web service, and assume an attacker can call it with any parameters. We covered the client side in Lightning Web Components security and the endpoint a site exposes in the sfsites Aura endpoint and the guest profile. This post is about the Apex itself: what a controller method must assume, and what it must check.
Who can call an @AuraEnabled method?
Since the Winter '21 enforcement of the release update Salesforce described in Ensure Users Have Access to @AuraEnabled Methods, a user can call an @AuraEnabled method only if their profile or a permission set grants access to the Apex class. Before that, any authenticated user could call it.
That rule is a gate on the class, not on the method or the record. Once a user has the class:
- They can call every
@AuraEnabledmethod in it, including ones no component they use ever calls. - They can pass any value for every parameter, not just the values the UI would send.
- They can call it as often as they like, outside the UI's flow and validation.
On an Experience Cloud site, granting the class to the guest profile extends all of that to unauthenticated visitors. The class access list on the guest profile is the most important list on the site.
The method's parameters are attacker input
Client-side validation in the component does not exist from the server's point of view. Three parameter shapes deserve particular care.
Record ids
// Vulnerable: any caller can read any invoice by id
public without sharing class InvoiceController {
@AuraEnabled
public static Invoice__c getInvoice(Id invoiceId) {
return [SELECT Id, Amount__c, Customer__r.Name, Bank_Account__c
FROM Invoice__c WHERE Id = :invoiceId];
}
}The component only ever passes the id of an invoice on the current page. The method accepts any id. With without sharing, every invoice in the org is one request away. This is an insecure direct object reference, and it is the most common real-world @AuraEnabled flaw.
// Fixed: sharing, user mode, and only the fields the page shows
public with sharing class InvoiceController {
@AuraEnabled
public static Invoice__c getInvoice(Id invoiceId) {
return [SELECT Id, Amount__c, Customer__r.Name
FROM Invoice__c WHERE Id = :invoiceId WITH USER_MODE];
}
}sObject parameters
A method that takes a whole record and writes it lets the caller choose every field:
@AuraEnabled
public static void saveContact(Contact c) {
update c; // the caller can set OwnerId, AccountId, any field
}Strip it before the write, or accept only the fields you mean to change:
@AuraEnabled
public static void saveContact(Contact c) {
SObjectAccessDecision d =
Security.stripInaccessible(AccessType.UPDATABLE, new List<Contact>{ c });
update as user d.getRecords();
}stripInaccessible removes fields the user cannot edit. It does not remove fields the user can edit but this form should not change, so a dedicated parameter list is still the tighter design for sensitive objects.
Strings used in queries
A string concatenated into dynamic SOQL (an object name, a sort field, a filter) is SOQL injection. Bind values, and check field and object names against an allowlist. See Apex SOQL injection.
Sharing and FLS on controller classes
For classes saved at API 66.0 and earlier, an @AuraEnabled controller with no sharing keyword runs with sharing as the entry point; Salesforce made Lightning controllers the exception to the system-context default. At API 67.0, every class without a declaration defaults to with sharing, and database operations run in user mode. Declare the keyword anyway: a helper class the controller calls follows its own keyword, and a without sharing helper quietly undoes the controller's.
Sharing still says nothing about objects and fields. On classes before 67.0, a with sharing controller returns any field it queries, including fields the user's profile hides on the record page. Use WITH USER_MODE or Security.stripInaccessible; the details are in Apex CRUD and FLS enforcement.
Return less than you query
A controller that returns a whole sObject or a wrapper with many fields sends all of it to the browser, where any user can read the network response. Return only what the component renders. If a method serves a lookup, return id and name, not the record. Messages in an AuraHandledException are also shown to the caller, so keep query text, record data and internal identifiers out of them.
cacheable=true caveats
@AuraEnabled(cacheable=true) is required for @wire and lets the client cache the response. It also forbids DML in the method. None of that is access control:
- A cacheable method is callable by every user who has the class, exactly like any other
@AuraEnabledmethod, and can be called imperatively with any parameters. - Read-only is not the same as safe. Most data exposure happens through reads, and a cacheable read with a caller-supplied id is still an IDOR.
- From API 55.0,
@AuraEnabled(cacheable=true scope='global')places results in a global cache, per the AuraEnabled annotation reference. Use it only for data that is the same for every caller, never for results that depend on who is asking.
Put state-changing logic in non-cacheable methods, and give those methods the strictest checks.
Checking controllers with Vulkro
Vulkro for Salesforce treats @AuraEnabled method parameters as untrusted sources and traces them to queries, DML and returned fields. Relevant checks include SF-GUEST-AURA-001 (an @AuraEnabled method in a class granted to the site guest user that performs DML, starts an async job or runs dynamic SOQL with no authorization check) and the IDOR check broken-object-level-auth/idor for methods that select by a caller-supplied id with no ownership filter. The entrypoints command lists every @AuraEnabled method with its cacheable flag, the class's sharing keyword and whether a guest profile in the source grants it:
vulkro-sf entrypoints ./force-app --guest-only
vulkro-sf scan ./force-appFAQ
Can users call @AuraEnabled methods directly?
Yes. Any user whose profile or permission set grants the Apex class can call its @AuraEnabled methods directly, with any parameters, without using the component. On a site, that includes the guest user if the guest profile has the class.
Does cacheable=true make an Apex method secure?
No. cacheable=true controls caching and blocks DML in the method. It does not limit who can call the method or which records it returns.
Do @AuraEnabled methods enforce field-level security?
Not by default on classes before API 67.0. Use WITH USER_MODE, AccessLevel.USER_MODE or Security.stripInaccessible to enforce object and field permissions, and declare with sharing for record access.
Start a 14-day trial from /start/ to check your controllers. Related: anatomy of a guest user data leak.