@capgo/capacitor-updater 9.0.0 → 9.0.2
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.
- package/CapgoCapacitorUpdater.podspec +3 -6
- package/Package.swift +30 -0
- package/README.md +2406 -291
- package/android/build.gradle +37 -17
- package/android/consumer-rules.pro +48 -0
- package/android/proguard-rules.pro +23 -0
- package/android/src/main/AndroidManifest.xml +0 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/AndroidAppExitReporter.java +92 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/AppLifecycleObserver.java +115 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +227 -195
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +24 -23
- package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +7927 -1243
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +5785 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +564 -121
- package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +44 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +43 -49
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +4 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +427 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DeviceIdHelper.java +230 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +2041 -118
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +301 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +569 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Http3.java +436 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/HttpEngineTransport.java +604 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +79 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +419 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeDetector.java +72 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +963 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/StatsBatchFailureDiagnostics.java +40 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ThreeFingerPinchDetector.java +323 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +306 -0
- package/dist/docs.json +4431 -840
- package/dist/esm/definitions.d.ts +2641 -225
- package/dist/esm/definitions.js +103 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/history.d.ts +1 -0
- package/dist/esm/history.js +283 -0
- package/dist/esm/history.js.map +1 -0
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +5 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +53 -42
- package/dist/esm/web.js +180 -37
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +570 -37
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +570 -37
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +262 -0
- package/ios/Sources/CapacitorUpdaterPlugin/AppHealthTracker.swift +82 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BundleInfo.swift +185 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BundleStatus.swift +127 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +6813 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoRawRsa.swift +36 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoSemanticVersion.swift +200 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +5099 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +387 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +343 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DeviceIdHelper.swift +120 -0
- package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +452 -0
- package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +380 -0
- package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RSA.swift +96 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +97 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +876 -0
- package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/UserDefaultsExtension.swift +0 -2
- package/ios/Sources/CapacitorUpdaterPlugin/WKWebView+CapgoObjcBridge.swift +20 -0
- package/ios/Sources/CapacitorUpdaterPlugin/WebViewStatsReporter.swift +304 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/BuiltinHashesTests.swift +210 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/BundleHardeningTests.swift +292 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CapacitorUpdaterPluginTests.swift +6533 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CapgoSemanticVersionTests.swift +64 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ChecksumRequiredTests.swift +156 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CliCryptoFixtureTests.swift +314 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/HttpsOnlyBuiltinMinimumTests.swift +653 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/InstallPerfTests.swift +174 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ListChannelsTests.swift +74 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/LoggerSecurityTests.swift +149 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/NativeContractTests.swift +160 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/NetworkTransportTests.swift +1109 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/PopulateDeltaCacheTests.swift +573 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/RedirectPolicyTests.swift +63 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/RsaContractTests.swift +281 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/SessionKeyRequiredTests.swift +196 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/TestZipWriter.swift +235 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/UpdateLifecycleTests.swift +470 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/WebKitOverlayGuardTests.swift +32 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ZipArchiveReaderTests.swift +302 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ZipBundleExtractionTests.swift +122 -0
- package/package.json +80 -43
- package/scripts/builtin-hashes-lib.mjs +148 -0
- package/scripts/capacitor-builtin-hashes-hook.mjs +65 -0
- package/scripts/resolve-real-path.mjs +25 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +0 -1130
- package/ios/Plugin/BundleInfo.swift +0 -113
- package/ios/Plugin/BundleStatus.swift +0 -48
- package/ios/Plugin/CapacitorUpdater.swift +0 -858
- package/ios/Plugin/CapacitorUpdaterPlugin.h +0 -10
- package/ios/Plugin/CapacitorUpdaterPlugin.m +0 -27
- package/ios/Plugin/CapacitorUpdaterPlugin.swift +0 -677
- package/ios/Plugin/CryptoCipher.swift +0 -240
- /package/{LICENCE → LICENSE} +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayCondition.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayUntilNext.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/Info.plist +0 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Security
|
|
3
|
+
import XCTest
|
|
4
|
+
@testable import CapacitorUpdaterPlugin
|
|
5
|
+
|
|
6
|
+
final class RsaContractTests: XCTestCase {
|
|
7
|
+
private static let contract: [String: Any] = {
|
|
8
|
+
do {
|
|
9
|
+
let data = try Data(contentsOf: contractFileURL())
|
|
10
|
+
let value = try JSONSerialization.jsonObject(with: data)
|
|
11
|
+
guard let contract = value as? [String: Any] else {
|
|
12
|
+
throw ContractError.invalidRoot
|
|
13
|
+
}
|
|
14
|
+
return contract
|
|
15
|
+
} catch {
|
|
16
|
+
XCTFail("Unable to load RSA contract fixture: \(error)")
|
|
17
|
+
return [:]
|
|
18
|
+
}
|
|
19
|
+
}()
|
|
20
|
+
|
|
21
|
+
private enum ContractError: Error {
|
|
22
|
+
case missingFixture
|
|
23
|
+
case invalidRoot
|
|
24
|
+
case invalidCases(String)
|
|
25
|
+
case invalidCase(String)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private struct SignedVector {
|
|
29
|
+
let key: String
|
|
30
|
+
let ciphertext: Data
|
|
31
|
+
let plaintext: Data
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
override class func setUp() {
|
|
35
|
+
super.setUp()
|
|
36
|
+
CryptoCipher.setLogger(Logger(withTag: "RsaContractTests", options: Logger.Options(level: .silent)))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private static func contractFileURL() throws -> URL {
|
|
40
|
+
let fileManager = FileManager.default
|
|
41
|
+
let roots = [
|
|
42
|
+
URL(fileURLWithPath: fileManager.currentDirectoryPath),
|
|
43
|
+
URL(fileURLWithPath: #filePath)
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
for root in roots {
|
|
47
|
+
var current = root
|
|
48
|
+
while current.path != "/" {
|
|
49
|
+
let candidate = current
|
|
50
|
+
.appendingPathComponent("native-contract-tests")
|
|
51
|
+
.appendingPathComponent("crypto-rsa.json")
|
|
52
|
+
if fileManager.fileExists(atPath: candidate.path) {
|
|
53
|
+
return candidate
|
|
54
|
+
}
|
|
55
|
+
current.deleteLastPathComponent()
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
throw ContractError.missingFixture
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private func contractCases(_ key: String) throws -> [[String: Any]] {
|
|
62
|
+
guard let cases = Self.contract[key] as? [[String: Any]] else {
|
|
63
|
+
throw ContractError.invalidCases(key)
|
|
64
|
+
}
|
|
65
|
+
return cases
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private func dictionary(_ source: [String: Any], _ key: String, id: String) throws -> [String: Any] {
|
|
69
|
+
guard let value = source[key] as? [String: Any] else {
|
|
70
|
+
throw ContractError.invalidCase("\(id).\(key)")
|
|
71
|
+
}
|
|
72
|
+
return value
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private func string(_ source: [String: Any], _ key: String, id: String) throws -> String {
|
|
76
|
+
guard let value = source[key] as? String else {
|
|
77
|
+
throw ContractError.invalidCase("\(id).\(key)")
|
|
78
|
+
}
|
|
79
|
+
return value
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private func bool(_ source: [String: Any], _ key: String, id: String) throws -> Bool {
|
|
83
|
+
guard let value = source[key] as? Bool else {
|
|
84
|
+
throw ContractError.invalidCase("\(id).\(key)")
|
|
85
|
+
}
|
|
86
|
+
return value
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private func fixturePublicKey() throws -> String {
|
|
90
|
+
if let key = Self.contract["publicKeyPem"] as? String {
|
|
91
|
+
return key
|
|
92
|
+
}
|
|
93
|
+
throw ContractError.invalidRoot
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private func hexToData(_ hex: String) -> Data? {
|
|
97
|
+
var data = Data()
|
|
98
|
+
var iterator = hex.makeIterator()
|
|
99
|
+
while let char1 = iterator.next(), let char2 = iterator.next() {
|
|
100
|
+
guard let byte = UInt8(String([char1, char2]), radix: 16) else {
|
|
101
|
+
return nil
|
|
102
|
+
}
|
|
103
|
+
data.append(byte)
|
|
104
|
+
}
|
|
105
|
+
return data
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private func dataToHex(_ data: Data) -> String {
|
|
109
|
+
data.map { String(format: "%02x", $0) }.joined()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
func testRsaPublicDecryptMatchesNativeContract() throws {
|
|
113
|
+
let publicKey = try fixturePublicKey()
|
|
114
|
+
guard let rsaPublicKey = RSAPublicKey.load(rsaPublicKey: publicKey) else {
|
|
115
|
+
XCTFail("Fixture public key must load")
|
|
116
|
+
return
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for testCase in try contractCases("rsaPublicDecrypt") {
|
|
120
|
+
let id = try string(testCase, "id", id: "rsaPublicDecrypt")
|
|
121
|
+
let input = try dictionary(testCase, "input", id: id)
|
|
122
|
+
let expect = try dictionary(testCase, "expect", id: id)
|
|
123
|
+
let ciphertextHex = try string(input, "ciphertextHex", id: id)
|
|
124
|
+
let expectedPlaintextHex = try string(expect, "plaintextHex", id: id)
|
|
125
|
+
|
|
126
|
+
guard let ciphertext = hexToData(ciphertextHex) else {
|
|
127
|
+
XCTFail("\(id): invalid ciphertext hex")
|
|
128
|
+
continue
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
guard let decrypted = rsaPublicKey.decrypt(data: ciphertext) else {
|
|
132
|
+
XCTFail("\(id): RSA public decrypt returned nil")
|
|
133
|
+
continue
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
XCTAssertEqual(dataToHex(decrypted), expectedPlaintextHex, id)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
func testDecryptChecksumMatchesNativeContract() throws {
|
|
141
|
+
let publicKey = try fixturePublicKey()
|
|
142
|
+
|
|
143
|
+
for testCase in try contractCases("decryptChecksum") {
|
|
144
|
+
let id = try string(testCase, "id", id: "decryptChecksum")
|
|
145
|
+
let input = try dictionary(testCase, "input", id: id)
|
|
146
|
+
let expect = try dictionary(testCase, "expect", id: id)
|
|
147
|
+
let checksumHex = try string(input, "checksumHex", id: id)
|
|
148
|
+
let expectedDecryptedHex = try string(expect, "decryptedHex", id: id)
|
|
149
|
+
|
|
150
|
+
let expectedCiphertextHex = try string(expect, "ciphertextHex", id: id)
|
|
151
|
+
|
|
152
|
+
let result = try CryptoCipher.decryptChecksum(checksum: checksumHex, publicKey: publicKey)
|
|
153
|
+
XCTAssertEqual(result, expectedDecryptedHex, id)
|
|
154
|
+
let signed = try CryptoCipher.decryptBundleChecksum(checksum: checksumHex, publicKey: publicKey)
|
|
155
|
+
XCTAssertEqual(signed.plain, expectedDecryptedHex, id)
|
|
156
|
+
XCTAssertEqual(signed.ciphertext, expectedCiphertextHex, id)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
func testDecryptChecksumInvalidMatchesNativeContract() throws {
|
|
161
|
+
let publicKey = try fixturePublicKey()
|
|
162
|
+
|
|
163
|
+
for testCase in try contractCases("decryptChecksumInvalid") {
|
|
164
|
+
let id = try string(testCase, "id", id: "decryptChecksumInvalid")
|
|
165
|
+
let input = try dictionary(testCase, "input", id: id)
|
|
166
|
+
let expect = try dictionary(testCase, "expect", id: id)
|
|
167
|
+
let checksumHex = try string(input, "checksumHex", id: id)
|
|
168
|
+
let shouldThrow = try bool(expect, "throws", id: id)
|
|
169
|
+
|
|
170
|
+
if shouldThrow {
|
|
171
|
+
XCTAssertThrowsError(
|
|
172
|
+
try CryptoCipher.decryptChecksum(checksum: checksumHex, publicKey: publicKey),
|
|
173
|
+
id
|
|
174
|
+
)
|
|
175
|
+
} else {
|
|
176
|
+
XCTAssertNoThrow(
|
|
177
|
+
try CryptoCipher.decryptChecksum(checksum: checksumHex, publicKey: publicKey),
|
|
178
|
+
id
|
|
179
|
+
)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
func testCalcKeyIdMatchesNativeContract() throws {
|
|
185
|
+
for testCase in try contractCases("calcKeyId") {
|
|
186
|
+
let id = try string(testCase, "id", id: "calcKeyId")
|
|
187
|
+
let input = try dictionary(testCase, "input", id: id)
|
|
188
|
+
let expect = try dictionary(testCase, "expect", id: id)
|
|
189
|
+
let publicKey = try string(input, "publicKeyPem", id: id)
|
|
190
|
+
let expectedKeyId = try string(expect, "keyId", id: id)
|
|
191
|
+
|
|
192
|
+
XCTAssertEqual(CryptoCipher.calcKeyId(publicKey: publicKey), expectedKeyId, id)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
func testRsaPublicKeyLoadMatchesNativeContract() throws {
|
|
197
|
+
for testCase in try contractCases("rsaPublicKeyLoad") {
|
|
198
|
+
let id = try string(testCase, "id", id: "rsaPublicKeyLoad")
|
|
199
|
+
let input = try dictionary(testCase, "input", id: id)
|
|
200
|
+
let expect = try dictionary(testCase, "expect", id: id)
|
|
201
|
+
let publicKey = try string(input, "publicKeyPem", id: id)
|
|
202
|
+
let shouldLoad = try bool(expect, "loads", id: id)
|
|
203
|
+
|
|
204
|
+
let loaded = RSAPublicKey.load(rsaPublicKey: publicKey) != nil
|
|
205
|
+
XCTAssertEqual(loaded, shouldLoad, id)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private func signedVector() throws -> SignedVector {
|
|
210
|
+
let testCase = try XCTUnwrap(contractCases("rsaPublicDecrypt").first)
|
|
211
|
+
let input = try dictionary(testCase, "input", id: "signedVector")
|
|
212
|
+
let expect = try dictionary(testCase, "expect", id: "signedVector")
|
|
213
|
+
let ciphertext = try XCTUnwrap(hexToData(string(input, "ciphertextHex", id: "signedVector")))
|
|
214
|
+
let plaintext = try XCTUnwrap(hexToData(string(expect, "plaintextHex", id: "signedVector")))
|
|
215
|
+
return SignedVector(key: try fixturePublicKey(), ciphertext: ciphertext, plaintext: plaintext)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
func testPublicDecryptRejectsModifiedAndWrongLengthSignatures() throws {
|
|
219
|
+
let vector = try signedVector()
|
|
220
|
+
let key = try XCTUnwrap(RSAPublicKey.load(rsaPublicKey: vector.key))
|
|
221
|
+
XCTAssertNil(key.decrypt(data: Data()))
|
|
222
|
+
XCTAssertNil(key.decrypt(data: Data(vector.ciphertext.dropLast())))
|
|
223
|
+
XCTAssertNil(key.decrypt(data: vector.ciphertext + Data([0])))
|
|
224
|
+
for index in vector.ciphertext.indices {
|
|
225
|
+
var changed = vector.ciphertext
|
|
226
|
+
changed[index] ^= 1
|
|
227
|
+
XCTAssertNil(key.decrypt(data: changed), "Modified signature byte \(index)")
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
func testTypeOnePaddingRequiresCompleteValidBlock() {
|
|
232
|
+
let payload = [UInt8](repeating: 0xab, count: 32)
|
|
233
|
+
let valid = Data([0, 1] + [UInt8](repeating: 0xff, count: 221) + [0] + payload)
|
|
234
|
+
XCTAssertEqual(RSAPublicKey.unpadSignature(valid), Data(payload))
|
|
235
|
+
|
|
236
|
+
// RFC 8017 permits a minimum of eight padding bytes.
|
|
237
|
+
let longestPayload = Data(repeating: 0xab, count: 245)
|
|
238
|
+
let minimumPadding = Data([0, 1] + [UInt8](repeating: 0xff, count: 8) + [0]) + longestPayload
|
|
239
|
+
XCTAssertEqual(RSAPublicKey.unpadSignature(minimumPadding), longestPayload)
|
|
240
|
+
let shortPadding = Data([0, 1] + [UInt8](repeating: 0xff, count: 7) + [0]) + Data(repeating: 0xab, count: 246)
|
|
241
|
+
XCTAssertNil(RSAPublicKey.unpadSignature(shortPadding))
|
|
242
|
+
|
|
243
|
+
for (index, replacement) in [(0, UInt8(1)), (1, UInt8(2)), (2, UInt8(0x7f))] {
|
|
244
|
+
var malformed = valid
|
|
245
|
+
malformed[index] = replacement
|
|
246
|
+
XCTAssertNil(RSAPublicKey.unpadSignature(malformed))
|
|
247
|
+
}
|
|
248
|
+
XCTAssertNil(RSAPublicKey.unpadSignature(Data([0, 1] + [UInt8](repeating: 0xff, count: 254))))
|
|
249
|
+
XCTAssertNil(RSAPublicKey.unpadSignature(Data([0, 1] + [UInt8](repeating: 0xff, count: 253) + [0])))
|
|
250
|
+
XCTAssertNil(RSAPublicKey.unpadSignature(Data(valid.dropLast())))
|
|
251
|
+
XCTAssertNil(RSAPublicKey.unpadSignature(valid + Data([0])))
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
func testKeyCacheHandlesInvalidKeysRotationAndConcurrentCallers() throws {
|
|
255
|
+
let vector = try signedVector()
|
|
256
|
+
let original = try XCTUnwrap(RSAPublicKey.load(rsaPublicKey: vector.key))
|
|
257
|
+
XCTAssertEqual(original.decrypt(data: vector.ciphertext), vector.plaintext)
|
|
258
|
+
XCTAssertNil(RSAPublicKey.load(rsaPublicKey: "not-a-public-key"))
|
|
259
|
+
XCTAssertNil(RSAPublicKey.load(rsaPublicKey: "YWJj"))
|
|
260
|
+
|
|
261
|
+
let attributes: [CFString: Any] = [kSecAttrKeyType: kSecAttrKeyTypeRSA, kSecAttrKeySizeInBits: 2048]
|
|
262
|
+
let privateKey = try XCTUnwrap(SecKeyCreateRandomKey(attributes as CFDictionary, nil))
|
|
263
|
+
let publicKey = try XCTUnwrap(SecKeyCopyPublicKey(privateKey))
|
|
264
|
+
let encoded = try XCTUnwrap(SecKeyCopyExternalRepresentation(publicKey, nil)) as Data
|
|
265
|
+
let otherPem = "-----BEGIN RSA PUBLIC KEY-----\n\(encoded.base64EncodedString())\n-----END RSA PUBLIC KEY-----"
|
|
266
|
+
let other = try XCTUnwrap(RSAPublicKey.load(rsaPublicKey: otherPem))
|
|
267
|
+
XCTAssertNil(other.decrypt(data: vector.ciphertext))
|
|
268
|
+
XCTAssertEqual(original.decrypt(data: vector.ciphertext), vector.plaintext, "Imported instances retain their own key")
|
|
269
|
+
|
|
270
|
+
DispatchQueue.concurrentPerform(iterations: 128) { index in
|
|
271
|
+
let useOriginal = index.isMultiple(of: 2)
|
|
272
|
+
let loaded = RSAPublicKey.load(rsaPublicKey: useOriginal ? vector.key : otherPem)
|
|
273
|
+
XCTAssertNotNil(loaded)
|
|
274
|
+
XCTAssertEqual(loaded?.decrypt(data: vector.ciphertext), useOriginal ? vector.plaintext : nil)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
let escaped = vector.key.replacingOccurrences(of: "\n", with: "\\n")
|
|
278
|
+
let reloaded = try XCTUnwrap(RSAPublicKey.load(rsaPublicKey: escaped))
|
|
279
|
+
XCTAssertEqual(reloaded.decrypt(data: vector.ciphertext), vector.plaintext)
|
|
280
|
+
}
|
|
281
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import XCTest
|
|
2
|
+
@testable import CapacitorUpdaterPlugin
|
|
3
|
+
|
|
4
|
+
final class SessionKeyRequiredTests: XCTestCase {
|
|
5
|
+
private enum Fixture {
|
|
6
|
+
static let contract: [String: Any] = {
|
|
7
|
+
guard let url = try? locateContractFile(),
|
|
8
|
+
let data = try? Data(contentsOf: url),
|
|
9
|
+
let value = try? JSONSerialization.jsonObject(with: data),
|
|
10
|
+
let dict = value as? [String: Any] else {
|
|
11
|
+
XCTFail("Unable to load RSA contract fixture")
|
|
12
|
+
return [:]
|
|
13
|
+
}
|
|
14
|
+
return dict
|
|
15
|
+
}()
|
|
16
|
+
|
|
17
|
+
static var publicKeyPem: String {
|
|
18
|
+
contract["publicKeyPem"] as? String ?? ""
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
private static func locateContractFile() throws -> URL {
|
|
22
|
+
let fileManager = FileManager.default
|
|
23
|
+
let roots = [
|
|
24
|
+
URL(fileURLWithPath: fileManager.currentDirectoryPath),
|
|
25
|
+
URL(fileURLWithPath: #filePath)
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
for root in roots {
|
|
29
|
+
var current = root
|
|
30
|
+
while current.path != "/" {
|
|
31
|
+
let candidate = current
|
|
32
|
+
.appendingPathComponent("native-contract-tests")
|
|
33
|
+
.appendingPathComponent("crypto-rsa.json")
|
|
34
|
+
if fileManager.fileExists(atPath: candidate.path) {
|
|
35
|
+
return candidate
|
|
36
|
+
}
|
|
37
|
+
current.deleteLastPathComponent()
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
throw NSError(domain: "SessionKeyRequiredTests", code: 1)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private class StatsTrackingCapgoUpdater: CapgoUpdater {
|
|
45
|
+
var sentStatsActions: [String] = []
|
|
46
|
+
|
|
47
|
+
override func sendStats(action: String, versionName: String? = nil, oldVersionName: String? = "") {
|
|
48
|
+
sentStatsActions.append(action)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private final class AutoUpdateSessionKeyCapgoUpdater: StatsTrackingCapgoUpdater {
|
|
53
|
+
override func getLatest(url: URL, channel: String?, appIdOverride: String? = nil) -> AppVersion {
|
|
54
|
+
let latest = AppVersion()
|
|
55
|
+
latest.version = "2.0.0"
|
|
56
|
+
latest.url = "https://example.com/update.zip"
|
|
57
|
+
return latest
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
override func getCurrentBundle() -> BundleInfo {
|
|
61
|
+
BundleInfo(id: "current-id", version: "1.0.0", status: .SUCCESS, downloaded: Date(), checksum: "abc")
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private final class PendingBundleAutoUpdateCapgoUpdater: StatsTrackingCapgoUpdater {
|
|
66
|
+
override func getLatest(url: URL, channel: String?, appIdOverride: String? = nil) -> AppVersion {
|
|
67
|
+
let latest = AppVersion()
|
|
68
|
+
latest.version = "2.0.0"
|
|
69
|
+
latest.url = "https://example.com/update.zip"
|
|
70
|
+
latest.checksum = "abc123"
|
|
71
|
+
return latest
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
override func getCurrentBundle() -> BundleInfo {
|
|
75
|
+
BundleInfo(id: "current-id", version: "1.0.0", status: .SUCCESS, downloaded: Date(), checksum: "abc")
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
override func getBundleInfoByVersionName(version: String) -> BundleInfo? {
|
|
79
|
+
if version == "2.0.0" {
|
|
80
|
+
return BundleInfo(
|
|
81
|
+
id: "pending-id",
|
|
82
|
+
version: "2.0.0",
|
|
83
|
+
status: .PENDING,
|
|
84
|
+
downloaded: Date(),
|
|
85
|
+
checksum: "abc123"
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
return nil
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
override func getBundleInfosByVersionName(version: String) -> [BundleInfo] {
|
|
92
|
+
getBundleInfoByVersionName(version: version).map { [$0] } ?? []
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private final class TestableCapacitorUpdaterPlugin: CapacitorUpdaterPlugin {
|
|
97
|
+
override func runBackgroundDownloadWork(_ work: @escaping () -> Void) {
|
|
98
|
+
work()
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private var implementation: StatsTrackingCapgoUpdater!
|
|
103
|
+
|
|
104
|
+
override func setUp() {
|
|
105
|
+
super.setUp()
|
|
106
|
+
implementation = StatsTrackingCapgoUpdater()
|
|
107
|
+
implementation.setLogger(Logger(withTag: "SessionKeyRequiredTests", options: Logger.Options(level: .silent)))
|
|
108
|
+
implementation.setPublicKey(Fixture.publicKeyPem)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
override func tearDown() {
|
|
112
|
+
implementation.shutdown()
|
|
113
|
+
implementation = nil
|
|
114
|
+
super.tearDown()
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
func testDownloadManifestRejectsWhenSessionKeyMissing() {
|
|
118
|
+
let manifest = [ManifestEntry(file_name: "index.html", file_hash: "abc", download_url: "https://example.com/index.html")]
|
|
119
|
+
|
|
120
|
+
XCTAssertThrowsError(try implementation.downloadManifest(manifest: manifest, version: "1.0.0", sessionKey: "")) { error in
|
|
121
|
+
XCTAssertEqual((error as NSError).domain, "CapgoUpdater")
|
|
122
|
+
XCTAssertEqual((error as NSError).code, 1)
|
|
123
|
+
}
|
|
124
|
+
XCTAssertTrue(implementation.sentStatsActions.contains("session_key_required"))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
func testDownloadRejectsWhenSessionKeyMissing() {
|
|
128
|
+
let url = URL(string: "https://example.com/update.zip")!
|
|
129
|
+
|
|
130
|
+
XCTAssertThrowsError(try implementation.download(url: url, version: "1.0.0", sessionKey: "")) { error in
|
|
131
|
+
XCTAssertEqual((error as NSError).domain, "CapgoUpdater")
|
|
132
|
+
XCTAssertEqual((error as NSError).code, 1)
|
|
133
|
+
}
|
|
134
|
+
XCTAssertTrue(implementation.sentStatsActions.contains("session_key_required"))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
func testDownloadRejectsWhenSessionKeyFormatInvalid() {
|
|
138
|
+
let url = URL(string: "https://example.com/update.zip")!
|
|
139
|
+
|
|
140
|
+
XCTAssertThrowsError(try implementation.download(url: url, version: "1.0.0", sessionKey: "invalid-format")) { error in
|
|
141
|
+
XCTAssertEqual((error as NSError).domain, "CapgoUpdater")
|
|
142
|
+
XCTAssertEqual((error as NSError).code, 1)
|
|
143
|
+
}
|
|
144
|
+
XCTAssertTrue(implementation.sentStatsActions.contains("session_key_required"))
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
func testIsValidSessionKeyRejectsEmptyComponents() {
|
|
148
|
+
XCTAssertFalse(CryptoCipher.isValidSessionKey(""))
|
|
149
|
+
XCTAssertFalse(CryptoCipher.isValidSessionKey(":"))
|
|
150
|
+
XCTAssertFalse(CryptoCipher.isValidSessionKey("abc:"))
|
|
151
|
+
XCTAssertFalse(CryptoCipher.isValidSessionKey(":xyz"))
|
|
152
|
+
XCTAssertFalse(CryptoCipher.isValidSessionKey("invalid-format"))
|
|
153
|
+
XCTAssertTrue(CryptoCipher.isValidSessionKey("abc:def"))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
func testAutoUpdateRejectsMissingSessionKeyWhenPendingBundleExists() {
|
|
157
|
+
let updater = PendingBundleAutoUpdateCapgoUpdater()
|
|
158
|
+
updater.setLogger(Logger(withTag: "SessionKeyRequiredTests", options: Logger.Options(level: .silent)))
|
|
159
|
+
updater.setPublicKey(Fixture.publicKeyPem)
|
|
160
|
+
|
|
161
|
+
let plugin = TestableCapacitorUpdaterPlugin()
|
|
162
|
+
plugin.implementation = updater
|
|
163
|
+
plugin.setAutoUpdateModeForTesting("onlyDownload")
|
|
164
|
+
plugin.setUpdateUrlForTesting("https://example.com/channel")
|
|
165
|
+
|
|
166
|
+
plugin.backgroundDownload()
|
|
167
|
+
|
|
168
|
+
XCTAssertTrue(updater.sentStatsActions.contains("session_key_required"))
|
|
169
|
+
updater.shutdown()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
func testAutoUpdateBackgroundDownloadRejectsWhenSessionKeyMissing() {
|
|
173
|
+
let updater = AutoUpdateSessionKeyCapgoUpdater()
|
|
174
|
+
updater.setLogger(Logger(withTag: "SessionKeyRequiredTests", options: Logger.Options(level: .silent)))
|
|
175
|
+
updater.setPublicKey(Fixture.publicKeyPem)
|
|
176
|
+
|
|
177
|
+
let plugin = TestableCapacitorUpdaterPlugin()
|
|
178
|
+
plugin.implementation = updater
|
|
179
|
+
plugin.setAutoUpdateModeForTesting("onlyDownload")
|
|
180
|
+
plugin.setUpdateUrlForTesting("https://example.com/channel")
|
|
181
|
+
|
|
182
|
+
plugin.backgroundDownload()
|
|
183
|
+
|
|
184
|
+
XCTAssertTrue(updater.sentStatsActions.contains("session_key_required"))
|
|
185
|
+
updater.shutdown()
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
func testAllowsUpdateWhenNoPublicKeyConfigured() throws {
|
|
189
|
+
implementation.setPublicKey("")
|
|
190
|
+
let manifest = [ManifestEntry(file_name: "index.html", file_hash: "abc", download_url: "http://[")]
|
|
191
|
+
|
|
192
|
+
// Invalid URL fails fast after the session-key gate, proving the gate did not block.
|
|
193
|
+
XCTAssertThrowsError(try implementation.downloadManifest(manifest: manifest, version: "1.0.0", sessionKey: ""))
|
|
194
|
+
XCTAssertFalse(implementation.sentStatsActions.contains("session_key_required"))
|
|
195
|
+
}
|
|
196
|
+
}
|