Yes, it can cause problems as it would allow the user to manipulate the query. In this answer there is a simple yet dangerous demonstration of how an attacker could exploit this. That’s why there is a class PreparedStatement
, to provide security when treating data that comes directly from the user. In this other answer there is a good explanation of why to use this class.
The PreparedStatement
has the feature to allow parameterize user entries to be concatenated with the query. In the code presented, despite using this class, you only created the variable, but did not parameterize anything and is passing directly in the query.
To parameterize, you need to isolate the data received from the query and leave it to the PreparedStatement
concatenation. Consider the type of data received in order to call the correct equivalent set method. Assuming some of the types of its variables, it would look something like this:
con.atualizar(nome, sexo, nasc, cpf, renda);
[...]
public void atualizar(String nome,char sexo, java.util.Date nasc, String cpf, double renda)throws SQLException{
String query = "INSERT INTO CLIENTES (nome, sexo, nascimento, cpf, renda)"
+ " VALUES: (?, ?, ?, ?, ?);"
try{
PreparedStatement st = con.prepareStatement(query);
st.setString(1, nome);
st.setString(2, sexo);
st.setDate(3, new java.sql.Date(nasc.getTime()));
st.setString(4, cpf);
st.setDouble(5, renda);
st.executeQuery();
}catch (SQLException e){
System.out.println("Erro na atualização. RollBack será efetuado.");
con.rollback();
}
}
Of course, one cannot rely on this alone as the only way to prevent injection breaches, it is only one layer of several that one should have to protect your application.
Use Preparedstatement, that there is besides leaving the bad code to read, still from the gap to this failure.
– user28595
@Article Yes, in the update method I get this String passed by parameter and with it I create a Preparedstatement: public void update(String sql)throws Sqlexception{ Preparedstatement st = con.prepareStatement(sql);
– Lucas Pletsch
Then edit the question and add the full snippet, because in this snippet there, the gap still continues.
– user28595