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:
// 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:
# 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}'// 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: Classify trusted vs untrusted sources
Label network-supplied inputs as untrusted and verified server session claims as trusted.
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).
Show hint
Use an explicit allowlist Set defaulting any unknown source to 'untrusted' (fail-closed).
Solution available after 3 attempts
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.
Define attackerPayload so passing { userId: 9, role: 'viewer', tenantId: 1 } and attackerPayload into authorizeExportUnsafe returns { allowed: true, tenantId: 2 }.
Show hint
Because authorizeExportUnsafe prefers requestBody over session, supply { role: 'admin', tenantId: 2 }.
Solution available after 3 attempts
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.
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.
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
Harden: Deny cross-tenant requests and emit safe audit logs
Block cross-tenant mismatches even for valid admins and produce a scrubbed telemetry event.
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'.
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
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?
Review this Express handler carefully. Which subtle flaw breaks the trust boundary?
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 });
});