Skip to content

Commit 07b89b8

Browse files
committed
Swift: Clean up a bit.
1 parent 19026e9 commit 07b89b8

File tree

1 file changed

+59
-65
lines changed

1 file changed

+59
-65
lines changed

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

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,78 +14,72 @@ import swift
1414
import codeql.swift.dataflow.DataFlow
1515
import DataFlow::PathGraph
1616

17-
predicate isSource0(Expr e) {
18-
// result of a call to to `String.count`
19-
exists(MemberRefExpr member |
20-
member.getBaseExpr().getType().toString() = "String" and // TODO: use of toString
21-
member.getMember().toString() = "count" and // TODO: use of toString
22-
e = member
23-
)
24-
// TODO: other sources such as NSString.length, with different set of sinks
25-
}
26-
27-
predicate isSink0(Expr e) {
28-
// arguments to method calls...
29-
exists(
30-
string className, string methodName, string argName, ClassDecl c, AbstractFunctionDecl f,
31-
CallExpr call, int arg
32-
|
33-
(
34-
// `NSRange.init`
35-
className = "NSRange" and
36-
methodName = "init" and
37-
argName = ["location", "length"]
38-
or
39-
// `NSString.character`
40-
className = ["NSString", "NSMutableString"] and
41-
methodName = "character" and
42-
argName = "at"
43-
or
44-
// `NSString.character`
45-
className = ["NSString", "NSMutableString"] and
46-
methodName = "substring" and
47-
argName = ["from", "to"]
48-
or
49-
// `NSMutableString.insert`
50-
className = "NSMutableString" and
51-
methodName = "insert" and
52-
argName = "at"
53-
) and
54-
c.toString() = className and // TODO: use of toString
55-
c.getAMember() = f and // TODO: will this even work if its defined in a parent class?
56-
call.getFunction().(ApplyExpr).getFunction().(DeclRefExpr).getDecl() = f and
57-
call.getFunction().(ApplyExpr).getFunction().toString() = methodName and // TODO: use of toString
58-
call.getFunction()
59-
.(ApplyExpr)
60-
.getFunction()
61-
.(DeclRefExpr)
62-
.getDecl()
63-
.(AbstractFunctionDecl)
64-
.getParam(arg)
65-
.getName() = argName and
66-
call.getArgument(arg).getExpr() = e
67-
)
68-
or
69-
// arguments to function calls...
70-
exists(string funcName, string argName, CallExpr call, int arg |
71-
// `NSMakeRange`
72-
funcName = "NSMakeRange" and
73-
argName = ["loc", "len"] and
74-
call.getStaticTarget().getName().matches(funcName + "%") and
75-
call.getStaticTarget().getParam(arg).getName() = argName and
76-
call.getArgument(arg).getExpr() = e
77-
)
78-
}
79-
8017
class StringLengthConflationConfiguration extends DataFlow::Configuration {
8118
StringLengthConflationConfiguration() { this = "StringLengthConflationConfiguration" }
8219

8320
override predicate isSource(DataFlow::Node node, string flowstate) {
84-
isSource0(node.asExpr()) and flowstate = "String"
21+
// result of a call to to `String.count`
22+
exists(MemberRefExpr member |
23+
member.getBaseExpr().getType().toString() = "String" and // TODO: use of toString
24+
member.getMember().toString() = "count" and // TODO: use of toString
25+
node.asExpr() = member and
26+
flowstate = "String"
27+
)
8528
}
8629

8730
override predicate isSink(DataFlow::Node node, string flowstate) {
88-
isSink0(node.asExpr()) and flowstate = "String"
31+
// arguments to method calls...
32+
exists(
33+
string className, string methodName, string argName, ClassDecl c, AbstractFunctionDecl f,
34+
CallExpr call, int arg
35+
|
36+
(
37+
// `NSRange.init`
38+
className = "NSRange" and
39+
methodName = "init" and
40+
argName = ["location", "length"]
41+
or
42+
// `NSString.character`
43+
className = ["NSString", "NSMutableString"] and
44+
methodName = "character" and
45+
argName = "at"
46+
or
47+
// `NSString.character`
48+
className = ["NSString", "NSMutableString"] and
49+
methodName = "substring" and
50+
argName = ["from", "to"]
51+
or
52+
// `NSMutableString.insert`
53+
className = "NSMutableString" and
54+
methodName = "insert" and
55+
argName = "at"
56+
) and
57+
c.toString() = className and // TODO: use of toString
58+
c.getAMember() = f and // TODO: will this even work if its defined in a parent class?
59+
call.getFunction().(ApplyExpr).getFunction().(DeclRefExpr).getDecl() = f and
60+
call.getFunction().(ApplyExpr).getFunction().toString() = methodName and // TODO: use of toString
61+
call.getFunction()
62+
.(ApplyExpr)
63+
.getFunction()
64+
.(DeclRefExpr)
65+
.getDecl()
66+
.(AbstractFunctionDecl)
67+
.getParam(arg)
68+
.getName() = argName and
69+
call.getArgument(arg).getExpr() = node.asExpr() and
70+
flowstate = "String" // `String` length flowing into `NSString`
71+
)
72+
or
73+
// arguments to function calls...
74+
exists(string funcName, string argName, CallExpr call, int arg |
75+
// `NSMakeRange`
76+
funcName = "NSMakeRange" and
77+
argName = ["loc", "len"] and
78+
call.getStaticTarget().getName().matches(funcName + "%") and
79+
call.getStaticTarget().getParam(arg).getName() = argName and
80+
call.getArgument(arg).getExpr() = node.asExpr() and
81+
flowstate = "String" // `String` length flowing into `NSString`
82+
)
8983
}
9084
}
9185

0 commit comments

Comments
 (0)