Rules / Swift
SHIELD-SWIFT-006
WebView HTML injection via loadHTMLString
What it detects
loadHTMLString renders HTML built from interpolated variable data.
How to fix
Sanitize and HTML-encode user data before embedding it into loaded markup.
Vulnerable — Shield flags thisCommentWebView.swift
import WebKit
func renderComment(in webView: WKWebView, comment: String) {
webView.loadHTMLString("<html><body><p>\(comment)</p></body></html>", baseURL: nil)
}
Fixed — scans cleanCommentWebView.swift
import WebKit
func renderComment(in webView: WKWebView, comment: String) {
var safe = comment.replacingOccurrences(of: "&", with: "&")
safe = safe.replacingOccurrences(of: "<", with: "<")
safe = safe.replacingOccurrences(of: ">", with: ">")
webView.loadHTMLString("<html><body><p>" + safe + "</p></body></html>", baseURL: nil)
}
Both snippets are verified against the shipped scanner: the vulnerable one triggers SHIELD-SWIFT-006, the fixed one does not.