-
Hi, I am trying to implement a service where the frontend can filter based on a number of criterias, the library i use for the front end allows me to export the filter as as SQL WHERE clause. pub fn find_song_filter_by(conn: &mut SqliteConnection, filter: String) -> Vec<models::Song> {
let query = models::SongHasTag::table().inner_join(schema::tag::table).inner_join(schema::song::table).filter(sql::<sql_types::Bool>(&filter)).select(models::Song::as_select());
info!("{:?}", debug_query::<diesel::sqlite::Sqlite, _>(&query));
return query.load(conn).expect("");
} If i try the following input:
Which (if I understand correctly) corresponds to this SQL query:
If I execute this query manually against my SQL database, the tag "we are in" gets created, demonstrating an SQL injection. However, executing the returned query with diesel, no such tag is created, which leads me to believe that diesel uses extra type information (the fact that i specified How does the executed query actually look like if |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Diesel uses prepared statements for any query. That usually prevents any form of SQL injections as parameters are send separate from the query. In this psecific case it does not help as you literally pass in a user supplied string as query part via To summarize that: I highly recommend not to accept raw strings as query additions in that way, it will result in injection like problems. Instead checkout the "Composing Applications", it demonstrates different ways to construct composable queries with diesel. |
Beta Was this translation helpful? Give feedback.
Diesel uses prepared statements for any query. That usually prevents any form of SQL injections as parameters are send separate from the query.
In this psecific case it does not help as you literally pass in a user supplied string as query part via
dsl::sql
. That function anddiesel::sql_query
are exceptions in so far that they do not process the strings provided by the user in any way, so if you pass in a string that is constructed viaformat!
your will likely run into SQL injection problems. It does not seem to work for your particular example as you mixed up the quotation marks. SQL uses'
to quote strings and because diesel always will only execute the first query in a particular stat…