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

【安全测试】File.getAbsolutePath()方法获取文件路径,没有过滤“.\或.\”,存在跨路径攻击风险,推荐使用getCan… #3319

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions android/sdk/src/main/java/org/apache/weex/WXEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public static String copySoDesDir() {
if (!desDir.exists()) {
desDir.mkdirs();
}
COPY_SO_DES_DIR = desDir.getAbsolutePath();
COPY_SO_DES_DIR = desDir.getCanonicalPath();
}
} catch (Throwable e) {
WXLogUtils.e(WXLogUtils.getStackTrace(e));
Expand Down Expand Up @@ -473,7 +473,12 @@ public static String getCrashFilePath(Context context) {
if (dir == null)
return "";

String crashDir = dir.getAbsolutePath();
String crashDir = null;
try {
crashDir = dir.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}

return crashDir;
}
Expand Down Expand Up @@ -523,7 +528,7 @@ public static String extractSo() {
final String soDesPath = copySoDesDir();
if (sourceFile.exists() && !TextUtils.isEmpty(soDesPath)) {
try {
WXFileUtils.extractSo(sourceFile.getAbsolutePath(), soDesPath);
WXFileUtils.extractSo(sourceFile.getCanonicalPath(), soDesPath);
} catch (IOException e) {
WXLogUtils.e("extractSo error " + e.getMessage());
// e.printStackTrace();
Expand Down Expand Up @@ -593,7 +598,11 @@ public static String findSoPath(String libName) {
File soFile = new File(soPath);
if (soFile.exists()) {
WXLogUtils.e(libName + "'s Path is" + soPath);
return soFile.getAbsolutePath();
try {
return soFile.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
} else {
WXLogUtils.e(libName + "'s Path is " + soPath + " but file does not exist");
}
Expand All @@ -608,10 +617,12 @@ public static String findSoPath(String libName) {


if (cacheDir.indexOf("/cache") > 0) {
soPath = new File(cacheDir.replace("/cache", "/lib"), realName).getAbsolutePath();
try {
soPath = new File(cacheDir.replace("/cache", "/lib"), realName).getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
}


final File soFile = new File(soPath);
if (soFile.exists()) {
WXLogUtils.e(libName + "use lib so");
Expand All @@ -620,7 +631,11 @@ public static String findSoPath(String libName) {
//unzip from apk file
final String extractSoPath = extractSo();
if (!TextUtils.isEmpty(extractSoPath)) {
return new File(extractSoPath, realName).getAbsolutePath();
try {
return new File(getCacheDir(), realName).getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return soPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,14 @@ public static void loadTypeface(final FontDO fontDo, boolean notify) {
if(!dir.exists()){
dir.mkdirs();
}
final String fullPath = dir.getAbsolutePath()+ File.separator +fileName;
if (!loadLocalFontFile(fullPath, fontFamily, false)) {
downloadFontByNetwork(url, fullPath, fontFamily);
final String fullPath;
try {
fullPath = dir.getCanonicalPath()+ File.separator +fileName;
if (!loadLocalFontFile(fullPath, fontFamily, false)) {
downloadFontByNetwork(url, fullPath, fontFamily);
}
} catch (IOException e) {
e.printStackTrace();
}
} else if (fontDo.getType() == FontDO.TYPE_FILE || fontDo.getType() == FontDO.TYPE_BASE64) {
boolean result = loadLocalFontFile(fontDo.getUrl(), fontDo.getFontFamilyName(), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ public static void copyFile(File oldFile, File newFile) {
inputStream.close();
outputStream.close();
} catch (Exception e) {
WXLogUtils.e("copyFile " + e.getMessage() + ": " + oldFile.getAbsolutePath() + ": " + newFile.getAbsolutePath());
try {
WXLogUtils.e("copyFile " + e.getMessage() + ": " + oldFile.getCanonicalPath() + ": " + newFile.getCanonicalPath());
} catch (IOException ioException) {
ioException.printStackTrace();
}
if (inputStream != null) {
try {
inputStream.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public static void copyStartUpSo() {
copyPath.mkdirs();
}
newfile = new File(copyPath, startSoPath);
WXEnvironment.CORE_JSB_SO_PATH = newfile.getAbsolutePath();
WXEnvironment.CORE_JSB_SO_PATH = newfile.getCanonicalPath();
String jsb = WXEnvironment.getDefaultSettingValue(startSoName, "-1");
if(newfile.exists() && TextUtils.equals(WXEnvironment.getAppVersionName(), jsb)) {
// no update so skip copy
Expand Down Expand Up @@ -307,7 +307,7 @@ public static void copyJssRuntimeSo(){
if (!TextUtils.equals(WXEnvironment.getAppVersionName(),defaultSettingValue)){
targetFile.delete();
}else {
WXEnvironment.CORE_JSS_SO_PATH= targetFile.getAbsolutePath();
WXEnvironment.CORE_JSS_SO_PATH= targetFile.getCanonicalPath();
WXEnvironment.sUseRunTimeApi = true;
WXLogUtils.e("weex", "copyJssRuntimeSo exist: return");
return;
Expand All @@ -321,7 +321,7 @@ public static void copyJssRuntimeSo(){
targetFile.createNewFile();
WXFileUtils.copyFileWithException(new File(fromPath),targetFile);
/**3. update flag **/
WXEnvironment.CORE_JSS_SO_PATH= targetFile.getAbsolutePath();
WXEnvironment.CORE_JSS_SO_PATH= targetFile.getCanonicalPath();
WXEnvironment.writeDefaultSettingsValue(keyVersionCode,WXEnvironment.getAppVersionName());
WXEnvironment.sUseRunTimeApi = true;
WXLogUtils.e("weex", "copyJssRuntimeSo: cp end and return ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,24 @@ public static String saveBitmapToGallery(Context context, Bitmap bitmap, final O
// Insert the image file into the system gallery
try {
MediaStore.Images.Media.insertImage(context.getContentResolver(),
file.getAbsolutePath(), fileName, null);
} catch (FileNotFoundException e) {
file.getCanonicalPath(), fileName, null);
} catch (Exception e) {
e.printStackTrace();
}

// Notify the system gallery update
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + appDir.getAbsolutePath() + "/" + fileName)));
try {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + appDir.getCanonicalPath() + "/" + fileName)));
} catch (IOException e) {
e.printStackTrace();
}

return file.getAbsolutePath();
try {
return file.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}

/**
Expand Down