Home / Learn Secure SQL / Validating Dynamic Identifiers (Revisited, in Practice)

Validating Dynamic Identifiers (Revisited, in Practice)

Turning the earlier allowlist idea into real validation code.

The short versionTurning the earlier allowlist idea into real validation code.

Theory in plain words

Identifier validation functionrequested name:"price"looked up in{"name","price","cre…found: the app's ownconstant "price" is…not found: requestrejected
Identifier validation function

The basic idea

When a feature must accept a table or column name from outside, the validation step is specifically: does this exact string appear in our fixed, hard-coded list of permitted names?

Why this comes up

This is the practical implementation of the "identifiers cannot bind" idea from earlier in the course, written as an actual validation function rather than just a principle.

The mechanics

Write a small function that takes the requested name and returns the matching, trusted constant from your own code if found, or raises/rejects if not; never pass the raw requested string onward, even after a successful check.

Where it is used

  • Web application code review
  • Backend development
  • Database administration
A safe identifier-validation function (Python)
ALLOWED_SORT_COLUMNS = {'name', 'price', 'created_at'}

def safe_sort_column(requested):
    if requested not in ALLOWED_SORT_COLUMNS:
        raise ValueError('Invalid sort column')
    return requested  # safe to place into the SQL text now

Quick check

1. A safe identifier-validation function should...