diff --git a/shared/mad/codeql/mad/ModelValidation.qll b/shared/mad/codeql/mad/ModelValidation.qll index a5e64c434992..cbe350c2d768 100644 --- a/shared/mad/codeql/mad/ModelValidation.qll +++ b/shared/mad/codeql/mad/ModelValidation.qll @@ -42,7 +42,8 @@ module KindValidation { "cors-origin", "mongodb.sink", "system-prompt-injection", "user-prompt-injection", // Swift-only currently, but may be shared in the future "database-store", "format-string", "hash-iteration-count", "predicate-injection", - "preferences-store", "tls-protocol-version", "transmission", "webview-fetch", "xxe", + "preferences-store", "tls-protocol-version", "transmission", "webview-fetch", + "webview-baseurl", "xxe", // Go-only currently, but may be shared in the future "jwt", // CPP-only currently diff --git a/unified/ql/lib/ext/webview.model.json b/unified/ql/lib/ext/webview.model.json new file mode 100644 index 000000000000..a72e2986d99d --- /dev/null +++ b/unified/ql/lib/ext/webview.model.json @@ -0,0 +1,21 @@ +{ + "extensions": [ + { + "addsTo": { + "pack": "codeql/unified-all", + "extensible": "legacySinkModel" + }, + "data": [ + // namespace, type, subtypes, name, signature, ext, input, kind, provenance + ["", "UIWebView", true, "loadHTMLString(_:baseURL:)", "", "", "Argument[0]", "webview-fetch", "manual"], + ["", "UIWebView", true, "loadHTMLString(_:baseURL:)", "", "", "Argument[baseURL:]", "webview-baseurl", "manual"], + ["", "WKWebView", true, "loadHTMLString(_:baseURL:)", "", "", "Argument[0]", "webview-fetch", "manual"], + ["", "WKWebView", true, "loadHTMLString(_:baseURL:)", "", "", "Argument[baseURL:]", "webview-baseurl", "manual"], + ["", "UIWebView", true, "load(_:mimeType:textEncodingName:baseURL:)", "", "", "Argument[0]", "webview-fetch", "manual"], + ["", "UIWebView", true, "load(_:mimeType:textEncodingName:baseURL:)", "", "", "Argument[baseURL:]", "webview-baseurl", "manual"], + ["", "WKWebView", true, "load(_:mimeType:characterEncodingName:baseURL:)", "", "", "Argument[0]", "webview-fetch", "manual"], + ["", "WKWebView", true, "load(_:mimeType:characterEncodingName:baseURL:)", "", "", "Argument[baseURL:]", "webview-baseurl", "manual"] + ] + } + ] +} diff --git a/unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetch.qhelp b/unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetch.qhelp new file mode 100644 index 000000000000..2b1ca4a58958 --- /dev/null +++ b/unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetch.qhelp @@ -0,0 +1,33 @@ + + + + +

Fetching data in a web view without restricting the base URL may allow an attacker to access sensitive local data, for example using file://. Data can then be extracted from the software using the URL of a machine under the attacker's control. More generally, an attacker may use a URL under their control as part of a cross-site scripting attack.

+ +
+ + +

When loading HTML into a web view, always set the baseURL to an appropriate URL that you control, or to about:blank. Do not use nil, as this does not restrict URLs that can be resolved. Also do not use a baseURL that could itself be controlled by an attacker.

+ +
+ + +

In the following example, a call to UIWebView.loadHTMLString has the baseURL set to nil, which does not restrict URLs that can be resolved from within the web page.

+ + + +

To fix the problem, we set the baseURL to about:blank. This avoids resolving relative URLs against the application's local file system. A base URL is not a general navigation restriction: absolute remote URLs still need appropriate navigation and content controls.

+ + + +
+ + +
  • + iOS Bug Hunting - Web View XSS. +
  • + +
    +
    \ No newline at end of file diff --git a/unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetch.ql b/unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetch.ql new file mode 100644 index 000000000000..25e2ceacaf9c --- /dev/null +++ b/unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetch.ql @@ -0,0 +1,86 @@ +/** + * @name Unsafe WebView fetch + * @description Fetching data in a WebView without restricting the base URL may allow an attacker to access sensitive local data, or enable cross-site scripting attack. + * @kind path-problem + * @problem.severity warning + * @security-severity 7.8 + * @precision high + * @id unified/swift/unsafe-webview-fetch + * @tags security + * external/cwe/cwe-079 + * external/cwe/cwe-095 + * external/cwe/cwe-749 + */ + +import unified + +/** + * A modeled WebView fetch. + */ +private class UnsafeWebViewFetchSink extends DataFlow::Node { + UnsafeWebViewFetchSink() { Models::isSink(this, "webview-fetch") } + + /** + * Gets a base URL argument of this WebView fetch. + */ + DataFlow::Node getABaseUrl() { + // `result` is a `webview-baseurl` sink that's part of the same + // call as this `webview-fetch` sink. + exists(CallExpr call | + Models::isSink(result, "webview-baseurl") and + call.getAnArgument().getValue() = this.asExpr() and + call.getAnArgument().getValue() = result.asExpr() + ) + } +} + +/** + * A taint configuration from taint sources to sinks for this query. + */ +module UnsafeWebViewFetchConfig implements DataFlow::ConfigSig { + predicate isSource(DataFlow::Node node) { Models::isSource(node, _) } + + predicate isSink(DataFlow::Node node) { node = any(UnsafeWebViewFetchSink sink) } + + predicate observeDiffInformedIncrementalMode() { any() } +} + +module UnsafeWebViewFetchFlow = TaintTracking::Global; + +import UnsafeWebViewFetchFlow::PathGraph + +/** + * A taint configuration from taint sources to base URL arguments of WebView fetches. + */ +module BaseUrlConfig implements DataFlow::ConfigSig { + predicate isSource(DataFlow::Node node) { Models::isSource(node, _) } + + predicate isSink(DataFlow::Node node) { node = any(UnsafeWebViewFetchSink sink).getABaseUrl() } + + predicate observeDiffInformedIncrementalMode() { + none() // only used as secondary configuration + } +} + +module BaseUrlFlow = TaintTracking::Global; + +from + UnsafeWebViewFetchFlow::PathNode sourceNode, UnsafeWebViewFetchFlow::PathNode sinkNode, + UnsafeWebViewFetchSink sink, string message +where + UnsafeWebViewFetchFlow::flowPath(sourceNode, sinkNode) and + sink = sinkNode.getNode() and + ( + // no base URL + not exists(sink.getABaseUrl()) and + message = "Tainted data is used in a WebView fetch." + or + // base URL is nil + sink.getABaseUrl().asExpr().(BuiltinExpr).getValue() = "nil" and + message = "Tainted data is used in a WebView fetch without restricting the base URL." + or + // base URL is also tainted + BaseUrlFlow::flowTo(sink.getABaseUrl()) and + message = "Tainted data is used in a WebView fetch with a tainted base URL." + ) +select sink, sourceNode, sinkNode, message diff --git a/unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetchBad.swift b/unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetchBad.swift new file mode 100644 index 000000000000..f85adb1b267b --- /dev/null +++ b/unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetchBad.swift @@ -0,0 +1,6 @@ + +let webview = UIWebView() + +... + +webview.loadHTMLString(htmlData, baseURL: nil) // BAD diff --git a/unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetchGood.swift b/unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetchGood.swift new file mode 100644 index 000000000000..5d89e5be672a --- /dev/null +++ b/unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetchGood.swift @@ -0,0 +1,6 @@ + +let webview = UIWebView() + +... + +webview.loadHTMLString(htmlData, baseURL: URL(string: "about:blank")) // GOOD diff --git a/unified/ql/test/query-tests/security/CWE-079/UnsafeWebViewFetch/UnsafeWebViewFetch.expected b/unified/ql/test/query-tests/security/CWE-079/UnsafeWebViewFetch/UnsafeWebViewFetch.expected new file mode 100644 index 000000000000..bbdfe8b07f3f --- /dev/null +++ b/unified/ql/test/query-tests/security/CWE-079/UnsafeWebViewFetch/UnsafeWebViewFetch.expected @@ -0,0 +1,88 @@ +#select +| UnsafeWebViewFetch.swift:103:25:103:84 | try! ... | UnsafeWebViewFetch.swift:103:30:103:84 | String(...) | UnsafeWebViewFetch.swift:103:25:103:84 | try! ... | Tainted data is used in a WebView fetch without restricting the base URL. | +| UnsafeWebViewFetch.swift:106:25:106:28 | data | UnsafeWebViewFetch.swift:105:18:105:72 | String(...) | UnsafeWebViewFetch.swift:106:25:106:28 | data | Tainted data is used in a WebView fetch without restricting the base URL. | +| UnsafeWebViewFetch.swift:109:25:109:53 | try! ... | UnsafeWebViewFetch.swift:109:30:109:53 | String(...) | UnsafeWebViewFetch.swift:109:25:109:53 | try! ... | Tainted data is used in a WebView fetch without restricting the base URL. | +| UnsafeWebViewFetch.swift:120:25:120:39 | getRemoteData(...) | UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | UnsafeWebViewFetch.swift:120:25:120:39 | getRemoteData(...) | Tainted data is used in a WebView fetch without restricting the base URL. | +| UnsafeWebViewFetch.swift:121:25:121:36 | remoteString | UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | UnsafeWebViewFetch.swift:121:25:121:36 | remoteString | Tainted data is used in a WebView fetch without restricting the base URL. | +| UnsafeWebViewFetch.swift:124:25:124:59 | ... + ... | UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | UnsafeWebViewFetch.swift:124:25:124:59 | ... + ... | Tainted data is used in a WebView fetch without restricting the base URL. | +| UnsafeWebViewFetch.swift:127:25:127:54 | StringInterpolationExpr | UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | UnsafeWebViewFetch.swift:127:25:127:54 | StringInterpolationExpr | Tainted data is used in a WebView fetch without restricting the base URL. | +| UnsafeWebViewFetch.swift:139:25:139:36 | remoteString | UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | UnsafeWebViewFetch.swift:139:25:139:36 | remoteString | Tainted data is used in a WebView fetch with a tainted base URL. | +| UnsafeWebViewFetch.swift:167:25:167:39 | getRemoteData(...) | UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | UnsafeWebViewFetch.swift:167:25:167:39 | getRemoteData(...) | Tainted data is used in a WebView fetch without restricting the base URL. | +| UnsafeWebViewFetch.swift:168:25:168:36 | remoteString | UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | UnsafeWebViewFetch.swift:168:25:168:36 | remoteString | Tainted data is used in a WebView fetch without restricting the base URL. | +| UnsafeWebViewFetch.swift:171:25:171:59 | ... + ... | UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | UnsafeWebViewFetch.swift:171:25:171:59 | ... + ... | Tainted data is used in a WebView fetch without restricting the base URL. | +| UnsafeWebViewFetch.swift:174:25:174:54 | StringInterpolationExpr | UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | UnsafeWebViewFetch.swift:174:25:174:54 | StringInterpolationExpr | Tainted data is used in a WebView fetch without restricting the base URL. | +| UnsafeWebViewFetch.swift:186:25:186:36 | remoteString | UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | UnsafeWebViewFetch.swift:186:25:186:36 | remoteString | Tainted data is used in a WebView fetch with a tainted base URL. | +| UnsafeWebViewFetch.swift:210:25:210:32 | htmlData | UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | UnsafeWebViewFetch.swift:210:25:210:32 | htmlData | Tainted data is used in a WebView fetch without restricting the base URL. | +edges +| UnsafeWebViewFetch.swift:94:10:94:37 | try ... | UnsafeWebViewFetch.swift:117:21:117:35 | getRemoteData(...) | provenance | | +| UnsafeWebViewFetch.swift:94:10:94:37 | try ... | UnsafeWebViewFetch.swift:120:25:120:39 | getRemoteData(...) | provenance | | +| UnsafeWebViewFetch.swift:94:10:94:37 | try ... | UnsafeWebViewFetch.swift:164:21:164:35 | getRemoteData(...) | provenance | | +| UnsafeWebViewFetch.swift:94:10:94:37 | try ... | UnsafeWebViewFetch.swift:167:25:167:39 | getRemoteData(...) | provenance | | +| UnsafeWebViewFetch.swift:94:10:94:37 | try ... | UnsafeWebViewFetch.swift:206:17:206:31 | getRemoteData(...) | provenance | | +| UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | UnsafeWebViewFetch.swift:94:10:94:37 | try ... | provenance | | +| UnsafeWebViewFetch.swift:103:30:103:84 | String(...) | UnsafeWebViewFetch.swift:103:25:103:84 | try! ... | provenance | | +| UnsafeWebViewFetch.swift:105:6:105:9 | data | UnsafeWebViewFetch.swift:106:25:106:28 | data | provenance | | +| UnsafeWebViewFetch.swift:105:18:105:72 | String(...) | UnsafeWebViewFetch.swift:105:6:105:9 | data | provenance | | +| UnsafeWebViewFetch.swift:109:30:109:53 | String(...) | UnsafeWebViewFetch.swift:109:25:109:53 | try! ... | provenance | | +| UnsafeWebViewFetch.swift:117:6:117:17 | remoteString | UnsafeWebViewFetch.swift:121:25:121:36 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:117:6:117:17 | remoteString | UnsafeWebViewFetch.swift:124:36:124:47 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:117:6:117:17 | remoteString | UnsafeWebViewFetch.swift:127:34:127:45 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:117:6:117:17 | remoteString | UnsafeWebViewFetch.swift:135:25:135:36 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:117:6:117:17 | remoteString | UnsafeWebViewFetch.swift:137:25:137:36 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:117:6:117:17 | remoteString | UnsafeWebViewFetch.swift:139:25:139:36 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:117:6:117:17 | remoteString | UnsafeWebViewFetch.swift:141:25:141:36 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:117:21:117:35 | getRemoteData(...) | UnsafeWebViewFetch.swift:117:6:117:17 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:124:36:124:47 | remoteString | UnsafeWebViewFetch.swift:124:25:124:59 | ... + ... | provenance | | +| UnsafeWebViewFetch.swift:127:34:127:45 | remoteString | UnsafeWebViewFetch.swift:127:25:127:54 | StringInterpolationExpr | provenance | | +| UnsafeWebViewFetch.swift:164:6:164:17 | remoteString | UnsafeWebViewFetch.swift:168:25:168:36 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:164:6:164:17 | remoteString | UnsafeWebViewFetch.swift:171:36:171:47 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:164:6:164:17 | remoteString | UnsafeWebViewFetch.swift:174:34:174:45 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:164:6:164:17 | remoteString | UnsafeWebViewFetch.swift:182:25:182:36 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:164:6:164:17 | remoteString | UnsafeWebViewFetch.swift:184:25:184:36 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:164:6:164:17 | remoteString | UnsafeWebViewFetch.swift:186:25:186:36 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:164:6:164:17 | remoteString | UnsafeWebViewFetch.swift:188:25:188:36 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:164:21:164:35 | getRemoteData(...) | UnsafeWebViewFetch.swift:164:6:164:17 | remoteString | provenance | | +| UnsafeWebViewFetch.swift:171:36:171:47 | remoteString | UnsafeWebViewFetch.swift:171:25:171:59 | ... + ... | provenance | | +| UnsafeWebViewFetch.swift:174:34:174:45 | remoteString | UnsafeWebViewFetch.swift:174:25:174:54 | StringInterpolationExpr | provenance | | +| UnsafeWebViewFetch.swift:206:6:206:13 | htmlData | UnsafeWebViewFetch.swift:210:25:210:32 | htmlData | provenance | | +| UnsafeWebViewFetch.swift:206:6:206:13 | htmlData | UnsafeWebViewFetch.swift:211:25:211:32 | htmlData | provenance | | +| UnsafeWebViewFetch.swift:206:17:206:31 | getRemoteData(...) | UnsafeWebViewFetch.swift:206:6:206:13 | htmlData | provenance | | +nodes +| UnsafeWebViewFetch.swift:94:10:94:37 | try ... | semmle.label | try ... | +| UnsafeWebViewFetch.swift:94:14:94:37 | String(...) | semmle.label | String(...) | +| UnsafeWebViewFetch.swift:103:25:103:84 | try! ... | semmle.label | try! ... | +| UnsafeWebViewFetch.swift:103:30:103:84 | String(...) | semmle.label | String(...) | +| UnsafeWebViewFetch.swift:105:6:105:9 | data | semmle.label | data | +| UnsafeWebViewFetch.swift:105:18:105:72 | String(...) | semmle.label | String(...) | +| UnsafeWebViewFetch.swift:106:25:106:28 | data | semmle.label | data | +| UnsafeWebViewFetch.swift:109:25:109:53 | try! ... | semmle.label | try! ... | +| UnsafeWebViewFetch.swift:109:30:109:53 | String(...) | semmle.label | String(...) | +| UnsafeWebViewFetch.swift:117:6:117:17 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:117:21:117:35 | getRemoteData(...) | semmle.label | getRemoteData(...) | +| UnsafeWebViewFetch.swift:120:25:120:39 | getRemoteData(...) | semmle.label | getRemoteData(...) | +| UnsafeWebViewFetch.swift:121:25:121:36 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:124:25:124:59 | ... + ... | semmle.label | ... + ... | +| UnsafeWebViewFetch.swift:124:36:124:47 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:127:25:127:54 | StringInterpolationExpr | semmle.label | StringInterpolationExpr | +| UnsafeWebViewFetch.swift:127:34:127:45 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:135:25:135:36 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:137:25:137:36 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:139:25:139:36 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:141:25:141:36 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:164:6:164:17 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:164:21:164:35 | getRemoteData(...) | semmle.label | getRemoteData(...) | +| UnsafeWebViewFetch.swift:167:25:167:39 | getRemoteData(...) | semmle.label | getRemoteData(...) | +| UnsafeWebViewFetch.swift:168:25:168:36 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:171:25:171:59 | ... + ... | semmle.label | ... + ... | +| UnsafeWebViewFetch.swift:171:36:171:47 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:174:25:174:54 | StringInterpolationExpr | semmle.label | StringInterpolationExpr | +| UnsafeWebViewFetch.swift:174:34:174:45 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:182:25:182:36 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:184:25:184:36 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:186:25:186:36 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:188:25:188:36 | remoteString | semmle.label | remoteString | +| UnsafeWebViewFetch.swift:206:6:206:13 | htmlData | semmle.label | htmlData | +| UnsafeWebViewFetch.swift:206:17:206:31 | getRemoteData(...) | semmle.label | getRemoteData(...) | +| UnsafeWebViewFetch.swift:210:25:210:32 | htmlData | semmle.label | htmlData | +| UnsafeWebViewFetch.swift:211:25:211:32 | htmlData | semmle.label | htmlData | +subpaths diff --git a/unified/ql/test/query-tests/security/CWE-079/UnsafeWebViewFetch/UnsafeWebViewFetch.qlref b/unified/ql/test/query-tests/security/CWE-079/UnsafeWebViewFetch/UnsafeWebViewFetch.qlref new file mode 100644 index 000000000000..b14b06a96d25 --- /dev/null +++ b/unified/ql/test/query-tests/security/CWE-079/UnsafeWebViewFetch/UnsafeWebViewFetch.qlref @@ -0,0 +1,3 @@ +query: queries/security/CWE-079/UnsafeWebViewFetch.ql +postprocess: + - utils/test/InlineExpectationsTestQuery.ql diff --git a/unified/ql/test/query-tests/security/CWE-079/UnsafeWebViewFetch/UnsafeWebViewFetch.swift b/unified/ql/test/query-tests/security/CWE-079/UnsafeWebViewFetch/UnsafeWebViewFetch.swift new file mode 100644 index 000000000000..c7b0ccc253bd --- /dev/null +++ b/unified/ql/test/query-tests/security/CWE-079/UnsafeWebViewFetch/UnsafeWebViewFetch.swift @@ -0,0 +1,217 @@ + +// --- stubs --- + +class NSObject +{ +} + +struct URL +{ + init?(string: String) {} + init?(string: String, relativeTo: URL?) {} +} + +extension String { + init(contentsOf: URL) throws { + var data = "" + + // ... + + self.init(data) + } +} + +class NSURLRequest : NSObject +{ + enum CachePolicy : UInt + { + case useProtocolCachePolicy + } +} + +typealias TimeInterval = Double + +class URLRequest +{ + typealias CachePolicy = NSURLRequest.CachePolicy + + init(url: URL, cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy, timeoutInterval: TimeInterval = 60.0) {} +} + +class Data +{ + init(_ elements: S) {} +} + +class WKNavigation : NSObject +{ +} + +class UIResponder : NSObject +{ +} + +class UIView : UIResponder +{ +} + +class UIWebView : UIView +{ + func loadRequest(_ request: URLRequest) {} // deprecated + + func load(_ data: Data, mimeType MIMEType: String, textEncodingName: String, baseURL: URL) {} // deprecated + + func loadHTMLString(_ string: String, baseURL: URL?) {} // deprecated +} + +class WKWebView : UIView +{ + func load(_ request: URLRequest) -> WKNavigation? { + // ... + + return WKNavigation() + } + + func load(_ data: Data, mimeType MIMEType: String, characterEncodingName: String, baseURL: URL) -> WKNavigation? { + // ... + + return WKNavigation() + } + + func loadHTMLString(_ string: String, baseURL: URL?) -> WKNavigation? { + // ... + + return WKNavigation() + } +} + +// --- tests --- + +func getRemoteData() -> String { + let url = URL(string: "http://example.com/") + do + { + return try String(contentsOf: url!) // $ Source + } catch { + return "" + } +} + +func testSimpleFlows() { + let webview = UIWebView() + + webview.loadHTMLString(try! String(contentsOf: URL(string: "http://example.com/")!), baseURL: nil) // $ Alert + + let data = try! String(contentsOf: URL(string: "http://example.com/")!) // $ Source + webview.loadHTMLString(data, baseURL: nil) // $ Alert + + let url = URL(string: "http://example.com/") + webview.loadHTMLString(try! String(contentsOf: url!), baseURL: nil) // $ Alert +} + +func testUIWebView() { + let webview = UIWebView() + + let localString = "

    Local HTML

    " + let localStringFragment = "

    Local HTML

    " + let remoteString = getRemoteData() + + webview.loadHTMLString(localString, baseURL: nil) // GOOD: the HTML data is local + webview.loadHTMLString(getRemoteData(), baseURL: nil) // $ Alert (HTML contains remote input, may access local secrets) + webview.loadHTMLString(remoteString, baseURL: nil) // $ Alert + + webview.loadHTMLString("" + localStringFragment + "", baseURL: nil) // GOOD: the HTML data is local + webview.loadHTMLString("" + remoteString + "", baseURL: nil) // $ Alert + + webview.loadHTMLString("\(localStringFragment)", baseURL: nil) // GOOD: the HTML data is local + webview.loadHTMLString("\(remoteString)", baseURL: nil) // $ Alert + + let localSafeURL = URL(string: "about:blank") + let localURL = URL(string: "http://example.com/") + let remoteURL = URL(string: remoteString) + let remoteURL2 = URL(string: "/path", relativeTo: remoteURL) + + webview.loadHTMLString(localString, baseURL: localSafeURL!) // GOOD: a safe baseURL is specified + webview.loadHTMLString(remoteString, baseURL: localSafeURL!) // GOOD: a safe baseURL is specified + webview.loadHTMLString(localString, baseURL: localURL!) // GOOD: a presumed safe baseURL is specified + webview.loadHTMLString(remoteString, baseURL: localURL!) // GOOD: a presumed safe baseURL is specified + webview.loadHTMLString(localString, baseURL: remoteURL!) // GOOD: the HTML data is local + webview.loadHTMLString(remoteString, baseURL: remoteURL!) // $ Alert + webview.loadHTMLString(localString, baseURL: remoteURL2!) // GOOD: the HTML data is local + webview.loadHTMLString(remoteString, baseURL: remoteURL2!) // $ MISSING: Alert + + let localRequest = URLRequest(url: localURL!) + let remoteRequest = URLRequest(url: remoteURL!) + + webview.loadRequest(localRequest) // GOOD: loadRequest is out of scope as it has no baseURL + webview.loadRequest(remoteRequest) // GOOD: loadRequest is out of scope as it has no baseURL + + let localData = Data(localString.utf8) + let remoteData = Data(remoteString.utf8) + webview.load(localData, mimeType: "text/html", textEncodingName: "utf-8", baseURL: localSafeURL!) // GOOD: the data is local + webview.load(remoteData, mimeType: "text/html", textEncodingName: "utf-8", baseURL: localSafeURL!) // GOOD: a safe baseURL is specified + webview.load(localData, mimeType: "text/html", textEncodingName: "utf-8", baseURL: remoteURL!) // GOOD: the HTML data is local + webview.load(remoteData, mimeType: "text/html", textEncodingName: "utf-8", baseURL: remoteURL!) // $ MISSING: Alert +} + +func testWKWebView() { + let webview = WKWebView() + // note: `WKWebView` is safer than `UIWebView` as it has better security configuration options + // and is more locked down by default. + + let localString = "

    Local HTML

    " + let localStringFragment = "

    Local HTML

    " + let remoteString = getRemoteData() + + webview.loadHTMLString(localString, baseURL: nil) // GOOD: the HTML data is local + webview.loadHTMLString(getRemoteData(), baseURL: nil) // $ Alert + webview.loadHTMLString(remoteString, baseURL: nil) // $ Alert + + webview.loadHTMLString("" + localStringFragment + "", baseURL: nil) // GOOD: the HTML data is local + webview.loadHTMLString("" + remoteString + "", baseURL: nil) // $ Alert + + webview.loadHTMLString("\(localStringFragment)", baseURL: nil) // GOOD: the HTML data is local + webview.loadHTMLString("\(remoteString)", baseURL: nil) // $ Alert + + let localSafeURL = URL(string: "about:blank") + let localURL = URL(string: "http://example.com/") + let remoteURL = URL(string: remoteString) + let remoteURL2 = URL(string: "/path", relativeTo: remoteURL) + + webview.loadHTMLString(localString, baseURL: localSafeURL!) // GOOD: a safe baseURL is specified + webview.loadHTMLString(remoteString, baseURL: localSafeURL!) // GOOD: a safe baseURL is specified + webview.loadHTMLString(localString, baseURL: localURL!) // GOOD: a presumed safe baseURL is specified + webview.loadHTMLString(remoteString, baseURL: localURL!) // GOOD: a presumed safe baseURL is specified + webview.loadHTMLString(localString, baseURL: remoteURL!) // GOOD: the HTML data is local + webview.loadHTMLString(remoteString, baseURL: remoteURL!) // $ Alert + webview.loadHTMLString(localString, baseURL: remoteURL2!) // GOOD: the HTML data is local + webview.loadHTMLString(remoteString, baseURL: remoteURL2!) // $ MISSING: Alert + + let localRequest = URLRequest(url: localURL!) + let remoteRequest = URLRequest(url: remoteURL!) + + webview.load(localRequest) // GOOD: loadRequest is out of scope as it has no baseURL + webview.load(remoteRequest) // GOOD: loadRequest is out of scope as it has no baseURL + + let localData = Data(localString.utf8) + let remoteData = Data(remoteString.utf8) + webview.load(localData, mimeType: "text/html", characterEncodingName: "utf-8", baseURL: localSafeURL!) // GOOD: the data is local + webview.load(remoteData, mimeType: "text/html", characterEncodingName: "utf-8", baseURL: localSafeURL!) // GOOD: a safe baseURL is specified + webview.load(localData, mimeType: "text/html", characterEncodingName: "utf-8", baseURL: remoteURL!) // GOOD: the HTML data is local + webview.load(remoteData, mimeType: "text/html", characterEncodingName: "utf-8", baseURL: remoteURL!) // $ MISSING: Alert +} + +func testQHelpExamples() { + let webview = UIWebView() + let htmlData = getRemoteData() + + // ... + + webview.loadHTMLString(htmlData, baseURL: nil) // $ Alert + webview.loadHTMLString(htmlData, baseURL: URL(string: "about:blank")) // GOOD +} + +testSimpleFlows() +testUIWebView() +testWKWebView() +testQHelpExamples()