Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 38 ยท Secure Development Lifecycle

Security Requirements and Secure Code Review

Write testable security requirements, use ASVS and abuse cases, and review code with a focused checklist covering access control, input, secrets and error handling.

"The app must be secure" is not a requirement: nobody can test it. Good security requirements are specific, testable and traceable to a risk. And once code exists, a secure code review is one of the most effective ways to catch flaws that automated tools cannot see, such as a missing authorization check.

This lesson gives you both skills. It builds on Threat Modeling with STRIDE, whose mitigations become requirements, and feeds into automated testing.

Writing testable security requirements

A usable requirement states who, what and a measurable condition.

Weak Better
The login must be secure. After 5 failed logins in 10 minutes from one IP, further attempts are delayed.
Protect user data. A user can read only invoices where invoice.user_id equals their own ID.
Use strong passwords. Passwords must be at least 12 characters and not appear in the breached-password list.
Log important things. Failed logins and access-denied events are logged with user, IP and request ID.

Sources of requirements:

  • Threat model: every mitigation becomes a requirement.
  • OWASP ASVS (Application Security Verification Standard): numbered, verifiable statements grouped by area and level (L1 basic, L2 standard, L3 high assurance). Pick the level that matches the app's risk.
  • Regulations and contracts: privacy rules, payment standards, customer agreements.
  • Past incidents and findings in your own systems.

Abuse cases

A use case says how a feature should work. An abuse case says how it could be misused. For each user story, add one:

  • Story: "As a user I can reset my password by email."
  • Abuse cases: an attacker triggers thousands of reset emails to harass a victim; a reset link is reused; a reset reveals whether an email exists; a link never expires.
  • Requirements that follow: rate limit resets per account; single-use links that expire in 15 minutes; identical response whether or not the email exists.

From risk to requirement

Risk From the threat model.
Requirement Specific and testable.
Test case Proves it works.
Acceptance Part of "done".

Add security items to the definition of done.

1 / 2

Turning requirements into test cases

Python Requirements as executable checks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

R3 shows a design choice: responding with the same 404 for "does not exist" and "not yours" avoids telling an attacker which IDs exist.

A secure code review checklist

You do not need to read every line with equal care. Focus review time on risky code: authentication, authorization, input parsing, file and network access, cryptography, deserialization and anything touching money or personal data. Walk these questions:

Access control. Is the check done on the server for every request? Does it verify ownership of the specific object, not only that the user is logged in? Are there routes or API methods that skip the middleware?

Input and output. Is all external input (parameters, headers, files, JSON) validated against an allow-list of types, length and format? Are queries parameterized? Is output encoded for its context (HTML, attribute, URL, JavaScript)?

Authentication and sessions. Are passwords hashed with a slow algorithm? Is the session rotated at login? Are tokens random and expiring?

Secrets. Any keys, passwords or tokens in code, tests, config or logs? Are they loaded from a secret store?

Cryptography. Are standard, current algorithms used through maintained libraries? Any custom crypto, fixed IVs, or == for comparing secrets?

Errors and logging. Do errors expose stack traces or queries? Are security events logged without sensitive values?

Dependencies and configuration. New dependency justified and scanned? Debug flags, default credentials and open CORS reviewed?

Business logic. Can steps be skipped or repeated, amounts be negative, limits be bypassed, coupons reused?

Spotting issues: a mini review

Read this handler as a reviewer. Which problems do you see?

PHP
public function show(Request $request, $id)
{
    $doc = DB::select("SELECT * FROM documents WHERE id = $id")[0];
    Log::info("User " . $request->user()->email . " token " . $request->bearerToken());
    return response()->json($doc);
}

Findings: (1) string-built SQL (injection; use bindings), (2) no ownership check on the document (broken access control), (3) the bearer token is written to the log (secret in logs), (4) it returns the whole row, which may contain fields the user should not see (excessive data exposure), (5) no handling when the row does not exist.

Python A tiny review linter for risky patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Pattern linters are noisy and easy to evade; they support review but never replace reasoning about how the code is used. Real tools (see SAST, DAST and SCA) analyze code structure.

Running reviews well

  • Keep changes small; reviewers miss problems in huge diffs.
  • Review the design intent, not only syntax: "what stops user A from reading user B's data?"
  • Use a checklist, and update it from real findings.
  • Treat review comments as learning, never as blame.
  • Require a second reviewer for security-sensitive areas.
  • Automate the mechanical checks so humans can focus on logic.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is a testable security requirement?

    It names the actor, the object and a condition that a test can verify.
  2. 2. What is an abuse case?

    Abuse cases turn misuse ideas into requirements, such as rate limits and single-use links.
  3. 3. In the sample handler, what is the access control problem?

    Being logged in is not enough; the object-level check ties the resource to the user.
  4. 4. Where should reviewers spend most security attention?

    Risk-based review gets more value per hour than reading all code equally.

Summary

  • Write specific, testable requirements from threat models, ASVS, regulations and abuse cases.
  • Turn requirements into automated checks and part of the definition of done.
  • Review risky code with a checklist: access control, input/output, secrets, crypto, errors, dependencies and business logic.
  • Small diffs, second reviewers and automation make reviews effective.

Next: SAST, DAST, SCA and IAST