Skip to main content

Code reviews rule: Hardcoded secrets

Written by David Martin
Updated yesterday

Hardcoded secrets

Why is this an issue?

Secrets in source code get committed to version control and become visible to anyone with repository access. They can't be rotated without a code change and redeployment, making incident response slow and error-prone.

Examples

Example of incorrect code:

String awsKey = 'AKIALALEMEL33243OLIA';

Example of correct code:

// Use Named Credentials for API authentication
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:My_API/endpoint');

How can I fix violations?

  1. Use Named Credentials: Store API credentials in Named Credentials.

  2. Rotate compromised secrets: If secrets were already committed, treat them as compromised and rotate them immediately.

Resources

Did this answer your question?