Skip to content

Commit f122005

Browse files
committed
Swift: Simplify out some variables.
1 parent b4d939a commit f122005

File tree

6 files changed

+44
-38
lines changed

6 files changed

+44
-38
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ abstract class SqlSink extends DataFlow::Node { }
2727
class CApiSqlSink extends SqlSink {
2828
CApiSqlSink() {
2929
// `sqlite3_exec` and variants of `sqlite3_prepare`.
30-
exists(AbstractFunctionDecl f, CallExpr call |
31-
f.getName() =
30+
exists(CallExpr call |
31+
call.getStaticTarget().getName() =
3232
[
3333
"sqlite3_exec(_:_:_:_:_:)", "sqlite3_prepare(_:_:_:_:_:)",
3434
"sqlite3_prepare_v2(_:_:_:_:_:)", "sqlite3_prepare_v3(_:_:_:_:_:_:)",
3535
"sqlite3_prepare16(_:_:_:_:_:)", "sqlite3_prepare16_v2(_:_:_:_:_:)",
3636
"sqlite3_prepare16_v3(_:_:_:_:_:_:)"
3737
] and
38-
call.getStaticTarget() = f and
3938
call.getArgument(1).getExpr() = this.asExpr()
4039
)
4140
}
@@ -47,16 +46,17 @@ class CApiSqlSink extends SqlSink {
4746
class SQLiteSwiftSqlSink extends SqlSink {
4847
SQLiteSwiftSqlSink() {
4948
// Variants of `Connection.execute`, `connection.prepare` and `connection.scalar`.
50-
exists(MethodDecl f, CallExpr call |
51-
f.hasQualifiedName("Connection", ["execute(_:)", "prepare(_:_:)", "run(_:_:)", "scalar(_:_:)"]) and
52-
call.getStaticTarget() = f and
49+
exists(CallExpr call |
50+
call.getStaticTarget()
51+
.(MethodDecl)
52+
.hasQualifiedName("Connection",
53+
["execute(_:)", "prepare(_:_:)", "run(_:_:)", "scalar(_:_:)"]) and
5354
call.getArgument(0).getExpr() = this.asExpr()
5455
)
5556
or
5657
// String argument to the `Statement` constructor.
57-
exists(MethodDecl f, CallExpr call |
58-
f.hasQualifiedName("Statement", "init(_:_:)") and
59-
call.getStaticTarget() = f and
58+
exists(CallExpr call |
59+
call.getStaticTarget().(MethodDecl).hasQualifiedName("Statement", "init(_:_:)") and
6060
call.getArgument(1).getExpr() = this.asExpr()
6161
)
6262
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ abstract class Stored extends DataFlow::Node { }
2828
class CoreDataStore extends Stored {
2929
CoreDataStore() {
3030
// values written into Core Data objects are a sink
31-
exists(MethodDecl f, CallExpr call |
32-
f.hasQualifiedName("NSManagedObject", ["setValue(_:forKey:)", "setPrimitiveValue(_:forKey:)"]) and
33-
call.getStaticTarget() = f and
31+
exists(CallExpr call |
32+
call.getStaticTarget()
33+
.(MethodDecl)
34+
.hasQualifiedName("NSManagedObject",
35+
["setValue(_:forKey:)", "setPrimitiveValue(_:forKey:)"]) and
3436
call.getArgument(0).getExpr() = this.asExpr()
3537
)
3638
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ abstract class Transmitted extends Expr { }
2828
class NWConnectionSend extends Transmitted {
2929
NWConnectionSend() {
3030
// `content` arg to `NWConnection.send` is a sink
31-
exists(MethodDecl f, CallExpr call |
32-
f.hasQualifiedName("NWConnection", "send(content:contentContext:isComplete:completion:)") and
33-
call.getStaticTarget() = f and
31+
exists(CallExpr call |
32+
call.getStaticTarget()
33+
.(MethodDecl)
34+
.hasQualifiedName("NWConnection", "send(content:contentContext:isComplete:completion:)") and
3435
call.getArgument(0).getExpr() = this
3536
)
3637
}
@@ -44,9 +45,10 @@ class Url extends Transmitted {
4445
Url() {
4546
// `string` arg in `URL.init` is a sink
4647
// (we assume here that the URL goes on to be used in a network operation)
47-
exists(MethodDecl f, CallExpr call |
48-
f.hasQualifiedName("URL", ["init(string:)", "init(string:relativeTo:)"]) and
49-
call.getStaticTarget() = f and
48+
exists(CallExpr call |
49+
call.getStaticTarget()
50+
.(MethodDecl)
51+
.hasQualifiedName("URL", ["init(string:)", "init(string:relativeTo:)"]) and
5052
call.getArgument(0).getExpr() = this
5153
)
5254
}

swift/ql/src/queries/Security/CWE-312/CleartextStoragePreferences.ql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ abstract class Stored extends DataFlow::Node {
2626
/** The `DataFlow::Node` of an expression that gets written to the user defaults database */
2727
class UserDefaultsStore extends Stored {
2828
UserDefaultsStore() {
29-
exists(MethodDecl f, CallExpr call |
30-
f.hasQualifiedName("UserDefaults", "set(_:forKey:)") and
31-
call.getStaticTarget() = f and
29+
exists(CallExpr call |
30+
call.getStaticTarget().(MethodDecl).hasQualifiedName("UserDefaults", "set(_:forKey:)") and
3231
call.getArgument(0).getExpr() = this.asExpr()
3332
)
3433
}
@@ -39,9 +38,10 @@ class UserDefaultsStore extends Stored {
3938
/** The `DataFlow::Node` of an expression that gets written to the iCloud-backed NSUbiquitousKeyValueStore */
4039
class NSUbiquitousKeyValueStore extends Stored {
4140
NSUbiquitousKeyValueStore() {
42-
exists(MethodDecl f, CallExpr call |
43-
f.hasQualifiedName("NSUbiquitousKeyValueStore", "set(_:forKey:)") and
44-
call.getStaticTarget() = f and
41+
exists(CallExpr call |
42+
call.getStaticTarget()
43+
.(MethodDecl)
44+
.hasQualifiedName("NSUbiquitousKeyValueStore", "set(_:forKey:)") and
4545
call.getArgument(0).getExpr() = this.asExpr()
4646
)
4747
}

swift/ql/src/queries/Security/CWE-321/HardcodedEncryptionKey.ql

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ class StringLiteralSource extends KeySource instanceof StringLiteralExpr { }
3737
class EncryptionKeySink extends Expr {
3838
EncryptionKeySink() {
3939
// `key` arg in `init` is a sink
40-
exists(MethodDecl f, CallExpr call, string fName |
41-
f.hasQualifiedName([
42-
"AES", "HMAC", "ChaCha20", "CBCMAC", "CMAC", "Poly1305", "Blowfish", "Rabbit"
43-
], fName) and
40+
exists(CallExpr call, string fName |
41+
call.getStaticTarget()
42+
.(MethodDecl)
43+
.hasQualifiedName([
44+
"AES", "HMAC", "ChaCha20", "CBCMAC", "CMAC", "Poly1305", "Blowfish", "Rabbit"
45+
], fName) and
4446
fName.matches("init(key:%") and
45-
call.getStaticTarget() = f and
4647
call.getArgument(0).getExpr() = this
4748
)
4849
}

swift/ql/src/queries/Security/ECB-Encryption/ECBEncryption.ql

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ abstract class BlockMode extends Expr { }
2626
class AES extends BlockMode {
2727
AES() {
2828
// `blockMode` arg in `AES.init` is a sink
29-
exists(MethodDecl f, CallExpr call |
30-
f.hasQualifiedName("AES", ["init(key:blockMode:)", "init(key:blockMode:padding:)"]) and
31-
call.getStaticTarget() = f and
29+
exists(CallExpr call |
30+
call.getStaticTarget()
31+
.(MethodDecl)
32+
.hasQualifiedName("AES", ["init(key:blockMode:)", "init(key:blockMode:padding:)"]) and
3233
call.getArgument(1).getExpr() = this
3334
)
3435
}
@@ -40,9 +41,10 @@ class AES extends BlockMode {
4041
class Blowfish extends BlockMode {
4142
Blowfish() {
4243
// `blockMode` arg in `Blowfish.init` is a sink
43-
exists(MethodDecl f, CallExpr call |
44-
f.hasQualifiedName("Blowfish", "init(key:blockMode:padding:)") and
45-
call.getStaticTarget() = f and
44+
exists(CallExpr call |
45+
call.getStaticTarget()
46+
.(MethodDecl)
47+
.hasQualifiedName("Blowfish", "init(key:blockMode:padding:)") and
4648
call.getArgument(1).getExpr() = this
4749
)
4850
}
@@ -56,9 +58,8 @@ class EcbEncryptionConfig extends DataFlow::Configuration {
5658
EcbEncryptionConfig() { this = "EcbEncryptionConfig" }
5759

5860
override predicate isSource(DataFlow::Node node) {
59-
exists(MethodDecl f, CallExpr call |
60-
f.hasQualifiedName("ECB", "init()") and
61-
call.getStaticTarget() = f and
61+
exists(CallExpr call |
62+
call.getStaticTarget().(MethodDecl).hasQualifiedName("ECB", "init()") and
6263
node.asExpr() = call
6364
)
6465
}

0 commit comments

Comments
 (0)