Sub topic 16
Security practices and least privilege
This chapter assumes you have already built a Named Credential, an External Credential with a principal, and a permission set that grants access to that principal. Here you review the setup as a whole and tighten it.
Three layers of protection
A callout is safe when three separate layers each do one job.
- The External Credential stores the secret. Apex never reads it.
- A permission set decides who can use the credential.
- The Named Credential fixes the host, so code can only choose a path.
Practices that matter
- Keep secrets in Setup only. Do not put them in Apex, custom settings, custom metadata or debug statements. Rotating a key then means editing the principal's parameter, with no code deployment.
- Grant access through dedicated permission sets. Create one permission set per credential or per business purpose, and add External Credential Principal Access to it. Assign it only to the users or integration user that need it. Avoid adding principal access to a broad profile-based permission set.
- Use formulas for header secrets. A header such as
{!$Credential.Weather_EC.ApiKey}is resolved at callout time. Leave formulas in the HTTP body disabled unless you need them, because they widen where the secret can appear. - Choose the principal type deliberately. Use a named principal for system-to-system calls. Use a per user principal when the external system must see and authorize the individual user.
- Limit power on the external side. Give the API key or OAuth client only the scopes the integration needs, such as read-only access.
- Never let input choose the host. The Named Credential supplies the base URL. Validate and encode anything you append to it.
Validate input before it reaches the path
The following class accepts a city name only if it matches a narrow allowlist, then encodes it.
public with sharing class WeatherLookup {
private static final Pattern CITY_PATTERN =
Pattern.compile('^[A-Za-z .\\-]{1,60}$');
public static String forecastPath(String city) {
if (String.isBlank(city) || !CITY_PATTERN.matcher(city).matches()) {
throw new IllegalArgumentException('Invalid city name');
}
return '/forecast?city=' + EncodingUtil.urlEncode(city, 'UTF-8');
}
}You would then build the endpoint as 'callout:Weather_API' + WeatherLookup.forecastPath(city).
Risks and controls
| Risk | Control |
|---|---|
| Secret committed to source control | Store it only on the principal in Setup |
| Too many users can call the API | Narrow permission sets, assigned per purpose |
| Input redirects or alters the request | Fixed host, allowlist validation, URL encoding |
| Stolen key does wide damage | Least-privilege scopes, regular rotation |
Deployments and logs
Metadata deployments move the credential definitions but not the secret values. Enter the secret again in each org, and do it through Setup rather than through a script that logs it. Also check that your debug logs and error messages do not print request headers. Surface the status code, as WeatherService does, and not the full request.
Exercise
A developer wants any user to be able to call the Weather API, so they add principal access for Weather_EC to the standard profile's permission set. Name two least-privilege problems and give a better setup.
Show answer
Every user, including those with no business need, can now trigger callouts with the shared named principal's credentials, and the access is hard to audit or revoke. A better setup is a dedicated permission set, such as WeatherCalloutAccess, that includes only the Weather_EC principal. Assign it just to the users or integration user who need it. Also limit the API key's scopes on the external side.