(#) Using `setJavaScriptEnabled` !!! WARNING: Using `setJavaScriptEnabled` This is a warning. Id : `SetJavaScriptEnabled` Summary : Using `setJavaScriptEnabled` Severity : Warning Category : Security Platform : Android Vendor : Android Open Source Project Feedback : https://issuetracker.google.com/issues/new?component=192708 Since : Initial Affects : Kotlin and Java files Editing : This check runs on the fly in the IDE editor See : https://goo.gle/SetJavaScriptEnabled See : https://developer.android.com/training/articles/security-tips Implementation : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/SetJavaScriptEnabledDetector.java) Tests : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/SetJavaScriptEnabledDetectorTest.kt) Copyright Year : 2012 Your code should not invoke `setJavaScriptEnabled` if you are not sure that your app really requires JavaScript support. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text src/test/pkg/SetJavaScriptEnabled.java:14:Warning: Using setJavaScriptEnabled can introduce XSS vulnerabilities into your application, review carefully [SetJavaScriptEnabled] webView.getSettings().setJavaScriptEnabled(true); // bad ------------------------------------------------ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `src/test/pkg/SetJavaScriptEnabled.java`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers package test.pkg; import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; public class SetJavaScriptEnabled extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WebView webView = (WebView)findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled(true); // bad webView.getSettings().setJavaScriptEnabled(false); // good webView.loadUrl("file:///android_asset/www/index.html"); } // Test Suppress // Constructor: See issue 35588 @android.annotation.SuppressLint("SetJavaScriptEnabled") public void HelloWebApp() { WebView webView = (WebView)findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled(true); // bad webView.getSettings().setJavaScriptEnabled(false); // good webView.loadUrl("file:///android_asset/www/index.html"); } public static final class R { public static final class layout { public static final int main = 0x7f0a0000; } public static final class id { public static final int webView = 0x7f0a0001; } } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also visit the [source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/SetJavaScriptEnabledDetectorTest.kt) for the unit tests for this check to see additional scenarios. The above example was automatically extracted from the first unit test found for this lint check, `SetJavaScriptEnabledDetector.test`. To report a problem with this extracted sample, visit https://issuetracker.google.com/issues/new?component=192708. (##) Suppressing You can suppress false positives using one of the following mechanisms: * Using a suppression annotation like this on the enclosing element: ```kt // Kotlin @Suppress("SetJavaScriptEnabled") fun method() { setJavaScriptEnabled(...) } ``` or ```java // Java @SuppressWarnings("SetJavaScriptEnabled") void method() { setJavaScriptEnabled(...); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection SetJavaScriptEnabled problematicStatement() ``` * Using a special `lint.xml` file in the source tree which turns off the check in that folder and any sub folder. A simple file might look like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <lint> <issue id="SetJavaScriptEnabled" severity="ignore" /> </lint> ``` Instead of `ignore` you can also change the severity here, for example from `error` to `warning`. You can find additional documentation on how to filter issues by path, regular expression and so on [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html). * In Gradle projects, using the DSL syntax to configure lint. For example, you can use something like ```gradle lintOptions { disable 'SetJavaScriptEnabled' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore SetJavaScriptEnabled ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).