Threat modeling: think like the attacker before coding
Threat modeling is the structured conversation that discovers what can go wrong while it is still cheap to fix: in the design, not in the pentest.
What it is
Threat modeling is the exercise of looking at a system (or a feature) and systematically asking what can go wrong, who could cause it and what we do about it. It happens before or during design, when changing a decision costs a conversation and not an entire sprint.
, and find defects that already exist in the code or the environment. The finds design defects: the service that trusts a header the client controls, the password reset flow that leaks whether the email exists, the bucket that mixes tenants' data. No scanner sees these because no line of code is "wrong".
The four questions
The simplest method fits in four questions, repeated at every relevant architecture change:
- What are we working on? A (DFD) with actors, processes, data stores and trust boundaries.
- What can go wrong? Walk through each element and each flow that crosses a looking for threats ( helps here).
- What are we going to do about it? For each threat: mitigate, eliminate, transfer or accept, with an owner and a recorded decision.
- Did we do a good job? Review the model after the release and after each incident, because the system changes and the model ages.
STRIDE: a threat checklist
STRIDE is a Microsoft mnemonic that maps each letter to a violated security property. When looking at a flow in the diagram, you ask whether each of the six categories applies:
- Spoofing (identity forgery) violates authenticity: someone impersonates another user or service.
- Tampering violates integrity: data or code altered in transit or at rest.
- Repudiation violates non-repudiation: there is no log to prove who did what.
- Information disclosure violates confidentiality: data exposed to those who should not see it.
- Denial of service violates availability: the service is exhausted or taken down.
- Elevation of privilege violates authorization: a regular user starts acting as an admin.
Threat model as code
The model does not need to live in a forgotten slide. Tools like pytm describe the system in code, generate the DFD and list the applicable threats. Versioned alongside the repository, the model goes through code review and changes in the same PR that changes the architecture.
from pytm import TM, Server, Datastore, Dataflow, Boundary, Actor
tm = TM("Checkout API")
internet = Boundary("Internet")
vpc = Boundary("Private VPC")
user = Actor("Customer", inBoundary=internet)
api = Server("Checkout API", inBoundary=vpc)
db = Datastore("Orders", inBoundary=vpc, isEncrypted=True)
Dataflow(user, api, "POST /checkout (HTTPS)", isEncrypted=True)
Dataflow(api, db, "INSERT order")
tm.process() # generates the DFD and the STRIDE threat listHow to fit it into the pipeline
The most practical trigger is the same as for code review: a new feature that touches authentication, sensitive data, an external service or a trust boundary calls for half an hour of threat modeling before the first commit. For everything else, a quick threat review in the PR template is enough.
Threats become traceable items: each mitigation points to a test, a SAST rule or a pipeline gate. That way the threat model stops being a document and starts generating checks that run on their own, and the map closes the loop: the design informs what SAST, DAST and the gates need to check.
connections on the map
- Shift-leftstarts at designThreat modeling: think like the attacker before coding
- Threat modeling: think like the attacker before codingdefines what to testSAST
- Threat modeling: think like the attacker before codingbecomes a gateDevSecOps
updated on 14/10/2026 · sources to go deeper · report an error
in other domains
- Security risk managementgrcthe threat model feeds the risk register
- A06 Insecure Designowaspthe method that finds the flaw before the code exists
- Zero-trust - trust modelcloudsecdefines the trust boundaries
- Guardrails - input and output filtersai-agentsmaps what the agent must not be able to do
Study before
test what you learned
Loading questions…