Building an HTML document from text that the user typed (for example the text of a UITextField, or the replacement string passed to a UITextFieldDelegate) and loading it with UIWebView.loadHTMLString, WKWebView.loadHTMLString or the load(_:mimeType:...:baseURL:) methods lets the input inject markup and script. If the baseURL is nil or also comes from the input, injected script may also be able to read local data.
The standard swift/unsafe-webview-fetch query only considers remote flow sources, so it does not report these cases. This query reports the same sinks when the data comes from local user input.
HTML-encode user input before adding it to an HTML document, or set it through a safe API (for example, assign it as text content from JavaScript). Always pass a safe baseURL such as about:blank and prefer WKWebView to UIWebView.
The following code loads text typed by the user straight into a WebView without restricting the base URL:
let webText = "Welcome! \(textField.text ?? "")"
webview.loadHTMLString(webText, baseURL: nil) // BADEncode the input and use a safe base URL instead:
let webText = "Welcome! \(htmlEscape(textField.text ?? ""))"
webview.loadHTMLString(webText, baseURL: URL(string: "about:blank")) // GOOD- OWASP: Cross Site Scripting Prevention Cheat Sheet.
- Apple Developer Documentation: WKWebView.loadHTMLString(_:baseURL:).
- Common Weakness Enumeration: CWE-79.
- Common Weakness Enumeration: CWE-95.
- Common Weakness Enumeration: CWE-749.