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