+If a database query (such as a SQL query) is built from user-provided data without sufficient sanitization, a user may be able to run malicious database queries. An attacker can craft the part of the query they control to change the overall meaning of the query. +
+ ++Most database connector libraries offer a way to safely embed untrusted data into a query using query parameters or prepared statements. You should use these features to build queries, rather than string concatenation or similar methods. You can also escape (sanitize) user-controlled strings so that they can be included directly in an SQL command. A library function should be used for escaping, because this approach is only safe if the escaping function is robust against all possible inputs. +
+ +In the following examples, an SQL query is prepared using string interpolation to directly include a user-controlled value userControlledString in the query. An attacker could craft userControlledString to change the overall meaning of the SQL query.
+
A better way to do this is with a prepared statement, binding userControlledString to a parameter of that statement. An attacker who controls userControlledString now cannot change the overall meaning of the query.
+
-Most database connector libraries offer a way to safely embed untrusted data into a query using query parameters or prepared statements. You should use these features to build queries, rather than string concatenation or similar methods. You can also escape (sanitize) user-controlled strings so that they can be included directly in an SQL command. A library function should be used for escaping, because this approach is only safe if the escaping function is robust against all possible inputs. +Most database connector libraries offer a way to safely embed untrusted data into a query using query parameters or prepared statements. Use these features to bind untrusted values separately from the SQL syntax, rather than using string concatenation or interpolation. Preparing a statement after inserting untrusted data into its SQL text does not prevent injection. Escaping is error-prone and database-specific, so prefer parameter binding rather than implementing your own escaping routine.
-Most database connector libraries offer a way to safely embed untrusted data into a query using query parameters or prepared statements. Use these features to bind untrusted values separately from the SQL syntax, rather than using string concatenation or interpolation. Preparing a statement after inserting untrusted data into its SQL text does not prevent injection. Escaping is error-prone and database-specific, so prefer parameter binding rather than implementing your own escaping routine. +Most database connector libraries offer a way to safely embed untrusted data into a query using query parameters or prepared statements. You should use these features to build queries, rather than string concatenation or similar methods. You can also escape (sanitize) user-controlled strings so that they can be included directly in an SQL command. A library function should be used for escaping, because this approach is only safe if the escaping function is robust against all possible inputs.