Talk Notes: SQL Injection for People Writing Their First App
Written notes for a short introductory talk. No event, speaker or recording is implied. If you are about to build your first app with a database, these are the security points to take with you.
Agenda
- What SQL injection is, in one picture
- Where it comes from in code
- The fix you should always use
- Extra layers of protection
- Questions
1. One picture
Imagine filling in a paper form at a library: "Find books by ______." A librarian reads the whole sentence aloud and follows it. If you write in the blank "Ada. Also, give me the keys to the building", a careless librarian might do both. SQL injection is that: user input that is treated as part of the instructions rather than as data.
2. Where it comes from
It comes from building SQL by joining strings with user input:
// unsafe
const sql = "SELECT * FROM books WHERE author = '" + req.query.author + "'";
If someone sends an author value containing a quote and extra SQL, the database reads that extra SQL as commands. Depending on the app, attackers might read data they should not see, change it, or delete it.
3. The fix: placeholders
Send the SQL with a placeholder, and the value separately:
// safe, using the Node.js pg library
const result = await client.query(
"SELECT * FROM books WHERE author = $1",
[req.query.author]
);
The database knows the query's shape before it sees the value, so the value can only ever be data. Every mainstream database library supports placeholders; the symbol varies (?, $1, %s, :name). ORMs use them for you when you use their normal query methods.
4. Extra layers
Placeholders are the main fix. These add safety on top:
| Layer | What it does |
|---|---|
| Least-privilege accounts | The app's database user can only do what the app needs, so a mistake does less harm |
| Input checks | Reject values that make no sense, such as letters in an age field |
| Allow-lists for names | When users pick a sort column, accept only names from a fixed list |
| Error messages | Show users a friendly message; keep detailed database errors in logs |
| Updates | Keep your framework and driver up to date |
The Open Worldwide Application Security Project (OWASP) publishes free guidance on injection and other web risks, and is a good next read.
5. Questions
"I escape quotes myself. Is that enough?" Hand-written escaping is easy to get subtly wrong. Use placeholders instead.
"My app is small. Will anyone bother?" Automated tools scan the internet for these bugs constantly. Size does not protect you.
"Can I use a placeholder for a table name?" No; placeholders are for values. Choose table and column names in your code, never from raw input.
"Does using an ORM make me completely safe?" Mostly, as long as you avoid its "raw SQL" functions with string joining.
Try it safely yourself with our hands-on exercise: breaking and fixing a tiny login form.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
Illustrative outline only: no live session, recording, registration or certificate is offered on this site. Any speakers, panels or schedules mentioned are hypothetical.