Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
fix: zip 路径穿透监测
Browse files Browse the repository at this point in the history
  • Loading branch information
painld6 committed Aug 18, 2021
1 parent 9f25e67 commit 000aa61
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object SVGACache {
}

// 清除目录下的所有文件
private fun clearDir(path: String) {
internal fun clearDir(path: String) {
try {
val dir = File(path)
dir.takeIf { it.exists() }?.let { parentDir ->
Expand Down
13 changes: 12 additions & 1 deletion library/src/main/java/com/opensource/svgaplayer/SVGAParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class SVGAParser(context: Context?) {
playCallback
)
}
} catch (e: java.lang.Exception) {
} catch (e: Exception) {
this.invokeErrorCallback(e, callback)
}
}
Expand Down Expand Up @@ -501,6 +501,7 @@ class SVGAParser(context: Context?) {
continue
}
val file = File(cacheDir, zipItem.name)
ensureUnzipSafety(file, cacheDir.absolutePath)
FileOutputStream(file).use { fileOutputStream ->
val buff = ByteArray(2048)
while (true) {
Expand All @@ -519,8 +520,18 @@ class SVGAParser(context: Context?) {
} catch (e: Exception) {
LogUtils.error(TAG, "================ unzip error ================")
LogUtils.error(TAG, "error", e)
SVGACache.clearDir(cacheDir.absolutePath)
cacheDir.delete()
throw e
}
}

// 检查 zip 路径穿透
private fun ensureUnzipSafety(outputFile: File, dstDirPath: String) {
val dstDirCanonicalPath = File(dstDirPath).canonicalPath
val outputFileCanonicalPath = outputFile.canonicalPath
if (!outputFileCanonicalPath.startsWith(dstDirCanonicalPath)) {
throw IOException("Found Zip Path Traversal Vulnerability with $dstDirCanonicalPath")
}
}
}

0 comments on commit 000aa61

Please sign in to comment.