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.
Diagram
From a helper call to a statement the database parses
Step 1 of 4
Call
Application code asks a builder for rows. The call looks like an API, not like SQL.
# Application code asks a builder for rows. # The call looks like an API, not like SQL. sort_key = "created"
A where-clause is built with a formatted string.
The field’s characters change the statement.
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.
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.
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.
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.
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.
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.