Skip to main content
Module 2 · Lesson 3 of 46/42 in the course~15 min
Module lessons (3/4)

Dynamic ORDER BY & Identifier Injection

Lesson preview6 hands-on exercises readyApplication & Web Security · Module 2 — Injection & Database Security (PostgreSQL)

Understand why query parameters cannot bind column names, exploit dynamic sorting, and build an allowlist.

What you'll learn in this lesson

  • Explain why $1 placeholders bind data values only and cannot parameterize SQL column names, table names, or ASC / DESC.
  • Exploit an interpolated dynamic ORDER BY query using conditional boolean subquery sorting.
  • Build a closed static allowlist mapper in both PL/pgSQL and JavaScript API handlers.

Overview

Developers reach for string interpolation in sorting handlers (ORDER BY ${sort}) because ORDER BY $1 does not treat $1 as a column identifier in PostgreSQL — it binds a constant string literal (ORDER BY 'price_cents'), sorting every row by the exact same constant!

Because SQL identifiers belong to the AST structure rather than scalar data slots, strict allowlist mapping to hardcoded SQL fragments is mandatory whenever user input chooses sort columns or directions.

Sections covered in this lesson

  1. 11. Why $1 Fails for Columns & How Attackers Exploit ORDER BY
  2. 22. Interactive SQL ORDER BY Allowlist Lab
  3. 33. Progressive Labs: Warm-Up → Attack → Defend → Harden
  4. 44. Checkpoints & Code Review
Pro Lesson · Module 2 — Injection & Database Security (PostgreSQL)

Unlock "Dynamic ORDER BY & Identifier Injection" and the full Application & Web Security curriculum

Understand why query parameters cannot bind column names, exploit dynamic sorting, and build an allowlist.

Unlock all 6 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Application & Web Security plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.