Query builders that still concatenate

An ORM or a query builder is safe only when the SQL text stays under your control. Helpers that format values into the string are still concatenation.

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

Diagram

From a helper call to a statement the database parses

Follow the path. One example at a time.

Step 1 of 4

Call

Application code asks a builder for rows. The call looks like an API, not like SQL.

Illustration: the call looks like an API
# Application code asks a builder for rows.
# The call looks like an API, not like SQL.
sort_key = "created"
What changes the outcome
Call

A where-clause is built with a formatted string.

SQL

The field’s characters change the statement.

Role

The connection can still see every table.

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 helper is not a guarantee

    What fails
    A builder prevents injection only for the calls that bind. A raw fragment or a formatted clause opts back out.
    Who is affected
    People whose rows that connection can read or change.
    What you would notice
    A review that finds .raw, format, or string addition inside a builder chain.
    What you change
    Ban unreviewed raw SQL. Bind every value that came from a caller or from storage.
  2. Identifiers are not values

    What fails
    Sort columns, table names, and column lists cannot be bound on most engines. They have to be chosen from a map.
    Who is affected
    Anyone who can be reached if a sort key changes which table is read.
    What you would notice
    A request parameter named sort, order, or column passed straight into the builder.
    What you change
    Map each public key to a column you wrote. Ignore every other key.
  3. The connection beside the builder

    What fails
    A perfect query still runs as the connection’s role. Builders do not shrink grants.
    Who is affected
    Other tenants, if the role can see them and a predicate forgets the owner.
    What you would notice
    One database user for the whole application, including migrations and the public API.
    What you change
    Use a runtime role that cannot alter the schema and cannot read other products.
  4. What a test can lock

    What fails
    The regression is a value that contains punctuation and a statement whose shape does not change.
    Who is affected
    The next person who adds a filter and copies a formatted example.
    What you would notice
    A test that only checks the happy path with a single word.
    What you change
    Assert the SQL text or the placeholder count, and assert another tenant’s id is absent.

A builder is a way of writing SQL

Object-relational mappers and query builders exist so you stop gluing strings. They do that for the methods that take a value and a placeholder. They also expose an escape hatch: raw SQL, a fragment, a where clause that is already a string. That hatch is the whole bug class, wearing a library import.

Read the call, not the dependency name. If a request field, a header, or a stored column is interpolated into a clause, the database will parse it. The rest of the chain being parameterized does not sanitize the one clause that opted out.

Values bind. Names do not.

A search string, an id, and a date are values. They belong in placeholders. A sort column, a table, or an operator is structure. Most drivers cannot bind structure. The safe pattern is a map from a small public key, such as “newest”, to a column you wrote, such as created_at. Unknown keys fall back to the default. They are not appended.

The same rule covers IN lists. Expand a list of placeholders and bind each element. Do not join the caller’s comma-separated text into the parentheses. The length may vary. The SQL around it should not.

The role is still yours to set

Teams adopt a builder and keep the connection string that the migration used. That login can alter tables. A bug in one raw clause then has the keys to the schema. Least privilege is not a feature of the library. It is a grant.

You would notice this in a connection configuration that is shared by migrations and the running app, and in code review when a raw call is added to meet a deadline. The finding names the call, the field, and the grant. It does not need a dump of the table.

How you keep it from coming back

Lint or review for the raw escape hatch. Require a comment and a ticket when it is genuinely needed, and require that the interpolated pieces are constants from the repo, not from the request. New filters copy existing calls, so the example in the codebase should be the bound one.

Add a test that passes punctuation in a value and checks that the query still returns only the caller’s rows. Add another that passes an unknown sort key and checks that the column did not change. Those two tests catch the regressions this page is about.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

A query builder is in use, and one filter is added with a formatted string because the example did it that way. 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. 01Second-order SQL injectionText that was stored safely can still change a later query if that job concatenates the stored value into SQL.

Practice

  1. LabParameterized vs concatenated queriesBuild the same product search three ways and watch the token view: concatenation lets input change the query, bound parameters and allowlists keep it a value.

Glossary

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