Prompt
How do I protect API keys and secrets in a SaaS application?
Latest observation
Protecting API keys and secrets in a SaaS app comes down to one rule:
Never let secrets live in places the user can inspect or control.
Here’s the practical approach.
1) Keep secrets on the server
- Store API keys, database passwords, signing keys, and third-party credentials only in backend systems.
- Never ship them in:
- frontend JavaScript
- mobile apps
- public config files
- Git repositories
- logs or error messages
If the browser can access it, assume the user can extract it.
2) Use a secrets manager
Use a dedicated secrets store instead of plaintext env vars or code:
- AWS Secrets Manager / Parameter Store
- GCP Secret Manager
- Azure Key Vault
- HashiCorp Vault
Benefits:
- encrypted at rest
- access-controlled
- auditable
- easier rotation
3) Load secrets at runtime, not from code
- Keep secrets out of source code.
- Inject them via:
- environment variables in production
- secret manager lookups at startup/runtime
- deployment platform secret features
Avoid committing .env files to git. Add them to .gitignore.
4) Use least privilege
Give each secret the minimum access it needs.
- Separate keys for separate services/environments
- Dev/staging/prod should all have different credentials
- Restrict API keys by:
- allowed IPs
- allowed domains
- scopes/roles
- specific endpoints if supported
If a key leaks, the damage should be limited.
5) Rotate secrets regularly
- Rotate on a schedule
- Rotate immediately if exposure is suspected
- Design systems so rotation is painless:
- support multiple active keys during transition
- avoid hard-coding long-lived credentials
6) Don’t expose provider keys to clients
If your SaaS integrates with OpenAI, Stripe, AWS, etc.:
- client app should call your backend
- backend calls the external API using the secret key
- frontend receives only the result it needs
If you must use a public client-side key, ensure it is truly public and tightly restricted.
7) Secure logs and error handling
- Never log secrets
- Redact headers, tokens, API keys, and passwords
- Be careful with stack traces and debug output
- Sanitize request/response logging middleware
A lot of secret leaks happen through logs, not code.
8) Encrypt sensitive data in transit and at rest
- Use HTTPS/TLS everywhere
- Encrypt databases and backups
- Use app-layer encryption for especially sensitive fields when needed
9) Use short-lived credentials when possible
Prefer temporary credentials over static long-lived secrets:
- OAuth tokens with refresh flow
- cloud IAM roles / workload identity
- signed short-lived tokens
- STS-style temporary credentials
These reduce blast radius if compromised.
10) Secure your CI/CD pipeline
CI/CD often becomes the weakest point.
- store secrets in the CI tool’s secret store
- restrict who can view or edit pipeline variables
- don’t print secrets in build logs
- use OIDC/workload identity instead of long-lived cloud keys where possible
11) Scan for leaked secrets
Use automated detection:
- secret scanning in git repos
- pre-commit hooks
- dependency and artifact scanning
- alerting for exposed credentials
Also immediately revoke anything found in public history.
12) Separate tenant data and tenant credentials
In SaaS, each customer may bring their own API key.
- encrypt per-tenant secrets
- isolate tenant access logically
- store metadata separately from the secret itself
- validate that one tenant can never read another tenant’s credentials
Common mistakes to avoid
- putting secrets in frontend code
- storing secrets in local storage or cookies
- hard-coding keys in mobile apps
- using the same key across all environments
- not rotating keys
- logging full request headers
- trusting
.envfiles as “secure” in production - exposing internal admin keys to customer-facing systems
Simple architecture pattern
Frontend → Your backend API → Third-party service
- frontend never sees the third-party secret
- backend authenticates and proxies the request
- backend applies authorization, rate limiting, validation, and auditing
That’s the safest default for most SaaS products.
If you want, I can also give you:
- a checklist for production,
- a reference architecture, or
- examples for Node.js, Python, or AWS/GCP/Azure.