Secrets and least privilege: what a leaked credential can reach
A secret is a credential someone will try to steal
Section titled “A secret is a credential someone will try to steal”A secret is a value that proves a right: a database password, an API key, a signing key, an SSH key, a certificate’s private key. OWASP’s Secrets Management Cheat Sheet starts from a plain observation: many organizations have such values hardcoded in source code in plaintext, and scattered through configuration files and configuration management tools. Often, it adds, services share the same secrets, which makes it hard to identify the source of a leak.
Design for the day one leaks. The questions are how long it stays usable and how much it can reach. Rotation answers the first one. Least privilege answers the second.
Mental model. A leaked secret does damage equal to what it can reach, times the time it stays valid. Rotation shrinks the time. Least privilege shrinks the reach.
Where secrets live
Section titled “Where secrets live”OWASP’s advice has a few threads.
- Not in code, images or configuration files. The sheet’s own list of places is the problem it describes. For containers it says secrets should never be hardcoded with
ENVorARGin a Dockerfile, because they leak with the container definition. - In one place that is built for it. Centralize the storage, provisioning, auditing, rotation and management of secrets. Cloud providers offer such services, and the sheet gives AWS Secrets Manager as an example of one. A sidecar or the application itself fetches what it needs at runtime.
- Not as environment variables, if you can avoid it. The sheet says environment variables are generally accessible to all processes and may be included in logs or system dumps, so it does not recommend them unless the other methods are not possible. Mounted volumes and a sidecar that fetches the secret are the alternatives it describes.
- Access to the store follows least privilege. Engineers should not have access to all secrets in the system, and the system should support fine-grained access control on each object.
- Everything is audited. Who requested a secret and for what, whether it was approved, when it was used, and when it expired.
Server-side encryption of the store protects the secret at rest. OWASP points out that when the secret is handed to another service it is decrypted first, so at-rest encryption is not a substitute for deciding who may ask. The previous lesson made the same point about databases.
Rotation, leases and what a leak is worth
Section titled “Rotation, leases and what a leak is worth”A static secret works until someone changes it. OWASP recommends rotating secrets regularly so that any stolen credentials will only work for a short time, and recommends automating it, since doing rotation by hand leads to mistakes. For secrets that can be created on demand it goes further: dynamic secrets, issued to an application when it starts, with a short lease, revoked when no longer needed. Dynamic secrets, it says, should be used where possible to reduce the surface area of credential reuse. A notable detail is that restarting the application does not itself revoke a stolen credential: it stays usable until the backing service expires or revokes it.
The arithmetic is short. Take a static database password rotated every 90 days and a dynamic one with a lease of 1 hour. Both are stolen just after they are created and nobody notices.
- Static: 90 days × 24 hours = 2,160 hours of use.
- Dynamic: 1 hour.
The static secret is usable 2,160 times as long. These numbers are an illustration of the arithmetic and not a recommendation, and a dynamic secret needs a service that can create credentials on demand. OWASP notes that creating dynamic secrets usually requires long-term static secrets, so the problem is smaller and not gone.
When a secret is exposed
Section titled “When a secret is exposed”Quick response, OWASP says, is perhaps the most critical consideration. Its containment steps are revocation, rotation and deletion: revoke the exposed key immediately, create and deploy a new secret quickly, preferably by an automated process, and remove the old one from the exposed system, including from code and logs. The documentation of the response should say whom to alert and what to log.
Deleting a line from a repository is not revocation. The old commits still hold the value, and anyone who has a copy still has it.
Check your understanding
Section titled “Check your understanding”Check your understanding: secrets and least privilege
This quiz needs JavaScript to check your answers.
Least privilege: limiting reach
Section titled “Least privilege: limiting reach”OWASP defines least privilege as assigning users only the minimum privileges necessary to complete their job, and says it must be applied both horizontally and vertically: an accountant and a sales representative at the same level need different resources, and the head of sales needs more than the team. It applies to a service as much as to a person. Each service gets a credential that reaches what that service needs.
Take five services that share one database credential, and a database with 20 tables.
| Design | Tables the credential reaches | If one service leaks it |
|---|---|---|
| One shared credential, all 20 tables | 20 | 20 tables exposed |
| One credential per service, 4 tables each | 4 | 4 tables exposed |
A leak now exposes 20 ÷ 4 = 5 times fewer tables. A separate credential also tells you which service leaked, which a shared one does not. The numbers are an illustration of the arithmetic. Splitting credentials has a cost: five secrets to store, rotate and audit instead of one, which is why the storage and rotation of the first half of this lesson comes first.
Least privilege is also a process, not a setting. OWASP asks for the permissions designed for each combination of user type and resource, for tests that check they are enforced, and for a periodic review for privilege creep, where privileges grow beyond the design. It adds that it is easier to grant extra permissions than to take away ones someone already enjoyed, so start narrow. NIST’s zero trust document puts the same idea in its third tenet: access is granted with the least privileges needed to complete the task, and for a session. The next lesson is about that.
Tokens carry the idea too. An OAuth scope names the range of access a token gives, and the authorization server may grant a narrower scope than the client asked for. A token with a small scope that leaks does less harm than one with a large scope.
What happens if the shared credential leaks and nobody knows which service lost it?
Section titled “What happens if the shared credential leaks and nobody knows which service lost it?”The database logs show that the credential read all 20 tables at 3 a.m. The five services share it, so the logs say it was used and not by whom. The team has to rotate the credential in five services at once, because they cannot rule any out, and then find the leak by reading five codebases. With a credential per service, the log names the service, the other four keep running, and the revocation affects one.
This is why OWASP lists the audit questions it does: who requested a secret, for what system and role, and when it was used and by whom or what.
- A leaked secret does damage equal to what it reaches times the time it stays valid. Rotation shortens the time, and least privilege narrows the reach.
- Keep secrets out of source code, images and configuration, fetch them at runtime from a store built for them, audit access, and avoid environment variables unless nothing else is possible.
- A static secret rotated every 90 days is usable 2,160 times as long as a dynamic one with a lease of 1 hour, in this illustration.
- When a secret is exposed: revoke, rotate and delete. Deleting a line from the repository does none of these.
- With one credential per service, a leak reaches 4 of 20 tables instead of all 20, 5 times fewer. Least privilege is reviewed over time, because permissions creep.
Next: zero trust, and what changes when the network is not trusted.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.