Reviewing AI-Generated Code
Can code that works still be dangerous? An assistant writes Campus Helper a neat function that looks up a student's fees by name. It works perfectly in testing. Then Navika, testing it the way a curious student might, types a strange name, and the function returns every student's fees.
That is SQL injection, one of the oldest security mistakes in software, and AI assistants can still produce it. This lesson shows the bug for real, and a checklist for reviewing any AI-generated code.
A Real SQL Injection
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE students (name TEXT, grade INTEGER, fee_due INTEGER)")
db.executemany("INSERT INTO students VALUES (?, ?, ?)",
[("Student A", 10, 45), ("Student B", 11, 0), ("Student C", 12, 120)])
def fees_unsafe(name):
# Looks fine, but builds SQL by pasting user text into the query
return db.execute(f"SELECT name, fee_due FROM students WHERE name = '{name}'").fetchall()
def fees_safe(name):
# A parameterized query: the database treats the input as data, never as SQL
return db.execute("SELECT name, fee_due FROM students WHERE name = ?", (name,)).fetchall()
attack = "x' OR '1'='1"
print("Unsafe, normal input:", fees_unsafe("Student A"))
print("Unsafe, attack input:", fees_unsafe(attack))
print("Safe, attack input: ", fees_safe(attack))Unsafe, normal input: [('Student A', 45)]
Unsafe, attack input: [('Student A', 45), ('Student B', 0), ('Student C', 120)]
Safe, attack input: []The attack text closes the quote and adds OR '1'='1', which is always true, so the query matches every row. The safe version passes the input separately, so the database never runs it as SQL. The students here are made up, in a temporary in-memory database.
A Review Checklist
| Check | What to look for |
|---|---|
| Correct | Does it do what the requirement says, including edge cases? |
| Tested | Are there tests, including empty, huge and malicious inputs? |
| Secure | No injection; inputs validated; no secrets in the code |
| Dependencies | Do suggested packages really exist, and are they trusted and maintained? |
| Licenses | Is any copied code allowed under your project's license? |
| Readable | Clear names and structure a teammate can maintain |
Check package names carefully. AI assistants sometimes suggest packages that don't exist, and attackers have published malicious packages under names like the ones models invent. Look each new dependency up on the official package index before installing it.
Common Mistakes
Trusting code because it passes a happy-path test. Test bad and malicious inputs too.
Installing a suggested package without checking it. Confirm it exists and is trusted.
Skipping review because "the AI wrote it". Review it more carefully, not less.
Interview Corner
How do you review AI-generated code?
Like any untrusted code: check correctness against the requirement, add tests including edge cases and malicious input, look for security issues such as injection and hard-coded secrets, verify every dependency exists and is trusted, check licenses, and make sure it's readable.
What is SQL injection, and how do you prevent it?
Malicious input that changes a database query because the code pastes user text into SQL. Prevent it with parameterized queries, which pass input as data separately from the SQL.
Practice
Exercise: break and fix. Run the program. Then try two other attack strings against fees_unsafe, and confirm fees_safe blocks them. Finally, ask an AI assistant to write a function that searches students by grade, and review its answer with the six-point checklist.
Check your understanding:
- Why did the attack input return every student?
- How does a parameterized query prevent injection?
- Why check that a suggested package really exists?
Key Takeaways
Quick reference
| Risk | Defense |
|---|---|
| SQL injection | Parameterized queries |
| Fake packages | Check the official index |
| Secrets in code | Environment variables, secret managers |
| License problems | Check copied code's license |
Was this tutorial helpful?