Lesson 8 of 9 · 18m
Lightweight Threat Modeling in Code Review
A practical review pass for security-relevant diffs: attacker-controlled input, trust boundaries, authorization, failure modes, and abuse at scale.
Most code review security advice fails because it asks reviewers to become a static analysis engine. You are not going to rediscover every injection manually. What a reviewer can do is spot security-relevant changes and ask the questions automation cannot: why is this trust decision correct, what happens when input is hostile, and does the failure mode match the requirement?
Recognize the diffs that deserve a threat pass
Slow down when the diff touches authentication, sessions, tokens, OAuth, authorization, tenancy, sharing, file parsing, templating, deserialization, webhooks, retries, queues, imports, exports, cryptography, secrets, signatures, rate limits, caching, logging, error handling, or security headers.
A one-line change in these files can carry more risk than a thousand-line styling PR.
Follow the input across trust boundaries
@router.post("/projects/{project_id}/export")
def export_project(project_id: int, request: ExportRequest, user=Depends(current_user)):
project = db.get_project(project_id)
if project.owner_id != user.id:
raise HTTPException(403)
queue_export.delay(project_id, user.id, request.include_private)The review questions fall out naturally: is project_id validated for existence and tenant scope? Is ownership enough, or can project members export too? Can include_private be supplied by a role that should not see private fields? Is the queued job re-checking authorization at execution time, especially if permissions change before the worker runs?
Check failure behavior, not just success behavior
Security bugs often live in error paths:
try:
verify_signature(raw_body, signature)
except SignatureError:
log.warning("bad signature body=%s", raw_body) # now secrets may be in logs
return Response(status_code=401)Ask what is logged, whether timing differs, whether a partial write happened before authorization, whether retries can multiply effects, and whether a dependency outage causes fail-open behavior. "It throws" is not enough — who catches it, and what state remains?
Review controls as a chain
A webhook handler might have signature verification, but the verification happens after JSON parsing mutates bytes. A rate limit exists, but it is keyed on easily rotated IP instead of account. Authorization exists, but a cached object was loaded before the tenant filter. Controls fail at seams.
A useful review comment is specific:
This checks project ownership before enqueueing, but the worker rehydrates the
project later and never rechecks permissions. If access is revoked in the
intervening minutes, the export still runs. Please pass the acting user id and
enforce authorization inside the worker too.Field notes
- Do not review for "security" in the abstract; review against the requirements and abuse cases.
- Be suspicious of new powerful operations masquerading as maintenance tools.
- Changed default behavior is a trust-boundary change even if no endpoint signature changes.
- A short threat note in the PR description helps reviewers focus: attacker, boundary, invariant, evidence.
Was this lesson helpful?