07. Security: cheatsheets

3 lessons, one page each when printed.

System Design · 07. Security · 10 min · Beginner

Authentication

Prove who you are, once, and keep it safe across many requests.

In short

Prove who you are once, with a salted slow-hashed password, a provider (OAuth or SSO) or both, plus a second factor. Then carry a small proof: a session cookie, or a short access token with a refresh token behind it.

Verify every token, keep lifetimes short, limit login tries, and use trusted libraries. Next, authorization decides what the proven user may do.

Key ideas

  • Authentication happens once. After that, a small proof (a cookie or a token) rides along with every request. (step 1)
  • Access token: short, sent everywhere. Refresh token: long, sent only to one endpoint, and stored with care. (step 5)

Good to know

  • HttpOnly hides the cookie from page scripts, Secure sends it over HTTPS only. (step 3)
  • Check the state value you sent, and use PKCE for apps that cannot keep a secret. They block forged logins. (step 6)
  • SMS codes are better than nothing, but SIM swapping can steal them. Prefer an app, a key or a passkey. (step 8)

Watch out

  • Never use a plain fast hash for passwords, and never invent your own scheme. Use a library. (step 2)
  • Always check the signature, the expiry (exp) and the algorithm. A token you do not verify proves nothing. (step 4)
  • The identity provider becomes a single point of failure and a prime target. Protect it, and have a backup way in. (step 7)
  • Do not build your own login system if you can use a trusted library or provider. The details are many and easy to get wrong. (step 9)
BrowserPhone appApp serverDatabase

Key numbers

Factors
2
Access lasts
15 min

Terms

Authentication
Proving who you are; authorization, the next lesson, decides what you may do.
Password hashing
Storing a one-way scramble of the password so a leaked database does not reveal it.
Salt
A random value added per user before hashing, so equal passwords give different hashes.
Session cookie
A random id sent as a cookie, which the server looks up to find the logged-in user.
JWT
A signed token holding claims such as the user id and expiry, checked without a lookup.
Refresh token
A long-lived token used only to get new short-lived access tokens.
OAuth and OpenID Connect
A way to log in through another provider: the app gets a code, swaps it for tokens and learns who the user is.
SSO (single sign-on)
One login at an identity provider that many apps trust.
MFA (multi-factor authentication)
Asking for two or more kinds of proof, such as a password and a code from a phone.
User enumeration
Learning which accounts exist from different error messages; avoid it with one message for both cases.

System Design · 07. Security · 8 min · Beginner

Authorization

After we know who you are, decide what you are allowed to do, on every request.

In short

Authentication says who; authorization says what, and it is checked on the server for every request. Roles keep the rules short (5,060 entries instead of 100,000), attributes handle the details, and an ownership check keeps Ann to her 3 invoices out of 10,000.

Check at the gateway and in the service, give each credential the least it needs, and log every decision.

Key ideas

  • Authentication: who. Authorization: what. They are two different checks and both are needed. (step 1)
  • Defence in depth: the gateway stops the obvious, the service has the last word. (step 6)

Good to know

  • RBAC is easy to explain and audit. Its weakness is role explosion when rules get specific, such as 'editor, but only for Europe'. (step 3)
  • Many systems mix both: roles for the broad rights, attributes for the details. (step 4)
  • Ask 'what is the worst thing this credential can do?' and make that answer small. (step 7)

Watch out

  • Deny by default: if no rule says yes, the answer is no. Forgetting a rule should lock a door, not open it. (step 2)
  • Random-looking ids do not protect you. The check must be in the code, never in the hope that nobody guesses. (step 5)
  • Logs help only if someone looks. Alert on bursts of denials and on use of admin rights. (step 8)
Ann's browserAPIDatabaseAudit log

Key numbers

Decision
Allow / Deny
Entries to manage
5,060
Invoices Ann can read
3

Terms

Authorization
Deciding what an identified user may do, as opposed to authentication, which says who they are.
Deny by default
If no rule allows an action, the answer is no.
403 Forbidden
The HTTP status for a request from a known caller who is not allowed to do this.
RBAC (role-based access control)
Permissions belong to roles, and people are given roles.
ABAC (attribute-based access control)
Rules decide from attributes of the user, the resource and the situation.
Ownership check
Making sure the resource belongs to the caller, for example with owner_id in the query.
Broken object-level authorization
The bug where a user can read or change other users' objects by changing an id.
Defence in depth
Checking at several layers, such as the gateway and the service.
Least privilege
Giving each person, service or token only the rights it needs.
Audit log
An append-only record of who did what and whether it was allowed.

System Design · 07. Security · 10 min · Intermediate

Encryption & Secrets

Protect data on the wire and on disk, and keep the keys and passwords that unlock it safe.

In short

TLS protects data in transit, encryption protects it at rest, and a KMS with envelope keys makes rotation cheap: 10 s instead of 83 min. Hash what you only compare (passwords), encrypt what you must read again.

Keep secrets out of code (20 clones is 20 leaks), put them in a secret manager, rotate them, and verify every call.

Key ideas

  • Public-key maths to agree on a key (slow, once), a symmetric cipher for the data (fast, always). (step 2)
  • Separate the key from the data, control who may use it, and log every use. (step 4)
  • Authenticate and authorize every request, wherever it comes from. Assume something is already breached. (step 9)

Good to know

  • Turn on encryption at rest everywhere by default: databases, volumes, backups and object storage. (step 3)
  • Cloud services (S3, EBS, databases) all work this way. Big data stays where it is; only keys move. (step 5)
  • Rotate on a schedule, not only after a leak, so rotation is a routine and not an emergency. (step 8)

Watch out

  • Never rely on the network being private. Assume the path is hostile. (step 1)
  • Do not encrypt passwords: a key that can decrypt them can leak. Do not hash data you need to read back. (step 6)
  • If a secret was ever committed, treat it as leaked and rotate it. Removing it from the next commit is not enough. (step 7)
AppKMS (key service)Secret managerFile store

Key numbers

Readable by
Only who needs it
Key rotation
10 s
Copies of secret
1

Terms

Data in transit
Data moving across a network, which others on the path could read or change.
TLS
The protocol behind HTTPS: it proves the server's identity and encrypts all data with a shared key.
Certificate
A signed proof that a public key belongs to a named site.
Encryption at rest
Storing data on disk in encrypted form so a stolen disk or backup is unreadable.
KMS (key management service)
A vault that holds master keys and encrypts or decrypts for authorized callers without revealing the key.
Envelope encryption
Encrypt data with a data key, and encrypt that data key with a master key held in a KMS.
Hash
A one-way function that turns any input into a fixed-size fingerprint.
Password hashing
Storing passwords with a slow, salted hash such as bcrypt or Argon2, so they cannot be read back.
Secrets in code
Passwords or keys written in source files, which then live in every clone of the repository.
Secret manager
A service that stores secrets, gives them only to allowed apps at run time and supports rotation.
Key rotation
Replacing a key or secret with a new one on a schedule or after a leak.
Zero trust
No caller is trusted for its network location; every request is authenticated and authorized.