Skip to content

Commit d1867b9

Browse files
authored
Merge pull request #10284 from geoffw0/stringlengthcleanup
Swift: Improve swift/string-length-conflation
2 parents f448965 + a14efcf commit d1867b9

File tree

3 files changed

+36
-40
lines changed

3 files changed

+36
-40
lines changed

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

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -56,44 +56,36 @@ class StringLengthConflationConfiguration extends DataFlow::Configuration {
5656
StringLengthConflationConfiguration() { this = "StringLengthConflationConfiguration" }
5757

5858
override predicate isSource(DataFlow::Node node, string flowstate) {
59-
// result of a call to `String.count`
60-
exists(MemberRefExpr member |
61-
member.getBase().getType().getName() = "String" and
62-
member.getMember().(VarDecl).getName() = "count" and
63-
node.asExpr() = member and
64-
flowstate = "String"
65-
)
66-
or
67-
// result of a call to `NSString.length`
68-
exists(MemberRefExpr member |
69-
member.getBase().getType().getName() = ["NSString", "NSMutableString"] and
70-
member.getMember().(VarDecl).getName() = "length" and
71-
node.asExpr() = member and
72-
flowstate = "NSString"
73-
)
74-
or
75-
// result of a call to `String.utf8.count`
76-
exists(MemberRefExpr member |
77-
member.getBase().getType().getName() = "String.UTF8View" and
78-
member.getMember().(VarDecl).getName() = "count" and
79-
node.asExpr() = member and
80-
flowstate = "String.utf8"
81-
)
82-
or
83-
// result of a call to `String.utf16.count`
84-
exists(MemberRefExpr member |
85-
member.getBase().getType().getName() = "String.UTF16View" and
86-
member.getMember().(VarDecl).getName() = "count" and
87-
node.asExpr() = member and
88-
flowstate = "String.utf16"
89-
)
90-
or
91-
// result of a call to `String.unicodeScalars.count`
92-
exists(MemberRefExpr member |
93-
member.getBase().getType().getName() = "String.UnicodeScalarView" and
94-
member.getMember().(VarDecl).getName() = "count" and
95-
node.asExpr() = member and
96-
flowstate = "String.unicodeScalars"
59+
exists(MemberRefExpr memberRef, string className, string varName |
60+
memberRef.getBase().getType().(NominalType).getABaseType*().getName() = className and
61+
memberRef.getMember().(VarDecl).getName() = varName and
62+
node.asExpr() = memberRef and
63+
(
64+
// result of a call to `String.count`
65+
className = "String" and
66+
varName = "count" and
67+
flowstate = "String"
68+
or
69+
// result of a call to `NSString.length`
70+
className = ["NSString", "NSMutableString"] and
71+
varName = "length" and
72+
flowstate = "NSString"
73+
or
74+
// result of a call to `String.utf8.count`
75+
className = "String.UTF8View" and
76+
varName = "count" and
77+
flowstate = "String.utf8"
78+
or
79+
// result of a call to `String.utf16.count`
80+
className = "String.UTF16View" and
81+
varName = "count" and
82+
flowstate = "String.utf16"
83+
or
84+
// result of a call to `String.unicodeScalars.count`
85+
className = "String.UnicodeScalarView" and
86+
varName = "count" and
87+
flowstate = "String.unicodeScalars"
88+
)
9789
)
9890
}
9991

@@ -135,7 +127,7 @@ class StringLengthConflationConfiguration extends DataFlow::Configuration {
135127
paramName = "at"
136128
) and
137129
c.getName() = className and
138-
c.getAMember() = funcDecl and
130+
c.getABaseTypeDecl*().(ClassDecl).getAMember() = funcDecl and
139131
call.getStaticTarget() = funcDecl and
140132
flowstate = "NSString"
141133
)

swift/ql/test/query-tests/Security/CWE-135/StringLengthConflation.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
edges
2+
| StringLengthConflation2.swift:35:36:35:38 | .count : | StringLengthConflation2.swift:35:36:35:46 | ... .-(_:_:) ... |
23
| StringLengthConflation2.swift:37:34:37:36 | .count : | StringLengthConflation2.swift:37:34:37:44 | ... .-(_:_:) ... |
34
| StringLengthConflation.swift:36:30:36:37 | len : | StringLengthConflation.swift:36:93:36:93 | len |
45
| StringLengthConflation.swift:60:47:60:50 | .length : | StringLengthConflation.swift:60:47:60:59 | ... ./(_:_:) ... |
@@ -27,6 +28,8 @@ edges
2728
| file://:0:0:0:0 | .length : | StringLengthConflation.swift:114:23:114:26 | .length : |
2829
| file://:0:0:0:0 | .length : | StringLengthConflation.swift:120:22:120:25 | .length : |
2930
nodes
31+
| StringLengthConflation2.swift:35:36:35:38 | .count : | semmle.label | .count : |
32+
| StringLengthConflation2.swift:35:36:35:46 | ... .-(_:_:) ... | semmle.label | ... .-(_:_:) ... |
3033
| StringLengthConflation2.swift:37:34:37:36 | .count : | semmle.label | .count : |
3134
| StringLengthConflation2.swift:37:34:37:44 | ... .-(_:_:) ... | semmle.label | ... .-(_:_:) ... |
3235
| StringLengthConflation.swift:36:30:36:37 | len : | semmle.label | len : |
@@ -73,6 +76,7 @@ nodes
7376
| file://:0:0:0:0 | .length : | semmle.label | .length : |
7477
subpaths
7578
#select
79+
| StringLengthConflation2.swift:35:36:35:46 | ... .-(_:_:) ... | StringLengthConflation2.swift:35:36:35:38 | .count : | StringLengthConflation2.swift:35:36:35:46 | ... .-(_:_:) ... | This String length is used in an NSString, but it may not be equivalent. |
7680
| StringLengthConflation2.swift:37:34:37:44 | ... .-(_:_:) ... | StringLengthConflation2.swift:37:34:37:36 | .count : | StringLengthConflation2.swift:37:34:37:44 | ... .-(_:_:) ... | This String length is used in an NSString, but it may not be equivalent. |
7781
| StringLengthConflation.swift:36:93:36:93 | len | StringLengthConflation.swift:72:33:72:35 | .count : | StringLengthConflation.swift:36:93:36:93 | len | This String length is used in an NSString, but it may not be equivalent. |
7882
| StringLengthConflation.swift:53:43:53:46 | .length | StringLengthConflation.swift:53:43:53:46 | .length | StringLengthConflation.swift:53:43:53:46 | .length | This NSString length is used in a String, but it may not be equivalent. |

swift/ql/test/query-tests/Security/CWE-135/StringLengthConflation2.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func test(s: String) {
3232
let ns = NSString(string: s)
3333

3434
let nstr1 = ns.substring(from: ns.length - 1) // GOOD
35-
let nstr2 = ns.substring(from: s.count - 1) // BAD: String length used in NSString [NOT DETECTED]
35+
let nstr2 = ns.substring(from: s.count - 1) // BAD: String length used in NSString
3636
let nstr3 = ns.substring(to: ns.length - 1) // GOOD
3737
let nstr4 = ns.substring(to: s.count - 1) // BAD: String length used in NSString
3838
print("substrings '\(nstr1)' '\(nstr2)' / '\(nstr3)' '\(nstr4)'")

0 commit comments

Comments
 (0)