@onekeyfe/react-native-zip-archive 3.0.52 → 3.0.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -38,16 +38,24 @@ class ReactNativeZipArchive : HybridReactNativeZipArchiveSpec() {
38
38
  override fun unzip(from: String, to: String): Promise<String> {
39
39
  return Promise.async {
40
40
  val destDir = File(to)
41
- if (!destDir.exists()) destDir.mkdirs()
41
+ if (!destDir.exists() && !destDir.mkdirs()) {
42
+ throw Exception("Failed to create unzip destination: $to")
43
+ }
44
+ val canonicalDestDir = destDir.canonicalFile
42
45
 
43
46
  ZipInputStream(BufferedInputStream(FileInputStream(from))).use { zis ->
44
47
  var entry: ZipEntry? = zis.nextEntry
45
48
  while (entry != null) {
46
- val outFile = File(destDir, entry.name)
49
+ val outFile = safeZipEntryFile(canonicalDestDir, entry)
47
50
  if (entry.isDirectory) {
48
- outFile.mkdirs()
51
+ if (!outFile.exists() && !outFile.mkdirs()) {
52
+ throw Exception("Failed to create unzip directory: ${outFile.absolutePath}")
53
+ }
49
54
  } else {
50
- outFile.parentFile?.mkdirs()
55
+ val parent = outFile.parentFile
56
+ if (parent != null && !parent.exists() && !parent.mkdirs()) {
57
+ throw Exception("Failed to create unzip directory: ${parent.absolutePath}")
58
+ }
51
59
  FileOutputStream(outFile).use { fos ->
52
60
  val buffer = ByteArray(BUFFER_SIZE)
53
61
  var len: Int
@@ -64,6 +72,15 @@ class ReactNativeZipArchive : HybridReactNativeZipArchiveSpec() {
64
72
  }
65
73
  }
66
74
 
75
+ private fun safeZipEntryFile(destDir: File, entry: ZipEntry): File {
76
+ val outFile = File(destDir, entry.name).canonicalFile
77
+ val destPath = destDir.path + File.separator
78
+ if (outFile.path != destDir.path && !outFile.path.startsWith(destPath)) {
79
+ throw Exception("Unsafe zip entry path: ${entry.name}")
80
+ }
81
+ return outFile
82
+ }
83
+
67
84
  override fun unzipWithPassword(from: String, to: String, password: String): Promise<String> {
68
85
  return Promise.async {
69
86
  // java.util.zip does not support password-protected zip extraction.
@@ -36,12 +36,14 @@ final class ReactNativeZipArchive: HybridReactNativeZipArchiveSpec {
36
36
  if success {
37
37
  return to
38
38
  }
39
- throw NSError(
39
+ // Preserve the underlying NSError (domain + code) so callers can tell a
40
+ // corrupt archive apart from a wrong password or an I/O failure. Only fall
41
+ // back to the generic "ZipArchive" error when SSZipArchive reported failure
42
+ // without populating an error.
43
+ throw error ?? NSError(
40
44
  domain: "ZipArchive",
41
45
  code: -1,
42
- userInfo: [
43
- NSLocalizedDescriptionKey: error?.localizedDescription ?? "unable to unzip"
44
- ]
46
+ userInfo: [NSLocalizedDescriptionKey: "unable to unzip"]
45
47
  )
46
48
  }
47
49
  }
@@ -64,7 +66,11 @@ final class ReactNativeZipArchive: HybridReactNativeZipArchiveSpec {
64
66
  if success {
65
67
  return to
66
68
  }
67
- throw NSError(
69
+ // Preserve the underlying NSError (domain + code) so callers can tell a
70
+ // corrupt archive apart from a wrong password or an I/O failure. Only fall
71
+ // back to the generic "ZipArchive" error when SSZipArchive reported failure
72
+ // without populating an error.
73
+ throw error ?? NSError(
68
74
  domain: "ZipArchive",
69
75
  code: -1,
70
76
  userInfo: [NSLocalizedDescriptionKey: "unable to unzip"]
@@ -121,10 +127,13 @@ final class ReactNativeZipArchive: HybridReactNativeZipArchiveSpec {
121
127
  // NSErrorPointer because the return is unannotated/non-nullable).
122
128
  var error: NSError?
123
129
  let wantedFileSize = SSZipArchive.payloadSizeForArchive(atPath: path, error: &error)
124
- if error == nil {
125
- return wantedFileSize.doubleValue
130
+ if let error {
131
+ // Contract: return -1 on failure. Log the underlying error so a missing
132
+ // file can be told apart from a corrupt archive when investigating.
133
+ NSLog("[ZipArchive] getUncompressedSize failed for \(path): \(error)")
134
+ return -1
126
135
  }
127
- return -1
136
+ return wantedFileSize.doubleValue
128
137
  }
129
138
  }
130
139
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-zip-archive",
3
- "version": "3.0.52",
4
- "description": "react-native-zip-archive TurboModule for OneKey",
3
+ "version": "3.0.54",
4
+ "description": "react-native-zip-archive Nitro HybridObject for OneKey",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
7
7
  "exports": {