Aller au contenu

SQL injection (ARCH4100)

Ce contenu n’est pas encore disponible dans votre langue.

Key java.security.sql-injection · Severity error · Category security · CWE CWE-89

Building a SQL query by concatenating user input lets attackers alter the query structure to read or modify data or bypass authentication.

String sql = "SELECT * FROM users WHERE name = '" + name + "'";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
PreparedStatement ps =
conn.prepareStatement("SELECT * FROM users WHERE name = ?");
ps.setString(1, name);
ResultSet rs = ps.executeQuery();

Use a PreparedStatement with bound parameters for all user input; never build SQL through string concatenation.