Learning center

Does this SQL injection comment point at query text or a bound value?

Ask what reaches the SQL text before the database parses it. A value sent as a bound parameter cannot change the query, so that comment gets a dismissal naming the parameter; untrusted text spliced into the query gets a change, by binding it if it is a value and by allowlisting it if it is an identifier.

Find what reaches the query text#

An automated reviewer usually flags SQL injection when it sees string building near a query: an f-string, +, format() or a StringBuilder. String building only shows you where to look. The defect to find is text that the database parses as SQL and that came from somewhere you do not control.

Two facts settle most of these comments. A bound value stays data whatever it contains, and a placeholder cannot stand in for a table or column name. Python's sqlite3 shows both:

>>> import sqlite3
>>> c = sqlite3.connect(":memory:")
>>> _ = c.execute("create table t(x)")
>>> c.execute("select ?", ("1; drop table t",)).fetchall()
[('1; drop table t',)]
>>> c.execute("select * from ?", ("t",))
Traceback (most recent call last):
  ...
sqlite3.OperationalError: near "?": syntax error

OWASP's cheat sheet puts the same limit in general terms: "Parameter markers represent data values, not table names, column names, or SQL keywords such as ASC and DESC." For those parts it recommends mapping input to "a strict allow-list of fixed, application-owned values", and it warns that "Relying on manually escaping dynamic inputs is error-prone and is strongly discouraged." OWASP SQL Injection Prevention Cheat Sheet PostgreSQL's format('%I', ...) and psycopg's sql.Identifier quote identifiers, but quoting only keeps the syntax intact. It does not limit which table a caller can name.

Follow a comment that was right about both kinds#

In April 2026, an Apache Beam pull request let SpannerIO.readChangeStream() read and union a list of change stream table-valued functions (TVFs). Gemini Code Assist's review summary raised "critical security concerns regarding SQL injection risks when concatenating TVF names into queries and suggests using parameterized queries or more robust identifier escaping mechanisms." Review summary Its inline comments are now resolved and their text is hidden on the page. One of them called concatenating tvfName into the query string an injection risk and noted that, although a TVF name cannot be parameterized in the FROM clause, it should be strictly validated. Pull request

The reviewed commit had two different problems under that one label. The change stream query put the TVF name in the FROM clause, where no placeholder can go. A new existence check also built an IN (...) list by wrapping each name in single quotes after stripping quote characters from it. Those names were values, and values can be bound:

inClauseBuilder
    .append("'")
    .append(PartitionMetadataDao.escapeTvfName(tvfNameList.get(i)))
    .append("'");

Code at the reviewed commit

The version merged on 27 April 2026 treats each kind differently. The existence check now emits one placeholder per name ($1, $2 for PostgreSQL, @p0, @p1 for GoogleSQL) and binds the names. The FROM clause still concatenates the TVF name, but only after checkTvfExistence has confirmed that every requested name appears in information_schema.routines, and an unknown name raises an IllegalArgumentException. Merged diff The database's own catalog became the allowlist.

Two limits apply to that reading. The quote stripping that remains is the manual escaping OWASP discourages; the catalog check, not the stripping, is what constrains the identifier. And the TVF list comes from the pipeline author's own configuration, so the person who could inject is already running code against that database. That lowers the severity. It does not make the bot wrong, because the bound IN list and the catalog check also stop a typo or an odd name from producing broken SQL.

Dismiss when the text is markers or constants#

The most common false positive looks like the fixed Beam code: a loop that builds ?, ?, ? from a list's length, with the values passed separately. Nothing from the list reaches the SQL text. The reply can say so in one sentence and point at the execute call that receives the values.

An identifier chosen from a fixed mapping in code is the second common case. If a sort option arrives as "newest" and the code maps it to ORDER BY created_at DESC from a dictionary, the only strings that reach the query are the dictionary's values. Name the mapping in the dismissal. If the mapping is far from the query, a short assertion where the identifier is used costs little and survives refactors.

When you cannot tell where a value comes from, write a test before you decide. Pass a value containing a single quote. With binding, the query runs and treats the quote as data; with concatenation, it fails with a syntax error. A single quote is enough to tell the two apart, and the test keeps its answer after the next refactor.

Decide from what reaches the SQL#

What reaches the SQL textEvidence to checkResponse
A placeholder, with the value passed separatelyThe driver call that receives the valueDismiss, naming the parameter
Placeholders generated from a countThe loop emits only markersDismiss, as in Beam's merged existence check
A value spliced into the stringWhere the value comes fromChange: bind it
An identifier from a fixed mapping in codeThe mapping and the call siteDismiss, or add an assertion beside the query
An identifier from input or configurationEvery caller that supplies itChange: allowlist it, or check it against the catalog, then quote it
A value whose origin you cannot traceA test with a single quoteTest first, then change or dismiss

Severity is a separate question from validity. A finding about configuration the operator controls can be correct and still rank below one about request data; the severity calibration guide covers how to score that. When you dismiss, record the parameter or mapping that answered the claim, as the guide to dismissed findings describes, so the next reviewer who raises it gets the same answer.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading