Skip to main content

Secrets in Salesforce metadata - Apex, custom settings, custom metadata and credentials compared

Secrets in Salesforce metadata end up in more places than most teams expect. An API key typed into an Apex class is in the Git repository, in every sandbox refreshed since, in every package version that contained the class, and in the laptop of every developer who retrieved the source. Salesforce offers several places to keep a secret instead, but they protect against different people, and two of them protect nothing at all outside a managed package.

This post compares the options, explains what each one exposes in a repository and in a package, and gives a short rule for choosing.

Hardcoded in Apex

// Vulnerable: the key travels with the source
public with sharing class ShippingClient {
    private static final String API_KEY = 'sk_live_9f2c...';
    public static HttpResponse quote(String body) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://api.shipper.example/v2/quote');
        req.setHeader('Authorization', 'Bearer ' + API_KEY);
        req.setMethod('POST');
        req.setBody(body);
        return new Http().send(req);
    }
}

Salesforce's Secure Secrets Storage module puts it plainly: anyone who can view the source can view an embedded secret, and rotating it means changing every copy in code. In practice that audience includes anyone with repository access, anyone who can open the class in Setup or retrieve it with the Metadata API, and, for an unmanaged package, every org that installs it.

The same is true of keys in Lightning web component JavaScript, Aura helpers and static resources, with a worse audience: that code is downloaded by every browser that loads the component.

Custom settings

Custom settings hold their values as data, not metadata. Deploying a custom setting moves its definition; the records stay behind in the source org. That keeps values out of the repository, which is a real advantage, but it is not the same as keeping them secret inside the org.

  • Public custom settings. Readable by Apex in system mode, and by users who have read access to them. Since Spring '20, users without the Customize Application permission need explicit access (for example the View All Custom Settings permission) to read them outside system context, as Salesforce explained in Secure Custom Metadata Types and Custom Settings. Administrators can still read every value.
  • List custom settings are often granted broadly so that formulas and components can use them. A secret placed there is readable by everyone who was granted the setting.
  • Protected custom settings. Per the Trailhead module, a protected custom setting in a managed package is not visible to subscriber orgs through Apex or the API, and only code in the same namespace can reference it. Outside a managed package, the protected flag offers no such boundary.

Because only the definition is packaged, a protected custom setting needs a post-install script or an admin step to populate the value.

Custom metadata types

Custom metadata records are metadata. They deploy with change sets, packages and source pushes, which is why teams like them for configuration, and why they are a poor place for an unprotected secret:

  • Every record is a file under customMetadata/ in the source tree, committed to Git with the rest of the project.
  • Anyone who can retrieve metadata from the org can retrieve the records.
  • A public custom metadata type is readable through SOQL by users granted access to it.

Managed protected custom metadata is the strongest packaged option: Salesforce recommends it for application secrets, and only code in the package's namespace can read protected records in a subscriber org. Note what that protects against. It hides the value from the subscriber. It does not hide it from the publisher's own repository, where the record file still holds the value in plain text, or from anyone who can read that repository.

Named Credentials and External Credentials

For outbound callouts, Named Credentials with External Credentials are the platform's answer. The authentication material is stored by Salesforce, Apex refers to the endpoint as callout:Shipping_API, and the secret never appears in code:

// Fixed: no secret in source
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Shipping_API/v2/quote');
req.setMethod('POST');
req.setBody(body);
HttpResponse res = new Http().send(req);

Access to an External Credential's principals is granted through permission sets, so the set of users and integrations that can make authenticated callouts is explicit. The limit, which the Trailhead module also notes, is that users with Author Apex or Modify All Data can make callouts through a Named Credential. It stops disclosure in code; it does not stop a highly privileged user from using the credential.

Two mistakes undo the benefit: a <password> element committed in *.namedCredential-meta.xml, and a secret-like parameter value committed in *.externalCredential-meta.xml. Both put the secret straight back into the repository. Our Named Credentials and External Credentials post covers setup in detail.

What leaks where

Storage In the Git repo? In a package? Readable inside the org by
Apex string literal Yes Yes Anyone who can view or retrieve the class
LWC, Aura or static resource Yes Yes Every browser that loads it
Public custom setting Definition only Definition only Admins and users granted the setting
Protected custom setting (managed) Definition only Definition only Package code only, in subscriber orgs
Public custom metadata Yes, with values Yes, with values Admins and users granted the type
Protected custom metadata (managed) Yes, with values Yes, hidden from subscribers Package code only, in subscriber orgs
Named / External Credential No, unless committed by mistake No Used, not read, by permitted principals

Which to use

  • Calling an external service from your own org: Named Credential with an External Credential.
  • Shipping a managed package that needs a secret per subscriber: Named Credential or a protected custom setting populated after install.
  • Shipping a managed package with a secret you control: protected custom metadata, and keep the repository holding it private and access-controlled.
  • Never: Apex literals, client-side code, public custom settings or public custom metadata.

Whichever you choose, a secret that was ever committed stays in Git history after it is removed from the file. Rotate it; deleting the line is not enough. See secrets in Git history.

How Vulkro finds them

Vulkro for Salesforce scans Apex for API keys, OAuth client secrets, JWT signing keys and passwords as string literals, client-side code for hardcoded secrets, Named Credential metadata for committed passwords, and External Credential metadata for hardcoded secret values (externalcredential-hardcoded-secret, Critical). The Named Credentials checks page lists those rules. The anti-pattern pass reports a list custom setting or a public custom metadata type that holds a secret (AP-047, AP-048).

vulkro-sf scan ./force-app
vulkro-sf antipatterns ./force-app

The scan runs on your machine, so the secrets it finds are not sent anywhere to be found.

FAQ

Where should I store API keys in Salesforce?

For callouts, use a Named Credential backed by an External Credential. Inside a managed package, protected custom settings or protected custom metadata can hold secrets that subscriber orgs must not read.

Are protected custom settings secure in a normal org?

Protected visibility is enforced at the managed package boundary. In an org's own unpackaged metadata, it does not hide values from administrators or from Apex in that org.

Is custom metadata safe for secrets?

Only managed protected custom metadata, and only against subscribers. Records are deployable metadata, so the value is in your source repository and visible to anyone who can retrieve it.

Start a 14-day trial from /start/ to find secrets in your Salesforce source. Related: hardcoded secrets in private repos.