How AI code goes wrong
Lesson 24 Security holes
injection, missing auth, trusted input, secrets, path traversal, eval and pickle
What you'll learn
- Follow outside input to the places where it becomes dangerous
- Spot SQL injection, path traversal, eval and client-side trust
- Keep secrets out of code
Security bugs don't crash. The code works perfectly, for the wrong person.
The idea
Anything that comes from outside is untrusted input: a form field, a URL, a file name, a request body. AI code is written for friendly input and works fine until someone is not friendly.
So follow each outside value to where it lands. Does it land in a SQL string, a file path, eval, a shell command, or an "is this user allowed?" decision? If it lands there unchecked, that is the hole. Also look for secrets (keys and passwords) typed into the code: once committed, they are visible to everyone who ever sees the repo.
1const path = require("path");2const API_KEY = "sk_live_EXAMPLE_not_a_real_key";3const REPORTS = "/srv/reports";45function findUser(db, email) {6 return db.query(`SELECT * FROM users WHERE email = '${email}'`);7}89function reportPath(name) {10 return path.join(REPORTS, name);11}1213app.post("/orders/:id/refund", (req, res) => {14 const { amount, isAdmin } = req.body;15 if (isAdmin) refund(req.params.id, amount);16 res.sendStatus(200);17});
- Line 2 A secret in the code. It will be in git history forever. It belongs in an environment variable or a secrets manager.
- Line 6 The email is pasted into the SQL text. Use placeholders, so the database gets the value separately from the command.
- Line 10
namecomes from the user. Can it climb out of/srv/reportswith..? - Line 15 The client says whether it is an admin. The server must decide that from the logged-in session, never from the request.
Take away
- Follow every value that comes from outside to where it lands.
- SQL, file paths, eval and the "is this an admin?" decision are the places to look first.
- Never let the client tell the server who it is, and never keep a secret in the code.
Quiz: test yourself
7 questions: a quick quiz, then a review drill. Pick an answer to see why it's right.