Skip to content

Commit fe57cd0

Browse files
authored
Merge pull request github#14488 from geoffw0/strlentest
Swift: Additional test cases for `swift\string-length-conflation`
2 parents 6ab2de1 + 9f683b8 commit fe57cd0

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

swift/ql/lib/codeql/swift/security/StringLengthConflationQuery.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ module StringLengthConflationConfig implements DataFlow::StateConfigSig {
3131

3232
predicate isBarrier(DataFlow::Node barrier) { barrier instanceof StringLengthConflationBarrier }
3333

34+
predicate isBarrierOut(DataFlow::Node node) {
35+
// make sinks barriers so that we only report the closest instance
36+
isSink(node, _)
37+
}
38+
3439
predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
3540
any(StringLengthConflationAdditionalFlowStep s).step(nodeFrom, nodeTo)
3641
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
edges
22
| StringLengthConflation2.swift:35:36:35:38 | .count | StringLengthConflation2.swift:35:36:35:46 | ... .-(_:_:) ... |
33
| StringLengthConflation2.swift:37:34:37:36 | .count | StringLengthConflation2.swift:37:34:37:44 | ... .-(_:_:) ... |
4-
| StringLengthConflation.swift:36:30:36:37 | len | StringLengthConflation.swift:36:93:36:93 | len |
54
| StringLengthConflation.swift:60:47:60:50 | .length | StringLengthConflation.swift:60:47:60:59 | ... ./(_:_:) ... |
65
| StringLengthConflation.swift:66:33:66:36 | .length | StringLengthConflation.swift:66:33:66:45 | ... ./(_:_:) ... |
7-
| StringLengthConflation.swift:72:33:72:35 | .count | StringLengthConflation.swift:36:30:36:37 | len |
86
| StringLengthConflation.swift:96:28:96:31 | .length | StringLengthConflation.swift:96:28:96:40 | ... .-(_:_:) ... |
97
| StringLengthConflation.swift:100:27:100:30 | .length | StringLengthConflation.swift:100:27:100:39 | ... .-(_:_:) ... |
108
| StringLengthConflation.swift:104:25:104:28 | .length | StringLengthConflation.swift:104:25:104:37 | ... .-(_:_:) ... |
@@ -48,8 +46,6 @@ nodes
4846
| StringLengthConflation2.swift:35:36:35:46 | ... .-(_:_:) ... | semmle.label | ... .-(_:_:) ... |
4947
| StringLengthConflation2.swift:37:34:37:36 | .count | semmle.label | .count |
5048
| StringLengthConflation2.swift:37:34:37:44 | ... .-(_:_:) ... | semmle.label | ... .-(_:_:) ... |
51-
| StringLengthConflation.swift:36:30:36:37 | len | semmle.label | len |
52-
| StringLengthConflation.swift:36:93:36:93 | len | semmle.label | len |
5349
| StringLengthConflation.swift:53:43:53:46 | .length | semmle.label | .length |
5450
| StringLengthConflation.swift:54:43:54:50 | .count | semmle.label | .count |
5551
| StringLengthConflation.swift:55:43:55:51 | .count | semmle.label | .count |
@@ -59,7 +55,6 @@ nodes
5955
| StringLengthConflation.swift:66:33:66:36 | .length | semmle.label | .length |
6056
| StringLengthConflation.swift:66:33:66:45 | ... ./(_:_:) ... | semmle.label | ... ./(_:_:) ... |
6157
| StringLengthConflation.swift:72:33:72:35 | .count | semmle.label | .count |
62-
| StringLengthConflation.swift:72:33:72:35 | .count | semmle.label | .count |
6358
| StringLengthConflation.swift:78:47:78:49 | .count | semmle.label | .count |
6459
| StringLengthConflation.swift:79:47:79:54 | .count | semmle.label | .count |
6560
| StringLengthConflation.swift:81:47:81:64 | .count | semmle.label | .count |
@@ -116,12 +111,14 @@ nodes
116111
| StringLengthConflation.swift:179:37:179:47 | ... .-(_:_:) ... | semmle.label | ... .-(_:_:) ... |
117112
| StringLengthConflation.swift:181:37:181:39 | .count | semmle.label | .count |
118113
| StringLengthConflation.swift:181:37:181:47 | ... .-(_:_:) ... | semmle.label | ... .-(_:_:) ... |
114+
| StringLengthConflation.swift:190:28:190:28 | .count | semmle.label | .count |
115+
| StringLengthConflation.swift:191:28:191:33 | .count | semmle.label | .count |
116+
| StringLengthConflation.swift:193:28:193:43 | .count | semmle.label | .count |
119117
| file://:0:0:0:0 | .length | semmle.label | .length |
120118
subpaths
121119
#select
122120
| 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. |
123121
| 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. |
124-
| 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. |
125122
| 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. |
126123
| StringLengthConflation.swift:53:43:53:46 | .length | file://:0:0:0:0 | .length | StringLengthConflation.swift:53:43:53:46 | .length | This NSString length is used in a String, but it may not be equivalent. |
127124
| StringLengthConflation.swift:54:43:54:50 | .count | StringLengthConflation.swift:54:43:54:50 | .count | StringLengthConflation.swift:54:43:54:50 | .count | This String.UTF8View length is used in a String, but it may not be equivalent. |
@@ -173,3 +170,6 @@ subpaths
173170
| StringLengthConflation.swift:178:35:178:48 | ... .-(_:_:) ... | file://:0:0:0:0 | .length | StringLengthConflation.swift:178:35:178:48 | ... .-(_:_:) ... | This NSString length is used in a String.UnicodeScalarView, but it may not be equivalent. |
174171
| StringLengthConflation.swift:179:37:179:47 | ... .-(_:_:) ... | StringLengthConflation.swift:179:37:179:39 | .count | StringLengthConflation.swift:179:37:179:47 | ... .-(_:_:) ... | This String length is used in a String.UTF8View, but it may not be equivalent. |
175172
| StringLengthConflation.swift:181:37:181:47 | ... .-(_:_:) ... | StringLengthConflation.swift:181:37:181:39 | .count | StringLengthConflation.swift:181:37:181:47 | ... .-(_:_:) ... | This String length is used in a String.UTF16View, but it may not be equivalent. |
173+
| StringLengthConflation.swift:190:28:190:28 | .count | StringLengthConflation.swift:190:28:190:28 | .count | StringLengthConflation.swift:190:28:190:28 | .count | This String length is used in an NSString, but it may not be equivalent. |
174+
| StringLengthConflation.swift:191:28:191:33 | .count | StringLengthConflation.swift:191:28:191:33 | .count | StringLengthConflation.swift:191:28:191:33 | .count | This String.UTF8View length is used in an NSString, but it may not be equivalent. |
175+
| StringLengthConflation.swift:193:28:193:43 | .count | StringLengthConflation.swift:193:28:193:43 | .count | StringLengthConflation.swift:193:28:193:43 | .count | This String.UnicodeScalarView length is used in an NSString, but it may not be equivalent. |

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,12 @@ func test(s: String) {
184184

185185
// `begin :thumbsup: end`, with thumbs up emoji and skin tone modifier
186186
test(s: "begin \u{0001F44D}\u{0001F3FF} end")
187+
188+
extension String {
189+
func newStringMethod() {
190+
_ = NSMakeRange(0, count) // BAD
191+
_ = NSMakeRange(0, utf8.count) // BAD
192+
_ = NSMakeRange(0, utf16.count) // GOOD (`String.UTF16View` and `NSString` lengths are equivalent)
193+
_ = NSMakeRange(0, unicodeScalars.count) // BAD
194+
}
195+
}

0 commit comments

Comments
 (0)