Home / Learn Secure SQL / Second-Order Injection: the Concept

Second-Order Injection: the Concept

When unsafe input is stored safely at first, then used unsafely later.

The short versionWhen unsafe input is stored safely at first, then used unsafely later.

Theory in plain words

Safety at one point is not safety everywherevalue received andsafely bound on…stored correctlyread back later bydifferent codeused unsafely(string-built) in a…
Safety at one point is not safety everywhere

What it is

Second-order injection (conceptually) happens when a value is stored correctly, using a bound parameter, at the point it is first received — but is later read back out of the database and then concatenated unsafely into a different query, somewhere else in the codebase, without being bound again at that second point.

Why it matters

This is a reminder that "we bind parameters" is a per-query-site property, not a property of the data itself: a value that was safely stored can still create a problem later if any downstream code path builds a query by hand instead of binding that value again.

Mechanically speaking

Audit every place a previously stored value is read back and used in a subsequent query (not just the original insertion point), and apply the same bound-parameter and allowlist rules at that second site, exactly as at the first.

Shows up in

  • Web application code review
  • Backend development
  • Database administration
Bind at the write site and at every later read-and-reuse site
// Site 1 (write) -- bound:
db.execute("INSERT INTO profiles (bio) VALUES (?)", [bio]);

// Site 2 (read, then reused in a later query) -- must ALSO bind:
const row = db.query("SELECT bio FROM profiles WHERE id = ?", [id]);
db.execute("INSERT INTO audit_log (note) VALUES (?)", [row.bio]); // bound again, not concatenated
Audit questionWhy it matters
Where is this value read back out of storage?Every read site is a fresh opportunity to reintroduce the risk
Is it bound again at that second site, or concatenated?Being safe once does not make a value safe everywhere
Does a code-review checklist cover read sites, not just write sites?Second-order issues are easy to miss if review only looks at the insert

Check yourself

1. Second-order injection is possible even when the original insert was safely bound because...