Skip to content

Unclear reason for why is Executor not implemented for String #1559

Answered by abonander
luke-biel asked this question in Q&A
Discussion options

You must be logged in to vote

Is this some security reason?

This. If String was possible to use directly, it would be extremely tempting to just format user input directly into the query instead of using bind parameters, which introduces SQL injection vulnerabilities. For example, to co-opt XKCD #327:

async fn insert_student(conn: &mut PgConnection, student_name: &str) -> sqlx::Result<StudentId> {
    sqlx::query_scalar(format!("INSERT INTO students(name) VALUES('{}') RETURNING student_id", student_name))
        .fetch_one(conn)
        .await
}

// imagine this is user input, and wasn't properly sanitized
let student_name = "Robert'); DROP TABLE students; --";

let student_id = insert_student(&mut conn, &student_name

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@abonander
Comment options

Answer selected by abonander
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants