A Printable Planner for Making a Small App Injection-Proof
SQL injection is easy to prevent, but only if every query is checked. In a small app, you can list them all. This planner walks you through doing exactly that. Print it or copy the tables into a document.
Step 1: list every place data comes in
Anything a user, a file or another program can send to your app is an input.
| Input | Where it appears (page, form, URL, file) | Example value |
|---|
Common inputs in a beginner app: login forms, search boxes, URL parts such as /posts/42, sorting choices, uploaded spreadsheets.
Step 2: list every query
Search your code for SQL keywords like SELECT, INSERT, UPDATE and DELETE, and for your ORM's raw SQL functions.
| File and line | What the query does | Inputs it uses |
|---|
Step 3: check how each input reaches the query
For every row in Step 2, tick one box.
| Query | Placeholder or ORM filter | Allow-list | Glued into the string |
|---|
- Placeholder or ORM filter: safe for values.
- Allow-list: safe for things like column names or sort order, chosen from a fixed set.
- Glued into the string: needs fixing.
Step 4: fix the glued ones
For each query marked "glued", write the fix:
| Query | Fix |
|---|
Typical fixes:
# before
cur.execute("SELECT * FROM posts WHERE id = " + post_id)
# after
cur.execute("SELECT * FROM posts WHERE id = ?", (post_id,))
Step 5: test
| Test | Result |
|---|---|
Type a single quote ' into each text input. Does the app show a database error? | |
Type ' OR '1'='1 into the login form on your practice copy. Does it log you in? | |
| Try an unexpected sort value in the URL. Is it rejected or replaced by a default? |
A database error message from a quote is a strong hint that something is glued. Only test your own app, on a practice copy.
Step 6: keep it fixed
- Add a note to your project README: "All queries use placeholders."
- When you add a new query, add a row to Step 2.
- Ask a classmate or friend to review the list.
Instructions
Work through the steps in order, in one sitting if possible. For a small app with a handful of pages, this usually takes an afternoon. Do not skip Step 1; queries are only dangerous because of the inputs that reach them.
For background, see SQL injection: short questions, plain answers and the comparison of five ways people try to stop SQL injection.
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.