I have a vulnerability in my code, and I'm trying to fix it.
To do this, in the SQL query, I'm using PreparedStatements to inject the parameters safely as follows:
String runQuery(String value) { String myReturn; String query = "select VALUE from " + tableName + " where config = ?"; try(PreparedStatement ps = con.prepareStatement(query)){ ps.setString(1, value); try(ResultSet rs = ps.executeQuery()) { if (rs.next()) { myReturn = rs.getString(1); } else { throw new BusinessException("noSQLResultSet"); } } } catch (Exception sqlException) { throw new BusinessException("SQLException"); } return myReturn; } My question is about the name of the table tableName, I am currently retrieving it from the properties file located on the server, but from what I see with PreparedStatements I cannot inject the name of the table since it is not allowed. How could you specify the table name in a safe way without concatenating the value in the query?
tableNamehas an appropriate value? Like you can check it is just an SQL identifier, not any kind of more complex expression; or you can check it is one of some known set of permitted table names.