QLI322691 (Community Member) asked a question.

Hello, may I know what's causing a sql injection(CWE-89) flaw in the following java code using parameterized queries? Is it a false positives ? Thanks!

PreparedStatement stmt = null;

ResultSet rs = null;

String sql = "SELECT MAX(triggerTs) as triggerTs FROM alert_notifications WHERE alert_src=?";

 

try{

stmt = connection.prepareStatement(sql);

stmt.setString(1, "UK");

rs = stmt.executeQuery(); // Flaw reported in this line

}catch(Exception e){

e.printStackTrace();

}finally{

// close all resources.

}

 


  • Anthony Fielding (Veracode)

    Hi @QLI322691 (Community Member)​,

    Thanks for your question about this CWE-89 flaw.

     

    The code that you have attached does not appear to exhibit the CWE-89 flaw due to the correct use of prepared statements. Furthermore, in this example as the value for alert_src is hard-coded to UK there would be no need to use a prepared statement as no SQL parameter is user-controllable. I suspect your example has been modified for inclusion in your question, and as such, if that is the case there could still be a flaw in the original code.

     

    If you have recently made a change to your code to make use of prepared statements in response to discovering this flaw via the Veracode Platform, it could be that the Veracode Platform is still identifying the flaw because the changed code may not have been re-packaged or re-uploaded for scanning correctly.

     

    I recommend scheduling a consultation with a Veracode Application Security Consultant if you would like to discuss the flaw further if you need clarification. You can find details on how to schedule a consultation here: https://help.veracode.com/reader/DGHxSJy3Gn3gtuSIN2jkRQ/7YQTCDJKFEQzL3gL_N90hQ.

     

    Thanks,

    Anthony Fielding

    Expand Post
  • DSambinelli850568 (Community Member)

    @Anthony Fielding (Veracode)​ , if a use a StringBuilder just for a matter of formatting a big query (split in multiple lines) and prepared statement, should I have a CWE-89?

    StringBuilder query = new StringBuilder();

    query.append("select name from user ");

          query.append(" where id = ?");

    ps_statement = cn_connection.prepareStatement(query.toString());

    ps_statement .setInt(1, 1);

    Expand Post
  • DSambinelli850568 (Community Member)

    I Know that is not a REAL threat. What I want to know is if veracode will rise a flaw due that. Thanks.

  • Anthony Fielding (Veracode)

    Hello @DSambinelli850568 (Community Member)​,

    We do not raise the flaw simply due to the use of a StringBuilder or string concatenation. From your example code snippet one would not expect a CWE-89 to be raised due to what looks to be the correct use of prepared statements. However if other variables were to be appended directly into the query StringBuilder without the use of placeholders ("?") then we would raise CWE-89 at the point where the application makes a call to executeQuery(). All untrusted data must be parameterised and for those areas where parameterisation is not supported (e.g. table and column names) you should use tight input validation controls. You may find these resources helpful: https://www.veracode.com/security/java/cwe-89, https://www.veracode.com/security/java/cwe-89.

     

    I hope this answers your question.

     

    Thanks,

    Anthony Fielding

    Expand Post

Topics (3)

No articles found
Loading

Ask the Community

Get answers, share a use case, discuss your favorite features, or get input from the community.