|
| 1 | +import swift |
| 2 | +private import codeql.swift.dataflow.DataFlow |
| 3 | +private import codeql.swift.dataflow.ExternalFlow |
| 4 | +private import codeql.swift.dataflow.FlowSources |
| 5 | +private import codeql.swift.dataflow.FlowSteps |
| 6 | + |
| 7 | +/** |
| 8 | + * A model for WKScriptMessage sources. Classes implementing the `WKScriptMessageHandler` protocol |
| 9 | + * act as a bridge between JavaScript and native code. The messages sent from JavaScript code are |
| 10 | + * stored in the `message` parameter of `userContentController`. |
| 11 | + */ |
| 12 | +private class WKScriptMessageSources extends SourceModelCsv { |
| 13 | + override predicate row(string row) { |
| 14 | + row = ";WKScriptMessageHandler;true;userContentController(_:didReceive:);;;Parameter[1];remote" |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +/** The class `WKScriptMessage`. */ |
| 19 | +private class WKScriptMessageDecl extends ClassDecl { |
| 20 | + WKScriptMessageDecl() { this.getName() = "WKScriptMessage" } |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * A content implying that, if a `WKScriptMessage` is tainted, its `body` field is tainted. |
| 25 | + */ |
| 26 | +private class WKScriptMessageBodyInheritsTaint extends TaintInheritingContent, |
| 27 | + DataFlow::Content::FieldContent { |
| 28 | + WKScriptMessageBodyInheritsTaint() { |
| 29 | + exists(FieldDecl f | this.getField() = f | |
| 30 | + f.getEnclosingDecl() instanceof WKScriptMessageDecl and |
| 31 | + f.getName() = "body" |
| 32 | + ) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * A model for `JSContext` sources. `JSContext` acts as a bridge between JavaScript and |
| 38 | + * native code, so any object obtained from it has the potential of being tainted by a malicious |
| 39 | + * website visited in the WebView. |
| 40 | + */ |
| 41 | +private class JsContextSources extends SourceModelCsv { |
| 42 | + override predicate row(string row) { |
| 43 | + row = |
| 44 | + [ |
| 45 | + ";JSContext;true;globalObject;;;;remote", |
| 46 | + ";JSContext;true;objectAtIndexedSubscript(_:);;;ReturnValue;remote", |
| 47 | + ";JSContext;true;objectForKeyedSubscript(_:);;;ReturnValue;remote" |
| 48 | + ] |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * A model for `JSValue` summaries. If a `JSValue` is tainted, any object it is converted into |
| 54 | + * is also tainted. |
| 55 | + */ |
| 56 | +private class JsValueSummaries extends SummaryModelCsv { |
| 57 | + override predicate row(string row) { |
| 58 | + row = |
| 59 | + [ |
| 60 | + ";JSValue;true;init(object:in:);;;Argument[0];ReturnValue;taint", |
| 61 | + ";JSValue;true;init(bool:in:);;;Argument[0];ReturnValue;taint", |
| 62 | + ";JSValue;true;init(double:in:);;;Argument[0];ReturnValue;taint", |
| 63 | + ";JSValue;true;init(int32:in:);;;Argument[0];ReturnValue;taint", |
| 64 | + ";JSValue;true;init(uInt32:in:);;;Argument[0];ReturnValue;taint", |
| 65 | + ";JSValue;true;init(point:in:);;;Argument[0];ReturnValue;taint", |
| 66 | + ";JSValue;true;init(range:in:);;;Argument[0];ReturnValue;taint", |
| 67 | + ";JSValue;true;init(rect:in:);;;Argument[0];ReturnValue;taint", |
| 68 | + ";JSValue;true;init(size:in:);;;Argument[0];ReturnValue;taint", |
| 69 | + ";JSValue;true;toObject();;;Argument[-1];ReturnValue;taint", |
| 70 | + ";JSValue;true;toObjectOf(_:);;;Argument[-1];ReturnValue;taint", |
| 71 | + ";JSValue;true;toBool();;;Argument[-1];ReturnValue;taint", |
| 72 | + ";JSValue;true;toDouble();;;Argument[-1];ReturnValue;taint", |
| 73 | + ";JSValue;true;toInt32();;;Argument[-1];ReturnValue;taint", |
| 74 | + ";JSValue;true;toUInt32();;;Argument[-1];ReturnValue;taint", |
| 75 | + ";JSValue;true;toNumber();;;Argument[-1];ReturnValue;taint", |
| 76 | + ";JSValue;true;toString();;;Argument[-1];ReturnValue;taint", |
| 77 | + ";JSValue;true;toDate();;;Argument[-1];ReturnValue;taint", |
| 78 | + ";JSValue;true;toArray();;;Argument[-1];ReturnValue;taint", |
| 79 | + ";JSValue;true;toDictionary();;;Argument[-1];ReturnValue;taint", |
| 80 | + ";JSValue;true;toPoint();;;Argument[-1];ReturnValue;taint", |
| 81 | + ";JSValue;true;toRange();;;Argument[-1];ReturnValue;taint", |
| 82 | + ";JSValue;true;toRect();;;Argument[-1];ReturnValue;taint", |
| 83 | + ";JSValue;true;toSize();;;Argument[-1];ReturnValue;taint", |
| 84 | + // TODO: These models could use content flow to be more precise |
| 85 | + ";JSValue;true;atIndex(_:);;;Argument[-1];ReturnValue;taint", |
| 86 | + ";JSValue;true;defineProperty(_:descriptor:);;;Argument[1];Argument[-1];taint", |
| 87 | + ";JSValue;true;forProperty(_:);;;Argument[-1];ReturnValue;taint", |
| 88 | + ";JSValue;true;setValue(_:at:);;;Argument[0];Argument[-1];taint", |
| 89 | + ";JSValue;true;setValue(_:forProperty:);;;Argument[0];Argument[-1];taint" |
| 90 | + ] |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** The `JSExport` protocol. */ |
| 95 | +private class JsExport extends ProtocolDecl { |
| 96 | + JsExport() { this.getName() = "JSExport" } |
| 97 | +} |
| 98 | + |
| 99 | +/** A protocol inheriting `JSExport`. */ |
| 100 | +private class JsExportedProto extends ProtocolDecl { |
| 101 | + JsExportedProto() { this.getABaseTypeDecl+() instanceof JsExport } |
| 102 | +} |
| 103 | + |
| 104 | +/** A type that adopts a `JSExport`-inherited protocol. */ |
| 105 | +private class JsExportedType extends ClassOrStructDecl { |
| 106 | + JsExportedType() { this.getABaseTypeDecl*() instanceof JsExportedProto } |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * A flow source that models properties and methods defined in a `JSExport`-inherited protocol |
| 111 | + * and implemented in a type adopting that protcol. These members are accessible from JavaScript |
| 112 | + * when the object is assigned to a `JSContext`. |
| 113 | + */ |
| 114 | +private class JsExportedSource extends RemoteFlowSource { |
| 115 | + JsExportedSource() { |
| 116 | + exists(MethodDecl adopter, MethodDecl base | |
| 117 | + base.getEnclosingDecl() instanceof JsExportedProto and |
| 118 | + adopter.getEnclosingDecl() instanceof JsExportedType |
| 119 | + | |
| 120 | + this.(DataFlow::ParameterNode).getParameter().getDeclaringFunction() = adopter and |
| 121 | + adopter.getName() = base.getName() |
| 122 | + ) |
| 123 | + or |
| 124 | + exists(FieldDecl adopter, FieldDecl base | |
| 125 | + base.getEnclosingDecl() instanceof JsExportedProto and |
| 126 | + adopter.getEnclosingDecl() instanceof JsExportedType |
| 127 | + | |
| 128 | + this.asExpr().(MemberRefExpr).getMember() = adopter and adopter.getName() = base.getName() |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + override string getSourceType() { result = "Member of a type exposed through JSExport" } |
| 133 | +} |
0 commit comments