Prompt
How do I secure secrets and API keys in a SaaS application?
Latest observation
Securing secrets and API keys in a SaaS app is mostly about minimizing exposure, isolating access, and preventing accidental leakage. Here’s a practical approach.
1) Never store secrets in the frontend
- Do not embed API keys in browser code, mobile apps, or public JS bundles.
- Anything shipped to the client should be treated as public.
- If the client needs to call a third-party API, route it through your backend or use a short-lived, restricted token.
2) Use a secrets manager
Store secrets in a dedicated secret management system rather than in code or plain env files:
- AWS Secrets Manager / SSM Parameter Store
- GCP Secret Manager
- Azure Key Vault
- HashiCorp Vault
- Kubernetes Secrets only if additionally encrypted and tightly controlled
Benefits:
- Encryption at rest
- Access control
- Audit logs
- Rotation support
3) Use environment variables only for local/simple cases
Environment variables are better than hardcoding, but they’re not a full security solution:
- Good for deployment-time injection
- Avoid committing
.envfiles - Restrict who can read runtime environment data
- Don’t log env values or dump process state
For production, a secrets manager is usually preferable.
4) Apply least privilege
Each secret should have the narrowest scope possible:
- Use separate credentials per service
- Restrict APIs to only required actions
- Limit IPs, regions, domains, or resource scopes if supported
- Separate development, staging, and production secrets
Example:
- A billing integration key should not have access to user data storage.
- A read-only reporting token should not be able to write or delete.
5) Rotate secrets regularly
- Use short-lived credentials where possible
- Automate rotation for long-lived secrets
- Make rotation part of incident response
- Ensure apps can reload credentials without downtime
Useful patterns:
- Dual-secret rotation
- Grace periods for old keys
- Versioned secret references
6) Prefer short-lived tokens over static keys
Where possible, use:
- OAuth access tokens
- STS/temporary cloud credentials
- Signed JWTs with expiration
- mTLS certificates with short lifetimes
This reduces blast radius if a secret leaks.
7) Encrypt secrets in transit and at rest
- Use HTTPS/TLS everywhere
- Encrypt stored secrets with strong encryption
- Protect encryption keys using a KMS/HSM
- Avoid rolling your own cryptography
8) Control access tightly
Limit who and what can access secrets:
- Role-based access control
- Service identities / workload identity
- Separate human access from application access
- Require MFA for admins
- Use just-in-time access for operators
9) Log carefully
- Never log secrets, tokens, auth headers, connection strings, or private keys
- Scrub sensitive fields from application and access logs
- Redact in error reporting tools
- Be careful with debug mode and stack traces
10) Prevent secrets from entering source control
- Add secret-scanning in CI/CD and git hooks
- Block commits containing known secret patterns
- Use tools like GitHub secret scanning, TruffleHog, Gitleaks
- Rotate any secret that has ever been committed
11) Protect CI/CD pipelines
CI/CD often becomes a leak path:
- Store pipeline secrets in the CI platform’s secret store
- Scope secrets to specific jobs/environments
- Avoid printing env vars
- Use OIDC/workload identity instead of long-lived cloud keys in CI
- Restrict forked PRs from accessing sensitive secrets
12) Separate tenant data and tenant secrets if applicable
For multi-tenant SaaS:
- Avoid shared credentials across tenants when possible
- Use tenant-specific encryption keys or namespaces
- Enforce tenant isolation in secret retrieval and authorization
- Track secret ownership by tenant/app/environment
13) Use secure storage for client-side tokens
If you must store auth tokens in the browser:
- Prefer httpOnly, Secure, SameSite cookies for session tokens
- Avoid localStorage for sensitive tokens if possible, since it’s vulnerable to XSS theft
- Use CSRF protections when using cookies
14) Monitor and alert
- Audit secret access
- Alert on unusual reads or usage
- Detect impossible travel, unusual geographies, spikes in API use
- Track secret version usage to catch stale deployments
15) Have an incident response plan
If a secret is exposed:
- Revoke it immediately
- Rotate it
- Check logs for misuse
- Notify impacted parties if needed
- Patch the leak source
- Add controls to prevent recurrence
Recommended architecture for a SaaS app
A solid default setup looks like this:
- Frontend: never holds long-term secrets
- Backend: reads secrets from a secrets manager at startup or via runtime fetch
- Cloud identity: workload identity / IAM role instead of static cloud keys
- CI/CD: uses OIDC to assume roles, not hardcoded deploy keys
- Database/app config: encrypted at rest, access tightly scoped
- Monitoring: audit logs + secret scanning + alerting
- Rotation: automated and tested
Common mistakes to avoid
- Hardcoding keys in code
- Shipping secrets in JS bundles
- Using one shared key across all environments
- Storing secrets in plain
.envfiles in production - Logging Authorization headers
- Letting CI jobs access production secrets by default
- Keeping old keys active forever
If you want, I can also give you:
- a reference architecture for AWS/GCP/Azure,
- a checklist for SaaS secret management, or
- examples for Node.js, Python, or Kubernetes.