# VNX-GO-017: Go SQL injection via fmt.Sprintf in db.Exec or db.Query

Detects fmt.Sprintf SQL that puts a string verb where SQL expects a literal (quoted, or after a comparison) and is run by Exec/Query/QueryRow/Prepare.

| Field | Value |
|---|---|
| Rule ID | `VNX-GO-017` |
| Severity | High |
| SARIF level | `error` |
| Kind | `sast` |
| Languages | Go |
| CWE | [CWE-89](https://cwe.mitre.org/data/definitions/89.html) |
| CAPEC | [CAPEC-66](https://capec.mitre.org/data/definitions/66.html) |
| ATT&CK | [T1190](https://attack.mitre.org/techniques/T1190/) |
| CWSS vector | `CWSS:1.0/TI:H/AP:A/AL:A/IC:N/FC:LT/RP:N/RL:A/AV:I/AS:N/IN:A/SC:A/BI:H/DI:H/EX:H/EC:N/P:H` |
| Tags | `sql-injection` `go` |
| Source | [`internal/sast/rules/vnx-go-017.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-go-017.rego) |

## Overview

This rule detects Go code where a SQL query string is constructed using `fmt.Sprintf` or string concatenation and then passed to a `database/sql` method such as `db.Exec()`, `db.Query()`, `db.QueryRow()`, or their `Context` variants. If any of the values interpolated into the query string are derived from user input, HTTP request parameters, form values, path segments, or JSON body fields, the code is vulnerable to SQL injection. This maps to CWE-89 (Improper Neutralization of Special Elements Used in an SQL Command).

Go's `database/sql` package provides first-class support for parameterised queries: any driver-compatible placeholder (`?` for MySQL/SQLite, `$1` for PostgreSQL) can be used, and the values are passed as separate arguments to the query method. This prevents SQL injection at the database driver level by ensuring user-supplied values are always treated as data, never as SQL syntax.

The rule fires in two cases: when `fmt.Sprintf` appears directly inside a database method call on the same line, and when `fmt.Sprintf` is used to build a string containing SQL keywords within a few lines of a subsequent database call.

## Why this matters

SQL injection remains one of the most exploited vulnerability classes in web applications. CAPEC-66 (SQL Injection) and MITRE ATT&CK T1190 (Exploit Public-Facing Application) both document it as a primary initial access technique. A successful SQL injection attack can allow an attacker to read arbitrary data from the database, modify or delete records, bypass authentication entirely, and in some configurations execute operating system commands.

In Go specifically, the temptation to use `fmt.Sprintf` for query construction is high because Go's string formatting is idiomatic and familiar. Unlike dynamic languages where ORM frameworks often enforce parameterisation, Go developers frequently interact with `database/sql` directly, making it easy to slip into `fmt.Sprintf(query, userValue)` patterns without recognising the risk. The fix, using placeholders and passing values as arguments, requires only a small code change and has zero performance cost.

## What gets flagged

```go
// FLAGGED: fmt.Sprintf used directly inside a Query call
rows, err := db.Query(fmt.Sprintf("SELECT * FROM users WHERE name = '%s'", username))

// FLAGGED: query string built with Sprintf then passed to Exec
query := fmt.Sprintf("DELETE FROM sessions WHERE token = '%s'", r.FormValue("token"))
_, err = db.Exec(query)

// FLAGGED: UPDATE with user-controlled column value
sql := fmt.Sprintf("UPDATE accounts SET role = '%s' WHERE id = %d", role, id)
db.ExecContext(ctx, sql)
```

## Remediation

1. **Use parameterised queries for all database calls.** Replace string interpolation with `?` placeholders (MySQL, SQLite) or `$N` placeholders (PostgreSQL) and pass values as additional arguments.

   ```go
   // SAFE: parameterised query, user input is treated as data, not SQL
   rows, err := db.QueryContext(ctx,
       "SELECT id, email FROM users WHERE name = ?",
       username,
   )
   ```

2. **For dynamic column or table names that cannot be parameterised**, use a strict allowlist and map user input to pre-approved identifiers. Never use raw user input as a table or column name.

   ```go
   // SAFE: allowlist for dynamic column names
   allowed := map[string]string{
       "name":  "name",
       "email": "email",
   }
   col, ok := allowed[r.FormValue("sort")]
   if !ok {
       col = "id"
   }
   rows, err := db.QueryContext(ctx,
       fmt.Sprintf("SELECT * FROM users ORDER BY %s", col), // col from allowlist only
   )
   ```

3. **Consider using a query builder library** such as `squirrel` or an ORM such as `sqlc` or `gorm` that enforces parameterisation by default, reducing the surface area for this class of mistake.

## References

- [CWE-89: Improper Neutralization of Special Elements Used in an SQL Command](https://cwe.mitre.org/data/definitions/89.html)
- [CAPEC-66: SQL Injection](https://capec.mitre.org/data/definitions/66.html)
- [MITRE ATT&CK T1190 – Exploit Public-Facing Application](https://attack.mitre.org/techniques/T1190/)
- [Go documentation – database/sql](https://pkg.go.dev/database/sql)
- [OWASP SQL Injection Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html)
- [OWASP Go-SCP – SQL injection](https://owasp.org/www-project-go-secure-coding-practices-guide/)
- [Go security best practices](https://go.dev/security/best-practices)

## Suppressing this rule

Put a `nosec` comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file's language (`#`, `//`, `--` or `;`):

```text
# nosec vnx-go-017
// nosec vnx-go-017
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-go-017 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
