Skip to content

Commit 9794309

Browse files
authored
Merge pull request github#17127 from geoffw0/swiftsql
Swift: Improve doc for swift/sql-injection
2 parents 6a49647 + e66cd05 commit 9794309

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ If a database query (such as a SQL query) is built from user-provided data witho
1212
<recommendation>
1313

1414
<p>
15-
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 without sufficient sanitization.
15+
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.
1616
</p>
1717

1818
</recommendation>
1919
<example>
2020

21-
<p>In the following example, a SQL query is prepared using string interpolation to directly include a user-controlled value <code>userControlledString</code> in the query. An attacker could craft <code>userControlledString</code> to change the overall meaning of the SQL query.
21+
<p>In the following examples, 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 <code>userControlledString</code> to change the overall meaning of the SQL query.
2222
</p>
2323

2424
<sample src="SqlInjectionBad.swift" />
@@ -35,4 +35,4 @@ Most database connector libraries offer a way to safely embed untrusted data int
3535
<li>OWASP: <a href="https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html">SQL Injection Prevention Cheat Sheet</a>.</li>
3636

3737
</references>
38-
</qhelp>
38+
</qhelp>
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
let unsafeQuery = "SELECT * FROM users WHERE username='\(userControlledString)'" // BAD
1+
// with SQLite.swift
22

3-
try db.execute(unsafeQuery)
3+
let unsafeQuery = "SELECT * FROM users WHERE username='\(userControlledString)'"
4+
5+
try db.execute(unsafeQuery) // BAD
6+
7+
let stmt = try db.prepare(unsafeQuery) // also BAD
8+
try stmt.run()
9+
10+
// with SQLite3 C API
11+
12+
let result = sqlite3_exec(db, unsafeQuery, nil, nil, nil) // BAD
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1+
// with SQLite.swift
2+
13
let safeQuery = "SELECT * FROM users WHERE username=?"
24

35
let stmt = try db.prepare(safeQuery, userControlledString) // GOOD
4-
try stmt2.run()
6+
try stmt.run()
7+
8+
// with sqlite3 C API
9+
10+
var stmt2: OpaquePointer?
11+
12+
if (sqlite3_prepare_v2(db, safeQuery, -1, &stmt2, nil) == SQLITE_OK) {
13+
if (sqlite3_bind_text(stmt2, 1, userControlledString, -1, SQLITE_TRANSIENT) == SQLITE_OK) { // GOOD
14+
let result = sqlite3_step(stmt2)
15+
16+
// ...
17+
}
18+
sqlite3_finalize(stmt2)
19+
}

0 commit comments

Comments
 (0)