Interview Security & DevSecOps

What is an injection attack, and how do you prevent SQL injection? [Advanced]

Answer

An injection attack occurs when untrusted input is interpreted as code or commands by an interpreter such as SQL, shell, LDAP, or NoSQL. SQL injection is prevented with parameterized queries, prepared statements, input validation, least-privilege database users, and avoiding dynamic SQL string concatenation.

Technical explanation

Escaping alone is error-prone and should not be the primary control when parameterization is available.

ORMs help but can still be unsafe if raw query strings are built from user input.

Least-privilege database roles limit damage if injection occurs.

Hands-on example

Unsafe: SELECT * FROM users WHERE name = '<user input>'

Safe Java pattern:

PreparedStatement ps = conn.prepareStatement("SELECT * FROM users WHERE name = ?");

ps.setString(1, userInput);

ResultSet rs = ps.executeQuery();

Preparing for an interview?

Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.

More Security & DevSecOps interview questions

← All Security & DevSecOps questions