Fix the pre-commit risk scan for an AI diff
Your task
You built a backend feature with an AI agent: a new endpoint, a database query, some config and two new dependencies. Rather than read every line, your team runs a quick pass over the diff for the places AI code most often goes wrong: secrets, injection, auth, validation, logging and dependencies.
An assistant wrote scan_ai_diff_for_risks(diff_lines). It is in the editor. It has a bug. Find it and fix it.
Input and output:
diff_lines: the lines of a unified diff.- Return the checks the diff trips, each at most once, in this fixed order:
"secrets","injection","auth","validation","logging","dependency".
Rules the tests check:
- Only added lines count: lines that start with
+, including a+++file header. Remove the first+, then lowercase the rest. Every check below looks at that text. "secrets": it containssk_live,api_keyorpassword."injection": it containsqueryorexec, and also contains a+, a sign the statement is built by joining strings."auth": it containsrouter.but notrequireauth."validation": it containsreq.bodybut notvalidate."logging": it containslog(orcapture(, and alsopayload."dependency": it contains^, the mark of a version range in a package manifest.
Examples
scan_ai_diff_for_risks(diff_lines: list[str]) -> list[str]Example 1
Input
scan_ai_diff_for_risks(["+const API_KEY = sk_live_4eC39HqLyjWDarjt;"])Output
["secrets"]Example 2
Input
scan_ai_diff_for_risks(["+ const rows = await db.query(SELECT * FROM users WHERE name = + name);"])Output
["injection"]Example 3
Input
scan_ai_diff_for_risks(["-const API_KEY = sk_live_4eC39HqLyjWDarjt;", " const port = 3000;", "+ res.json(ok);"])Output
[]