DML School of Technology
Menu

Open to everyone Reading, lesson 58 of 100, 2 min read

Reviewing code for security

Reading code with a security eye

You do not need to be an expert programmer to spot many security problems. Code review is about asking the same questions again and again.

Questions to ask of any feature

  1. Where does input come from? Forms, URLs, cookies, headers, uploaded files, other systems.
  2. Is it checked on the server? Type, length, format, allowed values.
  3. Where does it go? Into a database query (injection), into a page (XSS), into a file path, into a shell command, into an email header.
  4. Is it made safe for where it goes? Parameterised queries, output encoding, safe file names.
  5. Who may do this? Is there an authorisation check for this user and this item?
  6. What happens on error? Does it fail safely, without revealing internal details?
  7. Are secrets handled properly? No passwords or keys in the code, logs or error messages.
  8. Is it logged? Important actions (sign-ins, permission changes, payments) should leave a record.

Common red flags

PatternWhy it is risky
Building SQL with string joins and user inputSQL injection
Printing user input into HTML without encodingXSS
if user.isAdmin checked only in JavaScriptBroken access control
Using a file name from the user to open filesPath traversal: ../../etc/passwd
Comparing passwords as plain textPasswords stored unhashed
catch (e) { show(e) } on public pagesInformation disclosure
A hard-coded password or API keyAnyone with the code has the secret
Turning off certificate checks "to make it work"Man-in-the-middle

Be kind and specific

Good review comments explain the risk and suggest a fix: "This builds the query from $_GET['id']; an attacker can change the query. Use a prepared statement with ? instead." Reviews are about the code, not the person.

Note

This module's assignment gives you a short, made-up piece of code with several problems to find and explain.