Sub topic 18
Security practices for tokens and secrets
This chapter assumes you have configured a connected app and made token requests with at least one of the three flows. It covers how to keep the credentials involved from leaking, and how to limit the damage if they do.
What needs protecting
Each flow leaves you holding secrets of different value.
| Item | Used in | Risk if leaked |
|---|---|---|
| Consumer secret | Web server, client credentials | Anyone can request tokens as your app |
| Private key (`server.key`) | JWT bearer | Anyone can sign assertions as the integration user |
| Refresh token | Web server | Long-lived access until revoked |
| Access token | All flows | Access until it expires, within the granted scopes |
| Authorization code and PKCE verifier | Web server | Short-lived, but usable once if intercepted |
The consumer key is an identifier, not a secret. Treat everything else in the table as a password.
Store secrets outside your code
Never commit secrets to source control or put them in logs. Load them at runtime from environment variables or a secrets manager.
import os
CONSUMER_KEY = os.environ["SF_CONSUMER_KEY"]
CONSUMER_SECRET = os.environ["SF_CONSUMER_SECRET"]
with open(os.environ["SF_PRIVATE_KEY_PATH"], "rb") as f:
private_key = f.read()For the private key, a secrets manager is better than a file on disk. If you must use a file, restrict it:
chmod 600 server.keyKeep separate connected apps, keys, and secrets for each environment. A leaked sandbox secret should never work in production.
Handle tokens carefully
- Send tokens only over HTTPS, and only in the
Authorizationheader. Do not put them in URLs, where they end up in logs and browser history. - Keep access tokens in memory where you can. Store refresh tokens encrypted, because they are the most valuable token you hold.
- Do not log full token responses. Log the
instance_urlor a token's last few characters if you need to correlate requests. - Always send and verify the
statevalue, and use PKCE, in the web server flow. - Keep the JWT
expclaim short, as the earlier example does with 120 seconds.
Limit what a leak can do
Least privilege reduces the damage from any single leak.
- Request only the scopes you need. Add
refresh_tokenonly if the app must work while the user is away. - Give the run-as user or the JWT integration user a dedicated profile or permission set with only the object and field access the integration needs. Do not use an administrator.
- Restrict the connected app with IP relaxation settings and, where the integration has fixed addresses, login IP ranges.
- Use a sandbox to test, and never reuse production credentials there.
Rotate and monitor
Plan rotation before you need it. Certificates created with -days 365 expire, so put the expiry date on a calendar. The connected app can hold more than one certificate during a changeover.
Review the connected app's usage and the login history of the integration user for unexpected IP addresses or times. Remove connected apps and users you no longer need.
Exercise
A teammate commits a file containing the consumer key, consumer secret, and a refresh token to a shared repository. Which of these need action, and what do you do?
Show answer
The consumer secret and the refresh token need action. The consumer key is an identifier and is not secret. Revoke the refresh token, generate a new consumer secret in the connected app, deploy it through your secrets manager, and remove the old one. Remove the file from the repository and its history, and check the integration user's login history for unexpected activity.
Exercise
Name two ways to limit the harm if a JWT bearer integration's private key is stolen.
Show answer
Give the integration user a minimal permission set instead of broad access, and restrict the connected app by IP range. Rotating the certificate and removing the old one also stops the stolen key from signing valid assertions.