Injection-Safe Queries with larzsql
SQL injection is one of the oldest and most damaging bugs - and it's still everywhere. larzsql makes it impossible by construction: every value is a bound parameter, never inlined text.
SQL injection - where user input is tricked into becoming executable SQL - remains one of the most common and catastrophic security bugs, decades after it was first understood. The usual advice ('always use parameterised queries') relies on developers remembering, every time. larzsql takes a stronger approach: it builds queries so that user values can't become SQL, making injection impossible by construction.
Why injection happens
Injection happens when a value is glued into the SQL text as code instead of treated as data. larzsql never does that.
| Dangerous (string-building) | Safe (larzsql) |
|---|---|
| f"...WHERE email='{input}'" | value becomes a bound ? parameter |
| Value glued into SQL text | Value travels in a separate params list |
| Relies on you remembering to escape | Impossible to inline a value at all |
| One slip = a breach | Safe by construction, every time |
The root cause is always the same: mixing untrusted data into the query as if it were trusted code. Remove the ability to do that, and injection can't occur.
Safe by construction
larzsql produces a (sql, params) pair - the SQL has placeholders, the values are passed separately to the driver - so user input is always data, never code.
from larzsql import select
sql, params = (select('users')
.where(email=user_input) # bound, never inlined
.where(active=True)
.order_by('-created').limit(10).build())
# sql: SELECT * FROM users WHERE email = ? AND active = ? ORDER BY created DESC LIMIT 10
# params: [user_input, True] # values kept separate from the SQL?) plus a separate params list, there is no code path where a value gets inlined into the SQL string - so injection isn't 'unlikely if you're careful', it's structurally impossible. Identifiers (table/column names) are validated too. You get injection-safety for free, without having to remember to do the right thing on every single query.Build queries safely
- Every value is a bound parameter - never inlined into SQL text.
- Identifiers validated - table/column names checked, not trusted.
- Rich builder - where/joins/order/limit, ? or %s paramstyles.
- Works with real databases - the (sql, params) pair feeds any DB-API driver.
๐ฏ Key takeaways
- SQL injection - user input becoming executable SQL - is still one of the most damaging bugs.
- It happens when values are glued into SQL text as code; larzsql never does that.
- larzsql emits a placeholder + a separate params list, so values are always data, never code - injection is structurally impossible.
- It validates identifiers and produces a (sql, params) pair for any DB-API driver.
Frequently asked
Doesn't every database library already prevent SQL injection?
The tools exist (parameterised queries), but injection is still rampant because they rely on the developer choosing to use them correctly on every single query - and under deadline pressure, people build SQL strings by hand, especially for dynamic queries, and one slip is a breach. larzsql's difference is that it removes the unsafe path entirely: there's no way to inline a value into the SQL, because the builder only ever produces placeholders plus a separate params list. So safety doesn't depend on discipline or remembering - it's a structural guarantee of the tool. That's a meaningful upgrade over 'we have the safe option available if you remember to use it'.
Can I still write complex queries, or only simple ones?
You can build genuinely useful queries - the builder supports WHERE with a range of operators, multiple conditions, joins, ordering, limits and offsets, and both ? and %s parameter styles for different drivers - all while keeping every value safely bound. For truly exotic, hand-tuned SQL beyond the builder, you can still write raw SQL with explicit parameters (the safe way), so you're never blocked. The point isn't to replace all SQL knowledge; it's to make the common and dynamic cases - exactly where injection bugs creep in - safe by default, while remaining expressive enough for most real application queries.
Related lessons
Storage Without a Database Server
Most projects reach for Postgres on day one - and take on a server to run, secure and backโฆ
Data & Storage with the Larz StackEvolving Your Schema with Migrations
Data outlives code, and its shape changes over time. Migrations let you evolve your databaโฆ
Building with the Larz StackData & Storage โ Persisting Without a Server
Most apps don't need a database server on day one. This family gives you crash-safe storagโฆ