Home / Learn Secure SQL / Output Encoding Basics

Output Encoding Basics

A brief, related topic: protecting what leaves the database too.

The short versionA brief, related topic: protecting what leaves the database too.

Theory in plain words

Two different boundariesinput validation:data entering your…database (safe viaprepared statements)output encoding: dataleaving to be…
Two different boundaries

The basic idea

Output encoding transforms data for safe display in a specific context (HTML-encoding a value before putting it in a web page) and is the output-side counterpart to input validation, protecting against a different problem (cross-site scripting) than SQL injection.

Why this comes up

Data read back from your own database is not automatically safe to display: it might contain a value another user entered, so it needs the same output encoding as any other displayed content.

The mechanics

Use your templating engine's automatic escaping for anything shown in HTML (nearly every modern template engine escapes by default), and avoid manually marking content as 'safe' unless you are certain of its origin.

Where it is used

  • Web application code review
  • Backend development
  • Database administration

This course focuses on database/SQL safety, so output encoding is covered only briefly here; it is a large topic of its own (commonly discussed as cross-site scripting prevention) that deserves separate, dedicated study for any web application.

Quick check

1. Output encoding mainly protects against...