Skip to content

Commit 000aa61

Browse files
committed
fix: zip 路径穿透监测
1 parent 9f25e67 commit 000aa61

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

library/src/main/java/com/opensource/svgaplayer/SVGACache.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ object SVGACache {
5656
}
5757

5858
// 清除目录下的所有文件
59-
private fun clearDir(path: String) {
59+
internal fun clearDir(path: String) {
6060
try {
6161
val dir = File(path)
6262
dir.takeIf { it.exists() }?.let { parentDir ->

library/src/main/java/com/opensource/svgaplayer/SVGAParser.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class SVGAParser(context: Context?) {
154154
playCallback
155155
)
156156
}
157-
} catch (e: java.lang.Exception) {
157+
} catch (e: Exception) {
158158
this.invokeErrorCallback(e, callback)
159159
}
160160
}
@@ -501,6 +501,7 @@ class SVGAParser(context: Context?) {
501501
continue
502502
}
503503
val file = File(cacheDir, zipItem.name)
504+
ensureUnzipSafety(file, cacheDir.absolutePath)
504505
FileOutputStream(file).use { fileOutputStream ->
505506
val buff = ByteArray(2048)
506507
while (true) {
@@ -519,8 +520,18 @@ class SVGAParser(context: Context?) {
519520
} catch (e: Exception) {
520521
LogUtils.error(TAG, "================ unzip error ================")
521522
LogUtils.error(TAG, "error", e)
523+
SVGACache.clearDir(cacheDir.absolutePath)
522524
cacheDir.delete()
523525
throw e
524526
}
525527
}
528+
529+
// 检查 zip 路径穿透
530+
private fun ensureUnzipSafety(outputFile: File, dstDirPath: String) {
531+
val dstDirCanonicalPath = File(dstDirPath).canonicalPath
532+
val outputFileCanonicalPath = outputFile.canonicalPath
533+
if (!outputFileCanonicalPath.startsWith(dstDirCanonicalPath)) {
534+
throw IOException("Found Zip Path Traversal Vulnerability with $dstDirCanonicalPath")
535+
}
536+
}
526537
}

0 commit comments

Comments
 (0)