Skip to content
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

Java: Query for detecting enabling Javascript in Android WebSettings #11238

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

egregius313
Copy link
Contributor

@egregius313 egregius313 commented Nov 12, 2022

Add a query for detecting the enabling of JavaScript execution in Android WebViews.

@egregius313 egregius313 requested a review from a team as a code owner Nov 12, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Nov 12, 2022

QHelp previews:

java/ql/src/Security/CWE/CWE-079/AndroidWebViewSettingsEnabledJavaScript.qhelp

Android WebView JavaScript settings

Enabling JavaScript in an Android WebView allows for the running of JavaScript code in the context of the running application. This opens the possibility for cross-site scripting if the attacker can inject arbitrary JavaScript.

For example, if your application's WebView allows for visitng web pages which you do not trust, it is possible for an attacker to lead the user to a page which loads malicious JavaScript.

You can enable or disbale Javascript execution using the setJavaScriptEnabled method of the settings of a WebView.

Recommendation

If Javascript does not need to be enabled, call setJavaScriptEnabled(false) on the settings of the WebView.

If JavaScript is necessary, only load content from trusted servers using encrypted channels, such as HTTPS with certificate verification.

Example

In the following (bad) example, a WebView has JavaScript enabled in its settings.

WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);

In the following (good) example, a WebView explicitly disallows JavaScript execution.

WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(false);

References

Copy link
Contributor

@atorralba atorralba left a comment

I added some comments. Also, this will need tests.

<p>
Enabling JavaScript in an Android WebView allows for the running of JavaScript
code in the context of the running application. This opens the possibility for a
man-in-the-middle attack, where the attacker can inject arbitrary JavaScript.
Copy link
Contributor

@atorralba atorralba Nov 14, 2022

Choose a reason for hiding this comment

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

The attack vector isn't only a MitM attack: if the application loads untrusted content in the WebView, attackers could provide malicious JavaScript to be executed as well.

</overview>

<recommendation>
<p>If Javascript does not need to be enabled, call <code>setJavaScriptEnabled(false)</code> on the settings of the webview.</p>
Copy link
Contributor

@atorralba atorralba Nov 14, 2022

Choose a reason for hiding this comment

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

Maybe mention that this is false by default, and thus it would also suffice to not explicitly enable it?

egregius313 and others added 4 commits Nov 14, 2022
Typos and formatting changes.

Co-authored-by: Tony Torralba <atorralba@users.noreply.github.com>
Co-authored-by: Tony Torralba <atorralba@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants