Skip to content

OWASP Top 10 Best Practices: Input Validation to Stop SQL Injection and XSS

Application Security Vignatrix AppSec 6 Nov 2026 9 min read

Injection flaws such as SQL injection and cross-site scripting have appeared in every edition of the OWASP Top 10. Here is how our testers see them happen, and the coding practices that stop them.

Why injection is still on the list

Injection has featured in every edition of the OWASP Top 10 since the list began, and since 2021 the Injection category also covers cross-site scripting (XSS). The root cause is always the same: the application lets untrusted data be interpreted as code, whether that code is SQL, HTML and JavaScript, an operating-system command or a query language.

In our application penetration tests, injection findings rarely come from developers who have never heard of SQL injection. They come from the one raw query in a reporting module, the one admin page that renders HTML from the database, or the one search filter built by joining strings. Good defaults matter more than good intentions.

Principle 1: Validate every input on the server

Treat everything from outside your code as untrusted: form fields, URL parameters, JSON bodies, headers, cookies, file names, webhook payloads and data from partner APIs. Client-side validation improves the user experience, but attackers send requests directly, so it is never a security control.

  • Use allow-lists, not deny-lists. Define what is valid (type, length, format, range or a fixed set of values) and reject everything else. Trying to block “bad” characters always misses something.
  • Validate type and length first. A quantity should parse as an integer between 1 and 100; a name should have a sensible maximum length.
  • Normalise before you validate. Decode and canonicalise input (URL encoding, Unicode normalisation) so the value you check is the value you use.
  • Reject invalid input instead of silently “cleaning” it and carrying on. Return a clear error and log repeated failures.
  • Use a schema library (for example Zod, Joi, Pydantic or Bean Validation) so validation rules are declared once and enforced consistently.

Principle 2: Validation is not enough, so keep data and code separate

Validation reduces risk, but many legitimate values contain characters that are dangerous in some context. The name O’Brien contains a quote; a product review can contain angle brackets. The reliable defence is to make sure data can never be executed as code, whatever it contains.

Stopping SQL injection

Never build a SQL statement by joining strings with user input. This is the pattern we exploit most often:

// Vulnerable: the input becomes part of the SQL statement
const sql = "SELECT * FROM orders WHERE customer_id = '" + req.query.id + "'";
db.query(sql);

// Safe: a parameterised query keeps the input as data
db.query('SELECT * FROM orders WHERE customer_id = $1', [req.query.id]);

With a parameterised query (also called a prepared statement), the database receives the query structure and the values separately, so an input like ' OR '1'='1 is just an unusual customer ID, not part of the query.

  • Use parameterised queries or a well-maintained ORM everywhere, and review every “raw query” escape hatch in your codebase.
  • Stored procedures are only safe if they do not build dynamic SQL inside.
  • Table and column names cannot be parameterised. If users can choose a sort column, map their choice to a fixed allow-list of column names.
  • Connect with a least-privilege database account, so a successful injection cannot drop tables or read unrelated data.
  • Do not return raw database errors to users; they help attackers map your schema.

Stopping cross-site scripting (XSS)

XSS happens when untrusted data reaches the browser and is treated as HTML or JavaScript. It lets an attacker run script in your users’ sessions: stealing data, performing actions as the user or rewriting the page. The core defence is context-aware output encoding: encode data for exactly where it is placed (HTML body, attribute, URL or JavaScript).

  • Rely on your framework’s automatic escaping. React, Angular, Vue and modern template engines encode output by default.
  • Treat the escape hatches as high-risk code: dangerouslySetInnerHTML, innerHTML, v-html, bypassSecurityTrust… and template “safe” or “raw” filters. Each one should be justified and reviewed.
  • When you genuinely need user-supplied rich text, sanitise it with a proven HTML sanitiser such as DOMPurify, configured with a strict allow-list of tags and attributes.
  • Validate URLs before using them in links or redirects, and allow only http: and https: so javascript: URLs cannot run.
  • Add a Content Security Policy that blocks inline and third-party scripts. It will not fix XSS, but it limits what an attacker can do if one slips through.
  • Set session cookies as HttpOnly, Secure and SameSite, so script cannot read them.

The same idea applies to every interpreter

  • OS commands: avoid calling a shell. Use APIs that take arguments as an array (for example execFile in Node.js or subprocess with shell=False in Python), and allow-list any values that select behaviour.
  • NoSQL databases: check that fields expected to be strings really are strings, so an object like {"$ne": null} cannot change the meaning of a MongoDB query.
  • LDAP, XPath and template engines: use their parameterisation or escaping functions, and never render user input as a template.
  • File uploads: check size limits and the real file type (not just the extension), generate new file names, and store uploads outside the web root.
  • LLM applications: prompt injection is the newest member of the family. See our guide to the OWASP Top 10 for LLM Applications.

Build it into your development lifecycle

Individual fixes do not scale; habits do. The teams with the fewest injection findings in our tests do the following:

  • Provide secure, shared helpers for database access, HTML rendering and validation, and make them the easiest option.
  • Run SAST (static analysis) in CI with rules that flag string-built queries and unsafe HTML sinks.
  • Add security unit tests that send classic payloads (quotes, script tags, encoded variants) to every input.
  • Include injection checks in code review checklists, with extra scrutiny for raw-query and raw-HTML code.
  • Run DAST and manual penetration tests before major releases. A WAF is useful extra protection, but it is not a substitute for fixing the code.

Quick checklist

  • Every input is validated on the server against an allow-list.
  • Every database query is parameterised; no SQL is built from strings.
  • All output is encoded for its context, and raw-HTML features are reviewed.
  • User-supplied HTML is sanitised with a proven library.
  • A strict Content Security Policy and secure cookie flags are in place.
  • Database and service accounts have least privilege.
  • SAST, security tests and code review run on every change.

How Vignatrix helps

Our application security team finds injection flaws before attackers do, through secure code review, web, mobile and API penetration testing, and developer training in secure coding. Every finding comes with a clear fix and a free retest.

Ready to strengthen your security posture?

Talk to our experts about a free security assessment tailored to your organisation.