CCyberwiki

Security code review

Security code review
TypeSecurity testing method
Also known asSecure code review, manual code review
ExaminesSource code, scripts and configuration, usually one change at a time
Main referencesOWASP Code Review Guide 2.0 (2017); NIST SSDF practice PW.7 (2022)
Related methodsSAST, DAST, Penetration testing, Threat modelling

Security code review is ordinary code review with the organisation's security standards put first: a person reads an application's source code to check that the security controls it needs are present, that they work, and that the code calls them in every place where they are required.1 NIST's Secure Software Development Framework (SSDF) lists it under practice PW.7, which separates code review, where a person reads the code, from code analysis, where tools search the code alone or with a person checking their output.2

Scope

The SSDF counts as reviewable any source code, script or other code that an organisation decides is readable by people.2 A review looks for the vulnerabilities that also appear in testing, such as SQL injection and cross-site scripting, and for flaws in the application's own business logic, such as an order that can be refunded twice. Logic flaws are hard to find by keyword search, because the functions involved carry whatever names the developer chose.1

Process

Preparation comes first. The reviewer learns what the application is for and which failures would hurt the business, who might attack it and why, and where its attack surface lies. The documents from threat modelling, where they exist, add to this picture.1

The review begins by listing every input the code accepts. The reviewer then follows each input through the code and checks whether the code applies the required controls on the way, for example Input validation, an Authorization check or Output encoding.1 Starting points come from "crawling" the code: searching with a tool such as grep for functions that handle outside input, files or user accounts.1

When the subject is a single change, the reviewer asks whether it widens the attack surface. A change that sends traffic over HTTP where the code used HTTPS before widens it, and so does a new hash function written in place of a call to the project's existing cryptography code.1 A change to an Express route shows the questions a reviewer puts to a diff:

// Added in this pull request
app.get("/api/invoices/:id", async (req, res) => {
  const invoice = await db.invoices.findById(req.params.id);
  res.json(invoice);
});

The new input, req.params.id, reaches the database lookup and the response with no check that the invoice belongs to the signed-in user, the pattern described in Insecure direct object reference.

Under SSDF task PW.7.2, every issue found is recorded and triaged, with a recommended fix, in the team's workflow or issue tracker. The examples given for the task also record each root cause.2

Timing and depth

Review can happen before or after the commit. Review before the commit keeps substandard code out of the repository but slows every check-in. Review after the commit is faster, but authors fix fewer small issues once code is merged, and other changes may land in the same module first.1

Effort should match the risk of the change. In one worked example, a change to a module rated risk level one needs three reviewers including a security architect, and one at level four needs a quick review by one peer. A planning figure of 250 lines per hour is for estimating how long a review takes and says nothing about its quality.1

Relation to other methods

OWASP's Code Review Guide calls code review probably the most effective technique for finding security bugs early. Combined with automated and manual Penetration testing, it makes verification more cost-effective.1 Tools such as static analysis cover large amounts of code, but they lack the context of the application, so a person has to confirm each result, decide whether it can be exploited and judge the risk; for many types of flaw, a tool that finds them automatically with few false positives is "beyond the state of the art".1 Penetration test findings lose value as the code changes after the test. With cross-site scripting, scanners and manual review each miss some flaws.1

The SSDF examples combine the methods. A peer review includes the results of earlier analysis and testing, a person reviews the issues a static analysis tool reports, and expert reviewers check code for backdoors and other malicious content.2 Testing the running code, as DAST does, is a separate practice, PW.8.

Enforcement on GitHub

On GitHub, review happens on the pull request. A branch protection rule with Require pull request reviews before merging blocks a merge into the protected branch until a set number of people with write access have approved it.3 Further options:

  • Dismiss stale pull request approvals when new commits are pushed withdraws an approval when the diff changes after it was given.
  • Require approval of the most recent reviewable push requires someone other than the last person to push to approve. GitHub's documentation advises dismissing stale reviews instead where approved pull requests could be "hijacked" by content added after approval.
  • Require review from Code Owners requires approval from the code owner of each changed file.3

Code owners are listed in a file named CODEOWNERS in the .github/ folder, the repository root or docs/. Each line pairs a file pattern with one or more owners, and the last matching line wins:4

* @global-owner1 @global-owner2
*.js @js-owner
/apps/ @octocat

GitHub reads the file from the pull request's base branch and assigns no owner where the named user or team lacks write access.4

Notable case

Between 9 and 21 August 2020, researchers at the University of Minnesota sent four patches to the Linux kernel for a case study the university called "hypocrite commits". By its account one of the four was valid, and all four were stopped before they passed review.5 A report by the Linux Foundation's Technical Advisory Board on 5 May 2021 covered the university's buggy patches, some sent on purpose, reviewed all earlier accepted kernel commits from the paper's authors and recommended an internal review process for patches from the community.6

Prompt for a coding agent

Review the changes on this branch against main for security flaws. List every new input (route parameters, query strings, form fields, headers, uploads, webhook bodies). Follow each one to where it is used and report whether it is validated, whether the code checks that the signed-in user may act on the record it names, and whether it reaches a database query, shell command, file path or HTML output unsafely. Also report new routes without authentication and secrets written in code. Give the file and line for each finding. Do not change any code.

In SecFlash

SecFlash reports a GitHub default branch with no protection rule, so code can reach it with no pull request and no checks, as SF-CLD-601, and a repository with no code scanning set up as SF-CLD-606. Both show on the Cloud page.

See also

References

  1. a b c d e f g h i j k Conklin, Larry; Robinson, Gary (project leaders) (2017). "Code Review Guide 2.0" (PDF). OWASP Foundation.
  2. a b c d Souppaya, Murugiah; Scarfone, Karen; Dodson, Donna (February 2022). "Secure Software Development Framework (SSDF) Version 1.1". NIST SP 800-218. NIST.
  3. a b GitHub (2026). "About protected branches". GitHub Docs.
  4. a b GitHub (2026). "About code owners". GitHub Docs.
  5. back Department of Computer Science & Engineering, University of Minnesota (9 May 2021). "Statement from Computer Science & Engineering confirming Linux Technical Advisory Board findings".
  6. back Corbet, Jonathan (5 May 2021). "The TAB report on the UMN affair". LWN.net.