Skip to content

Unified: Port UnsafeWebViewFetch query - #23217

Open
geoffw0 wants to merge 15 commits into
github:mainfrom
geoffw0:unifiedwebview
Open

geoffw0 wants to merge 15 commits into
github:mainfrom
geoffw0:unifiedwebview

Conversation

@geoffw0

@geoffw0 geoffw0 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Port the "Unsafe WebView fetch" query to unified, as unified/swift/unsafe-webview-fetch. Developed with Copilot + the porting skill + GPT-6.1 Sol High Effort. Some cleanup afterwards - in particular the way the original query handled baseURL wasn't great, it's considerably better now IMO. The four MISSING results in the test have been diagnosed and written up as issues (not fixed here).

DCA run to follow...

geoffw0 and others added 11 commits November 23, 2026 18:02
Create a byte-for-byte baseline from original Swift for the unified Swift port, including query help and samples. Adaptations follow separately.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy the data flow configuration and query-specific models from the original Swift libraries without API or behavior adaptations. Keep source paths for provenance; the compiling port follows separately.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use unified taint tracking and declarative UIWebView/WKWebView sink models. Preserve checks for nil, absent, and tainted base URLs, and point the inline-expectation test at the unified query.

The focused test passes with 12 result rows and no inline expectation failures. Retain six tainted-base-URL cases as MISSING for follow-up; this commit does not repair shared data flow or extraction.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use node-based flowTo instead of flowToExpr, whose unified exprNode adapter is not yet implemented. This restores the UIWebView and WKWebView direct tainted-base-URL cases without changing shared data flow.

Preserve all 12 baseline result rows and add two detections. Four relative-URL and Data cases remain explicitly MISSING. Both the focused test and the related JavaScript-evaluation test pass without learning.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Explain that about:blank avoids local relative-URL resolution rather than promising to block absolute remote URLs. Keep navigation and content restrictions distinct from setting a trusted base URL.

Validate the help XML and local samples. The external Web View XSS reference was checked with the restricted link-checking agent and remains unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ore readable and cleans up observeDiffInformedIncrementalMode).
@geoffw0 geoffw0 added no-change-note-required This PR does not need a change note Unified labels Nov 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

QHelp previews:

unified/ql/src/queries/security/CWE-079/UnsafeWebViewFetch.qhelp

Unsafe WebView fetch

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.

Recommendation

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.

Example

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.

let webview = UIWebView()

...

webview.loadHTMLString(htmlData, baseURL: nil) // BAD

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.

let webview = UIWebView()

...

webview.loadHTMLString(htmlData, baseURL: URL(string: "about:blank")) // GOOD

References

@geoffw0

geoffw0 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

DCA LGTM, and there are no semantic merge conflicts with the model format change in this PR. So ready for review.

@geoffw0
geoffw0 marked this pull request as ready for review November 26, 2026 16:04
@geoffw0
geoffw0 requested review from a team as code owners November 26, 2026 16:04
Copilot AI adaptive review requested due to automatic review settings November 26, 2026 16:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved findings show false positives from overly broad source and receiver matching, plus missed unsafe cases involving nil aliases, local-file base URLs, and diff-informed filtering.

5 open issues
What changed in this PR

Ports Swift’s Unsafe WebView fetch query to unified analysis as unified/swift/unsafe-webview-fetch, with separate taint analysis for HTML content and base URLs.

Changes:

  • Add the query, WebView sink models, and base-URL sink-kind validation.
  • Add query help with safe and unsafe examples.
  • Add regression fixtures and expected paths, documenting four known missing alerts.
File Description
unified/​ql/​test/​query-tests/​security/​CWE-079/​UnsafeWebViewFetch/​UnsafeWebViewFetch.swift Tests safe and unsafe UIWebView and WKWebView loads.
unified/​ql/​test/​query-tests/​security/​CWE-079/​UnsafeWebViewFetch/​UnsafeWebViewFetch.qlref Connects the query to inline-expectation checking.
unified/​ql/​test/​query-tests/​security/​CWE-079/​UnsafeWebViewFetch/​UnsafeWebViewFetch.expected Records expected alerts and flow paths.
unified/​ql/​src/​queries/​security/​CWE-079/​UnsafeWebViewFetchGood.swift Demonstrates an about:blank base URL.
unified/​ql/​src/​queries/​security/​CWE-079/​UnsafeWebViewFetchBad.swift Demonstrates an unsafe nil base URL.
unified/​ql/​src/​queries/​security/​CWE-079/​UnsafeWebViewFetch.ql Implements the unified query and base-URL analysis.
unified/​ql/​src/​queries/​security/​CWE-079/​UnsafeWebViewFetch.qhelp Explains risks, recommendations, and mitigation limits.
unified/​ql/​lib/​ext/​webview.model.json Models WebView content and base-URL arguments as sinks.
shared/​mad/​codeql/​mad/​ModelValidation.qll Accepts the new webview-baseurl sink kind.

Review effort: Max (adaptive)

Comment thread unified/ql/lib/ext/webview.model.json Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation no-change-note-required This PR does not need a change note Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants