Skip to content

Secure code review: checklists and automated tools

B2Bnetwork team · · 7 min

Security

In brief

Secure code review works best in two layers: automated tools on every pull request catch known patterns (SAST, secrets, vulnerable libraries), and a person with a checklist reads by hand only the changes in high-risk areas: sign-in, permissions, payments, file handling and cryptography. A tool does not know whether customer A can see customer B's invoice. That question is asked by a reviewer who understands the system's logic.

Two kinds of review

The OWASP guidance on code review distinguishes reviewing a change from reviewing the whole codebase. Both are needed, at a different pace.

Change reviewBaseline review
Scopeone pull requesta whole module or application
Whenon every change to high-risk codebefore the first release, when taking over code from another team, before an audit
Whoa reviewer from the team with a checklista security engineer, often external
Timeminutes, an hour at mostdays, depending on the size of the code
Outputcomments on the pull requesta report with findings and priorities

A baseline review starts with a map: where the application accepts external data, where it checks permissions, where it stores secrets and where it connects to other systems. The threat model described in Threat modeling with STRIDE helps here.

What a tool catches and what a person catches

ProblemToolPerson
an SQL query concatenated with a parameterwell (SAST)confirms whether the data really comes from outside
an API key in the repositorywell (secret scanner)revokes the key and checks the history
a library with a known vulnerabilitywell (SCA)judges whether the vulnerable function is actually used
no check that a record belongs to the userpoorlywell, because they know the data model
skipping an approval step in a workflownowell, because they know the process
a race condition between two simultaneous withdrawalsnowell, if they know where to look
an unsafe framework settingpartlychecks whether the setting reaches production

The conclusion is simple: tools remove from the review everything a pattern can find, and the reviewer spends time on logic. How to add SAST, SCA and secret scanning to the pipeline and where to set blocking thresholds is covered in SAST, DAST and SCA, and secrets have their own article, Secrets in applications.

Which changes to read by hand

A manual security review of every pull request will stall the team. It is better to agree on triggers that require a second pair of eyes with a checklist:

  • a change to sign-in, sessions, password reset, MFA or the identity provider integration,
  • a new API endpoint or a change to the permission check on an existing one,
  • code that calculates money, limits or discounts, or changes payment details,
  • accepting files from users, generating documents, parsing XML and binary formats,
  • cryptography, signatures, tokens and key handling,
  • a change to security configuration: CORS, headers, cookie policy, the web application firewall.

The repository can enforce this easily. A code owners file (for example `CODEOWNERS` in GitHub and GitLab) assigns the directories with authentication and payment code to people who must approve any change. A "security" label on the pull request switches on the checklist in the description template.

The reviewer checklist

The list has to be short, because nobody reads a long one. Below is an example for a web application and API, organized by the areas in the OWASP Secure Code Review Cheat Sheet:

  1. Input. Every value from a request, file, queue and header is validated on the server. Database queries are parameterized.
  2. Access control. The endpoint checks not only that the user is signed in, but also that this specific record belongs to them or to their company.
  3. Authentication and sessions. Tokens have a short lifetime, signing out invalidates them, and a failed sign-in does not reveal whether the account exists.
  4. Cryptography and secrets. The code uses standard libraries, not home-grown algorithms. Keys come from a secrets manager, not from a file in the repository.
  5. Business logic. Workflow steps cannot be skipped or repeated (for example the same withdrawal twice). Amounts and limits are calculated on the server.
  6. Errors and logs. The user gets a generic message and the details go to the log. The log contains no passwords, tokens or full document numbers.
  7. Dependencies and configuration. A new library has a reason and an active maintainer. Debug mode and default accounts do not reach production.

An example of item 2 that tools usually miss:

# przed: zwraca fakturę każdemu zalogowanemu, wystarczy zmienić numer w adresie
@app.get("/api/faktury/{faktura_id}")
def pobierz_fakture(faktura_id: int, user=Depends(zalogowany)):
    return repo.faktura(faktura_id)

# po: rekord musi należeć do firmy użytkownika
@app.get("/api/faktury/{faktura_id}")
def pobierz_fakture(faktura_id: int, user=Depends(zalogowany)):
    faktura = repo.faktura_firmy(faktura_id, firma_id=user.firma_id)
    if faktura is None:
        raise HTTPException(status_code=404)
    return faktura

The first version returns any invoice to any signed-in user who changes the number in the URL. The second only returns invoices that belong to the user's company.

OWASP ASVS, the Application Security Verification Standard, is a good source of requirements for building the list, organized into chapters and levels. Compare the list with findings from penetration tests every quarter: a bug a tester found twice should go onto the list or into a tool.

How to fit review into the process

NIST SSDF (SP 800-218) lists reviewing and analyzing code as one of the practices of secure software development, alongside training, threat modeling and testing. In practice the team needs a few decisions:

  1. Who reviews. A team reviewer for ordinary changes, and a person with a security role (a security champion) for changes that hit a trigger.
  2. What blocks the merge. New high-severity findings from the tools, and a missing approval from the code owner for high-risk directories. Everything else goes into a register with a due date.
  3. What to measure. Findings by category, time from report to fix, and the share of pull requests with a trigger that went through a checklist review. The number of comments does not count.
  4. When to bring in an external review. Before the first release of a system that handles money or sensitive data, when taking over code from another supplier and before a customer audit.

Code review does not replace testing the running application. The difference between a penetration test and a reward program for researchers is covered in Bug bounty or penetration test. B2Bnetwork engineers run code reviews and application tests as part of Security testing: from the risk map, through review and testing, to a report with fixes to verify.

Questions and answers

Can a SAST tool replace manual code review?

No. SAST is good at finding patterns of known bugs, such as concatenated SQL queries or dangerous functions. It does not understand the data model or the workflow, so it will not notice that a user can download someone else's invoice or skip an approval step. A manual review focused on those places complements the tool.

How long does a secure code review take?

Reviewing a single pull request with a checklist usually takes from a quarter of an hour to an hour. A baseline review of a whole application takes days and depends on the size of the code, the number of integrations and the quality of the documentation. A risk map prepared before the start and tool results that point to places to check shorten the time.

Who on the team should do security code review?

Every reviewer should know the short checklist. Changes in high-risk areas are also approved by a team member with a security role and more experience in the field. An external engineer is useful for a baseline review and for code taken over from another supplier.

Is secure code review required by regulation?

Regulations usually do not mandate code review directly, but they require a secure development process and vulnerability handling, for example the Cyber Resilience Act for manufacturers of products with digital elements. A documented review with a checklist and tool results is good evidence that such a process works.

Want to check your application's security? Talk to an engineer.

Let's talk

Read more: Security

All in Security →