Most of this series so far has been about fixing classes of bugs in code. Some of the most damaging problems, though, are design flaws: a feature that trusts the wrong party, an architecture where one compromised service reaches everything, a workflow with no limit on abuse. No amount of careful coding fixes a flawed design. Threat modeling is the practice of finding those flaws while they're still drawings and tickets, and secure by design is the set of architectural habits that keep them from appearing in the first place. This lesson gives you a lightweight, repeatable method you can run in an hour with your team.
Before you start
You should be comfortable with the material in the Intermediate level, especially API Security: Protecting Tokens, Secrets, and Sensitive Data. The worked example is a small web application: a booking site with customer accounts, an admin dashboard, a PostgreSQL database, and an email provider. Bring a real feature from your own work to practice on.
What threat modeling is
Threat modeling is a structured way of asking what could go wrong with a system and what to do about it. The Threat Modeling Manifesto, written by a group of practitioners, frames it as four questions:
- What are we working on?
- What can go wrong?
- What are we going to do about it?
- Did we do a good enough job?
It doesn't require special tools or a security team. A whiteboard and the people who know the system are enough. The value comes from doing it early (when a design is being decided), often (when it changes significantly), and in proportion to the risk (an hour for a feature, longer for a payment system).
Running a threat model
Step 1: draw what you're building
Start with a data flow diagram (DFD). It shows four kinds of things:
- External entities: people and systems outside your control, such as customers, admins, and the email provider.
- Processes: code you run, such as the web application and a background job worker.
- Data stores: databases, caches, file storage, logs.
- Data flows: arrows showing data moving between them, labeled with what moves (for example, "sign-in credentials" or "booking confirmation email").
Then draw trust boundaries: lines where the level of trust changes. Typical boundaries are between the internet and your server, between the application and the database, between your system and a third-party provider, and between ordinary users and administrators. Every arrow that crosses a boundary is a place to ask questions, because data arriving from the less-trusted side can't be assumed to be what it claims.
For the booking site, the boundaries are the browser and the web app (the internet), the web app and the database, the web app and the email provider, and the admin dashboard and the rest of the app.
Step 2: ask what can go wrong with STRIDE
STRIDE, a mnemonic developed at Microsoft, gives you six categories of threat to ask about at each boundary crossing:
- Spoofing: can someone pretend to be another user or system? (Weak sign-in, unverified webhooks.)
- Tampering: can someone change data they shouldn't? (Editing another user's booking, changing a price in a request.)
- Repudiation: can someone deny an action because there's no reliable record? (Admin deletes data with no audit log.)
- Information disclosure: can data leak to someone who shouldn't see it? (API returning other customers' phone numbers, verbose errors.)
- Denial of service: can someone make the system unavailable? (Unlimited booking requests, expensive search.)
- Elevation of privilege: can someone gain rights they shouldn't have? (Customer reaching the admin API.)
Walk each boundary crossing and each category. Write down every plausible threat as a short sentence: "A signed-in customer can cancel another customer's booking by changing the booking ID." Aim for quantity at first; you'll prioritize in the next step.
For privacy-specific threats, the LINDDUN framework plays the same role as STRIDE, with categories such as linking, identifying, data disclosure, unawareness, and non-compliance. It's useful for features that process a lot of personal data.
Step 3: decide what to do about each threat
For each threat, choose one of four responses:
- Mitigate: change the design or add a control. Most threats end here.
- Eliminate: remove the feature or the data that creates the threat. Often the cheapest option.
- Transfer: move the risk to a party better placed to handle it, such as using a payment provider so you never handle card numbers.
- Accept: consciously decide the risk is low enough, and write down who decided and why.
Rank threats by likelihood and impact so effort goes where it matters. A simple high, medium, or low for each is enough; heavyweight scoring systems rarely change the decisions. Then turn every mitigation into something trackable: a design change, a ticket with acceptance criteria, and ideally a test.
A threat register for the booking site might start like this:
id,threat,stride,likelihood,impact,response,mitigation,ticket
T1,Customer cancels another customer's booking by changing the ID,Tampering,high,high,mitigate,Scope booking queries to the customer; two-account test,BOOK-112
T2,Attacker sends fake payment-confirmed webhooks,Spoofing,medium,high,mitigate,Verify provider signature and timestamp,BOOK-113
T3,Admin deletes bookings with no record,Repudiation,medium,medium,mitigate,Append-only audit log of admin actions,BOOK-114
T4,Booking form floods email provider,Denial of service,medium,medium,mitigate,Per-IP and per-account rate limits,BOOK-115
T5,Customer birth dates leak in a breach,Information disclosure,low,high,eliminate,Stop collecting birth date; ask for age confirmation,BOOK-116Step 4: check your work
After the mitigations are built, revisit the model. Do the tests exist and pass? Did the design change in ways that add new boundaries? Did anything you accepted turn out to matter? A threat model is a living document: keep it in the repository next to the code, and update it when the architecture changes.
Secure-by-design patterns
Threat modeling finds problems; good architecture prevents whole categories of them. These patterns appear again and again in secure systems, and in guidance such as CISA's Secure by Design principles and OWASP ASVS chapter V15 (Secure Coding and Architecture).
Least privilege
Every component gets only the access it needs. The web application's database role can't alter tables. The background worker that sends emails can read bookings but not passwords. The CI pipeline's cloud credentials can deploy one service, not create new accounts. When something is compromised, least privilege limits how far the attacker gets.
Secure defaults and deny by default
The safe behavior should be what happens when someone forgets to configure something. New routes should require authentication unless explicitly marked public. New users should get the lowest role. New storage buckets should be private. Permission checks should return "no" for unknown roles or actions.
// Every route requires a session unless it's listed here. Forgetting a check fails closed.
const PUBLIC_ROUTES = new Set(["/api/health", "/api/sign-in", "/api/password-reset"]);
app.use((req, res, next) => (PUBLIC_ROUTES.has(req.path) ? next() : requireSession(req, res, next)));Make the secure way the easy way
Developers take the easiest path, so make it the secure one. Provide a data access layer where every query function takes the current user and scopes results automatically. Provide a single HTTP client for outbound requests with SSRF protection built in. Provide a template system that encodes by default. A shared, well-tested abstraction removes the need for every developer to remember every rule.
// The only way the app reads bookings: always scoped to the caller.
export function bookingsFor(session: { customerId: string }) {
return {
list: () => db.booking.findMany({ where: { customerId: session.customerId } }),
get: (id: string) => db.booking.findFirst({ where: { id, customerId: session.customerId } }),
};
}Validate at trust boundaries
Data crossing into a more trusted zone is parsed and validated once, at the boundary, into a well-typed internal form. Code inside the boundary then works with values that are known to be valid. This is sometimes summarized as "parse, don't validate."
Defense in depth
Assume any single control can fail, and layer them: input validation and parameterized queries; output encoding and CSP; authorization in the API and row scoping in queries; network rules and least-privilege credentials. An attacker then needs several things to go wrong at once.
Minimize what you hold and expose
Every stored field, open port, endpoint, and dependency is attack surface. Remove features and data that nobody needs, and keep administrative interfaces off the public internet or behind strong authentication. This is the design-level form of the data minimization covered in lesson 4.
Fail safely
When something unexpected happens, the system should end in a safe state: deny access, roll back the transaction, show a generic error, and log the details for operators. This is the design side of OWASP's A10, Mishandling of Exceptional Conditions.
Fitting threat modeling into your workflow
- Design reviews: add a "What can go wrong?" section to design documents for any feature that touches authentication, payments, personal data, file handling, or external integrations.
- Pull requests: a checklist question such as "Does this add a new trust boundary, data store, or external call?" signals when to update the model.
- Time-box it: one hour for a typical feature is often enough. The goal is better decisions, not a perfect document.
- Include the right people: developers who know the code, someone who knows the product, and, if available, someone with security experience. Diversity of perspective finds more threats.
Common mistakes and troubleshooting
- Modeling too late. After launch, design changes are expensive. Model during design.
- Trying to model everything at once. Start with one feature or one flow.
- Producing a document no one uses. Turn every mitigation into a ticket and a test, and keep the model in the repository.
- Only thinking about outside attackers. Include malicious or curious users, compromised dependencies, and insiders with too much access.
- Accepting risks silently. Record who accepted a risk, why, and when to revisit it.
- Endless debate about scores. High, medium, low is enough to prioritize.
If a session stalls, go back to the diagram and pick one arrow that crosses a trust boundary. Ask the six STRIDE questions about that arrow alone. Concrete questions about one flow unblock most discussions.
Practical exercise: model one feature
Pick a feature from your own work, such as file sharing, team invitations, or checkout:
- Draw a data flow diagram with external entities, processes, data stores, flows, and trust boundaries.
- For every flow that crosses a boundary, list at least one threat per STRIDE category, or note why one doesn't apply.
- Rank the threats by likelihood and impact, and choose a response for each.
- Write a threat register like the one above, with tickets for every mitigation.
- Pick the top threat and write the test that would prove its mitigation works.
Key takeaways
- Threat modeling answers four questions: what are we building, what can go wrong, what will we do, and did we do enough.
- Data flow diagrams with trust boundaries show where to look; STRIDE (and LINDDUN for privacy) structure what to ask.
- Mitigate, eliminate, transfer, or consciously accept each threat, and turn mitigations into tickets and tests.
- Secure-by-design patterns, such as least privilege, deny by default, secure shared abstractions, boundary validation, defense in depth, and failing safely, prevent whole classes of bugs.
References
- Threat Modeling Manifesto (opens in a new tab)
- OWASP Threat Modeling Cheat Sheet (opens in a new tab)
- Microsoft Threat Modeling Tool: STRIDE threat categories (opens in a new tab)
- LINDDUN privacy threat modeling (opens in a new tab)
- CISA: Secure by Design (opens in a new tab)
- OWASP ASVS 5.0 (opens in a new tab)
- OWASP Top 10:2025, A06 Insecure Design (opens in a new tab)
What's next
Some features carry more risk than others by their nature. Advanced Application Hardening: SSRF, File Uploads, and Security Headers works through three of the riskiest and the controls that tame them.

