Sub topic 12
Security and field-level access
This chapter covers who can receive change events, what they see in them, and which identity runs your Apex trigger. It assumes you can already enable CDC for an object, find its channel name, and read a ChangeEventHeader. It also assumes you know basic Apex triggers, permission sets, and field-level security (FLS).
Why security needs separate attention
A change event is a copy of record data placed on the event bus. Anyone who can subscribe to the channel can read that copy. Salesforce therefore checks the subscriber's access before it delivers an event, but it checks different things for different kinds of subscriber. If you treat all subscribers the same, you can leak fields or give an integration more access than it needs.
Three questions cover most of the design work:
- Which identity receives the event?
- Which objects and fields can that identity see?
- Where is the event data stored, and for how long?
Subscribers run as a user
Every subscriber has an identity, and that identity decides what the event contains.
| Subscriber | Identity | Where the identity comes from |
|---|---|---|
| Lightning web component using `lightning/empApi` | The logged-in user | The user's session |
| External client using Pub/Sub API or CometD | The user who authenticated | The OAuth token or session ID the client presents |
| Apex trigger on a change event | Automated Process user by default | Salesforce, unless you configure another user |
The first two follow a simple rule. The event is filtered to what that user is allowed to read. The Apex trigger is the exception, and it gets its own section below.
Object and field permissions filter the event
When Salesforce delivers a change event to a user, it applies that user's object permissions and field-level security.
- A user without read access to the object does not receive its change events.
- A user without read access to a field receives the event without that field's value.
Suppose an Account update changes Phone and AnnualRevenue. A sales manager who can see both fields receives both values. An integration user whose profile hides AnnualRevenue receives the event with only Phone. Two subscribers on the same channel can get different payloads for the same change.
This has a practical consequence for debugging. If a subscriber reports a missing field, check the subscribing user's permission sets before you suspect a CDC fault. Test with the real integration user, not with your own admin account.
Sharing rules and record visibility
Field-level security and object permissions are the controls to plan around. Do not rely on record sharing to limit which records a subscriber hears about. Treat the channel as carrying data for every record of the enabled object that the user's object permissions allow. If you need to keep certain records away from a subscriber, restrict the object access itself, or do not give that identity a subscription. Confirm the exact sharing behavior for your API version in the current Salesforce documentation before you depend on it.
Apex triggers run as the Automated Process user
A trigger on AccountChangeEvent is not tied to the user who edited the account. It runs asynchronously, after the transaction commits, under the Automated Process user. Two things follow from that:
- Records the trigger creates show Automated Process as the creator. This is a common surprise in audit reports.
- Apex runs in system mode, so the trigger can read and write data the original editor could not.
You can change the running user for a change event trigger by deploying a PlatformEventSubscriberConfig metadata record. It also lets you set the batch size for the trigger.
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventSubscriberConfig xmlns="http://soap.sforce.com/2006/04/metadata">
<batchSize>200</batchSize>
<masterLabel>AccountChangeTriggerConfig</masterLabel>
<platformEventConsumer>AccountChangeTrigger</platformEventConsumer>
<user>[email protected]</user>
</PlatformEventSubscriberConfig>Here platformEventConsumer is the API name of the trigger and user is the username of the identity that should run it. Save the file as AccountChangeTriggerConfig.platformEventSubscriberConfig-meta.xml in the platformEventSubscriberConfigs folder of your project, then deploy it with the trigger.
Changing the running user does not make Apex enforce permissions by itself. Apex still runs in system mode. The configured user controls the owner and audit fields on records the trigger creates, and the limits that apply to it. To enforce that user's CRUD and field-level security on your own DML, request user mode explicitly:
trigger AccountChangeTrigger on AccountChangeEvent (after insert) {
List<Task> followUps = new List<Task>();
for (AccountChangeEvent event : Trigger.new) {
EventBus.ChangeEventHeader header = event.ChangeEventHeader;
if (header.changeType == 'UPDATE' && header.changedFields.contains('Phone')) {
for (String recordId : header.recordIds) {
followUps.add(new Task(
Subject = 'Verify updated phone number',
WhatId = recordId
));
}
}
}
if (!followUps.isEmpty()) {
Database.insert(followUps, AccessLevel.USER_MODE);
}
}With AccessLevel.USER_MODE, the insert fails with an exception if the running user lacks create access on Task or on a field you set. That failure is usually what you want. A trigger that quietly writes data its configured user should not touch is harder to detect than one that fails loudly.
Choosing the running user
| Option | Good for | Watch out for |
|---|---|---|
| Automated Process (default) | Simple internal automation | Broad system-mode access, and audit fields that show Automated Process |
| Dedicated integration user | Clear audit trail and a minimal permission set | You must keep its permission sets in step with the logic |
| An existing named user | Quick experiments | Breaks when the user is deactivated or changes role |
A dedicated user with a purpose-built permission set is the usual choice for production. If that user is deactivated, the trigger stops running correctly, so include it in your deployment checklist.
Securing external subscribers
External clients authenticate through OAuth, typically with a connected app. Apply the same least-privilege thinking you would apply to any API integration:
- Create a dedicated integration user for each subscribing system, not a shared admin.
- Give that user only read access to the objects and fields the system needs.
- Scope the connected app's OAuth scopes narrowly, and rotate or revoke credentials on a schedule.
- Prefer a short-lived access token and refresh it, rather than embedding a long-lived session ID.
Because filtering follows the authenticated user, the integration user's permission set is your main control. Reviewing that permission set is more reliable than reviewing the client code.
Data at rest on the event bus
Change events are retained on the event bus for a limited window so subscribers can replay them. During that window the field values sit in the bus, outside the normal record store. Two decisions follow:
- Enable CDC only for objects you need. Each enabled object adds its data to the retained stream. Enabling an object with sensitive fields widens the surface for no benefit if nothing consumes it.
- Decide whether the data needs extra protection. If your org uses Shield Platform Encryption, check the current documentation for how it applies to change events, and for which field types are supported, before you enable CDC on encrypted data.
A review checklist
Before you release a CDC integration, check each of these:
- The subscribing identity is a dedicated user, not an administrator.
- Its permission set grants read on the enabled objects and only the fields it needs.
- Triggers have a deliberate running user, set through
PlatformEventSubscriberConfigif the default is not acceptable. - DML inside triggers uses user mode when the configured user's permissions should apply.
- Only the objects with a real consumer are enabled for CDC.
- You have tested the integration with the actual restricted user and confirmed which fields arrive.
Exercise
An integration user subscribes to /data/AccountChangeEvent. A rep updates both Phone and AnnualRevenue on an Account, but the integration receives an event containing only Phone. Name the most likely cause and the fix. Then explain why a change event trigger that creates a Task shows "Automated Process" as the Task's creator, and how to change it.
Show answer
The integration user's field-level security hides AnnualRevenue, so Salesforce removes that field from the event delivered to that user. The fix is to grant the user read access to AnnualRevenue through a permission set, if the integration really needs it. Triggers on change events run asynchronously as the Automated Process user by default, so records they create show that user as creator. To change this, deploy a PlatformEventSubscriberConfig metadata record that names the trigger in platformEventConsumer and the chosen username in user. Use AccessLevel.USER_MODE on the DML if you also want that user's permissions enforced.