+
+
+
+If a database query (such as an 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 formatting to directly include a user-controlled value remote_controlled_string
. An attacker could craft remote_controlled_string
to change the overall meaning of the SQL query.
+
+
+
+
+A better way to do this is with a prepared statement, binding remote_controlled_string
to a parameter of that statement. An attacker who controls remote_controlled_string
now cannot change the overall meaning of the query.
+
+
+
+
+
+
+
+Wikipedia: SQL injection.
+OWASP: SQL Injection Prevention Cheat Sheet.
+
+
+
diff --git a/rust/ql/src/queries/security/CWE-089/SqlInjection.ql b/rust/ql/src/queries/security/CWE-089/SqlInjection.ql
new file mode 100644
index 000000000000..ee2a3d144868
--- /dev/null
+++ b/rust/ql/src/queries/security/CWE-089/SqlInjection.ql
@@ -0,0 +1,35 @@
+/**
+ * @name Database query built from user-controlled sources
+ * @description Building a database query from user-controlled sources is vulnerable to insertion of malicious code by attackers.
+ * @kind path-problem
+ * @problem.severity error
+ * @security-severity 8.8
+ * @precision high
+ * @id rust/sql-injection
+ * @tags security
+ * external/cwe/cwe-089
+ */
+
+import rust
+import codeql.rust.dataflow.DataFlow
+import codeql.rust.dataflow.TaintTracking
+import codeql.rust.security.SqlInjectionExtensions
+import SqlInjectionFlow::PathGraph
+
+/**
+ * A taint configuration for tainted data that reaches a SQL sink.
+ */
+module SqlInjectionConfig implements DataFlow::ConfigSig {
+ predicate isSource(DataFlow::Node node) { node instanceof SqlInjection::Source }
+
+ predicate isSink(DataFlow::Node node) { node instanceof SqlInjection::Sink }
+
+ predicate isBarrier(DataFlow::Node barrier) { barrier instanceof SqlInjection::Barrier }
+}
+
+module SqlInjectionFlow = TaintTracking::Global