Skip to content

Commit 227b10a

Browse files
committed
Swift: Qhelp.
1 parent 24c6bb4 commit 227b10a

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

swift/ql/src/queries/Security/CWE-089/SqlInjection.qhelp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,36 @@
44
<qhelp>
55
<overview>
66

7-
<p>TODO</p>
7+
<p>
8+
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.
9+
</p>
810

911
</overview>
1012
<recommendation>
1113

12-
<p>TODO</p>
14+
<p>
15+
Most database connector libraries offer a way of safely embedding untrusted data into a query by means of query parameters or prepared statements. Use these features rather than building queries by string concatenation.
16+
</p>
1317

1418
</recommendation>
1519
<example>
1620

17-
<p>TODO</p>
21+
<p>In the following example, an SQL query is prepared using string interpolation to directly include a user-controlled value <code>userControlledString</code> in the query. An attacker could craft the part they control to change the overall meaning of the SQL query.
22+
</p>
1823

1924
<sample src="SqlInjectionBad.swift" />
2025

21-
<p>TODO</p>
26+
<p>A better way to do this is with a prepared statement, binding <code>userControlledString</code> to a parameter of that statement. An attacker who controls the contents of that parameter cannot 'break out' and change the overall meaning of the SQL query.
27+
</p>
2228

2329
<sample src="SqlInjectionGood.swift" />
2430

2531
</example>
2632
<references>
2733

2834
<li>
29-
TODO
35+
<li>Wikipedia: <a href="https://en.wikipedia.org/wiki/SQL_injection">SQL injection</a>.</li>
36+
<li>OWASP: <a href="https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html">SQL Injection Prevention Cheat Sheet</a>.</li>
3037
</li>
3138

3239
</references>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
let unsafeQuery = "SELECT * FROM users WHERE username='\(userControlledString)'" // BAD
12

2-
TODO
3+
try db.execute(unsafeQuery)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
let safeQuery = "SELECT * FROM users WHERE username=?"
12

2-
TODO
3+
let stmt = try db.prepare(safeQuery, userControlledString) // GOOD
4+
try stmt2.run()

0 commit comments

Comments
 (0)