The text of an SQL statement contains a value taken from an HTTP request

tx0/go:request-value-in-sqlrecommendedwarningsecurity/injectiondepth fastsecurity, cwe-89

What it finds

db.Query("SELECT * FROM items WHERE owner = '" + c.Query("owner") + "'"): a value from the request becomes part of the text of the statement, so the client can close the literal and append SQL of its own. Placeholders send the value apart from the statement; names of columns or tables are picked from a fixed list.

The statement is the first argument of Query, QueryRow, Exec and Prepare, of Queryx, QueryRowx and MustExec of sqlx and of Raw, Where, Order and Having of gorm, the argument after the context for their Context forms (and for the methods of pgx, whose first argument is a context), and the argument after the destination for Select and Get of sqlx. The file imports database/sql, sqlx, gorm or pgx, or the receiver is the handle of a GetDB() call, as gin and echo apps keep a shared gorm handle in a package of their own (db := common.GetDB()), and the statement holds a request value: directly, in a concatenation, through fmt.Sprintf, or through a local variable built so that the function does not check in a condition, as described for the request values of the pack, including those of gin, echo, fiber, chi and gorilla/mux.

From the rule's source, request_value_in_sql.rs.

Example from the rule's tests
package p import (	"database/sql"	"fmt"	"net/http" 	"github.com/gin-gonic/gin") func search(db *sql.DB, w http.ResponseWriter, r *http.Request) {	name := r.FormValue("name")	db.Query("SELECT id FROM users WHERE name = '" + name + "'")tx0 reports: the statement contains a value taken from the request	db.Query("SELECT id FROM users WHERE name = ?", name)	q := fmt.Sprintf("DELETE FROM items WHERE id = %s", r.URL.Query().Get("id"))	db.ExecContext(r.Context(), q)tx0 reports: the statement contains a value taken from the request} func list(db *sql.DB) gin.HandlerFunc {	return func(c *gin.Context) {		order := c.DefaultQuery("order", "name")		db.Query("SELECT * FROM items ORDER BY " + order)tx0 reports: the statement contains a value taken from the request		c.Query("other")	}}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:request-value-in-sql" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/go:request-value-in-sql] reason="..."

tx0 explain tx0/go:request-value-in-sql shows the same in the terminal. Found a false positive? Report it.