Skip to content

Commit ee3bf12

Browse files
[#557]完善解决对webview选择文件的返回值做校验 (#586)
1 parent 322ad88 commit ee3bf12

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

core/runtime/android/widgets/src/main/java/org/hapjs/widgets/view/NestedWebView.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,18 +1375,48 @@ private Uri[] blockPrivatePaths(Uri[] resultList) {
13751375
String dataData = "/data/data/" + mContext.getPackageName();
13761376
String dataUser = dataData;
13771377
File dataDir = ContextCompat.getDataDir(mContext);
1378+
String filePath = "";
1379+
try {
1380+
filePath = new File(path).getCanonicalPath();
1381+
} catch (IOException e) {
1382+
Log.e(TAG, "blockPrivatePaths: ", e);
1383+
return new Uri[0];
1384+
}
1385+
String externalData = "/sdcard/Android/data/" + mContext.getPackageName();
13781386
if (dataDir != null) {
13791387
dataUser = dataDir.getPath();
13801388
}
1381-
if (!TextUtils.isEmpty(path) && (path.startsWith(dataData) || path.startsWith(dataUser))) {
1382-
return new Uri[0];
1389+
if (!TextUtils.isEmpty(filePath)) {
1390+
if (filePath.startsWith(dataData) || filePath.startsWith(dataUser)) {
1391+
return new Uri[0];
1392+
}
1393+
if (filePath.toLowerCase().startsWith(externalData.toLowerCase())) {
1394+
return new Uri[0];
1395+
}
1396+
File[] externalFilesDirs = mContext.getExternalFilesDirs(null);
1397+
if (checkPath(filePath, externalFilesDirs)) return new Uri[0];
1398+
File[] externalCacheDirs = mContext.getExternalCacheDirs();
1399+
if (checkPath(filePath, externalCacheDirs)) return new Uri[0];
1400+
File[] externalMediaDirs = mContext.getExternalMediaDirs();
1401+
if (checkPath(filePath, externalMediaDirs)) return new Uri[0];
13831402
}
13841403
}
13851404
}
13861405
}
13871406
return resultList;
13881407
}
13891408

1409+
private boolean checkPath(String path, File[] files) {
1410+
if (files != null) {
1411+
for (File file : files) {
1412+
if (path.toLowerCase().startsWith(file.getAbsolutePath().toLowerCase())) {
1413+
return true;
1414+
}
1415+
}
1416+
}
1417+
return false;
1418+
}
1419+
13901420
private void resolveLowApiResult() {
13911421
final HybridView hybridView =
13921422
getComponent() != null ? getComponent().getHybridView() : null;
@@ -1423,6 +1453,16 @@ && getComponent().getCallback() != null) {
14231453
mCacheVideoFile = null;
14241454
}
14251455
result = tmpResults;
1456+
} else {
1457+
//photo or video sometimes go here
1458+
if ((mCachePhotoFile == null || !mCachePhotoFile.exists() || mCachePhotoFile.length() == 0) && (mCacheVideoFile == null || !mCacheVideoFile.exists() || mCacheVideoFile.length() == 0)) {
1459+
//not check photo or video
1460+
Uri[] results = new Uri[]{result};
1461+
Uri[] resultsAfterCheck = blockPrivatePaths(results);
1462+
if (resultsAfterCheck.length == 0) {
1463+
result = null;
1464+
}
1465+
}
14261466
}
14271467
}
14281468
if (null != mSingleFileCallback) {

0 commit comments

Comments
 (0)