Skip to content

Disable WebView file access when the image cache is off#1497

Open
jim-daf wants to merge 1 commit into
wallabag:masterfrom
jim-daf:fix-webview-file-access
Open

Disable WebView file access when the image cache is off#1497
jim-daf wants to merge 1 commit into
wallabag:masterfrom
jim-daf:fix-webview-file-access

Conversation

@jim-daf
Copy link
Copy Markdown

@jim-daf jim-daf commented May 13, 2026

Closes #1496.

ReadArticleActivity.initWebView() already gates setAllowFileAccess(true) on the image cache being enabled, but the gate is one-directional:

if (settings.isImageCacheEnabled() && !webViewSettings.getAllowFileAccess()) {
    Log.d(TAG, "initWebView() enabling WebView file access");
    webViewSettings.setAllowFileAccess(true);
}

It only enables the flag, it never disables it. On minSdkVersion 23 the WebView default for setAllowFileAccess is true on Android 9 and below, so a WebView on those versions kept file URL access regardless of whether the user had the image cache turned on. The article WebView also attaches two JS bridges (hostWebViewTextController, hostAnnotationController), which is what makes the lingering flag matter.

Change

Make the flag mirror the cache setting explicitly:

boolean needsFileAccess = settings.isImageCacheEnabled();
if (webViewSettings.getAllowFileAccess() != needsFileAccess) {
    Log.d(TAG, "initWebView() setting WebView file access to " + needsFileAccess);
    webViewSettings.setAllowFileAccess(needsFileAccess);
}

Behaviour

  • Image cache on: identical to before (file access on).
  • Image cache off: file access is now explicitly off on pre-API-30 devices, not just API 30+.
  • loadDataWithBaseURL("file:///android_asset/", ...) (the line that loads the article HTML further down) continues to work because the android_asset scheme is permitted regardless of this flag.

ReadArticleActivity.initWebView turned on setAllowFileAccess(true) only
when the image cache feature is enabled, but it never turned it back
off. On minSdkVersion 23 setAllowFileAccess defaults to true, so a
WebView created on Android 9 or earlier kept file:// access enabled
even when the user had disabled the image cache from settings.

The article WebView already attaches two JavascriptInterface objects
(hostWebViewTextController and hostAnnotationController), so any code
path that loaded a file:// document into this WebView could reach
both bridges from a same-origin file:// page.

Rewrite the conditional to set the flag explicitly each time
initWebView runs, mirroring whether the image cache is on:

    boolean needsFileAccess = settings.isImageCacheEnabled();
    if (webViewSettings.getAllowFileAccess() != needsFileAccess) {
        webViewSettings.setAllowFileAccess(needsFileAccess);
    }

Behaviour with image cache enabled is unchanged. With image cache off,
the WebView no longer carries the legacy default-on file access on
pre-API-30 devices.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ReadArticleActivity enables WebView file access on opt-in but never turns it back off

1 participant