Repository navigation
Unified: Add Unsafe JS eval / JavaScript injection query #22998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
geoffw0
wants to merge
6
commits into
github:main
Choose a base branch
from
geoffw0:unifiedjseval
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e6bb963
unified: copy unsafe JavaScript eval query unchanged
geoffw0 9a7bfa0
unified: inline unsafe JavaScript eval configuration
geoffw0 3ca865e
unified: port unsafe JavaScript eval query
geoffw0 d41e91b
unified: update unsafe JavaScript eval help link
geoffw0 5eb5876
Merge remote-tracking branch 'upstream/main' into unifiedjseval
geoffw0 bfa0e85
unified: Accept consistency failures in the test.
geoffw0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
unified/ql/src/queries/security/CWE-094/UnsafeJsEval.qhelp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
| <overview> | ||
| <p>Evaluating JavaScript that contains a substring from a remote origin may lead to remote code execution. Code written by an attacker can execute unauthorized actions, including exfiltration of local data through a third party web service.</p> | ||
|
|
||
| </overview> | ||
| <recommendation> | ||
|
|
||
| <p>When loading JavaScript into a web view, evaluate only known, locally-defined source code. If part of the input comes from a remote source, do not inject it into the JavaScript code to be evaluated. Instead, send it to the web view as data using an API such as <code>WKWebView.callAsyncJavaScript</code> with the <code>arguments</code> dictionary to pass remote data objects.</p> | ||
|
|
||
| </recommendation> | ||
| <example> | ||
|
|
||
| <p>In the following (bad) example, a call to <code>WKWebView.evaluateJavaScript</code> evaluates JavaScript source code that is tainted with remote data, potentially introducing a code injection vulnerability.</p> | ||
|
|
||
| <sample src="UnsafeJsEvalBad.swift" /> | ||
|
|
||
| <p>In the following (good) example, we sanitize the remote data by passing it using the <code>arguments</code> dictionary of <code>WKWebView.callAsyncJavaScript</code>. This ensures that untrusted data cannot be evaluated as JavaScript source code.</p> | ||
|
|
||
| <sample src="UnsafeJsEvalGood.swift" /> | ||
|
|
||
| </example> | ||
| <references> | ||
|
|
||
| <li> | ||
| Apple Developer Documentation: <a href="https://developer.apple.com/documentation/webkit/wkwebview/callasyncjavascript(_:arguments:in:contentworld:)">WKWebView.callAsyncJavaScript(_:arguments:in:contentWorld:)</a> | ||
| </li> | ||
|
|
||
| </references> | ||
| </qhelp> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /** | ||
| * @name JavaScript Injection | ||
| * @description Evaluating JavaScript code containing a substring from a remote source may lead to remote code execution. | ||
| * @kind path-problem | ||
| * @problem.severity warning | ||
| * @security-severity 9.3 | ||
| * @precision medium | ||
| * @id unified/swift/unsafe-js-eval | ||
| * @tags security | ||
| * external/cwe/cwe-094 | ||
| * external/cwe/cwe-095 | ||
| * external/cwe/cwe-749 | ||
| */ | ||
|
|
||
| import unified | ||
|
|
||
| /** | ||
| * A taint configuration from taint sources to sinks for this query. | ||
| */ | ||
| module UnsafeJsEvalConfig implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node node) { Models::isSource(node, _) } | ||
|
geoffw0 marked this conversation as resolved.
Resolved
|
||
|
|
||
| predicate isSink(DataFlow::Node node) { Models::isSink(node, "code-injection") } | ||
|
|
||
| predicate isBarrier(DataFlow::Node node) { | ||
| // TODO: Port the numeric-type barrier from original Swift's UnsafeJsEvalExtensions.qll. | ||
| none() | ||
| } | ||
|
|
||
| predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { | ||
| // TODO: Port the base-address flow step from original Swift's UnsafeJsEvalExtensions.qll. | ||
| none() | ||
| } | ||
|
|
||
| predicate observeDiffInformedIncrementalMode() { any() } | ||
| } | ||
|
|
||
| /** | ||
| * Detect taint flow of taint sources to sinks for this query. | ||
| */ | ||
| module UnsafeJsEvalFlow = TaintTracking::Global<UnsafeJsEvalConfig>; | ||
|
|
||
| import UnsafeJsEvalFlow::PathGraph | ||
|
|
||
| from UnsafeJsEvalFlow::PathNode source, UnsafeJsEvalFlow::PathNode sink | ||
| where UnsafeJsEvalFlow::flowPath(source, sink) | ||
| select sink.getNode(), source, sink, "Evaluation of uncontrolled JavaScript from a remote source." | ||
6 changes: 6 additions & 0 deletions
6
unified/ql/src/queries/security/CWE-094/UnsafeJsEvalBad.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| let webview: WKWebView | ||
| let remoteData = try String(contentsOf: URL(string: "http://example.com/evil.json")!) | ||
|
|
||
| ... | ||
|
|
||
| _ = try await webview.evaluateJavaScript("console.log(" + remoteData + ")") // BAD |
10 changes: 10 additions & 0 deletions
10
unified/ql/src/queries/security/CWE-094/UnsafeJsEvalGood.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| let webview: WKWebView | ||
| let remoteData = try String(contentsOf: URL(string: "http://example.com/evil.json")!) | ||
|
|
||
| ... | ||
|
|
||
| _ = try await webview.callAsyncJavaScript( | ||
| "console.log(data)", | ||
| arguments: ["data": remoteData], // GOOD | ||
| contentWorld: .page | ||
| ) |
5 changes: 5 additions & 0 deletions
5
...l/test/query-tests/security/CWE-094/UnsafeJsEval/CONSISTENCY/DataFlowConsistency.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| uniqueParameterNodeAtPosition | ||
| | UnsafeJsEval.swift:31:2:38:2 | func WKWebView.evaluateJavaScript | in: | UnsafeJsEval.swift:33:6:33:10 | frame | Parameters with overlapping positions. | | ||
| | UnsafeJsEval.swift:31:2:38:2 | func WKWebView.evaluateJavaScript | in: | UnsafeJsEval.swift:34:6:34:17 | contentWorld | Parameters with overlapping positions. | | ||
| | UnsafeJsEval.swift:46:2:54:2 | func WKWebView.callAsyncJavaScript | in: | UnsafeJsEval.swift:49:6:49:10 | frame | Parameters with overlapping positions. | | ||
| | UnsafeJsEval.swift:46:2:54:2 | func WKWebView.callAsyncJavaScript | in: | UnsafeJsEval.swift:50:6:50:17 | contentWorld | Parameters with overlapping positions. | |
61 changes: 61 additions & 0 deletions
61
unified/ql/test/query-tests/security/CWE-094/UnsafeJsEval/UnsafeJsEval.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #select | ||
| | UnsafeJsEval.swift:266:22:266:107 | WKUserScript(...) | UnsafeJsEval.swift:204:12:204:66 | String(...) | UnsafeJsEval.swift:266:22:266:107 | WKUserScript(...) | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| | UnsafeJsEval.swift:266:22:266:107 | WKUserScript(...) | UnsafeJsEval.swift:205:12:205:35 | String(...) | UnsafeJsEval.swift:266:22:266:107 | WKUserScript(...) | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| | UnsafeJsEval.swift:266:22:266:107 | WKUserScript(...) | UnsafeJsEval.swift:208:30:208:53 | String(...) | UnsafeJsEval.swift:266:22:266:107 | WKUserScript(...) | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| | UnsafeJsEval.swift:269:22:269:124 | WKUserScript(...) | UnsafeJsEval.swift:204:12:204:66 | String(...) | UnsafeJsEval.swift:269:22:269:124 | WKUserScript(...) | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| | UnsafeJsEval.swift:269:22:269:124 | WKUserScript(...) | UnsafeJsEval.swift:205:12:205:35 | String(...) | UnsafeJsEval.swift:269:22:269:124 | WKUserScript(...) | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| | UnsafeJsEval.swift:269:22:269:124 | WKUserScript(...) | UnsafeJsEval.swift:208:30:208:53 | String(...) | UnsafeJsEval.swift:269:22:269:124 | WKUserScript(...) | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| | UnsafeJsEval.swift:277:26:277:31 | string | UnsafeJsEval.swift:204:12:204:66 | String(...) | UnsafeJsEval.swift:277:26:277:31 | string | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| | UnsafeJsEval.swift:277:26:277:31 | string | UnsafeJsEval.swift:205:12:205:35 | String(...) | UnsafeJsEval.swift:277:26:277:31 | string | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| | UnsafeJsEval.swift:277:26:277:31 | string | UnsafeJsEval.swift:208:30:208:53 | String(...) | UnsafeJsEval.swift:277:26:277:31 | string | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| | UnsafeJsEval.swift:280:26:280:31 | string | UnsafeJsEval.swift:204:12:204:66 | String(...) | UnsafeJsEval.swift:280:26:280:31 | string | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| | UnsafeJsEval.swift:280:26:280:31 | string | UnsafeJsEval.swift:205:12:205:35 | String(...) | UnsafeJsEval.swift:280:26:280:31 | string | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| | UnsafeJsEval.swift:280:26:280:31 | string | UnsafeJsEval.swift:208:30:208:53 | String(...) | UnsafeJsEval.swift:280:26:280:31 | string | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| | UnsafeJsEval.swift:320:44:320:76 | ... + ... | UnsafeJsEval.swift:318:24:318:87 | String(...) | UnsafeJsEval.swift:320:44:320:76 | ... + ... | Evaluation of uncontrolled JavaScript from a remote source. | | ||
| edges | ||
| | UnsafeJsEval.swift:204:7:204:66 | try! ... | UnsafeJsEval.swift:265:13:265:18 | string | provenance | | | ||
| | UnsafeJsEval.swift:204:7:204:66 | try! ... | UnsafeJsEval.swift:268:13:268:18 | string | provenance | | | ||
| | UnsafeJsEval.swift:204:7:204:66 | try! ... | UnsafeJsEval.swift:276:13:276:18 | string | provenance | | | ||
| | UnsafeJsEval.swift:204:7:204:66 | try! ... | UnsafeJsEval.swift:279:13:279:18 | string | provenance | | | ||
| | UnsafeJsEval.swift:204:12:204:66 | String(...) | UnsafeJsEval.swift:204:7:204:66 | try! ... | provenance | | | ||
| | UnsafeJsEval.swift:205:7:205:35 | try! ... | UnsafeJsEval.swift:265:13:265:18 | string | provenance | | | ||
| | UnsafeJsEval.swift:205:7:205:35 | try! ... | UnsafeJsEval.swift:268:13:268:18 | string | provenance | | | ||
| | UnsafeJsEval.swift:205:7:205:35 | try! ... | UnsafeJsEval.swift:276:13:276:18 | string | provenance | | | ||
| | UnsafeJsEval.swift:205:7:205:35 | try! ... | UnsafeJsEval.swift:279:13:279:18 | string | provenance | | | ||
| | UnsafeJsEval.swift:205:12:205:35 | String(...) | UnsafeJsEval.swift:205:7:205:35 | try! ... | provenance | | | ||
| | UnsafeJsEval.swift:208:7:208:60 | ... + ... | UnsafeJsEval.swift:265:13:265:18 | string | provenance | | | ||
| | UnsafeJsEval.swift:208:7:208:60 | ... + ... | UnsafeJsEval.swift:268:13:268:18 | string | provenance | | | ||
| | UnsafeJsEval.swift:208:7:208:60 | ... + ... | UnsafeJsEval.swift:276:13:276:18 | string | provenance | | | ||
| | UnsafeJsEval.swift:208:7:208:60 | ... + ... | UnsafeJsEval.swift:279:13:279:18 | string | provenance | | | ||
| | UnsafeJsEval.swift:208:30:208:53 | String(...) | UnsafeJsEval.swift:208:7:208:60 | ... + ... | provenance | | | ||
| | UnsafeJsEval.swift:265:13:265:18 | string | UnsafeJsEval.swift:266:43:266:48 | string | provenance | | | ||
| | UnsafeJsEval.swift:266:43:266:48 | string | UnsafeJsEval.swift:266:22:266:107 | WKUserScript(...) | provenance | | | ||
| | UnsafeJsEval.swift:268:13:268:18 | string | UnsafeJsEval.swift:269:43:269:48 | string | provenance | | | ||
| | UnsafeJsEval.swift:269:43:269:48 | string | UnsafeJsEval.swift:269:22:269:124 | WKUserScript(...) | provenance | | | ||
| | UnsafeJsEval.swift:276:13:276:18 | string | UnsafeJsEval.swift:277:26:277:31 | string | provenance | | | ||
| | UnsafeJsEval.swift:279:13:279:18 | string | UnsafeJsEval.swift:280:26:280:31 | string | provenance | | | ||
| | UnsafeJsEval.swift:318:7:318:16 | remoteData | UnsafeJsEval.swift:320:61:320:70 | remoteData | provenance | | | ||
| | UnsafeJsEval.swift:318:24:318:87 | String(...) | UnsafeJsEval.swift:318:7:318:16 | remoteData | provenance | | | ||
| | UnsafeJsEval.swift:320:61:320:70 | remoteData | UnsafeJsEval.swift:320:44:320:76 | ... + ... | provenance | | | ||
| nodes | ||
| | UnsafeJsEval.swift:204:7:204:66 | try! ... | semmle.label | try! ... | | ||
| | UnsafeJsEval.swift:204:12:204:66 | String(...) | semmle.label | String(...) | | ||
| | UnsafeJsEval.swift:205:7:205:35 | try! ... | semmle.label | try! ... | | ||
| | UnsafeJsEval.swift:205:12:205:35 | String(...) | semmle.label | String(...) | | ||
| | UnsafeJsEval.swift:208:7:208:60 | ... + ... | semmle.label | ... + ... | | ||
| | UnsafeJsEval.swift:208:30:208:53 | String(...) | semmle.label | String(...) | | ||
| | UnsafeJsEval.swift:265:13:265:18 | string | semmle.label | string | | ||
| | UnsafeJsEval.swift:266:22:266:107 | WKUserScript(...) | semmle.label | WKUserScript(...) | | ||
| | UnsafeJsEval.swift:266:43:266:48 | string | semmle.label | string | | ||
| | UnsafeJsEval.swift:268:13:268:18 | string | semmle.label | string | | ||
| | UnsafeJsEval.swift:269:22:269:124 | WKUserScript(...) | semmle.label | WKUserScript(...) | | ||
| | UnsafeJsEval.swift:269:43:269:48 | string | semmle.label | string | | ||
| | UnsafeJsEval.swift:276:13:276:18 | string | semmle.label | string | | ||
| | UnsafeJsEval.swift:277:26:277:31 | string | semmle.label | string | | ||
| | UnsafeJsEval.swift:279:13:279:18 | string | semmle.label | string | | ||
| | UnsafeJsEval.swift:280:26:280:31 | string | semmle.label | string | | ||
| | UnsafeJsEval.swift:318:7:318:16 | remoteData | semmle.label | remoteData | | ||
| | UnsafeJsEval.swift:318:24:318:87 | String(...) | semmle.label | String(...) | | ||
| | UnsafeJsEval.swift:320:44:320:76 | ... + ... | semmle.label | ... + ... | | ||
| | UnsafeJsEval.swift:320:61:320:70 | remoteData | semmle.label | remoteData | | ||
| subpaths |
3 changes: 3 additions & 0 deletions
3
unified/ql/test/query-tests/security/CWE-094/UnsafeJsEval/UnsafeJsEval.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| query: queries/security/CWE-094/UnsafeJsEval.ql | ||
| postprocess: | ||
| - utils/test/InlineExpectationsTestQuery.ql |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.