Sub topic 2
Least privilege applied to Salesforce access
This chapter assumes you know basic Salesforce administration (users, profiles, objects, fields). It also assumes you have used the REST API and SOQL, and that OAuth 2.0 is familiar. You do not need to have built an integration user yet. This chapter sets the rules that the later chapters follow.
What least privilege means
The principle of least privilege says an identity should have only the access it needs to do its job, for only as long as it needs it. For an integration user, this means the account can do what the integration's code does and nothing else.
The reason is the blast radius. Integration credentials are stored in middleware, CI systems, or configuration files, and nobody watches them as closely as a person logging in. If the credentials leak, an attacker gets exactly what the integration user can do. An integration that only reads two fields on Account is a small incident. An integration with Modify All Data is a full breach.
Least privilege also protects you from your own bugs. A faulty sync job can delete or overwrite only the records it has permission to touch.
How Salesforce decides what a user can do
Salesforce does not have one switch for access. Several layers each answer a different question, and a request succeeds only if every layer allows it.
Each layer has its own chapter later in this tutorial. For now, keep two ideas in mind.
- Permissions are additive. Salesforce has no "deny" permission. A user's effective access is the union of the profile and every assigned permission set. You cannot grant a broad permission and then carve out an exception with a second permission set. To reduce access, you must remove the grant that provides it.
- The user is the ceiling. An integration authenticates as a Salesforce user and acts with that user's permissions. OAuth scopes can narrow what a token does, but they never add rights the user lacks. This is why the user's own permissions are the main control.
The permissions that break least privilege
Most over-privilege in integration users comes from a small set of permissions. Treat each one as something that needs an explicit justification.
| Permission | What it does | Why it is risky for an integration |
|---|---|---|
| Modify All Data | Read, edit, delete, and transfer every record in the org, regardless of sharing | Bypasses object permissions, field-level security checks on records, and sharing |
| View All Data | Read every record in the org | Exposes data the integration never needs |
| View All / Modify All on one object | Same bypass for a single object | Skips the sharing model for that object |
| Delete on an object | Allows deletion through the API | Turns a bug into data loss |
| Author Apex, Customize Application | Allows changes to code and metadata | Lets a stolen credential change the org itself |
| Manage Users, Reset Passwords | Administers other accounts | Allows privilege escalation |
| Profile of System Administrator | Includes many of the above | Gives the integration an admin's access |
Note that View All and Modify All, whether org-wide or per object, override the sharing model. This matters because teams often add them to "fix" a missing-records problem. The fix works, but it replaces a narrow gap with unlimited visibility.
Apply least privilege in four steps
Use the same method every time.
- List what the integration does. Write down each object it reads or writes, each field it touches, and each operation (read, create, update, delete). Include API access itself.
- Start from nothing. Use a minimal profile with no object permissions. Add access through a permission set built from your list.
- Test both directions. Confirm that allowed operations succeed. Also confirm that operations outside the list fail.
- Remove, don't hide. If a test shows extra access, remove the grant that provides it. Do not try to offset it elsewhere, because permissions are additive.
A worked example
Suppose a reporting tool reads the Account name and Industry once an hour. It never writes. The requirement list looks like this.
| Need | Object or field | Operation | Grant |
|---|---|---|---|
| Log in and call the API | System | API access | API Enabled system permission |
| Read accounts | Account | Read | Object permission: Read |
| Read the Industry field | Account.Industry | Read | Field permission: readable, not editable |
| Write anything | none | none | Nothing |
| Delete anything | none | none | Nothing |
| See all accounts | Account | Read | Sharing, covered in a later chapter |
The matching permission set grants Read on Account, one readable field, and API Enabled, and leaves every other flag false:
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<description>Read-only access to Account for the integration user</description>
<fieldPermissions>
<editable>false</editable>
<field>Account.Industry</field>
<readable>true</readable>
</fieldPermissions>
<hasActivationRequired>false</hasActivationRequired>
<label>Integration Account Read</label>
<objectPermissions>
<allowCreate>false</allowCreate>
<allowDelete>false</allowDelete>
<allowEdit>false</allowEdit>
<allowRead>true</allowRead>
<modifyAllRecords>false</modifyAllRecords>
<object>Account</object>
<viewAllRecords>false</viewAllRecords>
</objectPermissions>
<userPermissions>
<enabled>true</enabled>
<name>ApiEnabled</name>
</userPermissions>
</PermissionSet>Setting the unwanted flags to false is explicit. It documents that you considered each one, and it makes reviews easier because a reader sees the intent.
Where the controls sit
Least privilege applies at more than one level. The table shows which control limits what, so you can see where each later chapter fits.
| Question | Control | Chapter topic |
|---|---|---|
| Can this account exist as an API-only identity? | User license and profile | License and profile |
| Which objects and operations are allowed? | Permission sets, object permissions | Permission sets, object and field permissions |
| Which fields are visible? | Field-level security | Object and field permissions |
| Which records are visible? | Sharing model | Sharing and record visibility |
| How does the integration prove who it is? | OAuth with a pre-authorized user | OAuth authentication |
| From where and how long can it connect? | Login IP ranges, session settings | Network and session controls |
| How do you know the access is right? | Verification queries and tests | Verifying permissions |
Checking for over-privilege with SOQL
You can inspect permissions with queries, which makes least privilege testable instead of a matter of opinion. This query lists the permission sets assigned to a user and shows whether any of them carry the two broadest data permissions:
SELECT PermissionSet.Name,
PermissionSet.IsOwnedByProfile,
PermissionSet.PermissionsViewAllData,
PermissionSet.PermissionsModifyAllData
FROM PermissionSetAssignment
WHERE AssigneeId = '005XXXXXXXXXXXXXXX'Replace the placeholder with the integration user's Id. Every row should show false in both permission columns. Rows where IsOwnedByProfile is true represent the profile's own permissions. Remember to read those rows too, since a profile with Modify All Data defeats a carefully built permission set.
The query only covers these two flags. A later chapter shows how to check object-level flags and other system permissions.
Failure modes when you apply the principle
Least privilege can fail in two directions, and both show up in practice.
- Too much access. Common causes are cloning the System Administrator profile, adding Modify All Data to stop "insufficient access" errors, and reusing a permission set built for a human role. The integration works, and nothing warns you that it is over-privileged.
- Too little access. The integration fails with errors such as
INSUFFICIENT_ACCESS, or a SOQL query returnsNo such columnbecause field-level security hides a field. The second error is easy to misread as a typo in the query. It can mean the field exists but the user cannot see it.
When you hit an access error, do not widen permissions until it goes away. Find which layer blocked the request and grant only that. The error message and the request in the debug log usually name the object or field.
Keep the principle over time
Access that was minimal at launch drifts. New fields appear, the integration gains features, and someone adds a quick fix. Treat the requirement list from the worked example as a living document. When the integration changes, update the list first, then the permission set. Later chapters cover scheduled access reviews and monitoring so that drift gets caught.
Exercise
An integration user has a permission set that grants Read on Account only. After a connectivity incident, an admin assigns a second permission set with Modify All Data "temporarily". The admin then edits the first permission set to remove Read on Account, expecting this to restrict the user further. Why does the user still have full access to Account, and what is the correct way to restore least privilege?
Show answer
Salesforce permissions are additive and there is no deny. Modify All Data grants read, edit, and delete on every object and bypasses sharing, so removing Read from the first permission set changes nothing. To restore least privilege, remove the assignment of the second permission set (or remove Modify All Data from it), and then confirm with a query on PermissionSetAssignment that no assigned permission set or the profile still carries PermissionsModifyAllData or PermissionsViewAllData.
Exercise
A SOQL query run as the integration user returns "No such column 'Industry' on entity 'Account'", although the field exists. Name the likely cause and the narrowest fix.
Show answer
Field-level security hides the field from the user. The narrowest fix is to add a field permission that makes Account.Industry readable (and leave it not editable) in the integration's permission set. Granting View All Data or switching to an admin profile would also work, but it would violate least privilege.