@storacha/encrypt-upload-client 1.1.56 → 1.1.58
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/dist/config/constants.d.ts +3 -3
- package/dist/config/constants.js +4 -3
- package/dist/config/env.d.ts +9 -6
- package/dist/config/service.d.ts +13 -13
- package/dist/core/client.d.ts +54 -41
- package/dist/core/client.js +68 -56
- package/dist/core/errors.d.ts +6 -6
- package/dist/core/metadata/encrypted-metadata.d.ts +13 -8
- package/dist/core/metadata/kms-metadata.d.ts +68 -36
- package/dist/core/metadata/lit-metadata.d.ts +63 -28
- package/dist/crypto/adapters/kms-crypto-adapter.d.ts +172 -137
- package/dist/crypto/adapters/lit-crypto-adapter.d.ts +107 -86
- package/dist/crypto/factories.browser.d.ts +9 -5
- package/dist/crypto/factories.browser.js +15 -7
- package/dist/crypto/factories.node.d.ts +13 -6
- package/dist/crypto/factories.node.js +19 -13
- package/dist/crypto/index.d.ts +5 -5
- package/dist/crypto/index.js +5 -5
- package/dist/crypto/symmetric/generic-aes-ctr-streaming-crypto.d.ts +58 -54
- package/dist/crypto/symmetric/generic-aes-ctr-streaming-crypto.js +174 -146
- package/dist/crypto/symmetric/node-aes-cbc-crypto.d.ts +36 -32
- package/dist/crypto/symmetric/node-aes-cbc-crypto.js +101 -95
- package/dist/examples/decrypt-test.d.ts +2 -2
- package/dist/examples/decrypt-test.js +78 -69
- package/dist/examples/encrypt-test.d.ts +5 -3
- package/dist/examples/encrypt-test.js +58 -55
- package/dist/handlers/decrypt-handler.d.ts +19 -5
- package/dist/handlers/encrypt-handler.d.ts +9 -3
- package/dist/handlers/encrypt-handler.js +93 -57
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/protocols/lit.d.ts +33 -9
- package/dist/protocols/lit.js +134 -98
- package/dist/test/cid-verification.spec.d.ts +2 -2
- package/dist/test/cid-verification.spec.js +341 -313
- package/dist/test/crypto-compatibility.spec.d.ts +2 -2
- package/dist/test/crypto-compatibility.spec.js +184 -120
- package/dist/test/crypto-counter-security.spec.d.ts +2 -2
- package/dist/test/crypto-counter-security.spec.js +177 -138
- package/dist/test/crypto-streaming.spec.d.ts +2 -2
- package/dist/test/crypto-streaming.spec.js +208 -126
- package/dist/test/encrypted-metadata.spec.d.ts +2 -2
- package/dist/test/encrypted-metadata.spec.js +89 -62
- package/dist/test/factories.spec.d.ts +2 -2
- package/dist/test/factories.spec.js +275 -139
- package/dist/test/file-metadata.spec.d.ts +2 -2
- package/dist/test/file-metadata.spec.js +472 -416
- package/dist/test/fixtures/test-fixtures.d.ts +25 -20
- package/dist/test/fixtures/test-fixtures.js +61 -53
- package/dist/test/helpers/test-file-utils.d.ts +19 -14
- package/dist/test/helpers/test-file-utils.js +78 -76
- package/dist/test/https-enforcement.spec.d.ts +2 -2
- package/dist/test/https-enforcement.spec.js +278 -124
- package/dist/test/kms-crypto-adapter.spec.d.ts +2 -2
- package/dist/test/kms-crypto-adapter.spec.js +473 -304
- package/dist/test/lit-crypto-adapter.spec.d.ts +2 -2
- package/dist/test/lit-crypto-adapter.spec.js +206 -118
- package/dist/test/memory-efficiency.spec.d.ts +2 -2
- package/dist/test/memory-efficiency.spec.js +100 -87
- package/dist/test/mocks/key-manager.d.ts +71 -38
- package/dist/test/mocks/key-manager.js +129 -113
- package/dist/test/node-crypto-adapter.spec.d.ts +2 -2
- package/dist/test/node-crypto-adapter.spec.js +155 -102
- package/dist/test/node-generic-crypto-adapter.spec.d.ts +2 -2
- package/dist/test/node-generic-crypto-adapter.spec.js +134 -94
- package/dist/test/setup.d.ts +2 -2
- package/dist/test/setup.js +8 -9
- package/dist/tsconfig.spec.tsbuildinfo +1 -1
- package/dist/types.d.ts +219 -181
- package/dist/utils/file-metadata.d.ts +19 -13
- package/dist/utils.d.ts +14 -5
- package/package.json +4 -4
|
@@ -1,314 +1,342 @@
|
|
|
1
|
-
import assert from 'node:assert'
|
|
2
|
-
import { describe, test } from 'node:test'
|
|
3
|
-
import { getCarFileFromPublicGateway } from '../src/handlers/decrypt-handler.js'
|
|
4
|
-
import { createTestCar } from './helpers/test-file-utils.js'
|
|
1
|
+
import assert from 'node:assert'
|
|
2
|
+
import { describe, test } from 'node:test'
|
|
3
|
+
import { getCarFileFromPublicGateway } from '../src/handlers/decrypt-handler.js'
|
|
4
|
+
import { createTestCar } from './helpers/test-file-utils.js'
|
|
5
5
|
await describe('CID Verification', async () => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
await
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
6
|
+
await describe('getCarFileFromPublicGateway', async () => {
|
|
7
|
+
await test('should construct correct gateway URL format', async () => {
|
|
8
|
+
// This is a basic test to verify the function exists and can be called
|
|
9
|
+
// Integration tests with real network calls would test full functionality
|
|
10
|
+
const gatewayURL = new URL('https://example.com')
|
|
11
|
+
const testCID =
|
|
12
|
+
'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq'
|
|
13
|
+
// Mock fetch to avoid network calls in unit tests
|
|
14
|
+
const originalFetch = globalThis.fetch
|
|
15
|
+
let capturedURL = ''
|
|
16
|
+
// @ts-ignore - Mock fetch for testing
|
|
17
|
+
globalThis.fetch = async (url) => {
|
|
18
|
+
capturedURL = url.toString()
|
|
19
|
+
// Return a mock response that looks like a failed fetch
|
|
20
|
+
return {
|
|
21
|
+
ok: false,
|
|
22
|
+
status: 404,
|
|
23
|
+
statusText: 'Not Found',
|
|
24
|
+
arrayBuffer: async () => {
|
|
25
|
+
throw new Error('Mock 404')
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
await getCarFileFromPublicGateway(gatewayURL, testCID)
|
|
31
|
+
} catch (error) {
|
|
32
|
+
// We expect this to fail with our mock, that's fine
|
|
33
|
+
} finally {
|
|
34
|
+
globalThis.fetch = originalFetch
|
|
35
|
+
}
|
|
36
|
+
// Verify the URL was constructed correctly
|
|
37
|
+
const expectedURL = `https://example.com/ipfs/${testCID}?format=car`
|
|
38
|
+
assert.strictEqual(
|
|
39
|
+
capturedURL,
|
|
40
|
+
expectedURL,
|
|
41
|
+
'Should construct correct gateway URL'
|
|
42
|
+
)
|
|
43
|
+
})
|
|
44
|
+
await test('should be exported and callable', async () => {
|
|
45
|
+
// Basic smoke test
|
|
46
|
+
assert.strictEqual(
|
|
47
|
+
typeof getCarFileFromPublicGateway,
|
|
48
|
+
'function',
|
|
49
|
+
'Should be a function'
|
|
50
|
+
)
|
|
51
|
+
// Verify it requires proper parameters
|
|
52
|
+
try {
|
|
53
|
+
// @ts-ignore - intentionally testing invalid input
|
|
54
|
+
await getCarFileFromPublicGateway(null, 'test')
|
|
55
|
+
assert.fail('Should throw error for null gateway URL')
|
|
56
|
+
} catch (error) {
|
|
57
|
+
// Expected to fail - good
|
|
58
|
+
assert(error instanceof Error, 'Should throw an Error')
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
await test('should validate CID format', async () => {
|
|
62
|
+
const gatewayURL = new URL('https://example.com')
|
|
63
|
+
// Mock fetch to return valid CAR response
|
|
64
|
+
const originalFetch = globalThis.fetch
|
|
65
|
+
// @ts-ignore - Mock fetch for testing
|
|
66
|
+
globalThis.fetch = async (url) => ({
|
|
67
|
+
ok: true,
|
|
68
|
+
status: 200,
|
|
69
|
+
statusText: 'OK',
|
|
70
|
+
arrayBuffer: async () => new ArrayBuffer(100), // Mock CAR data
|
|
71
|
+
})
|
|
72
|
+
try {
|
|
73
|
+
await getCarFileFromPublicGateway(gatewayURL, 'invalid-cid')
|
|
74
|
+
assert.fail('Should throw error for invalid CID')
|
|
75
|
+
} catch (error) {
|
|
76
|
+
// Expected to fail due to invalid CID format
|
|
77
|
+
assert(error instanceof Error, 'Should throw an Error for invalid CID')
|
|
78
|
+
} finally {
|
|
79
|
+
globalThis.fetch = originalFetch
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
await test('should accept valid CAR file with matching root CID', async () => {
|
|
83
|
+
const gatewayURL = new URL('https://example.com')
|
|
84
|
+
// Create a valid KMS metadata CAR file
|
|
85
|
+
const testContent = {
|
|
86
|
+
encryptedDataCID:
|
|
87
|
+
'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq',
|
|
88
|
+
encryptedSymmetricKey: 'test-encrypted-key',
|
|
89
|
+
space: 'did:key:z6MkwDK3M4PxU1FqcSt6quBH1xRBSGnPRdQYP9B13h3Wq5X1',
|
|
90
|
+
kms: {
|
|
91
|
+
provider: 'google-kms',
|
|
92
|
+
keyId: 'test-key-id',
|
|
93
|
+
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
94
|
+
},
|
|
95
|
+
}
|
|
96
|
+
const { car, actualRootCID } = await createTestCar(testContent)
|
|
97
|
+
// Mock fetch to return the valid CAR
|
|
98
|
+
const originalFetch = globalThis.fetch
|
|
99
|
+
// @ts-ignore - Mock fetch for testing
|
|
100
|
+
globalThis.fetch = async (url) => ({
|
|
101
|
+
ok: true,
|
|
102
|
+
status: 200,
|
|
103
|
+
statusText: 'OK',
|
|
104
|
+
arrayBuffer: async () => car.buffer,
|
|
105
|
+
})
|
|
106
|
+
try {
|
|
107
|
+
const result = await getCarFileFromPublicGateway(
|
|
108
|
+
gatewayURL,
|
|
109
|
+
actualRootCID.toString()
|
|
110
|
+
)
|
|
111
|
+
assert(result instanceof Uint8Array, 'Should return Uint8Array')
|
|
112
|
+
assert.deepStrictEqual(result, car, 'Should return the exact CAR file')
|
|
113
|
+
} finally {
|
|
114
|
+
globalThis.fetch = originalFetch
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
await test('should reject CAR file with wrong root CID (tampering detection)', async () => {
|
|
118
|
+
const gatewayURL = new URL('https://example.com')
|
|
119
|
+
// Create a CAR file with content A
|
|
120
|
+
const originalContent = {
|
|
121
|
+
encryptedDataCID:
|
|
122
|
+
'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq',
|
|
123
|
+
encryptedSymmetricKey: 'original-encrypted-key',
|
|
124
|
+
space: 'did:key:z6MkwDK3M4PxU1FqcSt6quBH1xRBSGnPRdQYP9B13h3Wq5X1',
|
|
125
|
+
kms: {
|
|
126
|
+
provider: 'google-kms',
|
|
127
|
+
keyId: 'original-key-id',
|
|
128
|
+
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
129
|
+
},
|
|
130
|
+
}
|
|
131
|
+
const { actualRootCID: originalCID } = await createTestCar(
|
|
132
|
+
originalContent
|
|
133
|
+
)
|
|
134
|
+
// Create a different CAR file with content B
|
|
135
|
+
const tamperedContent = {
|
|
136
|
+
encryptedDataCID:
|
|
137
|
+
'bafkreidb6v6sjfnpnf6lqkh7p4w7zfzqfuzn2lqhp5x6zkojfuzwzlhpny',
|
|
138
|
+
encryptedSymmetricKey: 'tampered-encrypted-key',
|
|
139
|
+
space: 'did:key:z6MkMaliciousSpaceDIDThatShouldNotBeAccepted',
|
|
140
|
+
kms: {
|
|
141
|
+
provider: 'google-kms',
|
|
142
|
+
keyId: 'tampered-key-id',
|
|
143
|
+
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
144
|
+
},
|
|
145
|
+
}
|
|
146
|
+
const { car: tamperedCar } = await createTestCar(tamperedContent)
|
|
147
|
+
// Mock fetch to return the tampered CAR when requesting the original CID
|
|
148
|
+
const originalFetch = globalThis.fetch
|
|
149
|
+
// @ts-ignore - Mock fetch for testing
|
|
150
|
+
globalThis.fetch = async (url) => ({
|
|
151
|
+
ok: true,
|
|
152
|
+
status: 200,
|
|
153
|
+
statusText: 'OK',
|
|
154
|
+
arrayBuffer: async () => tamperedCar.buffer, // Return wrong CAR!
|
|
155
|
+
})
|
|
156
|
+
try {
|
|
157
|
+
await getCarFileFromPublicGateway(gatewayURL, originalCID.toString())
|
|
158
|
+
assert.fail('Should throw error for CID verification failure')
|
|
159
|
+
} catch (error) {
|
|
160
|
+
assert(error instanceof Error, 'Should throw an Error')
|
|
161
|
+
assert(
|
|
162
|
+
error.message.includes('CID verification failed'),
|
|
163
|
+
`Should mention CID verification failure. Got: ${error.message}`
|
|
164
|
+
)
|
|
165
|
+
} finally {
|
|
166
|
+
globalThis.fetch = originalFetch
|
|
167
|
+
}
|
|
168
|
+
})
|
|
169
|
+
await test('should detect when malicious gateway serves completely different CAR', async () => {
|
|
170
|
+
const gatewayURL = new URL('https://example.com')
|
|
171
|
+
// CID we're requesting
|
|
172
|
+
const requestedCID =
|
|
173
|
+
'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq'
|
|
174
|
+
// Create a completely different CAR file
|
|
175
|
+
const maliciousContent = {
|
|
176
|
+
encryptedDataCID:
|
|
177
|
+
'bafkreig6h5fimhfvj3wmlsf4fzj2d2ndqxd7qnugcgcmkn6dcqzxcq5zdu',
|
|
178
|
+
encryptedSymmetricKey: 'malicious-encrypted-key',
|
|
179
|
+
space: 'did:key:z6MkMaliciousSpaceDIDControlledByAttacker',
|
|
180
|
+
kms: {
|
|
181
|
+
provider: 'google-kms',
|
|
182
|
+
keyId: 'attacker-controlled-key',
|
|
183
|
+
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
184
|
+
},
|
|
185
|
+
}
|
|
186
|
+
const { car: maliciousCar } = await createTestCar(maliciousContent)
|
|
187
|
+
// Mock fetch to return the malicious CAR
|
|
188
|
+
const originalFetch = globalThis.fetch
|
|
189
|
+
// @ts-ignore - Mock fetch for testing
|
|
190
|
+
globalThis.fetch = async (url) => ({
|
|
191
|
+
ok: true,
|
|
192
|
+
status: 200,
|
|
193
|
+
statusText: 'OK',
|
|
194
|
+
arrayBuffer: async () => maliciousCar.buffer,
|
|
195
|
+
})
|
|
196
|
+
try {
|
|
197
|
+
await getCarFileFromPublicGateway(gatewayURL, requestedCID)
|
|
198
|
+
assert.fail('Should throw error when gateway serves wrong CAR')
|
|
199
|
+
} catch (error) {
|
|
200
|
+
assert(error instanceof Error, 'Should throw an Error')
|
|
201
|
+
assert(
|
|
202
|
+
error.message.includes('CID verification failed'),
|
|
203
|
+
`Should mention CID verification failure. Got: ${error.message}`
|
|
204
|
+
)
|
|
205
|
+
} finally {
|
|
206
|
+
globalThis.fetch = originalFetch
|
|
207
|
+
}
|
|
208
|
+
})
|
|
209
|
+
await test('should handle network errors gracefully', async () => {
|
|
210
|
+
const gatewayURL = new URL('https://example.com')
|
|
211
|
+
const testCID =
|
|
212
|
+
'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq'
|
|
213
|
+
// Mock fetch to return network error
|
|
214
|
+
const originalFetch = globalThis.fetch
|
|
215
|
+
// @ts-ignore - Mock fetch for testing
|
|
216
|
+
globalThis.fetch = async (url) => ({
|
|
217
|
+
ok: false,
|
|
218
|
+
status: 500,
|
|
219
|
+
statusText: 'Internal Server Error',
|
|
220
|
+
arrayBuffer: async () => {
|
|
221
|
+
throw new Error('Network error')
|
|
222
|
+
},
|
|
223
|
+
})
|
|
224
|
+
try {
|
|
225
|
+
await getCarFileFromPublicGateway(gatewayURL, testCID)
|
|
226
|
+
assert.fail('Should throw error for network errors')
|
|
227
|
+
} catch (error) {
|
|
228
|
+
assert(error instanceof Error, 'Should throw an Error')
|
|
229
|
+
assert(
|
|
230
|
+
error.message.includes('Failed to fetch'),
|
|
231
|
+
`Should mention fetch failure. Got: ${error.message}`
|
|
232
|
+
)
|
|
233
|
+
} finally {
|
|
234
|
+
globalThis.fetch = originalFetch
|
|
235
|
+
}
|
|
236
|
+
})
|
|
237
|
+
})
|
|
238
|
+
await describe('Metadata Tampering Detection', async () => {
|
|
239
|
+
await test('should prevent modification of space DID in metadata', async () => {
|
|
240
|
+
const gatewayURL = new URL('https://example.com')
|
|
241
|
+
// Original metadata with legitimate space DID
|
|
242
|
+
const originalMetadata = {
|
|
243
|
+
encryptedDataCID:
|
|
244
|
+
'bafkreie2hvzhqzj3ixnmjh7h3nkhdyp6qxhqltkq6qxf3wxq7hqxd6nzde',
|
|
245
|
+
encryptedSymmetricKey: 'encrypted-key-data',
|
|
246
|
+
space: 'did:key:z6MkwDK3M4PxU1FqcSt6quBH1xRBSGnPRdQYP9B13h3Wq5X1', // Original space
|
|
247
|
+
kms: {
|
|
248
|
+
provider: 'google-kms',
|
|
249
|
+
keyId: 'test-key-id',
|
|
250
|
+
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
251
|
+
},
|
|
252
|
+
}
|
|
253
|
+
// Tampered metadata with different space DID
|
|
254
|
+
const tamperedMetadata = {
|
|
255
|
+
...originalMetadata,
|
|
256
|
+
space: 'did:key:z6MkMaliciousSpaceDIDThatShouldNotBeAccepted', // Malicious space!
|
|
257
|
+
}
|
|
258
|
+
// Create CAR files for both
|
|
259
|
+
const { actualRootCID: originalCID } = await createTestCar(
|
|
260
|
+
originalMetadata
|
|
261
|
+
)
|
|
262
|
+
const { car: tamperedCar } = await createTestCar(tamperedMetadata)
|
|
263
|
+
// Mock fetch to return tampered CAR when requesting original CID
|
|
264
|
+
const originalFetch = globalThis.fetch
|
|
265
|
+
// @ts-ignore - Mock fetch for testing
|
|
266
|
+
globalThis.fetch = async (url) => ({
|
|
267
|
+
ok: true,
|
|
268
|
+
status: 200,
|
|
269
|
+
statusText: 'OK',
|
|
270
|
+
arrayBuffer: async () => tamperedCar.buffer, // Returns tampered metadata!
|
|
271
|
+
})
|
|
272
|
+
try {
|
|
273
|
+
await getCarFileFromPublicGateway(gatewayURL, originalCID.toString())
|
|
274
|
+
assert.fail('Should detect space DID tampering via CID verification')
|
|
275
|
+
} catch (error) {
|
|
276
|
+
assert(error instanceof Error, 'Should throw an Error')
|
|
277
|
+
assert(
|
|
278
|
+
error.message.includes('CID verification failed'),
|
|
279
|
+
`Should catch tampering via CID verification. Got: ${error.message}`
|
|
280
|
+
)
|
|
281
|
+
} finally {
|
|
282
|
+
globalThis.fetch = originalFetch
|
|
283
|
+
}
|
|
284
|
+
})
|
|
285
|
+
await test('should prevent complete metadata substitution attacks', async () => {
|
|
286
|
+
const gatewayURL = new URL('https://example.com')
|
|
287
|
+
// Original legitimate metadata
|
|
288
|
+
const originalMetadata = {
|
|
289
|
+
encryptedDataCID:
|
|
290
|
+
'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq',
|
|
291
|
+
encryptedSymmetricKey: 'original-encrypted-key',
|
|
292
|
+
space: 'did:key:z6MkwDK3M4PxU1FqcSt6quBH1xRBSGnPRdQYP9B13h3Wq5X1',
|
|
293
|
+
kms: {
|
|
294
|
+
provider: 'google-kms',
|
|
295
|
+
keyId: 'legitimate-key-id',
|
|
296
|
+
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
297
|
+
},
|
|
298
|
+
}
|
|
299
|
+
// Completely different malicious metadata (using same CID but different content)
|
|
300
|
+
const maliciousMetadata = {
|
|
301
|
+
encryptedDataCID:
|
|
302
|
+
'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq',
|
|
303
|
+
encryptedSymmetricKey: 'malicious-encrypted-key',
|
|
304
|
+
space: 'did:key:z6MkMaliciousSpaceDIDControlledByAttacker',
|
|
305
|
+
kms: {
|
|
306
|
+
provider: 'google-kms',
|
|
307
|
+
keyId: 'attacker-controlled-key',
|
|
308
|
+
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
309
|
+
},
|
|
310
|
+
}
|
|
311
|
+
// Create CAR files for both
|
|
312
|
+
const { actualRootCID: originalCID } = await createTestCar(
|
|
313
|
+
originalMetadata
|
|
314
|
+
)
|
|
315
|
+
const { car: maliciousCar } = await createTestCar(maliciousMetadata)
|
|
316
|
+
// Mock fetch to return completely different metadata
|
|
317
|
+
const originalFetch = globalThis.fetch
|
|
318
|
+
// @ts-ignore - Mock fetch for testing
|
|
319
|
+
globalThis.fetch = async (url) => ({
|
|
320
|
+
ok: true,
|
|
321
|
+
status: 200,
|
|
322
|
+
statusText: 'OK',
|
|
323
|
+
arrayBuffer: async () => maliciousCar.buffer, // Complete substitution attack!
|
|
324
|
+
})
|
|
325
|
+
try {
|
|
326
|
+
await getCarFileFromPublicGateway(gatewayURL, originalCID.toString())
|
|
327
|
+
assert.fail(
|
|
328
|
+
'Should detect complete metadata substitution via CID verification'
|
|
329
|
+
)
|
|
330
|
+
} catch (error) {
|
|
331
|
+
assert(error instanceof Error, 'Should throw an Error')
|
|
332
|
+
assert(
|
|
333
|
+
error.message.includes('CID verification failed'),
|
|
334
|
+
`Should catch complete substitution via CID verification. Got: ${error.message}`
|
|
335
|
+
)
|
|
336
|
+
} finally {
|
|
337
|
+
globalThis.fetch = originalFetch
|
|
338
|
+
}
|
|
339
|
+
})
|
|
340
|
+
})
|
|
341
|
+
})
|
|
342
|
+
//# sourceMappingURL=cid-verification.spec.js.map
|