Skip to content

Commit d976829

Browse files
committed
Swift: Add Alamofire sink for cpp/cleartext-transmission.
1 parent 23ff376 commit d976829

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

swift/ql/lib/codeql/swift/elements/expr/ApplyExpr.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ class ApplyExpr extends Generated::ApplyExpr {
1919
/** Gets the method qualifier, if this is applying a method */
2020
Expr getQualifier() { none() }
2121

22+
/**
23+
* Gets the argument of this `ApplyExpr` called `label` (if any).
24+
*/
25+
final Argument getArgumentWithLabel(string label) {
26+
result = getAnArgument() and
27+
result.getLabel() = label
28+
}
29+
2230
override string toString() {
2331
result = "call to " + this.getStaticTarget().toString()
2432
or

swift/ql/src/queries/Security/CWE-311/CleartextTransmission.ql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ class Url extends Transmitted {
5454
}
5555
}
5656

57+
/**
58+
* An `Expr` that transmitted through the Alamofire library.
59+
*/
60+
class AlamofireTransmitted extends Transmitted {
61+
AlamofireTransmitted() {
62+
// sinks are the first argument containing the URL, and the `parameters`
63+
// and `headers` arguments to appropriate methods of `Session`.
64+
exists(CallExpr call, string fName |
65+
call.getStaticTarget().(MethodDecl).hasQualifiedName("Session", fName) and
66+
fName.regexpMatch("(request|streamRequest|download)\\(.*") and
67+
(
68+
call.getArgument(0).getExpr() = this or
69+
call.getArgumentWithLabel("parameters").getExpr() = this or
70+
call.getArgumentWithLabel("headers").getExpr() = this
71+
)
72+
)
73+
}
74+
}
75+
5776
/**
5877
* A taint configuration from sensitive information to expressions that are
5978
* transmitted over a network.

swift/ql/test/query-tests/Security/CWE-311/CleartextTransmission.expected

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
edges
2+
| testAlamofire.swift:150:45:150:45 | password : | testAlamofire.swift:150:13:150:45 | ... .+(_:_:) ... |
3+
| testAlamofire.swift:152:51:152:51 | password : | testAlamofire.swift:152:19:152:51 | ... .+(_:_:) ... |
4+
| testAlamofire.swift:154:38:154:38 | email : | testAlamofire.swift:154:14:154:46 | ... .+(_:_:) ... |
25
| testSend.swift:41:10:41:18 | data : | testSend.swift:41:45:41:45 | data : |
36
| testSend.swift:45:13:45:13 | password : | testSend.swift:52:27:52:27 | str1 |
47
| testSend.swift:46:13:46:13 | password : | testSend.swift:53:27:53:27 | str2 |
@@ -8,6 +11,12 @@ edges
811
| testURL.swift:13:54:13:54 | passwd : | testURL.swift:13:22:13:54 | ... .+(_:_:) ... |
912
| testURL.swift:16:55:16:55 | credit_card_no : | testURL.swift:16:22:16:55 | ... .+(_:_:) ... |
1013
nodes
14+
| testAlamofire.swift:150:13:150:45 | ... .+(_:_:) ... | semmle.label | ... .+(_:_:) ... |
15+
| testAlamofire.swift:150:45:150:45 | password : | semmle.label | password : |
16+
| testAlamofire.swift:152:19:152:51 | ... .+(_:_:) ... | semmle.label | ... .+(_:_:) ... |
17+
| testAlamofire.swift:152:51:152:51 | password : | semmle.label | password : |
18+
| testAlamofire.swift:154:14:154:46 | ... .+(_:_:) ... | semmle.label | ... .+(_:_:) ... |
19+
| testAlamofire.swift:154:38:154:38 | email : | semmle.label | email : |
1120
| testSend.swift:29:19:29:19 | passwordPlain | semmle.label | passwordPlain |
1221
| testSend.swift:41:10:41:18 | data : | semmle.label | data : |
1322
| testSend.swift:41:45:41:45 | data : | semmle.label | data : |
@@ -26,6 +35,9 @@ nodes
2635
subpaths
2736
| testSend.swift:47:17:47:17 | password : | testSend.swift:41:10:41:18 | data : | testSend.swift:41:45:41:45 | data : | testSend.swift:47:13:47:25 | call to pad(_:) : |
2837
#select
38+
| testAlamofire.swift:150:13:150:45 | ... .+(_:_:) ... | testAlamofire.swift:150:45:150:45 | password : | testAlamofire.swift:150:13:150:45 | ... .+(_:_:) ... | This operation transmits '... .+(_:_:) ...', which may contain unencrypted sensitive data from $@. | testAlamofire.swift:150:45:150:45 | password : | password |
39+
| testAlamofire.swift:152:19:152:51 | ... .+(_:_:) ... | testAlamofire.swift:152:51:152:51 | password : | testAlamofire.swift:152:19:152:51 | ... .+(_:_:) ... | This operation transmits '... .+(_:_:) ...', which may contain unencrypted sensitive data from $@. | testAlamofire.swift:152:51:152:51 | password : | password |
40+
| testAlamofire.swift:154:14:154:46 | ... .+(_:_:) ... | testAlamofire.swift:154:38:154:38 | email : | testAlamofire.swift:154:14:154:46 | ... .+(_:_:) ... | This operation transmits '... .+(_:_:) ...', which may contain unencrypted sensitive data from $@. | testAlamofire.swift:154:38:154:38 | email : | email |
2941
| testSend.swift:29:19:29:19 | passwordPlain | testSend.swift:29:19:29:19 | passwordPlain | testSend.swift:29:19:29:19 | passwordPlain | This operation transmits 'passwordPlain', which may contain unencrypted sensitive data from $@. | testSend.swift:29:19:29:19 | passwordPlain | passwordPlain |
3042
| testSend.swift:52:27:52:27 | str1 | testSend.swift:45:13:45:13 | password : | testSend.swift:52:27:52:27 | str1 | This operation transmits 'str1', which may contain unencrypted sensitive data from $@. | testSend.swift:45:13:45:13 | password : | password |
3143
| testSend.swift:53:27:53:27 | str2 | testSend.swift:46:13:46:13 | password : | testSend.swift:53:27:53:27 | str2 | This operation transmits 'str2', which may contain unencrypted sensitive data from $@. | testSend.swift:46:13:46:13 | password : | password |

swift/ql/test/query-tests/Security/CWE-311/testAlamofire.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ struct MyEncodable: Encodable {
147147
func test1(username: String, password: String, email: String, harmless: String) {
148148
// sensitive data in URL
149149

150-
AF.request("http://example.com/login?p=" + password) // BAD [NOT DETECTED]
150+
AF.request("http://example.com/login?p=" + password) // BAD
151151
AF.request("http://example.com/login?h=" + harmless) // GOOD (not sensitive)
152-
AF.streamRequest("http://example.com/login?p=" + password) // BAD [NOT DETECTED]
152+
AF.streamRequest("http://example.com/login?p=" + password) // BAD
153153
AF.streamRequest("http://example.com/login?h=" + harmless) // GOOD (not sensitive)
154-
AF.download("http://example.com/" + email + ".html") // BAD [NOT DETECTED]
154+
AF.download("http://example.com/" + email + ".html") // BAD
155155
AF.download("http://example.com/" + harmless + ".html") // GOOD (not sensitive)
156156

157157
// sensitive data in parameters

0 commit comments

Comments
 (0)