|
1 | 1 | /**
|
2 | 2 | * @name String length conflation
|
3 |
| - * @description TODO |
| 3 | + * @description Using a length value from an `NSString` in a `String`, or a count from a `String` in an `NSString`, may cause unexpected behavior. |
4 | 4 | * @kind problem
|
5 |
| - * @problem.severity TODO |
6 |
| - * @security-severity TODO |
| 5 | + * @problem.severity error |
| 6 | + * @security-severity 7.8 |
7 | 7 | * @precision TODO
|
8 | 8 | * @id swift/string-length-conflation
|
9 | 9 | * @tags security
|
10 | 10 | * external/cwe/cwe-135
|
11 | 11 | */
|
12 | 12 |
|
13 | 13 | import swift
|
| 14 | +import codeql.swift.dataflow.DataFlow |
| 15 | +import DataFlow::PathGraph |
14 | 16 |
|
15 |
| -select "TODO" |
| 17 | +class StringLengthConflationConfiguration extends DataFlow::Configuration { |
| 18 | + StringLengthConflationConfiguration() { this = "StringLengthConflationConfiguration" } |
| 19 | + |
| 20 | + override predicate isSource(DataFlow::Node node, string flowstate) { |
| 21 | + // result of a call to to `String.count` |
| 22 | + exists(MemberRefExpr member | |
| 23 | + member.getBaseExpr().getType().getName() = "String" and |
| 24 | + member.getMember().(VarDecl).getName() = "count" and |
| 25 | + node.asExpr() = member and |
| 26 | + flowstate = "String" |
| 27 | + ) |
| 28 | + } |
| 29 | + |
| 30 | + override predicate isSink(DataFlow::Node node, string flowstate) { |
| 31 | + // arguments to method calls... |
| 32 | + exists( |
| 33 | + string className, string methodName, string paramName, ClassDecl c, AbstractFunctionDecl f, |
| 34 | + CallExpr call, int arg |
| 35 | + | |
| 36 | + ( |
| 37 | + // `NSRange.init` |
| 38 | + className = "NSRange" and |
| 39 | + methodName = "init(location:length:)" and |
| 40 | + paramName = ["location", "length"] |
| 41 | + or |
| 42 | + // `NSString.character` |
| 43 | + className = ["NSString", "NSMutableString"] and |
| 44 | + methodName = "character(at:)" and |
| 45 | + paramName = "at" |
| 46 | + or |
| 47 | + // `NSString.character` |
| 48 | + className = ["NSString", "NSMutableString"] and |
| 49 | + methodName = "substring(from:)" and |
| 50 | + paramName = "from" |
| 51 | + or |
| 52 | + // `NSString.character` |
| 53 | + className = ["NSString", "NSMutableString"] and |
| 54 | + methodName = "substring(to:)" and |
| 55 | + paramName = "to" |
| 56 | + or |
| 57 | + // `NSMutableString.insert` |
| 58 | + className = "NSMutableString" and |
| 59 | + methodName = "insert(_:at:)" and |
| 60 | + paramName = "at" |
| 61 | + ) and |
| 62 | + c.getName() = className and |
| 63 | + c.getAMember() = f and // TODO: will this even work if its defined in a parent class? |
| 64 | + call.getFunction().(ApplyExpr).getStaticTarget() = f and |
| 65 | + f.getName() = methodName and |
| 66 | + f.getParam(arg).getName() = paramName and |
| 67 | + call.getArgument(arg).getExpr() = node.asExpr() and |
| 68 | + flowstate = "String" // `String` length flowing into `NSString` |
| 69 | + ) |
| 70 | + or |
| 71 | + // arguments to function calls... |
| 72 | + exists(string funcName, string paramName, CallExpr call, int arg | |
| 73 | + // `NSMakeRange` |
| 74 | + funcName = "NSMakeRange(_:_:)" and |
| 75 | + paramName = ["loc", "len"] and |
| 76 | + call.getStaticTarget().getName() = funcName and |
| 77 | + call.getStaticTarget().getParam(arg).getName() = paramName and |
| 78 | + call.getArgument(arg).getExpr() = node.asExpr() and |
| 79 | + flowstate = "String" // `String` length flowing into `NSString` |
| 80 | + ) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +from |
| 85 | + StringLengthConflationConfiguration config, DataFlow::PathNode source, DataFlow::PathNode sink, |
| 86 | + string flowstate, string message |
| 87 | +where |
| 88 | + config.hasFlowPath(source, sink) and |
| 89 | + config.isSink(sink.getNode(), flowstate) and |
| 90 | + ( |
| 91 | + flowstate = "String" and |
| 92 | + message = "This String length is used in an NSString, but it may not be equivalent." |
| 93 | + or |
| 94 | + flowstate = "NSString" and |
| 95 | + message = "This NSString length is used in a String, but it may not be equivalent." |
| 96 | + ) |
| 97 | +select sink.getNode(), source, sink, message |
0 commit comments