Skip to main content

You find code that builds a SQL query by joining strings with user input. What is the risk and how do you fix it?

AI security · Beginner 3

Short answer

It is open to SQL injection: an attacker can type input that changes the query itself, to read, change or delete data. The fix is parameterised queries, so input is always treated as data and never as SQL. I would add least-privilege database accounts, input validation as a second layer, and a test with malicious input.

Deeper answer

When code does something like "SELECT * FROM users WHERE email = '" + email + "'", a quote in the input ends the string and the rest is run as SQL. Parameterised queries, or an ORM used properly, send the query and the values separately, so the database never parses the input as code.

Escaping by hand and blocklists of words like DROP are weak because attackers find encodings and edge cases. Defence in depth helps: the app's database user should have only the rights it needs, errors should not show SQL to users, and a web application firewall can catch common payloads. Search the codebase for the same pattern, because one bad query usually means others.

What a strong answer covers

Answer it out loud in the mock interview about 1.5 min

Follow-ups to expect

Common mistakes

Learn it

Web app security and the OWASP Top 10

Practice AI security questions

More on this topic

Today's AI turns are used

Lessons, roadmaps, videos, practice cards and the code playground stay free with your account, with no limit.