apache / apache/cordova-android

WebViewAssetLoader: Application private path Raw File access at JavaScript using XMLHttpRequest fails.

Open
#1,329 2 comments 2 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
3.8k
Forks
1.6k
Avg merge
16h 18m
Merged PRs (30d)
11

Description

**1) Image residing in Application private path.**
lFileUrl =data/user/0/com.company.stub/files/myfolder/IMG-20210826-204423558thumb.jpg

**2)XMLHttpRequest request done from javascript level in cordova based android app.**

var xhr = new XMLHttpRequest();
xhr.open("GET", lFileUrl, true);
xhr.responseType = "blob";
xhr.onload = function(e) {
try {
}

**3)WebResourceResponse shouldInterceptRequest called.**
2021-08-26 20:44:34.534 32333-4065/com.company.stub D/SystemWebViewClient: shouldInterceptRequest request.getUrl(): https://localhost/data/user/0/com.company.stub/files/myfolder/IMG-20210826-204423558thumb.jpg

https://github.com/apache/cordova-android/blob/015db819aed3f35892d2b6035781ea4bd752149d/framework/src/org/apache/cordova/engine/SystemWebViewClient.java#L423

**4)File not found exception seen as its searching in "www/"**

https://github.com/apache/cordova-android/blob/015db819aed3f35892d2b6035781ea4bd752149d/framework/src/org/apache/cordova/engine/SystemWebViewClient.java#L112

2021-08-26 20:44:34.537 32333-4065/com.company.stub E/SystemWebViewClient: www/data/user/0/com.company.stub/files/myfolder/IMG-20210826-204423558thumb.jpg

**### What is expected to happen?**
1)private File access should be pass.
https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.InternalStoragePathHandler

**### What does actually happen?**

File not found exception seen as WebViewAssetLoader.InternalStoragePathHandler is not handled. Suggest how to handle raw file access.

**## Information**

https://bugs.chromium.org/p/chromium/issues/detail?id=1101250
**Below change works.**
https://github.com/apache/cordova-android/blob/015db819aed3f35892d2b6035781ea4bd752149d/framework/src/org/apache/cordova/engine/SystemWebViewClient.java#L95

```
if(filecaseboolean){ is = new FileInputStream(new File(path));//Imagecase
}else{
is = parentEngine.webView.getContext().getAssets().open("www/" + path, AssetManager.ACCESS_STREAMING);
}
```

**### Version information**

Cordova: Android 10.1.0
device : android 11
app targetsdk : 30 .

**Note:** if `` **everything works fine.**

- [ ✓] I searched for existing GitHub issues
- [ ✓] I updated all Cordova tooling to most recent version
- [✓ ] I included all the necessary information above

Contributor guide

Open the contributing guide

Research direction

Start with framework/src/org/apache/cordova/engine/SystemWebViewClient.java at the linked shouldInterceptRequest and asset-loading locations, then reproduce the XMLHttpRequest for the application-private image on Android 11 with AndroidInsecureFileModeEnabled disabled. Compare the failing www/ lookup with the WebViewAssetLoader.InternalStoragePathHandler behavior. Done means private raw files load through XMLHttpRequest without enabling insecure file mode.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java, javascript
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.