Secure Coding & Release Discipline ยท Lesson 3 of 4

Input validation and parameterised queries

Study notesAbout 3 minutes. A full lesson with quick checks is on its way.

In short

Never build database queries by pasting in user input. Check input against what you expect, and use parameterised queries so it's only ever treated as data.

In your work

A search box on a client's member directory builds SQL by joining strings. Typing a quote mark breaks the page, a classic sign of SQL injection. You switch to a parameterised query and add input checks.

What the UK rules say

  • UK GDPR requires appropriate security for personal data, which includes protecting databases against well-known attacks such as injection.
  • OWASP's guidance is to use parameterised queries, so the database always treats input as data, never as code.

What to do

  1. Use parameterised queries or a safe ORM method for every query.
  2. Check input against what you expect: type, length and format.
  3. Never show raw database errors to users.
  4. Add tests that try quote marks and other special characters.

Watch out for

  • Escaping input by hand instead of using parameters.
  • Trusting input because it came from your own front end.

In your organisation

Which secure coding standard does your organisation follow, and is it checked in code review?

Your employer's own policies come first.

Sources

UK law and guidance, explained for learning. Not legal advice.