@capgo/capacitor-updater 8.53.0 → 8.54.0

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.
@@ -158,45 +158,27 @@ public struct CryptoCipher {
158
158
  }
159
159
 
160
160
  public static func calcChecksum(filePath: URL) -> String {
161
- let bufferSize = checksumBufferBytes()
162
- var sha256 = SHA256()
163
-
164
- let fileHandle: FileHandle
165
161
  do {
166
- fileHandle = try FileHandle(forReadingFrom: filePath)
162
+ return try calcChecksumOrThrow(filePath: filePath)
167
163
  } catch {
168
- logger.error("Cannot open file for checksum calculation")
164
+ logger.error("Cannot calculate checksum")
169
165
  logger.debug("Path: \(filePath.path), Error: \(error)")
170
166
  return ""
171
167
  }
168
+ }
172
169
 
173
- defer {
174
- do {
175
- try fileHandle.close()
176
- } catch {
177
- logger.error("Error closing file during checksum")
178
- logger.debug("Error: \(error)")
170
+ static func calcChecksumOrThrow(filePath: URL) throws -> String {
171
+ let file = try FileHandle(forReadingFrom: filePath)
172
+ defer { try? file.close() }
173
+ var sha256 = SHA256()
174
+ while true {
175
+ let count = try autoreleasepool {
176
+ let data = try file.read(upToCount: checksumBufferBytes()) ?? Data()
177
+ sha256.update(data: data)
178
+ return data.count
179
179
  }
180
+ if count == 0 { break }
180
181
  }
181
-
182
- while autoreleasepool(invoking: {
183
- let fileData: Data
184
- do {
185
- fileData = try fileHandle.read(upToCount: bufferSize) ?? Data()
186
- } catch {
187
- logger.error("Error reading file during checksum")
188
- logger.debug("Error: \(error)")
189
- return false
190
- }
191
-
192
- if fileData.count > 0 {
193
- sha256.update(data: fileData)
194
- return true // Continue
195
- } else {
196
- return false // End of file
197
- }
198
- }) {}
199
-
200
182
  return hexString(from: sha256)
201
183
  }
202
184
 
@@ -238,7 +220,45 @@ public struct CryptoCipher {
238
220
  logger.info("Encryption not set, no public key or session, ignored")
239
221
  return
240
222
  }
223
+ try DecryptionSession(publicKey: publicKey, sessionKey: sessionKey).decryptFile(filePath: filePath)
224
+ }
225
+
226
+ // Scoped to one manifest. Key material is immutable; CBC state stays local to each file.
227
+ final class DecryptionSession {
228
+ private let publicKey: String
229
+ private let sessionKey: String
230
+ private let lock = NSLock()
231
+ private var prepared: Result<AES128Key, Error>?
232
+
233
+ init(publicKey: String, sessionKey: String) {
234
+ self.publicKey = publicKey
235
+ self.sessionKey = sessionKey
236
+ }
237
+
238
+ private func key() throws -> AES128Key {
239
+ lock.lock()
240
+ defer { lock.unlock() }
241
+ if let prepared { return try prepared.get() }
242
+ let result = Result { try CryptoCipher.prepareDecryptionKey(publicKey: publicKey, sessionKey: sessionKey) }
243
+ prepared = result
244
+ return try result.get()
245
+ }
246
+
247
+ var isEncrypted: Bool {
248
+ !publicKey.isEmpty
249
+ }
250
+
251
+ func decryptFile(filePath: URL) throws {
252
+ guard isEncrypted else {
253
+ CryptoCipher.logger.info("Encryption not set, no public key or session, ignored")
254
+ return
255
+ }
256
+ try CryptoCipher.decryptPreparedFile(filePath: filePath, aesPrivateKey: key())
257
+ }
258
+ }
241
259
 
260
+ private static func prepareDecryptionKey(publicKey: String, sessionKey: String) throws -> AES128Key {
261
+ guard isValidSessionKey(sessionKey) else { throw CustomError.cannotDecode }
242
262
  // The bundle is encrypted from here on: an unusable key must fail, never leave it encrypted.
243
263
  if !publicKey.hasPrefix("-----BEGIN RSA PUBLIC KEY-----") {
244
264
  logger.error("The public key is not a valid RSA Public key")
@@ -284,8 +304,15 @@ public struct CryptoCipher {
284
304
  throw NSError(domain: "Invalid decrypted session key", code: 5, userInfo: nil)
285
305
  }
286
306
 
287
- let aesPrivateKey = AES128Key(iv: ivData, aes128Key: sessionKeyDataDecrypted, logger: logger)
307
+ return AES128Key(iv: ivData, aes128Key: sessionKeyDataDecrypted, logger: logger)
308
+ } catch {
309
+ logger.error("File decryption failed")
310
+ throw CustomError.cannotDecode
311
+ }
312
+ }
288
313
 
314
+ private static func decryptPreparedFile(filePath: URL, aesPrivateKey: AES128Key) throws {
315
+ do {
289
316
  let encryptedSize = (try FileManager.default.attributesOfItem(atPath: filePath.path)[.size] as? NSNumber)?.uint64Value ?? 0
290
317
  if encryptedSize == 0 {
291
318
  logger.error("Encrypted file data is empty")
@@ -298,7 +325,6 @@ public struct CryptoCipher {
298
325
  logger.error("Decrypted data is empty")
299
326
  throw NSError(domain: "Empty decrypted data", code: 7, userInfo: nil)
300
327
  }
301
-
302
328
  } catch {
303
329
  logger.error("File decryption failed")
304
330
  throw CustomError.cannotDecode
@@ -428,25 +428,6 @@ extension CustomError: LocalizedError {
428
428
  }
429
429
  }
430
430
 
431
- /// Thread-safe atomic counter for concurrent operations
432
- final class AtomicCounter {
433
- private var value: Int = 0
434
- private let lock = NSLock()
435
-
436
- func increment() -> Int {
437
- lock.lock()
438
- defer { lock.unlock() }
439
- value += 1
440
- return value
441
- }
442
-
443
- var current: Int {
444
- lock.lock()
445
- defer { lock.unlock() }
446
- return value
447
- }
448
- }
449
-
450
431
  /// Thread-safe atomic boolean for concurrent operations
451
432
  final class AtomicBool {
452
433
  private var _value: Bool
@@ -11,6 +11,20 @@ import Foundation
11
11
  final class RedirectPolicyDelegate: NSObject, URLSessionTaskDelegate {
12
12
  private let lock = NSLock()
13
13
  private var allowDowngrade = false
14
+ private var httpsOnlyEnabled = true
15
+ /// When true every redirect target must be https, whatever `allowHttpsToHttpRedirect` says.
16
+ var httpsOnly: Bool {
17
+ get {
18
+ lock.lock()
19
+ defer { lock.unlock() }
20
+ return httpsOnlyEnabled
21
+ }
22
+ set {
23
+ lock.lock()
24
+ httpsOnlyEnabled = newValue
25
+ lock.unlock()
26
+ }
27
+ }
14
28
  /// Called when a downgrade redirect is refused. Must not retain the updater strongly.
15
29
  var onBlockedRedirect: ((URL?, URL?) -> Void)?
16
30
 
@@ -42,6 +56,12 @@ final class RedirectPolicyDelegate: NSObject, URLSessionTaskDelegate {
42
56
  completionHandler: @escaping (URLRequest?) -> Void
43
57
  ) {
44
58
  let source = response.url ?? task.currentRequest?.url
59
+ if httpsOnly && request.url?.scheme?.lowercased() != "https" {
60
+ onBlockedRedirect?(source, request.url)
61
+ completionHandler(nil)
62
+ task.cancel()
63
+ return
64
+ }
45
65
  if !allowHttpsToHttpRedirect && Self.isHttpsToHttpDowngrade(from: source, to: request.url) {
46
66
  onBlockedRedirect?(source, request.url)
47
67
  completionHandler(nil)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-updater",
3
- "version": "8.53.0",
3
+ "version": "8.54.0",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Live update for capacitor apps",
6
6
  "main": "dist/plugin.cjs.js",