Skip to content

Commit 0a3d0c4

Browse files
authored
Merge pull request github#11031 from geoffw0/simplify
Swift: Simplify queries using MethodDecl.hasQualifiedName
2 parents 142e500 + 368f37a commit 0a3d0c4

File tree

6 files changed

+25
-44
lines changed

6 files changed

+25
-44
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,15 @@ class CApiSqlSink extends SqlSink {
4747
class SQLiteSwiftSqlSink extends SqlSink {
4848
SQLiteSwiftSqlSink() {
4949
// Variants of `Connection.execute`, `connection.prepare` and `connection.scalar`.
50-
exists(ClassDecl c, AbstractFunctionDecl f, CallExpr call |
51-
c.getName() = "Connection" and
52-
c.getAMember() = f and
53-
f.getName() = ["execute(_:)", "prepare(_:_:)", "run(_:_:)", "scalar(_:_:)"] and
50+
exists(MethodDecl f, CallExpr call |
51+
f.hasQualifiedName("Connection", ["execute(_:)", "prepare(_:_:)", "run(_:_:)", "scalar(_:_:)"]) and
5452
call.getStaticTarget() = f and
5553
call.getArgument(0).getExpr() = this.asExpr()
5654
)
5755
or
5856
// String argument to the `Statement` constructor.
59-
exists(ClassDecl c, AbstractFunctionDecl f, CallExpr call |
60-
c.getName() = "Statement" and
61-
c.getAMember() = f and
62-
f.getName() = "init(_:_:)" and
57+
exists(MethodDecl f, CallExpr call |
58+
f.hasQualifiedName("Statement", "init(_:_:)") and
6359
call.getStaticTarget() = f and
6460
call.getArgument(1).getExpr() = this.asExpr()
6561
)

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ abstract class Stored extends DataFlow::Node { }
2828
class CoreDataStore extends Stored {
2929
CoreDataStore() {
3030
// `content` arg to `NWConnection.send` is a sink
31-
exists(ClassOrStructDecl c, AbstractFunctionDecl f, CallExpr call |
32-
c.getName() = "NSManagedObject" and
33-
c.getAMember() = f and
34-
f.getName() = ["setValue(_:forKey:)", "setPrimitiveValue(_:forKey:)"] and
31+
exists(MethodDecl f, CallExpr call |
32+
f.hasQualifiedName("NSManagedObject", ["setValue(_:forKey:)", "setPrimitiveValue(_:forKey:)"]) and
3533
call.getStaticTarget() = f and
3634
call.getArgument(0).getExpr() = this.asExpr()
3735
)

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ abstract class Transmitted extends Expr { }
2828
class NWConnectionSend extends Transmitted {
2929
NWConnectionSend() {
3030
// `content` arg to `NWConnection.send` is a sink
31-
exists(ClassOrStructDecl c, AbstractFunctionDecl f, CallExpr call |
32-
c.getName() = "NWConnection" and
33-
c.getAMember() = f and
34-
f.getName() = "send(content:contentContext:isComplete:completion:)" and
31+
exists(MethodDecl f, CallExpr call |
32+
f.hasQualifiedName("NWConnection", "send(content:contentContext:isComplete:completion:)") and
3533
call.getStaticTarget() = f and
3634
call.getArgument(0).getExpr() = this
3735
)
@@ -46,10 +44,8 @@ class Url extends Transmitted {
4644
Url() {
4745
// `string` arg in `URL.init` is a sink
4846
// (we assume here that the URL goes on to be used in a network operation)
49-
exists(ClassOrStructDecl c, AbstractFunctionDecl f, CallExpr call |
50-
c.getName() = "URL" and
51-
c.getAMember() = f and
52-
f.getName() = ["init(string:)", "init(string:relativeTo:)"] and
47+
exists(MethodDecl f, CallExpr call |
48+
f.hasQualifiedName("URL", ["init(string:)", "init(string:relativeTo:)"]) and
5349
call.getStaticTarget() = f and
5450
call.getArgument(0).getExpr() = this
5551
)

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +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(ClassDecl c, AbstractFunctionDecl f, CallExpr call |
30-
c.getName() = "UserDefaults" and
31-
c.getAMember() = f and
32-
f.getName() = "set(_:forKey:)" and
29+
exists(MethodDecl f, CallExpr call |
30+
f.hasQualifiedName("UserDefaults", "set(_:forKey:)") and
3331
call.getStaticTarget() = f and
3432
call.getArgument(0).getExpr() = this.asExpr()
3533
)
@@ -41,10 +39,8 @@ class UserDefaultsStore extends Stored {
4139
/** The `DataFlow::Node` of an expression that gets written to the iCloud-backed NSUbiquitousKeyValueStore */
4240
class NSUbiquitousKeyValueStore extends Stored {
4341
NSUbiquitousKeyValueStore() {
44-
exists(ClassDecl c, AbstractFunctionDecl f, CallExpr call |
45-
c.getName() = "NSUbiquitousKeyValueStore" and
46-
c.getAMember() = f and
47-
f.getName() = "set(_:forKey:)" and
42+
exists(MethodDecl f, CallExpr call |
43+
f.hasQualifiedName("NSUbiquitousKeyValueStore", "set(_:forKey:)") and
4844
call.getStaticTarget() = f and
4945
call.getArgument(0).getExpr() = this.asExpr()
5046
)

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

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

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ abstract class BlockMode extends Expr { }
2626
class AES extends BlockMode {
2727
AES() {
2828
// `blockMode` arg in `AES.init` is a sink
29-
exists(ClassOrStructDecl c, AbstractFunctionDecl f, CallExpr call |
30-
c.getName() = "AES" and
31-
c.getAMember() = f and
32-
f.getName() = ["init(key:blockMode:)", "init(key:blockMode:padding:)"] and
29+
exists(MethodDecl f, CallExpr call |
30+
f.hasQualifiedName("AES", ["init(key:blockMode:)", "init(key:blockMode:padding:)"]) and
3331
call.getStaticTarget() = f and
3432
call.getArgument(1).getExpr() = this
3533
)
@@ -42,10 +40,8 @@ class AES extends BlockMode {
4240
class Blowfish extends BlockMode {
4341
Blowfish() {
4442
// `blockMode` arg in `Blowfish.init` is a sink
45-
exists(ClassOrStructDecl c, AbstractFunctionDecl f, CallExpr call |
46-
c.getName() = "Blowfish" and
47-
c.getAMember() = f and
48-
f.getName() = "init(key:blockMode:padding:)" and
43+
exists(MethodDecl f, CallExpr call |
44+
f.hasQualifiedName("Blowfish", "init(key:blockMode:padding:)") and
4945
call.getStaticTarget() = f and
5046
call.getArgument(1).getExpr() = this
5147
)
@@ -60,10 +56,8 @@ class EcbEncryptionConfig extends DataFlow::Configuration {
6056
EcbEncryptionConfig() { this = "EcbEncryptionConfig" }
6157

6258
override predicate isSource(DataFlow::Node node) {
63-
exists(ClassOrStructDecl s, AbstractFunctionDecl f, CallExpr call |
64-
s.getName() = "ECB" and
65-
s.getAMember() = f and
66-
f.getName() = "init()" and
59+
exists(MethodDecl f, CallExpr call |
60+
f.hasQualifiedName("ECB", "init()") and
6761
call.getStaticTarget() = f and
6862
node.asExpr() = call
6963
)

0 commit comments

Comments
 (0)