From 49142725c8d94aa69d4ab0967f5544b41e823402 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 31 Oct 2026 03:41:45 +0000 Subject: [PATCH 1/3] Initial plan From 64b7afa6c23279761ad01c673865f819274f0bde Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 31 Oct 2026 04:47:40 +0000 Subject: [PATCH 2/3] Add swift/custom/unsafe-webview-fetch-local-input query and tests Co-authored-by: felickz <1760475+felickz@users.noreply.github.com> --- .../UnsafeWebViewFetchLocalInput.md | 33 ++++++ .../UnsafeWebViewFetchLocalInput.ql | 28 +++++ .../UnsafeWebViewFetchLocalInput.qll | 60 ++++++++++ ...-10-31-unsafe-webview-fetch-local-input.md | 5 + languages/swift/custom/src/qlpack.yml | 5 + .../UnsafeWebViewFetchLocalInputTest.expected | 0 .../UnsafeWebViewFetchLocalInputTest.ql | 19 +++ .../UnsafeWebViewFetchLocalInput/test.swift | 109 ++++++++++++++++++ languages/swift/custom/test/qlpack.yml | 5 + 9 files changed, 264 insertions(+) create mode 100644 languages/swift/custom/src/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInput.md create mode 100644 languages/swift/custom/src/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInput.ql create mode 100644 languages/swift/custom/src/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInput.qll create mode 100644 languages/swift/custom/src/change-notes/2026-10-31-unsafe-webview-fetch-local-input.md create mode 100644 languages/swift/custom/src/qlpack.yml create mode 100644 languages/swift/custom/test/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInputTest.expected create mode 100644 languages/swift/custom/test/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInputTest.ql create mode 100644 languages/swift/custom/test/UnsafeWebViewFetchLocalInput/test.swift create mode 100644 languages/swift/custom/test/qlpack.yml diff --git a/languages/swift/custom/src/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInput.md b/languages/swift/custom/src/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInput.md new file mode 100644 index 00000000..0ef88345 --- /dev/null +++ b/languages/swift/custom/src/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInput.md @@ -0,0 +1,33 @@ +# 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: + +```swift +let webText = "Welcome! \(textField.text ?? "")" +webview.loadHTMLString(webText, baseURL: nil) // BAD +``` + +Encode the input and use a safe base URL instead: + +```swift +let webText = "Welcome! \(htmlEscape(textField.text ?? ""))" +webview.loadHTMLString(webText, baseURL: URL(string: "about:blank")) // GOOD +``` + +## References + +- OWASP: [Cross Site Scripting Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html). +- Apple Developer Documentation: [WKWebView.loadHTMLString(_:baseURL:)](https://developer.apple.com/documentation/webkit/wkwebview/1415004-loadhtmlstring). +- Common Weakness Enumeration: [CWE-79](https://cwe.mitre.org/data/definitions/79.html). +- Common Weakness Enumeration: [CWE-95](https://cwe.mitre.org/data/definitions/95.html). +- Common Weakness Enumeration: [CWE-749](https://cwe.mitre.org/data/definitions/749.html). diff --git a/languages/swift/custom/src/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInput.ql b/languages/swift/custom/src/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInput.ql new file mode 100644 index 00000000..973b9f24 --- /dev/null +++ b/languages/swift/custom/src/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInput.ql @@ -0,0 +1,28 @@ +/** + * @name Unsafe WebView fetch of local user input + * @description Loading HTML built from local user input (such as text typed into a + * `UITextField`) into a WebView without restricting the base URL may enable + * cross-site scripting or give injected script access to local data. + * @kind path-problem + * @problem.severity warning + * @security-severity 6.1 + * @precision medium + * @id swift/custom/unsafe-webview-fetch-local-input + * @tags security + * external/cwe/cwe-079 + * external/cwe/cwe-095 + * external/cwe/cwe-749 + */ + +import swift +import UnsafeWebViewFetchLocalInput +import UnsafeWebViewFetchLocalInputFlow::PathGraph + +from + UnsafeWebViewFetchLocalInputFlow::PathNode sourceNode, + UnsafeWebViewFetchLocalInputFlow::PathNode sinkNode, UnsafeWebViewFetchSink sink, string message +where + UnsafeWebViewFetchLocalInputFlow::flowPath(sourceNode, sinkNode) and + sink = sinkNode.getNode() and + unsafeWebViewFetchLocalInput(sink, message) +select sink, sourceNode, sinkNode, message diff --git a/languages/swift/custom/src/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInput.qll b/languages/swift/custom/src/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInput.qll new file mode 100644 index 00000000..50c677e6 --- /dev/null +++ b/languages/swift/custom/src/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInput.qll @@ -0,0 +1,60 @@ +/** + * Provides a taint-tracking configuration for reasoning about local user input + * (for example text typed into a `UITextField`) that is loaded into a WebView. + * + * This complements the standard `swift/unsafe-webview-fetch` query, which only + * considers remote flow sources. + */ + +import swift +import codeql.swift.dataflow.DataFlow +import codeql.swift.dataflow.TaintTracking +import codeql.swift.dataflow.FlowSources +import codeql.swift.security.UnsafeWebViewFetchExtensions + +/** + * A taint configuration from local user input to WebView fetch sinks (and + * `baseURL` arguments). + */ +module UnsafeWebViewFetchLocalInputConfig implements DataFlow::ConfigSig { + predicate isSource(DataFlow::Node node) { node instanceof LocalFlowSource } + + predicate isSink(DataFlow::Node node) { + exists(UnsafeWebViewFetchSink sink | + node = sink or + node.asExpr() = sink.getBaseUrl() + ) + } + + predicate isBarrier(DataFlow::Node barrier) { barrier instanceof UnsafeWebViewFetchBarrier } + + predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { + any(UnsafeWebViewFetchAdditionalFlowStep s).step(nodeFrom, nodeTo) + } +} + +/** + * Taint flow from local user input to WebView fetch sinks (and `baseURL` arguments). + */ +module UnsafeWebViewFetchLocalInputFlow = TaintTracking::Global; + +/** + * Holds if `sink` receives local user input and is unsafe because of its + * `baseURL` argument, as described by `message`. + */ +predicate unsafeWebViewFetchLocalInput(UnsafeWebViewFetchSink sink, string message) { + UnsafeWebViewFetchLocalInputFlow::flowTo(sink) and + ( + // no base URL + not exists(sink.getBaseUrl()) and + message = "Local user input is used in a WebView fetch." + or + // base URL is nil + sink.getBaseUrl() instanceof NilLiteralExpr and + message = "Local user input is used in a WebView fetch without restricting the base URL." + or + // base URL is also tainted + UnsafeWebViewFetchLocalInputFlow::flowToExpr(sink.getBaseUrl()) and + message = "Local user input is used in a WebView fetch with a tainted base URL." + ) +} diff --git a/languages/swift/custom/src/change-notes/2026-10-31-unsafe-webview-fetch-local-input.md b/languages/swift/custom/src/change-notes/2026-10-31-unsafe-webview-fetch-local-input.md new file mode 100644 index 00000000..f4f236ab --- /dev/null +++ b/languages/swift/custom/src/change-notes/2026-10-31-unsafe-webview-fetch-local-input.md @@ -0,0 +1,5 @@ +--- +category: newQuery +--- + +- Added a custom `swift/custom/unsafe-webview-fetch-local-input` query that complements `swift/unsafe-webview-fetch` by reporting local user input (such as `UITextField.text` or `UITextFieldDelegate` replacement strings) that is loaded into a `UIWebView` or `WKWebView` without a safe base URL. The standard query only considers remote flow sources, and Swift has no threat-model setting to enable local sources. diff --git a/languages/swift/custom/src/qlpack.yml b/languages/swift/custom/src/qlpack.yml new file mode 100644 index 00000000..397f59b7 --- /dev/null +++ b/languages/swift/custom/src/qlpack.yml @@ -0,0 +1,5 @@ +name: languages-swift-custom-src +version: 0.0.2 +library: false +dependencies: + codeql/swift-all: "*" diff --git a/languages/swift/custom/test/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInputTest.expected b/languages/swift/custom/test/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInputTest.expected new file mode 100644 index 00000000..e69de29b diff --git a/languages/swift/custom/test/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInputTest.ql b/languages/swift/custom/test/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInputTest.ql new file mode 100644 index 00000000..1b454cca --- /dev/null +++ b/languages/swift/custom/test/UnsafeWebViewFetchLocalInput/UnsafeWebViewFetchLocalInputTest.ql @@ -0,0 +1,19 @@ +import swift +import UnsafeWebViewFetchLocalInput.UnsafeWebViewFetchLocalInput +import utils.test.InlineExpectationsTest + +module UnsafeWebViewFetchLocalInputTest implements TestSig { + string getARelevantTag() { result = "unsafeWebViewFetchLocalInput" } + + predicate hasActualResult(Location location, string element, string tag, string value) { + exists(UnsafeWebViewFetchSink sink | + unsafeWebViewFetchLocalInput(sink, _) and + location = sink.getLocation() and + element = sink.toString() and + tag = "unsafeWebViewFetchLocalInput" and + value = "" + ) + } +} + +import MakeTest diff --git a/languages/swift/custom/test/UnsafeWebViewFetchLocalInput/test.swift b/languages/swift/custom/test/UnsafeWebViewFetchLocalInput/test.swift new file mode 100644 index 00000000..6ed372f9 --- /dev/null +++ b/languages/swift/custom/test/UnsafeWebViewFetchLocalInput/test.swift @@ -0,0 +1,109 @@ +// --- stubs --- + +class NSObject { } + +struct _NSRange { } + +typealias NSRange = _NSRange + +struct URL { + init?(string: String) {} +} + +extension String { + init(contentsOf: URL) throws { + self.init("") + } +} + +class WKNavigation : NSObject { } + +class UIResponder : NSObject { } + +class UIView : UIResponder { } + +class UIControl : UIView { } + +class UIViewController : UIResponder { } + +class UITextField : UIControl { + var text: String? { + get { nil } + set { } + } + + var placeholder: String? { + get { nil } + set { } + } +} + +protocol UITextFieldDelegate { + func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool +} + +class UIWebView : UIView { + func loadHTMLString(_ string: String, baseURL: URL?) {} // deprecated +} + +class WKWebView : UIView { + func loadHTMLString(_ string: String, baseURL: URL?) -> WKNavigation? { + return WKNavigation() + } +} + +// --- tests --- + +// Reduced from the iGoat-Swift XSS exercise. +class CrossSiteScriptingExerciseVC : UIViewController { + var webview: UIWebView! + var textField: UITextField! + + func loadItemPressed() { + let webText = "Welcome to XSS Exercise !!! \n Here is UIWebView ! \(textField.text ?? "")" + webview.loadHTMLString(webText, baseURL: nil) // $ unsafeWebViewFetchLocalInput + } +} + +class WKWebViewExerciseVC : UIViewController { + var webview: WKWebView! + var textField: UITextField! + + func concatenated() { + let html = "" + (textField.text ?? "") + "" + _ = webview.loadHTMLString(html, baseURL: nil) // $ unsafeWebViewFetchLocalInput + } + + func taintedBaseUrl() { + let text = textField.text ?? "" + _ = webview.loadHTMLString("

\(text)

", baseURL: URL(string: text)) // $ unsafeWebViewFetchLocalInput + } + + func safeBaseUrl() { + let text = textField.text ?? "" + _ = webview.loadHTMLString("

\(text)

", baseURL: URL(string: "about:blank")) // GOOD: a safe baseURL is specified + } + + func constantHtml() { + _ = webview.loadHTMLString("

Hello

", baseURL: nil) // GOOD: the HTML is constant + } + + func placeholderIsNotUserInput() { + let html = "

\(textField.placeholder ?? "")

" + _ = webview.loadHTMLString(html, baseURL: nil) // GOOD: the placeholder is set by the app, not typed by the user + } + + func remoteInputIsCoveredByStandardQuery() { + let html = try! String(contentsOf: URL(string: "http://example.com/")!) + _ = webview.loadHTMLString(html, baseURL: nil) // GOOD: remote input is reported by swift/unsafe-webview-fetch instead + } +} + +class MyTextFieldDelegate : UITextFieldDelegate { + var webview: UIWebView! + + func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { + webview.loadHTMLString("

\(string)

", baseURL: nil) // $ unsafeWebViewFetchLocalInput + return true + } +} diff --git a/languages/swift/custom/test/qlpack.yml b/languages/swift/custom/test/qlpack.yml new file mode 100644 index 00000000..8f8e1af6 --- /dev/null +++ b/languages/swift/custom/test/qlpack.yml @@ -0,0 +1,5 @@ +name: languages-swift-custom-test +version: 0.0.2 +dependencies: + languages-swift-custom-src: ${workspace} +extractor: swift From 1606dcd22cc6a58aad19c06b55089231e8aaf16e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 2 Nov 2026 20:43:13 +0000 Subject: [PATCH 3/3] Allow Swift pack installation Co-authored-by: felickz <1760475+felickz@users.noreply.github.com> --- scripts/install-codeql-packs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/install-codeql-packs.sh b/scripts/install-codeql-packs.sh index 03481a93..ba4d2aa5 100755 --- a/scripts/install-codeql-packs.sh +++ b/scripts/install-codeql-packs.sh @@ -13,7 +13,7 @@ Install CodeQL packs for all packs discovered in the CodeQL workspace. OPTIONS: --language Install packs only for the specified language Valid values: actions, cpp, csharp, go, java, javascript, - python, ruby + python, ruby, swift -h, --help Show this help message By default, the script installs packs for all languages in the workspace. @@ -44,7 +44,7 @@ while [[ $# -gt 0 ]]; do done ## Validate language if provided -VALID_LANGUAGES=("actions" "cpp" "csharp" "go" "java" "javascript" "python" "ruby") +VALID_LANGUAGES=("actions" "cpp" "csharp" "go" "java" "javascript" "python" "ruby" "swift") if [[ -n "${LANGUAGE}" ]]; then LANGUAGE_VALID=false for valid_lang in "${VALID_LANGUAGES[@]}"; do