Skip to main content
Module 1 · Lesson 1 of 31/42 in the course~14 min
Module lessons (1/3)

Threat Modeling & Least Privilege

Every critical web vulnerability — SQL injection, Cross-Site Scripting (XSS), forged JWTs, broken multi-tenant isolation, or leaked .env secrets — stems from one core structural bug: untrusted client data crosses a trust boundary and is treated as trusted instruction or identity.

1. Anatomy of a Trust Boundary & STRIDE

Compare what arrives over the network versus what your server already verified inside a signed session:

In Microsoft's STRIDE model, six failure categories cover almost every production web flaw:

JS
// Annotated Example 1: Why spreading `req.body` destroys identity boundaries
function handleExportUnsafe(session, reqBody) {
  // BUG: attacker-controlled `reqBody` sits AFTER or overrides `session`!
  const ctx = { role: session.role, tenantId: session.tenantId, ...reqBody };
  return ctx.role === 'admin' ? exportTenant(ctx.tenantId) : deny();
}

// Annotated Example 2: Hardened server-verified context (trusted claims win)
function handleExportSafe(session) {
  if (!session || session.role !== 'admin') {
    return { allowed: false, tenantId: null };
  }
  // Never read `role` or `tenantId` from request payloads
  return { allowed: true, tenantId: session.tenantId };
}

2. Interactive Explorers: Trust Boundaries & Audit Telemetry

Click through each architectural boundary and toggle Least Privilege, then inspect how correlated trace IDs and log redaction capture the incident trail:

Bash
# Annotated Example 3: Bypassing React UI form restrictions with a single CLI request
curl -X POST https://api.app.test/v1/export \
  -H "Cookie: sid=viewer_session_tok" \
  -H "Content-Type: application/json" \
  -d '{"role":"admin","tenantId":2}'
JS
// Annotated Example 4: Structured least-privilege audit record (never logs raw secrets)
function buildAuditEntry(allowed, actorRole, tenantId, traceId) {
  return {
    event: allowed ? 'EXPORT_GRANTED' : 'EXPORT_DENIED',
    actorRole: String(actorRole || 'anonymous'),
    tenantId: tenantId ?? null,
    traceId: String(traceId || 'missing'),
  };
}

3. Progressive Labs: Warm-Up → Attack → Defend → Harden

Warm-Up Stage

Warm-Up: Classify trusted vs untrusted sources

Label network-supplied inputs as untrusted and verified server session claims as trusted.

OWASP A01:2021 — Broken Access Control
Exercise#security.m1.l1.e1
Attempts: 0Loading…

Implement classifyTrustSource(sourceName): return 'trusted' ONLY for 'verified_session' and 'db_policy', and 'untrusted' for any client input ('req_body', 'query_param', 'http_header', or unknown strings).

Loading editor…
Show hint

Use an explicit allowlist Set defaulting any unknown source to 'untrusted' (fail-closed).

Solution available after 3 attempts

Attack Stage

Attack: Exploit body-claim shadowing in authorizeExportUnsafe

Craft a malicious JSON body that escalates a low-privilege viewer in tenant 1 into exporting tenant 2 as admin.

OWASP A01:2021 — Broken Access Control
Exercise#security.m1.l1.e2
Attempts: 0Loading…

Define attackerPayload so passing { userId: 9, role: 'viewer', tenantId: 1 } and attackerPayload into authorizeExportUnsafe returns { allowed: true, tenantId: 2 }.

Loading editor…
Show hint

Because authorizeExportUnsafe prefers requestBody over session, supply { role: 'admin', tenantId: 2 }.

Solution available after 3 attempts

Defend Stage

Defend: Ignore body claims and pin tenantId to verified session

Ensure only verified session.role === 'admin' succeeds and tenantId is copied strictly from session.tenantId.

OWASP A01:2021 — Broken Access Control
Exercise#security.m1.l1.e3
Attempts: 0Loading…

Fix authorizeExport(session, requestBody): ignore untrusted requestBody.role and requestBody.tenantId overrides, allow access ONLY when session && session.role === 'admin', and always scope { allowed, tenantId } strictly to session.tenantId.

Loading editor…
Show hint

Untrusted request bodies can forge role: 'admin' or another customer's tenantId. Read both role and tenantId strictly from the verified session object.

Solution available after 3 attempts

Hardening Stage

Harden: Deny cross-tenant requests and emit safe audit logs

Block cross-tenant mismatches even for valid admins and produce a scrubbed telemetry event.

OWASP A09:2021 — Security Logging & Monitoring Failures
Exercise#security.m1.l1.e4
Attempts: 0Loading…

Implement auditExportAccess(session, requestedTenantId, traceId): permit (allowed: true) ONLY when session && session.role === 'admin' && session.tenantId === requestedTenantId, and always return { allowed, tenantId: session ? session.tenantId : null, event, traceId } with event: allowed ? 'EXPORT_GRANTED' : 'EXPORT_DENIED'.

Loading editor…
Show hint

Verify BOTH session.role === 'admin' AND session.tenantId === requestedTenantId; fail closed with 'EXPORT_DENIED' whenever any condition is false.

Solution available after 3 attempts

4. Checkpoints & Code Review

Security Quiz#security.m1.l1.e5
Ready

An API endpoint relies only on a React form validator to ensure users cannot pass an empty tenant ID or admin role. Why does this fail as a security control?

Answer options
Security Quiz#security.m1.l1.e6
Ready

Review this Express handler carefully. Which subtle flaw breaks the trust boundary?

JS
app.post('/api/profile', (req, res) => {
  const update = { userId: req.session.id, role: 'member', ...req.body };
  db.users.update(req.session.id, update);
  res.json({ ok: true });
});
Answer options