Skip to main content

SKY-D258 · Security in code

Android WebView lets file pages read other files and sites

MediumCWE-200

A WebView can open local HTML through file:// URLs. The settings allowFileAccessFromFileURLs and allowUniversalAccessFromFileURLs let scripts in such a page also read other local files, or any website. SKY-D258 reports a Kotlin line that sets either of them to true.

Languages
Kotlin
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Catalog name
Android WebView file URL access
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

If an opened file contains a script an attacker controls, for example a downloaded report or an attachment, that script can read the app's private files, such as saved login tokens, and send them to the attacker's server.

What Skylos flags

app/src/main/java/com/acme/reports/ReportActivity.kt · KotlinReported on line 13
package com.acme.reports import android.os.Bundleimport android.webkit.WebViewimport androidx.appcompat.app.AppCompatActivityimport java.io.File class ReportActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        val webView = WebView(this)        webView.settings.allowFileAccess = true        webView.settings.allowUniversalAccessFromFileURLs = true        val report = File(filesDir, "reports/latest.html")        webView.loadUrl("file://" + report.absolutePath)        setContentView(webView)    }}

What Skylos leaves alone

app/src/main/java/com/acme/reports/ReportActivity.kt · Kotlin
package com.acme.reports import android.os.Bundleimport android.webkit.WebViewimport androidx.appcompat.app.AppCompatActivityimport java.io.File class ReportActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        val webView = WebView(this)        webView.settings.allowFileAccess = true        webView.settings.allowFileAccessFromFileURLs = false        webView.settings.allowUniversalAccessFromFileURLs = false        val report = File(filesDir, "reports/latest.html")        webView.loadUrl("file://" + report.absolutePath)        setContentView(webView)    }}

Why Skylos leaves it alone

allowFileAccess only lets the WebView open the report file itself; with the two cross-access settings false, scripts in it cannot read other files or other sites. Only true values of those two settings are reported.

How Skylos decides

  1. Runs with --danger on Kotlin (.kt) files, after comments and string contents are blanked out.
  2. Reports MEDIUM on each line that sets allowFileAccessFromFileURLs or allowUniversalAccessFromFileURLs to the literal true, either as a property (= true) or with the setter (setAllowUniversalAccessFromFileURLs(true)).
  3. Nothing else is read: a value held in a variable, allowFileAccess, allowContentAccess and the URL the WebView loads are not checked.

How to fix it

Leave both settings at their default, false, and turn off allowFileAccess too. Serve the app's own files over an https:// address with AndroidX WebViewAssetLoader, so the page follows the normal same-origin rules of the web.

app/src/main/java/com/acme/reports/ReportActivity.kt · Kotlin · the fix, against the flagged file
package com.acme.reports import android.os.BundleAdded: import android.webkit.WebResourceRequestAdded: import android.webkit.WebResourceResponseimport android.webkit.WebViewAdded: import android.webkit.WebViewClientimport androidx.appcompat.app.AppCompatActivityAdded: import androidx.webkit.WebViewAssetLoaderimport java.io.File class ReportActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)Added:         val reports = WebViewAssetLoader.InternalStoragePathHandler(Added:             this,Added:             File(filesDir, "reports"),Added:         )Added:         val loader = WebViewAssetLoader.Builder()Added:             .addPathHandler("/reports/", reports)Added:             .build()        val webView = WebView(this)Removed:         webView.settings.allowFileAccess = trueRemoved:         webView.settings.allowUniversalAccessFromFileURLs = trueRemoved:         val report = File(filesDir, "reports/latest.html")Removed:         webView.loadUrl("file://" + report.absolutePath)Added:         webView.settings.allowFileAccess = falseAdded:         webView.webViewClient = object : WebViewClient() {Added:             override fun shouldInterceptRequest(Added:                 view: WebView,Added:                 request: WebResourceRequest,Added:             ): WebResourceResponse? = loader.shouldInterceptRequest(request.url)Added:         }Added:         webView.loadUrl("https://appassets.androidplatform.net/reports/latest.html")        setContentView(webView)    }}

When it can be wrong

  • A WebView that only opens HTML shipped inside the app, which no one outside can change, is still reported.
  • Missed: Java source files, which this rule does not read, and a value set through a variable, such as allowUniversalAccessFromFileURLs = BuildConfig.DEBUG.

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 setting line when the WebView only opens bundled files, and say so.

    Kotlin
    settings.allowFileAccessFromFileURLs = true // skylos: ignore[SKY-D258] bundled docs
  • skylos baseline ., then --baseline, accepts existing settings by file and line.

  • [tool.skylos] ignore = ["SKY-D258"] turns the rule off for the whole project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.