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

WebView ERR_ACCESS_DENIED for local files on Android SDK 30 #71822

Closed
ertong opened this issue Dec 7, 2020 · 32 comments
Closed

WebView ERR_ACCESS_DENIED for local files on Android SDK 30 #71822

ertong opened this issue Dec 7, 2020 · 32 comments
Labels
e: OS-version specific Affects only some versions of the relevant operating system found in release: 1.22 Found to occur in 1.22 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: webview The WebView plugin P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels. platform-android Android applications specifically r: fixed Issue is closed as already fixed in a newer version team-android Owned by Android platform team triaged-android Triaged by Android platform team

Comments

@ertong
Copy link

ertong commented Dec 7, 2020

Steps to Reproduce

Consider the following code

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:webview_flutter/webview_flutter.dart';


void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  // This widget is the root of your application.
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String file;

  @override
  void initState() {
    ()async{
      var basePath = (await getApplicationSupportDirectory()).path;

      file = "$basePath/1.htm";

      File(file).writeAsString("<h1>Test page contents</h1>");
      setState(() {});
    }();

  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: file == null?Container():WebView(
        initialUrl: Uri.file(file).toString(),
      )
    );
  }
}

with dependencies

  webview_flutter: ^1.0.7
  path_provider: ^1.6.22

Run it on android 30 emulator/device.
If you have in build.gradle

compileSdkVersion 29
targetSdkVersion 29

everything works as expected (you see WebView with test contents).

But it if you change to

compileSdkVersion 30
targetSdkVersion 30

WebView shows net::ERR_ACCESS_DENIED

It looks like this issue comes from changes of default value of https://developer.android.com/reference/android/webkit/WebSettings#setAllowFileAccess(boolean)

We can try to allow changing this setting from flutter widget (which is looks like not good, because setAllowFileAccessFromFileURLs is deprecated).

And/or, maybe better, to implement the recommended mechanism of intercepting url loading (WebViewAssetLoader). In this case, it will be possible to implement file loading from custom scheme in the own code.

[√] Flutter (Channel stable, 1.22.4, on Microsoft Windows [Version 10.0.19042.662], locale uk-UA)
    • Flutter version 1.22.4 at D:\Programs\Android\flutter
    • Framework revision 1aafb3a8b9 (3 weeks ago), 2020-11-13 09:59:28 -0800
    • Engine revision 2c956a31c0
    • Dart version 2.10.4

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at D:\Programs\Android\sdk
    • Platform android-30, build-tools 30.0.2
    • ANDROID_SDK_ROOT = D:\Programs\Android\sdk
    • Java binary at: D:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (1 available)
    • sdk gphone x86 arm (mobile) • emulator-5554 • android-x86 • Android 11 (API 30) (emulator)

@darshankawar
Copy link
Member

@ertong
Add below permission in manifest and try again to see if it works:

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

@darshankawar darshankawar added in triage Presently being triaged by the triage team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds labels Dec 7, 2020
@ertong
Copy link
Author

ertong commented Dec 7, 2020

@darshankawar
Thanks for suggestion.
In my setup, result is the same SDK 29 - works, SDK 30 - access denied.

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Dec 7, 2020
@darshankawar
Copy link
Member

Replicable when sdk is set to 30. It shows:

Screenshot 2020-12-07 at 5 22 17 PM

With sdk set to 29, data shows up properly:

Screenshot 2020-12-07 at 5 20 37 PM

flutter doctor -v
[✓] Flutter (Channel stable, 1.22.4, on Mac OS X 10.15.4 19E2269 darwin-x64,
    locale en-IN)
    • Flutter version 1.22.4 at /Users/dhs/documents/Fluttersdk/flutter
    • Framework revision 1aafb3a8b9 (3 weeks ago), 2020-11-13 09:59:28 -0800
    • Engine revision 2c956a31c0
    • Dart version 2.10.4

 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
    • Android SDK at /Users/dhs/Library/Android/sdk
    • Platform android-30, build-tools 30.0.0
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.0.1, Build version 12A7300
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 46.0.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.51.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.16.0

[✓] Connected device (2 available)
    • sdk gphone x86 arm (mobile)         • emulator-5554
      • android-x86 • Android 11 (API 30) (emulator)
    • iPhone SE (2nd generation) (mobile) • 6C85835D-FBFD-4AB3-8DE8-B4FAD35E5367
      • ios         • com.apple.CoreSimulator.SimRuntime.iOS-14-0 (simulator)

• No issues found!


@darshankawar
Copy link
Member

Probably has to do with giving access to load local files / html with sdk 30.
May be similar to #27086

@darshankawar darshankawar added found in release: 1.22 Found to occur in 1.22 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: first party p: webview The WebView plugin e: OS-version specific Affects only some versions of the relevant operating system platform-android Android applications specifically and removed in triage Presently being triaged by the triage team labels Dec 7, 2020
@stuartmorgan stuartmorgan added the P3 Issues that are less important to the Flutter project label Jun 17, 2021
@uc-dve
Copy link

uc-dve commented Jun 18, 2021

Hey, I have the same issue, I can't access any files from documentDirectory inside webview in SDK 30.
Any solution?

@Aulig
Copy link

Aulig commented Sep 10, 2021

Adding webView.getSettings().setAllowFileAccess(true); in FlutterWebView.java works, as suggested by @ertong. The method is also not deprecated I think, the method below it in the docs (setAllowFileAccessFromFileURLs) is.

I guess we should make this configurable via Flutter as the Android docs mention security reasons as reasons why the default was set to false.

Surprised that this issues has so few likes considering that SDK 30 is a requirement for PlayStore submission now.

@fengwenhua
Copy link

It works for me!

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

android:usesCleartextTraffic="true"

image

webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);

image

@Aulig
Copy link

Aulig commented Sep 13, 2021

It works for me!

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

android:usesCleartextTraffic="true"

2980×1068 image

webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);

image

Are all of those necessary for you? For me it works with just webSettings.setAllowFileAccess(true);

@rvgmartins
Copy link

rvgmartins commented Oct 14, 2021

what version of the webview are you using?

It didn't works for me...

Thanks

@Aulig
Copy link

Aulig commented Oct 15, 2021

I put webView.getSettings().setAllowFileAccess(true); below the line webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
in plugins\packages\webview_flutter\android\src\main\java\io\flutter\plugins\webviewflutter\FlutterWebView.java

Hope it helps you @rvgmartins :)

@alexagat
Copy link

alexagat commented Oct 21, 2021

While the webView.getSettings().setAllowFileAccess(true); fix works for v1.x.x of the webview_flutter package, I can't seem to get it working on v2.1.1 (latest). Since the structure of FlutterWebView.java changed, I attempted to add webView.getSettings().setAllowFileAccess(true); into the FlutterWebView constructor but no luck.

@Aulig based on your code snippet, I'm assuming you were modifying a previous version of the package? Has anybody been able to get this working on a modern version of the package and if so, what code was modified?

@Aulig
Copy link

Aulig commented Oct 21, 2021

While the webView.getSettings().setAllowFileAccess(true); fix works for v1.x.x of the webview_flutter package, I can't seem to get it working on v2.1.1 (latest). Since the structure of FlutterWebView.java changed, I attempted to add webView.getSettings().setAllowFileAccess(true); into the FlutterWebView constructor but no luck.

@Aulig based on your code snippet, I'm assuming you were modifying a previous version of the package? Has anybody been able to get this working on a modern version of the package and if so, what code was modified?

Hi @alexagat - you're correct, I modified an old version. I didn't notice that it changed in newer versions. I'd assume that you could just add it to the webviewbuilder in the createWebView function something like this:

webViewBuilder
        .setUsesHybridComposition(usesHybridComposition)
        .setDomStorageEnabled(true) // Always enable DOM storage API.
        .setJavaScriptCanOpenWindowsAutomatically(
            true) // Always allow automatically opening of windows.
        .setSupportMultipleWindows(true) // Always support multiple windows.
        .setWebChromeClient(webChromeClient)
        .setAllowFileAccess(true) // <-- HERE'S MY CHANGE
        .setDownloadListener(
            downloadListener); // Always use {@link FlutterWebChromeClient} as web Chrome client.

But I haven't tested that. Or is that what you already tried Alex?

@alexagat
Copy link

alexagat commented Oct 21, 2021

But I haven't tested that. Or is that what you already tried Alex?

@Aulig correct, I tried a number of variations including adding the .setAllowFileAccess(true) as a chained call to the webViewBuilder (as your snippet shows). If I downgrade back to 1.0.7 (the version I was at before trying to upgrade to 2.1.1) the fix works.

I'll have to bump the version 1 by 1 to try and identify the specific bump of the webview_flutter package that broke this fix unless anybody else has ideas..

@Aulig
Copy link

Aulig commented Oct 21, 2021

But I haven't tested that. Or is that what you already tried Alex?

@Aulig correct, I tried a number of variations including adding the .setAllowFileAccess(true) as a chained call to the webViewBuilder (as your snippet shows). If I downgrade back to 1.0.7 (the version I was at before trying to upgrade to 2.1.1) the fix works.

I'll have to bump the version 1 by 1 to try and identify the specific bump of the webview_flutter package that broke this fix unless anybody else has ideas..

I see, that's unfortunate. I think I forked off version 2.0.2, which works for me. So you can start your search there if you'd like. Good luck!

@Deepjyoti120
Copy link

Deepjyoti120 commented Nov 15, 2021

webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);

Thank you its worked

@ahmadkhedr
Copy link

Same issue here

@ahmadkhedr
Copy link

None of the above solutions are working since the plugin was updated rapidly in the few months and I think the structure had big changes and modifications

@iulian0512
Copy link

P5 is way to low for this

@vgsrivathsan
Copy link

adding the line
webView.settings.setAllowFileAccess(true);
under the class WebViewAndroidPlatformController along with the other settings in the file webview_android_widget.dart in the dependency plugin webview_flutter_android works, local files can be accessed

checked with version webview_flutter_android 2.8.2 & android sdk 31

@AdrianDev0
Copy link

@vgsrivathsan approach also works for me. Will there be an update in the webview plugin to be able to enable this? Currently I have to add the plugin as a local dependency to my Code and add the line from @vgsrivathsan to it.

@stephg
Copy link

stephg commented Feb 7, 2022

Looks like the issue is resolved when using WebView's upstream loadFile method: https://pub.dev./packages/webview_flutter/changelog#240
This works for me - tested with some files located below getTemporaryDirectory() of path_provider package.

@AdrianDev0
Copy link

Oh yes, indeed, that works. Thank you @stephg !

@filipenanclarez
Copy link

filipenanclarez commented Feb 8, 2022

Looks like the issue is resolved when using WebView's upstream loadFile method: https://pub.dev./packages/webview_flutter/changelog#240 This works for me - tested with some files located below getTemporaryDirectory() of path_provider package.

With this workaround the anchors not work. So o think enable fileaccess is best solution

@filipenanclarez
Copy link

adding the line webView.settings.setAllowFileAccess(true); under the class WebViewAndroidPlatformController along with the other settings in the file webview_android_widget.dart in the dependency plugin webview_flutter_android works, local files can be accessed

checked with version webview_flutter_android 2.8.2 & android sdk 31

You saved my life !!!

@sigpoggy
Copy link

sigpoggy commented Feb 18, 2022

I'm using version 3.0.0 (webview_flutter_android 2.8.3) and have discovered that calling controller.loadUrl() with a file scheme results in the error. However, looking at the source for loadFile(), it sets webView.settings.setAllowFileAccess(true); before calling loadUrl(). This fixes the previous version of loadFile() that did not allow accessing local files from the html page.
Thus it seems the answer to this issue is simply use loadFile().

@ritiksoni73
Copy link

Looks like the issue is resolved when using WebView's upstream loadFile method: https://pub.dev./packages/webview_flutter/changelog#240 This works for me - tested with some files located below getTemporaryDirectory() of path_provider package.

how to use load file method can you please suggest me

@sigpoggy
Copy link

loadFile is a WebViewController method:

loadFile( String absoluteFilePath ) -> Future
Loads the file located at the specified absoluteFilePath.

The absoluteFilePath parameter should contain the absolute path to the file as it is stored on the device. For example: /Users/username/Documents/www/index.html.

Throws an ArgumentError if the absoluteFilePath does not exist.`
Code from example in plugins repo:

`Future _onLoadLocalFileExample(
WebViewController controller, BuildContext context) async {
final String pathToIndex = await _prepareLocalFile();

await controller.loadFile(pathToIndex);

}`

@vikivyas
Copy link

It works for me!

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

android:usesCleartextTraffic="true"

2980×1068 image

webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);

image

Are all of those necessary for you? For me it works with just webSettings.setAllowFileAccess(true);

I think this will not work on iOS or does it?

@dinonovak
Copy link

I am unable to find file to change this settings in current code?
Can someone please advise how to enable
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
Screenshot 2022-11-22 at 19 56 25

@vgsrivathsan
Copy link

I am unable to find file to change this settings in current code? Can someone please advise how to enable webSettings.setAllowFileAccess(true); webSettings.setAllowFileAccessFromFileURLs(true); Screenshot 2022-11-22 at 19 56 25

You don't need to change anything
https://pub.dev/packages/webview_flutter/example
There is an example to load files from storage

@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
@Hixie Hixie removed the plugin label Jul 6, 2023
@flutter-triage-bot flutter-triage-bot bot added multiteam-retriage-candidate team-android Owned by Android platform team triaged-android Triaged by Android platform team labels Jul 8, 2023
@danagbemava-nc
Copy link
Member

I just tested the plugin example app on an android emulator running sdk 30 and it works as expected.
The webview plugin has undergone several changes since this issue was filed and since we haven't seen any comments on this issue, I will be closing this as fixed.

If anyone is still running into this issue with webview_flutter: ^4.7.0, kindly file a new issue and provide detailed steps to reproduce so we can investigate this. Thank you

Screencast.from.04-12-2024.10.31.11.AM.webm
flutter doctor -v
[!] Flutter (Channel stable, 3.19.5, on Ubuntu 22.04.4 LTS 6.5.0-27-generic, locale en_US.UTF-8)
    • Flutter version 3.19.5 on channel stable at /home/daniel/fvm/versions/stable
    ! Warning: `dart` on your path resolves to /home/linuxbrew/.linuxbrew/Cellar/dart/3.2.2/libexec/bin/dart, which is not inside your current Flutter SDK checkout at /home/daniel/fvm/versions/stable. Consider adding /home/daniel/fvm/versions/stable/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 300451adae (2 weeks ago), 2024-03-27 21:54:07 -0500
    • Engine revision e76c956498
    • Dart version 3.3.3
    • DevTools version 2.31.1
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /home/daniel/Android/Sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /home/daniel/.local/share/JetBrains/Toolbox/apps/android-studio/jbr/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • Ubuntu clang version 14.0.0-1ubuntu1.1
    • cmake version 3.22.1
    • ninja version 1.10.1
    • pkg-config version 0.29.2

[✓] Android Studio (version 2023.1)
    • Android Studio at /home/daniel/.local/share/JetBrains/Toolbox/apps/android-studio
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] VS Code (version 1.87.2)
    • VS Code at /usr/share/code
    • Flutter extension version 3.86.0

[✓] Connected device (3 available)
    • sdk gphone x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • Linux (desktop)         • linux         • linux-x64      • Ubuntu 22.04.4 LTS 6.5.0-27-generic
    • Chrome (web)            • chrome        • web-javascript • Google Chrome 103.0.5060.53

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

@danagbemava-nc danagbemava-nc added the r: fixed Issue is closed as already fixed in a newer version label Apr 12, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
e: OS-version specific Affects only some versions of the relevant operating system found in release: 1.22 Found to occur in 1.22 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: webview The WebView plugin P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels. platform-android Android applications specifically r: fixed Issue is closed as already fixed in a newer version team-android Owned by Android platform team triaged-android Triaged by Android platform team
Projects
None yet
Development

No branches or pull requests