Skip to content

Latest commit

 

History

History
33 lines (22 loc) · 1.92 KB

UnsafeWebViewFetchLocalInput.md

File metadata and controls

33 lines (22 loc) · 1.92 KB

Unsafe WebView fetch of local user input

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.

Recommendation

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.

Example

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) // BAD

Encode the input and use a safe base URL instead:

let webText = "Welcome! \(htmlEscape(textField.text ?? ""))"
webview.loadHTMLString(webText, baseURL: URL(string: "about:blank")) // GOOD

References