Skip to content

Commit 0bd94a6

Browse files
committed
Swift: Clean up isSink (2 - rename methodName -> funcName and move that out as well).
1 parent 4854679 commit 0bd94a6

File tree

1 file changed

+44
-48
lines changed

1 file changed

+44
-48
lines changed

swift/ql/src/queries/Security/CWE-135/StringLengthConflation.ql

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -41,87 +41,83 @@ class StringLengthConflationConfiguration extends DataFlow::Configuration {
4141
}
4242

4343
override predicate isSink(DataFlow::Node node, string flowstate) {
44-
exists(CallExpr call, string paramName, int arg |
44+
exists(CallExpr call, string funcName, string paramName, int arg |
4545
// arguments to method calls...
46-
exists(string className, string methodName, ClassDecl c, AbstractFunctionDecl f |
46+
exists(string className, ClassDecl c, AbstractFunctionDecl f |
4747
(
4848
// `NSRange.init`
4949
className = "NSRange" and
50-
methodName = "init(location:length:)" and
50+
funcName = "init(location:length:)" and
5151
paramName = ["location", "length"]
5252
or
5353
// `NSString.character`
5454
className = ["NSString", "NSMutableString"] and
55-
methodName = "character(at:)" and
55+
funcName = "character(at:)" and
5656
paramName = "at"
5757
or
5858
// `NSString.character`
5959
className = ["NSString", "NSMutableString"] and
60-
methodName = "substring(from:)" and
60+
funcName = "substring(from:)" and
6161
paramName = "from"
6262
or
6363
// `NSString.character`
6464
className = ["NSString", "NSMutableString"] and
65-
methodName = "substring(to:)" and
65+
funcName = "substring(to:)" and
6666
paramName = "to"
6767
or
6868
// `NSMutableString.insert`
6969
className = "NSMutableString" and
70-
methodName = "insert(_:at:)" and
70+
funcName = "insert(_:at:)" and
7171
paramName = "at"
7272
) and
7373
c.getName() = className and
7474
c.getAMember() = f and // TODO: will this even work if its defined in a parent class?
7575
call.getFunction().(ApplyExpr).getStaticTarget() = f and
76-
f.getName() = methodName and
76+
f.getName() = funcName and
7777
f.getParam(pragma[only_bind_into](arg)).getName() = paramName and
7878
call.getArgument(pragma[only_bind_into](arg)).getExpr() = node.asExpr() and
7979
flowstate = "String" // `String` length flowing into `NSString`
8080
)
8181
or
8282
// arguments to function calls...
83-
exists(string funcName |
84-
// `NSMakeRange`
85-
funcName = "NSMakeRange(_:_:)" and
86-
paramName = ["loc", "len"] and
87-
call.getStaticTarget().getName() = funcName and
88-
call.getStaticTarget().getParam(pragma[only_bind_into](arg)).getName() = paramName and
89-
call.getArgument(pragma[only_bind_into](arg)).getExpr() = node.asExpr() and
90-
flowstate = "String" // `String` length flowing into `NSString`
91-
)
83+
// `NSMakeRange`
84+
funcName = "NSMakeRange(_:_:)" and
85+
paramName = ["loc", "len"] and
86+
call.getStaticTarget().getName() = funcName and
87+
call.getStaticTarget().getParam(pragma[only_bind_into](arg)).getName() = paramName and
88+
call.getArgument(pragma[only_bind_into](arg)).getExpr() = node.asExpr() and
89+
flowstate = "String" // `String` length flowing into `NSString`
9290
or
9391
// arguments to function calls...
94-
exists(string funcName |
95-
(
96-
// `String.dropFirst`, `String.dropLast`, `String.removeFirst`, `String.removeLast`
97-
funcName = ["dropFirst(_:)", "dropLast(_:)", "removeFirst(_:)", "removeLast(_:)"] and
98-
paramName = "k"
99-
or
100-
// `String.prefix`, `String.suffix`
101-
funcName = ["prefix(_:)", "suffix(_:)"] and
102-
paramName = "maxLength"
103-
or
104-
// `String.Index.init`
105-
funcName = "init(encodedOffset:)" and
106-
paramName = "offset"
107-
or
108-
// `String.index`
109-
funcName = ["index(_:offsetBy:)", "index(_:offsetBy:limitBy:)"] and
110-
paramName = "n"
111-
or
112-
// `String.formIndex`
113-
funcName = ["formIndex(_:offsetBy:)", "formIndex(_:offsetBy:limitBy:)"] and
114-
paramName = "distance"
115-
) and
116-
call.getFunction().(ApplyExpr).getStaticTarget().getName() = funcName and
117-
call.getFunction()
118-
.(ApplyExpr)
119-
.getStaticTarget()
120-
.getParam(pragma[only_bind_into](arg))
121-
.getName() = paramName and
122-
call.getArgument(pragma[only_bind_into](arg)).getExpr() = node.asExpr() and
123-
flowstate = "NSString" // `NSString` length flowing into `String`
124-
)
92+
(
93+
// `String.dropFirst`, `String.dropLast`, `String.removeFirst`, `String.removeLast`
94+
funcName = ["dropFirst(_:)", "dropLast(_:)", "removeFirst(_:)", "removeLast(_:)"] and
95+
paramName = "k"
96+
or
97+
// `String.prefix`, `String.suffix`
98+
funcName = ["prefix(_:)", "suffix(_:)"] and
99+
paramName = "maxLength"
100+
or
101+
// `String.Index.init`
102+
funcName = "init(encodedOffset:)" and
103+
paramName = "offset"
104+
or
105+
// `String.index`
106+
funcName = ["index(_:offsetBy:)", "index(_:offsetBy:limitBy:)"] and
107+
paramName = "n"
108+
or
109+
// `String.formIndex`
110+
funcName = ["formIndex(_:offsetBy:)", "formIndex(_:offsetBy:limitBy:)"] and
111+
paramName = "distance"
112+
) and
113+
call.getFunction().(ApplyExpr).getStaticTarget().getName() = funcName and
114+
call.getFunction()
115+
.(ApplyExpr)
116+
.getStaticTarget()
117+
.getParam(pragma[only_bind_into](arg))
118+
.getName() = paramName and
119+
call.getArgument(pragma[only_bind_into](arg)).getExpr() = node.asExpr() and
120+
flowstate = "NSString" // `NSString` length flowing into `String`
125121
)
126122
}
127123

0 commit comments

Comments
 (0)