SQL injection via string concatenation
An AI agent generated this Postgres PL/pgSQL function so an analytics service can fetch a metric for a tenant, with a caller-supplied column to sort by. It runs and sorts correctly in testing. Review it for production.
Implement
build_metrics_query(tenant: str, sort_col: str, allowed_cols: list[str]) → list[str]Examples
in
["acme","created_at",["created_at","value"]]out["SELECT * FROM metrics WHERE tenant_id = $1 ORDER BY \"created_at\"","acme"]in
["acme","id; DROP TABLE metrics; --",["created_at","value"]]out[]in
["x' OR '1'='1","value",["created_at","value"]]out["SELECT * FROM metrics WHERE tenant_id = $1 ORDER BY \"value\"","x' OR '1'='1"]What a strong answer looks like
Treat the AI’s output as a draft to verify, not an answer to trust. Name the specific flaw and the input that triggers it, say how you’d catch it (tests, edge cases, reading critically), and how you’d re-prompt or decompose to get it right.
0:00 of about 18 min
Vibe & agentic: describe the solution in plain language (or narrate it) and the coach grades your approach.
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.