Security basics
Read first: Authentication and authorization, Networking for request paths
A design needs TLS on every hop, a narrow identity, and secrets that arrive at runtime. Those fall out of a one-paragraph threat model rather than a checklist of product names. Injection is untrusted bytes parsed as code, and SSRF is your server fetching a destination the caller picked.
Security on a design is a short account of who is outside, what they can send, and what you refuse to trust. The controls fall out of that paragraph. A pile of product names, with no attacker and no action you will not take, is not a design. You are not collecting tools. You are deciding which bytes are data and which decisions stay yours.
Core concepts
The threat model, one paragraph. Someone outside the service can send any bytes they want: a path, a header, a body, and a destination they would like you to contact. Treat those bytes as data. Do not execute them as a query, and do not connect where they point unless that destination is one you already decided was safe. Trust the code you deployed, a TLS peer you actually verified, and secrets that came from a secret store at runtime. Do not trust the private network by itself, another service merely because it sits on that network, or an image layer as a hiding place for a credential. When the untrusted bytes ask you to run them or to fetch them, the answer is no, and the design should make the no structural rather than a comment.
That paragraph is the whole method. Everything else on this page is one of its sentences unpacked. If you cannot write the paragraph for the system in front of you, you are not ready to pick a control, because you do not yet know what you are refusing.
TLS. Transport security means the bytes on the wire are not readable by whoever is sitting on the path, and the peer you reached is the one you meant. Networking gives you the handshake shape. TCP is one round trip before data on a cold connection. TLS 1.3 adds about one more. HTTP/3 on QUIC can fold the cryptographic handshake into the first flight. A resumed session can be cheaper. Those are shapes, not a promise about a particular browser or a particular pool. Use them to budget a cold connection. Do not use them as a slogan.
TLS belongs on every hop the request takes, including the hop between services inside the private network. Terminating it at the gateway and speaking cleartext behind that is a design that protects the outside and trusts every process, every misconfigured node, and every packet capture on the inside. “We are in a private network” is not a threat model. It is a hope that the paragraph above already told you to refuse. Verify the peer. Encryption to whoever answered is not TLS. It is a private conversation with an impersonator.
Least privilege. The identity the process runs as can do the job that process exists to do, and it cannot do the adjacent jobs. The webhook worker can open outbound connections to destinations you allowed. It cannot read the payments tables. The payments database user can read and write payment rows. It cannot alter schemas, and it cannot read the user table. The human who debugs production is not the identity baked into the server. A single superuser credential shared by every service means every bug is an account-wide bug. Least privilege does not make the bug impossible. It makes the blast radius match the job. That is the same instinct as a service boundary drawn for compliance: the restricted bytes live in one small place, and the credential that can see them is not the credential everyone else holds.
Secrets out of the image. Configuration that is public can ship in the image. A credential cannot. Anyone who can pull the image can read the layers. Old tags keep the old secret after you edited the file you think is the source of truth. A registry replica, a laptop that pulled the image last month, and the build cache are all copies. Inject the secret at runtime from a store, and let the process read it into memory. Rotation then means the store has a new value and the process picks it up. Rotation does not mean you built a new image and hoped every pull happened. If a secret lands in a log line or an error body, treat it as compromised. You would not leave the access token in a query string. Do not leave the database password in a stack trace either.
Two classes of bug, not a catalog. Injection and server-side request forgery are the classes that show up the moment you treat caller bytes as instructions.
Injection: some other program parses those bytes as code. The database parses a string as SQL. A shell parses a string as a command. A page parser parses a string as markup. The symptom is that a character the user was allowed to type changes the shape of the statement, the command, or the page. The fix is to pass the user bytes as data. Bind parameters for the query. Pass arguments that are not a single shell string. Encode for the markup context you are actually in. You are not memorizing a payload. You are noticing every parser on the path and refusing to concatenate untrusted bytes into its language.
SSRF: the server makes a request to a target the caller chose. Your process’s network position becomes the caller’s. Hosts the caller could not reach, because they are private, link-local, or simply not routed from the internet, are suddenly reachable by asking you nicely. The metadata service on a cloud VM is the sharp version of this. It is how the VM asks who it is, and the answer includes temporary credentials for the instance’s role. It is not a public destination. It is reachable from the VM. A feature that fetches a caller-supplied destination is a bridge to it.
These are classes. A new framework will invent a new parser and a new client library, and the classes will still be the classes. A ten-line checklist of scanners does not survive that. The paragraph at the top does.
flowchart LR
caller[Untrusted caller]
hook[Webhook fetcher]
meta[Metadata service]
creds[Instance credentials]
caller --> hook
hook --> meta
meta --> creds
The caller never touches the metadata service. The fetcher does, because the caller was allowed to name the destination and the fetcher has the VM’s network position. The credentials come back as the body of a request your own code made.
A worked example
You run a payments product. Merchants register a webhook. On each payment event your worker contacts that destination and delivers the event. The worker runs on a virtual machine in the same place as the rest of the fleet. That VM can reach the instance metadata service, the way cloud VMs can ask for their identity and receive temporary credentials. It can also reach other internal services, because nobody put it anywhere else. The only check in the code is that the merchant’s string looks like a destination you might fetch.
Assumptions, so the rate is visible and then set aside:
- 1,000,000 payment events a day. A planning assumption.
- One successful fetch of the metadata service is the whole incident. The rate does not have to be high.
1 day = 86,400 seconds
shortcut ≈ 100,000 seconds
1,000,000 / 86,400 ≈ 12 events/s
1,000,000 / 100,000 = 10 events/s
one fetched credential ends the argument
About a dozen deliveries a second is a boring, healthy worker. The danger is not the QPS. A merchant, or anyone who can edit the webhook, names the metadata service as the destination. Your worker requests it, because requesting destinations is the job. The response body contains the temporary credentials of the instance’s role. The attacker reads that body from whatever place you store delivery responses, or from their own receiver if you followed a redirect, and then uses the credentials as the service. You built a proxy into your own privilege. No public address was required. The metadata service was never on the public internet. The fetcher was the bridge.
Apply the paragraph, in order, and the design changes.
The fetcher moves to a network that has no route to the metadata service and no route to the internal control plane. A mistake in the filter then fails closed. There is nothing useful to connect to except the public destinations you built the worker to reach. This is the structural no. A string check inside a process that can still route to the metadata service is a no you have to get right on every code path, including the ones you have not written yet.
Destinations are allowlisted, or private, link-local, and loopback addresses are refused. The check uses the address you are about to connect to, after name resolution, not the text the merchant typed. A name that looked acceptable can resolve to an address that is not. If the client follows a redirect, the check runs again on the address of the next hop. A check on the first string does nothing once the library has been talked into connecting somewhere else. You do not need a clever destination to understand the bug. You need to notice that you validated a different thing than the socket.
The credential the worker uses for its own calls is not an administrator of the account. Least privilege: this process delivers events. A role that can only do that, living on a machine that cannot reach the metadata service anyway, means a confused fetch returns something small. The superuser role on the same VM would have turned the same bug into control of the account. The bug class does not change. The blast radius does.
Rate limiting the fetcher does not fix this. Twelve requests a second and one hostile request a day have the same shape if that one request returns credentials. A limit is for abuse volume. This is a trust bug. Put the limit on anyway, for the merchant who points you at a huge download, and do not describe it as the mitigation.
The same merchant bytes will also try to be code. The delivery body is data you accepted and stored. Later, a support tool concatenates that body into a query that lists recent deliveries. A character the merchant was allowed to send changes the statement the database runs. That is injection, sitting next to the SSRF, fed by the same untrusted input. Bind the parameter. The body stays a value in a row. It never becomes part of the language. The user-supplied destination is the SSRF class. The user-supplied body, once a parser executes it, is the injection class. One feature, two parsers, two refusals.
What you refuse, written as the paragraph for this worker: callers outside the company can send a destination and a body, and both are hostile. You trust the runtime secret store and the allowlist of networks this process can route to. You do not trust the destination string, the resolved address, a redirect, or the body. You will not execute the body as a query. You will not connect to the metadata service or to any internal address, because this process has no route there and would refuse the address even if it did.
Tradeoffs
| Choice | What you gain | What you spend |
|---|---|---|
| TLS on every hop, peer verified | The private network is not enough to read or impersonate the call | Certificates to issue and rotate, and a handshake on cold connections |
| TLS only at the gateway | Less machinery inside the fleet | Any observer on the private network can read the inner calls |
| One narrow identity per service | A stolen credential stays inside that job | More identities to mint and to audit |
| One shared superuser | Deploys are easy to explain | The webhook bug becomes account takeover |
| Secrets injected at runtime | Rotation does not depend on rebuilding an image | The process must be able to reach the store at start |
| Secrets baked into the image | The process always has them, even offline | The registry, every old tag, and every laptop that pulled it |
| Fetcher on a network with nothing internal to reach | A bad destination fails closed | A separate network to operate, and webhooks that truly must be public |
| String check on the merchant’s text, process still fully networked | A small code change | The check is on the wrong thing, and one miss returns credentials |
Pitfalls
- TLS terminated at the gateway and cleartext inside. The symptom is a packet capture, or a compromised node, reading tokens and bodies that you described as encrypted.
- TLS that does not verify the peer. The symptom is a client happily talking to whoever answered the address.
- A credential in the image, the Dockerfile, or the git history that builds the image. The symptom is the old tag still authenticating after you “rotated” the value in the source file.
- One database superuser in every service’s config. The symptom is a bug in the least interesting service becoming a full read of payments and users.
- Concatenating caller bytes into a query or a shell command. The symptom is a character in a name, a webhook body, or a search box changing what the database executes.
- Fetching a caller-supplied destination from a fully networked VM. The symptom is a delivery response that contains instance credentials, or a response from an internal admin port.
- Validating the destination string and then following a redirect. The symptom is a check that passed on the text the merchant typed, and a connection that went somewhere else.
- Logging secrets, tokens, or the authorization header. The symptom is the credential living in the log store for as long as you retain logs, which is the theft window.
- Calling a ten-item tool list a threat model. The symptom is a design review that names products and never says who is outside or what the service refuses to fetch.
In the interview
Say the paragraph first, in the specific form for the system you are designing. “Callers can send a body and, if we have webhooks, a destination. I do not trust either. TLS on every hop, the service runs as a limited identity, secrets come from a store at runtime, and the webhook worker cannot route to the metadata service or to internal admin addresses.” Then one sentence of consequence: a fetcher that can reach the metadata service will eventually be asked to, and the response is the instance’s credentials.
Skip cipher suites. Skip a catalog of header names. Skip compliance acronyms. None of those tell the interviewer you know what you refuse to trust. If you list ten tools, they will ask you what one of them does, and the honest answer will be that you hoped the tool was the design.
The question that usually follows is “customers need webhooks to arbitrary public destinations.” Put the worker on a network with no path to metadata or to the internal fleet. Check the address after resolution, and check it again if you follow a redirect. Give that worker a credential that can deliver events and nothing else. Accept that a customer can still point you at a hostile public server, and do not send that server your secrets, in the URL or in the body. The tradeoff is an extra network to operate, against a structural no. Take the structural no. A string check in a process that can already reach everything is a bug you will eventually write around.
The number, if you need one, is the handshake, not a fake breach cost. A cold connection is a TCP round trip plus about one more for TLS 1.3. That belongs in the latency budget. It is not a reason to skip TLS inside the fleet.
Check yourself
What has to be in the one-paragraph threat model?
Who is outside, what they can send, and what you refuse to trust. For a normal service that is the caller and their bytes, including a destination if you fetch one. You trust verified TLS peers, your code, and the secret store. You do not trust the private network, a neighbor service’s mere location, or an image layer as a place to keep a credential. The paragraph should name the refusal: you will not execute their bytes as a query, and you will not connect where they point unless you already decided that destination was safe.
You rotated a database password in git. The running image was built last week. What is still true?
The old password is in that image’s layers, and in any registry replica or laptop that pulled the tag. Editing the source file did not edit the bytes already built. A secret has to arrive at runtime from a store, so rotation is a new value the process reads, not a rebuild you hope rolled out. If the old value was also copied into logs or an error report, those copies are still live too.
A webhook worker on a normal VM fetches a merchant-supplied destination and can reach the metadata service. What does the attacker get?
They get the worker to request the metadata service for them. The response carries the temporary credentials of the instance’s role, which the attacker then uses as the service. The metadata service was never publicly routed. The worker was the bridge. The fix is a network with no route there, a check on the address you actually connect to, including after redirects, and a role that could not administer the account even if the fetch succeeded.
Why is a check on the destination string not enough if the HTTP client follows redirects?
You validated the text the merchant typed. The client then connected to a different place, because a redirect said to. The check and the socket disagree. Run the same address check on the hop you are about to open, after name resolution, and again on any redirect. Better, run the worker somewhere those internal addresses do not route, so a missed check has nothing to find.
Based on: Google SRE book and SRE workbook; Designing Data-Intensive Applications, Kleppmann and Riccomini.