Skip to content

Lesson 1 of 5

Getting started with secure code review

Listen to an AI summary of Getting started with secure code review

0:00 / 0:30

What is source code

Source code is the text of a program, written by a person in a programming language. A programming language is a set of words and rules for writing programs, such as JavaScript. A JavaScript program that a beginner writes is source code.

An application is software that people use to do a task, such as a shopping website or a messaging app. Every application is built from source code.

What is a secure code review

A weakness is a mistake in software that could let someone misuse the software. An attacker is a person who tries to make software do something harmful. A secure code review is reading source code to find weaknesses before an attacker finds the weaknesses.

A reviewer is the person who reads the source code. A reviewer reads to learn what the software does, then asks what else the software can be made to do.

A sign-in function with one mistake

The example in this course is a sports shop website that sells kit and match tickets. A person signs in to the sports shop with a username and a password. A password is a word or phrase that only one person knows, typed to prove who the person is.

One username, admin, belongs to the administrator, the person who runs the sports shop and can change every order.

The JavaScript function signIn decides who gets in. The function returns true to let a person in and false to keep a person out. Read the eight lines and look for a way in without the right password.

function signIn(username, password) {
  const correct = checkPassword(username, password)
  if (username === 'admin' || correct) {
    return true
  } else {
    return false
  }
}

The function line by line

  1. function signIn(username, password) {// the username and the password a person typed
  2. const correct = checkPassword(username, password)// true when the password is right for the username
  3. if (username === 'admin' || correct) {// the mistake. The username admin gets in with any password
  4. return true// lets the person in
  5. } else {
  6. return false// keeps the person out
  7. }
  8. }
Line 3 of signIn lets anyone sign in as the administrator.

On line 3, === means is equal to and || means or. Line 3 joins two checks with ||. With ||, one true check is enough to reach return true. The first check looks at the username only, so the username admin gets in with any password.

The fix is to take the first check out, so that line 3 reads if (correct) {. The administrator then needs the right password too.

Finding the mistake by reading

The writer of the function may have added the first check as a shortcut while building the sports shop. The sports shop still works for every person with the right password, so normal use does not show the mistake. A reviewer finds the mistake by reading line 3.

Weaknesses and vulnerabilities

Line 3 of signIn is a weakness. A vulnerability is a weakness that an attacker can use to cause harm. When the sports shop goes on the internet with line 3 unchanged, an attacker can sign in as the administrator. The weakness is then a vulnerability.

Every vulnerability starts as a weakness. Not every weakness is a vulnerability. The same function in a practice program on one laptop is a weakness that no attacker can use.

  1. 1.A mistake is written

    line 3 of signIn

  2. 2.The weakness is in the software

    a review can remove the weakness here

  3. 3.An attacker finds the weakness

    the username admin and any password

  4. 4.Harm follows

    the attacker changes orders

A secure code review removes the weakness at step 2.

A reviewer looks for weaknesses and does not need to prove that an attack works.

CWE and CVE

Two public lists give names and numbers to weaknesses and vulnerabilities.

CWE is a public list of the kinds of weakness, and each kind has a number. CWE is short for Common Weakness Enumeration, and an enumeration is a numbered list.

CVE is a public list of vulnerabilities found in real software, and each vulnerability has a number. CVE is short for Common Vulnerabilities and Exposures.

CWE
Each entry is a kind of mistake that can be in many programs, such as SQL injection (CWE-89).
CVE
Each entry is one vulnerability in one named piece of software, such as CVE-2021-44228 in Log4j.

Check your understanding

  1. State what a secure code review is.

    Show the answer

    A secure code review is reading source code to find weaknesses before an attacker finds the weaknesses.

  2. Which line of the signIn function has the weakness, and what does the line allow?

    Show the answer

    Line 3. A person who types the username admin is signed in with any password.

  3. Give one reason why a weakness is not always a vulnerability.

    Show the answer

    An attacker may have no way to reach the weakness, such as in a practice program on one laptop.