Second-order SQL injection

Text that was stored safely can still change a later query if that job concatenates the stored value into SQL.

Lesson 50 of 69 · 15 min read · practice only with authorization

Diagram

From a stored field to a later statement

Follow the path. One example at a time.

Step 1 of 4

Save

A name, a comment, or a filename is written to a table. The insert may already use a bound parameter.

Illustration: the stored name is data
display_name = "Avery"
# This was saved earlier.
# Sitting in a table does not make it SQL.
What changes the outcome
Insert

The first write looks safe.

Later SQL

The stored field is concatenated into a new statement.

Result

The job can reach rows the screen never listed.

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. The insert is not the whole story

    What fails
    A bound parameter on the way in does not protect a later statement that concatenates the same bytes.
    Who is affected
    Anyone whose rows the later job’s database login can read or change.
    What you would notice
    A review that only checks the request handler and never the report, the export, or the queue.
    What you change
    Read every statement that uses the stored column, not only the statement that wrote it.
  2. The job’s login

    What fails
    The later query runs as whatever role the job uses. An owner role turns one bad statement into every tenant.
    Who is affected
    The other tenants and tables that role can see, including ones the feature does not show.
    What you would notice
    The job connects as a user that owns the schema, or the grants include tables the report never displays.
    What you change
    Create a role that can read only the columns this job needs, and nothing it can write by accident.
  3. What a review can see

    What fails
    The failure is visible as string building around a column that came from the database, not only around a request field.
    Who is affected
    The person who has to explain a row that showed up in the wrong report.
    What you would notice
    SQL text built with concatenation, a template, or a format string, and a column from an earlier write in the middle of it.
    What you change
    Make the SQL text a constant. Pass the stored column as a parameter beside the current user.
  4. The change you can schedule

    What fails
    The durable fix is the later statement and the role. Filtering the name on the way in does not fix a statement that still concatenates.
    Who is affected
    People who were exposed by the job, and the owner who has to restore trust in the report.
    What you would notice
    A test where a stored name contains SQL punctuation and the later query still returns only the intended row.
    What you change
    Bind every use of the stored field, and keep the job’s grants smaller than the application owner.

Stored does not mean trusted

Teams often audit the request that writes a row and stop. The name is bound, the comment is bound, and the review says the query is safe. The data is still just bytes a caller chose. Nothing about sitting in a table turns those bytes into a trusted identifier.

Second-order SQL injection is the failure on the next use. A search index, a PDF export, an admin report, or a background job reads the column and builds a new statement by gluing that text into SQL. The database does not remember that the insert was careful. It parses the new string.

Where the later statement hides

Look for jobs that do not have a request in front of them. Reporting databases, “export all matching rows” handlers, and migrations that copy a column into a dynamic ORDER BY or a dynamic table name are typical. An identifier is a different problem from a value: a column name cannot be a bound parameter, so it has to come from a list you wrote, not from the stored string.

Also look at code that builds SQL in a template because “it is only internal.” Internal means the caller is your job. The data can still be someone else’s text. If the SQL text changes shape when the column changes, the column is not a value.

Who that job can reach

The screen that shows one user’s report is not the privilege boundary. The login the job uses is. If that login owns the schema, a statement that breaks open can read or change other tenants, other products, and tables the report never renders. Write the finding as those people, not as a clever string.

You would notice this in review as concatenation or a format string around a column, and in a lab as a report that includes a row the account was never granted in the interface. You do not need a copy of the table to say that. Name the job, the column, and the role.

What you change

Make every statement that uses the column a constant SQL string, with the column passed as a parameter, and with the current tenant in the predicate. Where the code needs an identifier, map an allow-listed key to a column you wrote down. Do not escape your way through a string you are still concatenating.

Then shrink the role. The report account should not be the account that owns the schema. A test can store punctuation in the field and assert that the later query’s text does not change and that another tenant’s row is absent. That is the regression you want.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

A profile name is stored with a bound parameter. A nightly report then builds SQL by joining that stored name into the statement. What failed?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapSQL injection: how it happens, how testers confirm it, and the fix that holdsHow concatenated queries let input change SQL, the error, boolean, time, and out-of-band behaviors testers look for in an authorized test, what scanners like sqlmap report, and parameterization plus least privilege as the fix.
  1. 01Query builders that still concatenateAn ORM or a query builder is safe only when the SQL text stays under your control.

Practice

  1. LabTerminal labRead the files on a scoped host and name the failure in a finding.

Glossary

  1. CardSQL injectionUntrusted input changes a database query, exposing or altering records the caller should not reach.