SKY-D257 · Security in code
Android WebView exposes app code to page JavaScript
A WebView shows web pages inside an Android app. addJavascriptInterface() hands it a Kotlin or Java object whose @JavascriptInterface methods page scripts can call. SKY-D257 reports that call when the same class also turns JavaScript on.
- Languages
- Kotlin
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
- Catalog name
- Android WebView JavaScript bridge
Why it matters
The bridge does not check which site is calling. Any page the WebView ends up on, through a link, a redirect, an injected script or a URL passed in by another app, can call the methods; here it can read the user's session token and send it anywhere.
What Skylos flags
package com.acme.help import android.annotation.SuppressLintimport android.os.Bundleimport android.webkit.JavascriptInterfaceimport android.webkit.WebViewimport androidx.appcompat.app.AppCompatActivity class HelpActivity : AppCompatActivity() { private val session by lazy { SessionStore(this) } @SuppressLint("SetJavaScriptEnabled") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val webView = WebView(this) webView.settings.javaScriptEnabled = true webView.addJavascriptInterface(AppBridge(), "App") webView.loadUrl(intent.getStringExtra("url") ?: "https://help.acme.example") setContentView(webView) } inner class AppBridge { @JavascriptInterface fun authToken(): String = session.token }}What Skylos leaves alone
package com.acme.help import android.annotation.SuppressLintimport android.os.Bundleimport android.webkit.WebViewimport androidx.appcompat.app.AppCompatActivity class HelpActivity : AppCompatActivity() { @SuppressLint("SetJavaScriptEnabled") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val webView = WebView(this) webView.settings.javaScriptEnabled = true webView.loadUrl("https://help.acme.example") setContentView(webView) }}Why Skylos leaves it alone
JavaScript is on, but no object is handed to the page, so page scripts can only do what they could do in a browser. JavaScript alone is not reported.
How Skylos decides
- Runs with
--dangeron Kotlin (.kt) files, after comments and string contents are blanked out. - Finds every
addJavascriptInterface(call, and everyjavaScriptEnabled = trueorsetJavaScriptEnabled(true). - Reports HIGH on the
addJavascriptInterface(line when JavaScript is turned on inside the same innermost class, or both sit outside any class in the same file. Which object is passed and which URL is loaded are not read.
How to fix it
Replace the bridge with WebViewCompat.addWebMessageListener from AndroidX WebKit. It takes a list of allowed origins, so only your own site can send messages, and the app decides what to answer.
package com.acme.help import android.annotation.SuppressLintimport android.os.BundleRemoved: import android.webkit.JavascriptInterfaceimport android.webkit.WebViewimport androidx.appcompat.app.AppCompatActivityAdded: import androidx.webkit.WebViewCompatAdded: import androidx.webkit.WebViewFeature class HelpActivity : AppCompatActivity() { private val session by lazy { SessionStore(this) } @SuppressLint("SetJavaScriptEnabled") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val webView = WebView(this) webView.settings.javaScriptEnabled = trueRemoved: webView.addJavascriptInterface(AppBridge(), "App")Removed: webView.loadUrl(intent.getStringExtra("url") ?: "https://help.acme.example")Added: if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {Added: WebViewCompat.addWebMessageListener(Added: webView,Added: "App",Added: setOf("https://help.acme.example"),Added: ) { _, message, _, _, reply ->Added: if (message.data == "authToken") reply.postMessage(session.token)Added: }Added: }Added: webView.loadUrl("https://help.acme.example") setContentView(webView) }Removed: Removed: inner class AppBridge {Removed: @JavascriptInterfaceRemoved: fun authToken(): String = session.tokenRemoved: }}When it can be wrong
- A WebView that only ever shows pages bundled in the app, with navigation to other sites blocked, is still reported.
- Missed: JavaScript turned on in one class (for example a shared WebView factory) and the bridge added in another, and Java source files, which this rule does not read.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Mark the
addJavascriptInterface(line when the WebView can only load your own bundled pages, and say how that is enforced.Kotlin webView.addJavascriptInterface(AppBridge(), "App") // skylos: ignore[SKY-D257] bundledskylos baseline ., then--baseline, accepts existing bridges by file and line.[tool.skylos] ignore = ["SKY-D257"]turns the rule off for the whole project.