sqlparser-rs icon indicating copy to clipboard operation
sqlparser-rs copied to clipboard

Improve fmt::Display for AST to IntoSql separating the SQL statement and values.

Open ivanceras opened this issue 6 years ago • 0 comments

The current implementation of converting the AST into SQL statement is using the fmt::Display trait which creates a string. With it, all the Value expression is embedded into the generated SQL string. This could potentially be used for SQL injection attacks. I believe this needs improvement and the simplest way to do it is to separate the values from the rest of the generated SQL leaving parameter binding in place of that value.

A simple trait to be used would be like:

trait IntoSql{
   into_sql(&self) -> (String, Vec<Value>);
}

It will be up to the underlying SQL statement executor to supply the values.

Pros:

  • Strong mitigation against SQL injection attack.

Cons:

  • different SQL dialect may have different syntax with parameter binding, ie: $1,?, :var1

ivanceras avatar Nov 26 '20 08:11 ivanceras