Skip to content

Commit 7d78df2

Browse files
committed
Swift: Define SQL sinks.
1 parent ce5631e commit 7d78df2

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,59 @@
1111
*/
1212

1313
import swift
14+
import codeql.swift.dataflow.DataFlow
15+
import codeql.swift.dataflow.TaintTracking
16+
import DataFlow::PathGraph
1417

15-
select "TODO"
18+
/**
19+
* A `DataFlow::Node` that is a sink for an SQL string to be executed.
20+
*/
21+
abstract class SqlSink extends DataFlow::Node { }
22+
23+
/**
24+
* A sink for the sqlite3 C API.
25+
*/
26+
class CApiSqlSink extends SqlSink {
27+
CApiSqlSink() {
28+
// `sqlite3_exec` and variants of `sqlite3_prepare`.
29+
exists(AbstractFunctionDecl f, CallExpr call |
30+
f.getName() =
31+
[
32+
"sqlite3_exec(_:_:_:_:_:)", "sqlite3_prepare(_:_:_:_:_:)",
33+
"sqlite3_prepare_v2(_:_:_:_:_:)", "sqlite3_prepare_v3(_:_:_:_:_:)",
34+
"sqlite3_prepare16(_:_:_:_:_:)", "sqlite3_prepare16_v2(_:_:_:_:_:)",
35+
"sqlite3_prepare16_v3(_:_:_:_:_:)"
36+
] and
37+
call.getStaticTarget() = f and
38+
call.getArgument(1).getExpr() = this.asExpr()
39+
)
40+
}
41+
}
42+
43+
/**
44+
* A sink for the SQLite.swift library.
45+
*/
46+
class SQLiteSwiftSqlSink extends SqlSink {
47+
SQLiteSwiftSqlSink() {
48+
// Variants of `Connection.execute`, `connection.prepare` and `connection.scalar`.
49+
exists(ClassDecl c, AbstractFunctionDecl f, CallExpr call |
50+
c.getName() = "Connection" and
51+
c.getAMember() = f and
52+
f.getName() = ["execute(_:)", "prepare(_:_:)", "run(_:_:)", "scalar(_:_:)"] and
53+
call.getStaticTarget() = f and
54+
call.getArgument(0).getExpr() = this.asExpr()
55+
)
56+
or
57+
// String argument to the `Statement` constructor.
58+
exists(ClassDecl c, AbstractFunctionDecl f, CallExpr call |
59+
c.getName() = "Statement" and
60+
c.getAMember() = f and
61+
f.getName() = "init(_:_:)" and
62+
call.getStaticTarget() = f and
63+
call.getArgument(1).getExpr() = this.asExpr()
64+
)
65+
}
66+
}
67+
68+
from SqlSink s
69+
select s
Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
1-
| TODO |
1+
edges
2+
nodes
3+
subpaths
4+
#select
5+
| SQLite.swift:29:86:29:86 | |
6+
| SQLite.swift:30:85:30:85 | |
7+
| SQLite.swift:31:93:31:93 | |
8+
| SQLite.swift:33:113:33:113 | |
9+
| SQLite.swift:34:112:34:112 | |
10+
| SQLite.swift:35:120:35:120 | |
11+
| SQLite.swift:45:113:45:113 | |
12+
| SQLite.swift:46:112:46:112 | |
13+
| SQLite.swift:47:120:47:120 | |
14+
| SQLite.swift:49:128:49:128 | |
15+
| SQLite.swift:50:127:50:127 | |
16+
| SQLite.swift:51:135:51:135 | |
17+
| SQLite.swift:73:17:73:17 | unsafeQuery1 |
18+
| SQLite.swift:74:17:74:17 | unsafeQuery2 |
19+
| SQLite.swift:75:17:75:17 | unsafeQuery3 |
20+
| SQLite.swift:76:17:76:17 | safeQuery1 |
21+
| SQLite.swift:77:17:77:17 | safeQuery2 |
22+
| SQLite.swift:83:29:83:29 | unsafeQuery3 |
23+
| SQLite.swift:86:29:86:29 | varQuery |
24+
| SQLite.swift:89:29:89:29 | varQuery |
25+
| SQLite.swift:92:28:92:28 | localString |
26+
| SQLite.swift:95:28:95:28 | remoteString |
27+
| SQLite.swift:100:29:100:29 | unsafeQuery1 |
28+
| SQLite.swift:103:29:103:29 | unsafeQuery1 |
29+
| SQLite.swift:106:29:106:29 | unsafeQuery1 |
30+
| SQLite.swift:109:9:109:9 | unsafeQuery1 |
31+
| SQLite.swift:111:9:111:9 | unsafeQuery1 |
32+
| SQLite.swift:113:9:113:9 | unsafeQuery1 |
33+
| SQLite.swift:115:12:115:12 | unsafeQuery1 |
34+
| SQLite.swift:117:12:117:12 | unsafeQuery1 |
35+
| SQLite.swift:119:12:119:12 | unsafeQuery1 |
36+
| SQLite.swift:121:29:121:29 | varQuery |
37+
| SQLite.swift:132:16:132:16 | remoteString |
38+
| sqlite3_c_api.swift:133:33:133:33 | (UnsafePointer<CChar>) ... |
39+
| sqlite3_c_api.swift:134:33:134:33 | (UnsafePointer<CChar>) ... |
40+
| sqlite3_c_api.swift:135:33:135:33 | (UnsafePointer<CChar>) ... |
41+
| sqlite3_c_api.swift:136:33:136:33 | (UnsafePointer<CChar>) ... |
42+
| sqlite3_c_api.swift:137:33:137:33 | (UnsafePointer<CChar>) ... |
43+
| sqlite3_c_api.swift:145:26:145:26 | (UnsafePointer<CChar>) ... |
44+
| sqlite3_c_api.swift:153:26:153:26 | (UnsafePointer<CChar>) ... |
45+
| sqlite3_c_api.swift:163:26:163:26 | (UnsafePointer<CChar>) ... |
46+
| sqlite3_c_api.swift:175:29:175:29 | (UnsafePointer<CChar>) ... |
47+
| sqlite3_c_api.swift:194:28:194:28 | buffer |
48+
| sqlite3_c_api.swift:202:31:202:31 | buffer |

0 commit comments

Comments
 (0)