Skip to content
jay-tankPublic

About

Zero-config cross-language static gate for SQL injection (CWE-89): flags user/request input reaching a raw SQL execute/query sink built by concatenation or interpolation (f-string, %, +, template literal, fmt.Sprintf, #{}) instead of a parameterized query, across Python/JS/Go/Java/PHP/Ruby. Single Go binary; SF001 blocker / SF002 warning.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

Repository files navigation

sqlfence

Flag SQL queries built from user input by string concatenation - before the injection ships.

📖 Read the write-up: sqlfence

cur.execute(f"SELECT * FROM users WHERE id = {request.args['id']}")

This runs. The tests pass. Review sees a normal query. But the SQL text now contains whatever the caller put in id - send 0 OR 1=1 and the WHERE collapses; send 0; DROP TABLE users; -- and the table is gone; send a UNION SELECT and they read tables they were never meant to. This is SQL injection, CWE-89 - the vulnerability at the top of the OWASP list for twenty years. The fix is a parameterized query: keep the SQL fixed and pass the value as a bound parameter. But you have to remember to do it, on every query, in every handler, in every language your codebase touches.

sqlfence is a zero-config, cross-language static gate that scans your code for raw SQL built from user/request input by concatenation or interpolation - instead of a parameterized query - and fails the build.

$ sqlfence .

● 2 SQL-injection risk(s):
  api/users.py:14:5  SQL string is built from user input (request.args) by concatenation/interpolation instead of a parameterized query (CWE-89).
      ↳ Use a parameterized query: cursor.execute("... WHERE id = %s", (user_id,)) - pass values as the second argument, never build the SQL with an f-string, %, .format() or +.
      [SF001 · CWE-89]
  routes/report.js:9:15  SQL string is built from user input (req.query) by concatenation/interpolation instead of a parameterized query (CWE-89).
      ↳ Use a parameterized query: db.query("... WHERE id = $1", [id]) - pass values as a separate array, never a template literal or + concatenation.
      [SF001 · CWE-89]

2 blockers · 0 warnings

Exit code 1 when it finds one, so it drops straight into pre-commit or CI.

sqlfence is the SQL side. It is distinct from the other injection/query tools in this catalog: noscape reasons about NoSQL / MongoDB query-operator injection (CWE-943), thawguard about unsafe deserialization, egresso about SSRF, and pathfence about path traversal. sqlfence reasons only about raw SQL built from user input (CWE-89).

How it works

For every raw-SQL execute/query sink - cursor.execute / SQLAlchemy text() / Django .raw() (Python), db.query / knex.raw (JS/TS), db.Query / Exec (Go), Statement.executeQuery (Java), mysqli_query / $pdo->query (PHP), connection.execute / .where("…") (Ruby) - sqlfence asks two questions:

  • Is the SQL string built by concatenation or interpolation? - an f-string, a % format operator, .format(), +, a template literal ${…}, fmt.Sprintf, PHP "…$var", or Ruby #{…}.
  • Does that fused string carry user input? - request.args, req.body, params[…], $_GET, getParameter, r.URL.Query(), and the like - directly, or through a variable assigned from user input within a small window (light data flow, reset at every function boundary).

If both hold, it's SF001 (blocker). A suspiciously-named dynamic-SQL variable (query, sql, stmt) reaching a sink with an unconfirmed origin is SF002 (warning).

It does not fire on a parameterized query (the value in a separate bound argument - execute("… %s", (uid,)), Query("… $1", id), a PreparedStatement with ?), on an ORM query builder (.filter(), .where(col: val)), on escaped or numeric-cast values (mysqli_real_escape_string, $pdo->quote, int(...)), or on a constant query. Parameterization is the recommended fix and is recognised as clean.

No code is executed, no network calls are made, and there's nothing to configure. It's a single static binary.

Install

go install github.com/jay-tank/sqlfence@latest

Or build from source: go build -o sqlfence .

Usage

sqlfence                 # scan the current directory
sqlfence ./src           # scan a path
sqlfence --json          # machine-readable output
sqlfence --strict        # treat warnings (SF002) as failures too

In CI

- run: go run github.com/jay-tank/sqlfence@latest ./

Exit codes: 0 clean · 1 a SQL-injection risk (or any finding under --strict) · 2 usage error.

Rules

Rule Severity What
SF001 blocker Raw SQL is built from user/request input by concatenation/interpolation, reaching an execute/query sink with no parameterization (CWE-89)
SF002 warning A suspiciously-named dynamic-SQL variable reaches a query/execute sink; origin unconfirmed

The fix hint is language-aware - parameterized %s/$1/?/:name placeholders for Python, JS, Go, Java, PHP and Ruby respectively.

Suppressing false positives

sqlfence is a name-and-flow heuristic. It can miss SQL that flows through a helper it doesn't follow, and it can over-flag (SF002 especially) a variable that happens to be named like a query. Two ways to suppress:

  • Per line: add a sqlfence:ignore comment on the query's line.
  • Per path: list a path substring in a .sqlfenceignore file in the scanned root (one per line, # for comments).

Scope & honesty

sqlfence answers one question well - "is this SQL query's string built from user input with no parameterization?" - across six languages, as a fast static gate. It is a line- and window-scoped heuristic, not a full taint engine: it tracks a query variable back to a user-input assignment within a small window and resets at each function boundary, so it can miss cross-function flows and can over-flag a benignly-named variable. sqlfence:ignore is the escape hatch. That single, targeted question still catches the most common shape of a bug that audits usually find only by hand.

License

MIT © Jay Tank

About

Zero-config cross-language static gate for SQL injection (CWE-89): flags user/request input reaching a raw SQL execute/query sink built by concatenation or interpolation (f-string, %, +, template literal, fmt.Sprintf, #{}) instead of a parameterized query, across Python/JS/Go/Java/PHP/Ruby. Single Go binary; SF001 blocker / SF002 warning.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages