Home / Learn Secure SQL / Code That Looks Safe but Is Not: a Language Tour

Code That Looks Safe but Is Not: a Language Tour

Modern syntax can disguise the exact same old mistake.

Quick summaryModern syntax can disguise the exact same old mistake.

How to think about this

Different syntax, same underlying mistakePython f-string: f"SELECT * FROM t WHERE id={id}"JS template literal: `SELECT * FROM t WHERE id=${id}`Java String.format: String.format("...WHERE id=%s", id)All three: one combined string handed to the database, exactly like…
Different syntax, same underlying mistake

In plain terms

Several modern language features can make an unsafe, string-built query look deceptively like safe, modern code: a Python f-string or a JavaScript template literal used to build a query string, or a Java String.format call feeding a Statement, all still produce one combined string handed to the database — structurally identical to old-style concatenation, just written with newer syntax.

Why we use it

A developer who has learned to avoid "+" string concatenation can still fall into this trap, because an f-string or template literal does not look like concatenation at a glance; recognizing that the risk is about what happens to the value, not which syntax built the string, prevents this specific, easy-to-miss mistake.

Under the hood

When reviewing code in any language, check whether a query-building expression (however it is written — f-string, template literal, format call, or plain concatenation) ever embeds a variable's value directly into the SQL text; if so, treat it exactly the same as old-style string concatenation, regardless of how modern the syntax looks.

Real-world use

  • Web application code review
  • Backend development
  • Database administration
The same mistake, four different "modern-looking" ways
# Python -- looks modern, is NOT safe:
cursor.execute(f"SELECT * FROM users WHERE id={user_id}")

// JavaScript -- looks modern, is NOT safe:
db.query(`SELECT * FROM users WHERE id=${userId}`);

// Java -- looks modern, is NOT safe:
stmt.executeQuery(String.format("SELECT * FROM users WHERE id=%s", userId));

# Any of the three, done safely instead (the pattern this course teaches throughout):
cursor.execute("SELECT * FROM users WHERE id = ?", (user_id,))

This is purely a syntax-recognition lesson: no new technique is introduced here, only a reminder to look past how modern a line of code looks and check what actually happens to the value.

Quick quiz

1. An f-string or template literal used to build a query string is...