@claude-flow/security 3.0.0-alpha.1 → 3.0.0-alpha.10

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 (115) hide show
  1. package/README.md +292 -234
  2. package/__tests__/acceptance/security-compliance.test.ts +674 -674
  3. package/__tests__/authorization-propagator.test.ts +155 -0
  4. package/__tests__/credential-generator.test.ts +310 -310
  5. package/__tests__/fixtures/configurations.ts +425 -419
  6. package/__tests__/fixtures/index.ts +21 -21
  7. package/__tests__/helpers/create-mock.ts +469 -469
  8. package/__tests__/helpers/index.ts +32 -32
  9. package/__tests__/input-validator.test.ts +382 -381
  10. package/__tests__/integration/security-flow.test.ts +606 -606
  11. package/__tests__/password-hasher.test.ts +239 -239
  12. package/__tests__/path-validator.test.ts +302 -302
  13. package/__tests__/plugin-integrity-verifier.test.ts +199 -0
  14. package/__tests__/safe-executor.test.ts +292 -292
  15. package/__tests__/token-generator.test.ts +371 -371
  16. package/__tests__/tool-output-guardrail.test.ts +256 -0
  17. package/__tests__/unit/credential-generator.test.ts +182 -182
  18. package/__tests__/unit/password-hasher.test.ts +359 -359
  19. package/__tests__/unit/path-validator.test.ts +509 -509
  20. package/__tests__/unit/safe-executor.test.ts +667 -667
  21. package/__tests__/unit/token-generator.test.ts +310 -310
  22. package/dist/authorization/propagator.d.ts +148 -0
  23. package/dist/authorization/propagator.d.ts.map +1 -0
  24. package/dist/authorization/propagator.js +182 -0
  25. package/dist/authorization/propagator.js.map +1 -0
  26. package/dist/credential-generator.js +9 -9
  27. package/dist/domain/services/security-domain-service.js +1 -1
  28. package/dist/domain/services/security-domain-service.js.map +1 -1
  29. package/dist/index.d.ts +4 -1
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +11 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/password-hasher.d.ts.map +1 -1
  34. package/dist/password-hasher.js +8 -1
  35. package/dist/password-hasher.js.map +1 -1
  36. package/dist/plugins/integrity-verifier.d.ts +125 -0
  37. package/dist/plugins/integrity-verifier.d.ts.map +1 -0
  38. package/dist/plugins/integrity-verifier.js +186 -0
  39. package/dist/plugins/integrity-verifier.js.map +1 -0
  40. package/dist/safe-executor.d.ts +7 -0
  41. package/dist/safe-executor.d.ts.map +1 -1
  42. package/dist/safe-executor.js +21 -1
  43. package/dist/safe-executor.js.map +1 -1
  44. package/dist/tool-output-guardrail.d.ts +126 -0
  45. package/dist/tool-output-guardrail.d.ts.map +1 -0
  46. package/dist/tool-output-guardrail.js +257 -0
  47. package/dist/tool-output-guardrail.js.map +1 -0
  48. package/package.json +28 -28
  49. package/src/CVE-REMEDIATION.ts +251 -251
  50. package/src/application/index.ts +10 -10
  51. package/src/application/services/security-application-service.ts +193 -193
  52. package/src/authorization/propagator.ts +279 -0
  53. package/src/credential-generator.ts +368 -368
  54. package/src/domain/entities/security-context.ts +173 -173
  55. package/src/domain/index.ts +17 -17
  56. package/src/domain/services/security-domain-service.ts +296 -296
  57. package/src/index.ts +314 -271
  58. package/src/input-validator.ts +466 -466
  59. package/src/password-hasher.ts +277 -270
  60. package/src/path-validator.ts +525 -525
  61. package/src/plugins/integrity-verifier.ts +265 -0
  62. package/src/safe-executor.ts +546 -525
  63. package/src/token-generator.ts +463 -463
  64. package/src/tool-output-guardrail.ts +360 -0
  65. package/tmp.json +0 -0
  66. package/tsconfig.json +9 -9
  67. package/tsconfig.tsbuildinfo +1 -1
  68. package/vitest.config.ts +20 -0
  69. package/.agentic-flow/intelligence.json +0 -16
  70. package/__tests__/coverage/base.css +0 -224
  71. package/__tests__/coverage/block-navigation.js +0 -87
  72. package/__tests__/coverage/coverage-final.json +0 -11
  73. package/__tests__/coverage/favicon.png +0 -0
  74. package/__tests__/coverage/index.html +0 -161
  75. package/__tests__/coverage/lcov-report/base.css +0 -224
  76. package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
  77. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  78. package/__tests__/coverage/lcov-report/index.html +0 -161
  79. package/__tests__/coverage/lcov-report/prettify.css +0 -1
  80. package/__tests__/coverage/lcov-report/prettify.js +0 -2
  81. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  82. package/__tests__/coverage/lcov-report/sorter.js +0 -210
  83. package/__tests__/coverage/lcov-report/src/CVE-REMEDIATION.ts.html +0 -838
  84. package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
  85. package/__tests__/coverage/lcov-report/src/application/services/security-application-service.ts.html +0 -664
  86. package/__tests__/coverage/lcov-report/src/credential-generator.ts.html +0 -1189
  87. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
  88. package/__tests__/coverage/lcov-report/src/domain/entities/security-context.ts.html +0 -604
  89. package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
  90. package/__tests__/coverage/lcov-report/src/domain/services/security-domain-service.ts.html +0 -973
  91. package/__tests__/coverage/lcov-report/src/index.html +0 -206
  92. package/__tests__/coverage/lcov-report/src/input-validator.ts.html +0 -1483
  93. package/__tests__/coverage/lcov-report/src/password-hasher.ts.html +0 -895
  94. package/__tests__/coverage/lcov-report/src/path-validator.ts.html +0 -1660
  95. package/__tests__/coverage/lcov-report/src/safe-executor.ts.html +0 -1660
  96. package/__tests__/coverage/lcov-report/src/token-generator.ts.html +0 -1474
  97. package/__tests__/coverage/lcov.info +0 -3650
  98. package/__tests__/coverage/prettify.css +0 -1
  99. package/__tests__/coverage/prettify.js +0 -2
  100. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  101. package/__tests__/coverage/sorter.js +0 -210
  102. package/__tests__/coverage/src/CVE-REMEDIATION.ts.html +0 -838
  103. package/__tests__/coverage/src/application/services/index.html +0 -116
  104. package/__tests__/coverage/src/application/services/security-application-service.ts.html +0 -664
  105. package/__tests__/coverage/src/credential-generator.ts.html +0 -1189
  106. package/__tests__/coverage/src/domain/entities/index.html +0 -116
  107. package/__tests__/coverage/src/domain/entities/security-context.ts.html +0 -604
  108. package/__tests__/coverage/src/domain/services/index.html +0 -116
  109. package/__tests__/coverage/src/domain/services/security-domain-service.ts.html +0 -973
  110. package/__tests__/coverage/src/index.html +0 -206
  111. package/__tests__/coverage/src/input-validator.ts.html +0 -1483
  112. package/__tests__/coverage/src/password-hasher.ts.html +0 -895
  113. package/__tests__/coverage/src/path-validator.ts.html +0 -1660
  114. package/__tests__/coverage/src/safe-executor.ts.html +0 -1660
  115. package/__tests__/coverage/src/token-generator.ts.html +0 -1474
@@ -1,359 +1,359 @@
1
- /**
2
- * V3 Claude-Flow Password Hasher Unit Tests
3
- *
4
- * London School TDD - Behavior Verification
5
- * Tests password hashing and verification behavior
6
- */
7
- import { describe, it, expect, vi, beforeEach } from 'vitest';
8
- import { createMock, createMockWithBehavior, type MockedInterface } from '../../helpers/create-mock';
9
- import { securityConfigs } from '../../fixtures/configurations';
10
-
11
- /**
12
- * Password hasher interface (to be implemented)
13
- */
14
- interface IPasswordHasher {
15
- hash(password: string): Promise<string>;
16
- verify(password: string, hash: string): Promise<boolean>;
17
- needsRehash(hash: string): boolean;
18
- }
19
-
20
- /**
21
- * Crypto provider interface (collaborator)
22
- */
23
- interface ICryptoProvider {
24
- argon2Hash(password: string, options: Argon2Options): Promise<string>;
25
- argon2Verify(hash: string, password: string): Promise<boolean>;
26
- generateSalt(length: number): Promise<string>;
27
- }
28
-
29
- interface Argon2Options {
30
- memoryCost: number;
31
- timeCost: number;
32
- parallelism: number;
33
- salt?: string;
34
- }
35
-
36
- /**
37
- * Password hasher implementation for testing
38
- */
39
- class PasswordHasher implements IPasswordHasher {
40
- constructor(
41
- private readonly cryptoProvider: ICryptoProvider,
42
- private readonly config: typeof securityConfigs.strict.hashing
43
- ) {}
44
-
45
- async hash(password: string): Promise<string> {
46
- if (!password || password.length < 8) {
47
- throw new Error('Password must be at least 8 characters');
48
- }
49
-
50
- const salt = await this.cryptoProvider.generateSalt(16);
51
-
52
- return this.cryptoProvider.argon2Hash(password, {
53
- memoryCost: this.config.memoryCost ?? 65536,
54
- timeCost: this.config.timeCost ?? 3,
55
- parallelism: this.config.parallelism ?? 4,
56
- salt,
57
- });
58
- }
59
-
60
- async verify(password: string, hash: string): Promise<boolean> {
61
- if (!password || !hash) {
62
- return false;
63
- }
64
-
65
- return this.cryptoProvider.argon2Verify(hash, password);
66
- }
67
-
68
- needsRehash(hash: string): boolean {
69
- // Check if hash uses current algorithm and parameters
70
- const currentVersion = `$argon2id$v=19$m=${this.config.memoryCost},t=${this.config.timeCost},p=${this.config.parallelism}`;
71
- return !hash.startsWith(currentVersion);
72
- }
73
- }
74
-
75
- describe('PasswordHasher', () => {
76
- let mockCryptoProvider: MockedInterface<ICryptoProvider>;
77
- let passwordHasher: PasswordHasher;
78
- const hashingConfig = securityConfigs.strict.hashing;
79
-
80
- beforeEach(() => {
81
- mockCryptoProvider = createMock<ICryptoProvider>();
82
-
83
- // Configure default mock behavior
84
- mockCryptoProvider.generateSalt.mockResolvedValue('randomsalt16byte');
85
- mockCryptoProvider.argon2Hash.mockResolvedValue(
86
- '$argon2id$v=19$m=65536,t=3,p=4$cmFuZG9tc2FsdA$hashedpassword'
87
- );
88
- mockCryptoProvider.argon2Verify.mockResolvedValue(true);
89
-
90
- passwordHasher = new PasswordHasher(mockCryptoProvider, hashingConfig);
91
- });
92
-
93
- describe('hash', () => {
94
- it('should generate salt before hashing', async () => {
95
- // Given
96
- const password = 'securePassword123!';
97
-
98
- // When
99
- await passwordHasher.hash(password);
100
-
101
- // Then - verify interaction
102
- expect(mockCryptoProvider.generateSalt).toHaveBeenCalledWith(16);
103
- expect(mockCryptoProvider.generateSalt).toHaveBeenCalledBefore(
104
- mockCryptoProvider.argon2Hash
105
- );
106
- });
107
-
108
- it('should hash password with configured parameters', async () => {
109
- // Given
110
- const password = 'securePassword123!';
111
-
112
- // When
113
- await passwordHasher.hash(password);
114
-
115
- // Then - verify interaction with correct parameters
116
- expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledWith(
117
- password,
118
- expect.objectContaining({
119
- memoryCost: 65536,
120
- timeCost: 3,
121
- parallelism: 4,
122
- })
123
- );
124
- });
125
-
126
- it('should return hash from crypto provider', async () => {
127
- // Given
128
- const password = 'securePassword123!';
129
- const expectedHash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
130
- mockCryptoProvider.argon2Hash.mockResolvedValue(expectedHash);
131
-
132
- // When
133
- const result = await passwordHasher.hash(password);
134
-
135
- // Then
136
- expect(result).toBe(expectedHash);
137
- });
138
-
139
- it('should reject passwords shorter than 8 characters', async () => {
140
- // Given
141
- const shortPassword = 'short';
142
-
143
- // When/Then
144
- await expect(passwordHasher.hash(shortPassword)).rejects.toThrow(
145
- 'Password must be at least 8 characters'
146
- );
147
-
148
- // Verify no interaction with crypto provider
149
- expect(mockCryptoProvider.generateSalt).not.toHaveBeenCalled();
150
- expect(mockCryptoProvider.argon2Hash).not.toHaveBeenCalled();
151
- });
152
-
153
- it('should reject empty passwords', async () => {
154
- // Given
155
- const emptyPassword = '';
156
-
157
- // When/Then
158
- await expect(passwordHasher.hash(emptyPassword)).rejects.toThrow(
159
- 'Password must be at least 8 characters'
160
- );
161
- });
162
-
163
- it('should use generated salt in hash options', async () => {
164
- // Given
165
- const password = 'securePassword123!';
166
- const generatedSalt = 'unique-salt-value';
167
- mockCryptoProvider.generateSalt.mockResolvedValue(generatedSalt);
168
-
169
- // When
170
- await passwordHasher.hash(password);
171
-
172
- // Then
173
- expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledWith(
174
- password,
175
- expect.objectContaining({
176
- salt: generatedSalt,
177
- })
178
- );
179
- });
180
- });
181
-
182
- describe('verify', () => {
183
- it('should delegate verification to crypto provider', async () => {
184
- // Given
185
- const password = 'securePassword123!';
186
- const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
187
-
188
- // When
189
- await passwordHasher.verify(password, hash);
190
-
191
- // Then - verify interaction
192
- expect(mockCryptoProvider.argon2Verify).toHaveBeenCalledWith(hash, password);
193
- });
194
-
195
- it('should return true for valid password', async () => {
196
- // Given
197
- const password = 'securePassword123!';
198
- const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
199
- mockCryptoProvider.argon2Verify.mockResolvedValue(true);
200
-
201
- // When
202
- const result = await passwordHasher.verify(password, hash);
203
-
204
- // Then
205
- expect(result).toBe(true);
206
- });
207
-
208
- it('should return false for invalid password', async () => {
209
- // Given
210
- const password = 'wrongPassword123!';
211
- const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
212
- mockCryptoProvider.argon2Verify.mockResolvedValue(false);
213
-
214
- // When
215
- const result = await passwordHasher.verify(password, hash);
216
-
217
- // Then
218
- expect(result).toBe(false);
219
- });
220
-
221
- it('should return false for empty password', async () => {
222
- // Given
223
- const emptyPassword = '';
224
- const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
225
-
226
- // When
227
- const result = await passwordHasher.verify(emptyPassword, hash);
228
-
229
- // Then
230
- expect(result).toBe(false);
231
- expect(mockCryptoProvider.argon2Verify).not.toHaveBeenCalled();
232
- });
233
-
234
- it('should return false for empty hash', async () => {
235
- // Given
236
- const password = 'securePassword123!';
237
- const emptyHash = '';
238
-
239
- // When
240
- const result = await passwordHasher.verify(password, emptyHash);
241
-
242
- // Then
243
- expect(result).toBe(false);
244
- expect(mockCryptoProvider.argon2Verify).not.toHaveBeenCalled();
245
- });
246
- });
247
-
248
- describe('needsRehash', () => {
249
- it('should return false for hash with current parameters', () => {
250
- // Given
251
- const currentHash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
252
-
253
- // When
254
- const result = passwordHasher.needsRehash(currentHash);
255
-
256
- // Then
257
- expect(result).toBe(false);
258
- });
259
-
260
- it('should return true for hash with different memory cost', () => {
261
- // Given
262
- const oldHash = '$argon2id$v=19$m=32768,t=3,p=4$salt$hash';
263
-
264
- // When
265
- const result = passwordHasher.needsRehash(oldHash);
266
-
267
- // Then
268
- expect(result).toBe(true);
269
- });
270
-
271
- it('should return true for hash with different time cost', () => {
272
- // Given
273
- const oldHash = '$argon2id$v=19$m=65536,t=2,p=4$salt$hash';
274
-
275
- // When
276
- const result = passwordHasher.needsRehash(oldHash);
277
-
278
- // Then
279
- expect(result).toBe(true);
280
- });
281
-
282
- it('should return true for hash with different parallelism', () => {
283
- // Given
284
- const oldHash = '$argon2id$v=19$m=65536,t=3,p=2$salt$hash';
285
-
286
- // When
287
- const result = passwordHasher.needsRehash(oldHash);
288
-
289
- // Then
290
- expect(result).toBe(true);
291
- });
292
-
293
- it('should return true for bcrypt hash', () => {
294
- // Given
295
- const bcryptHash = '$2b$10$salt.hash';
296
-
297
- // When
298
- const result = passwordHasher.needsRehash(bcryptHash);
299
-
300
- // Then
301
- expect(result).toBe(true);
302
- });
303
- });
304
-
305
- describe('error handling', () => {
306
- it('should propagate crypto provider errors on hash', async () => {
307
- // Given
308
- const password = 'securePassword123!';
309
- const cryptoError = new Error('Crypto operation failed');
310
- mockCryptoProvider.argon2Hash.mockRejectedValue(cryptoError);
311
-
312
- // When/Then
313
- await expect(passwordHasher.hash(password)).rejects.toThrow('Crypto operation failed');
314
- });
315
-
316
- it('should propagate crypto provider errors on verify', async () => {
317
- // Given
318
- const password = 'securePassword123!';
319
- const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
320
- const cryptoError = new Error('Verification failed');
321
- mockCryptoProvider.argon2Verify.mockRejectedValue(cryptoError);
322
-
323
- // When/Then
324
- await expect(passwordHasher.verify(password, hash)).rejects.toThrow(
325
- 'Verification failed'
326
- );
327
- });
328
- });
329
-
330
- describe('interaction verification', () => {
331
- it('should not call argon2Hash if salt generation fails', async () => {
332
- // Given
333
- const password = 'securePassword123!';
334
- mockCryptoProvider.generateSalt.mockRejectedValue(new Error('Salt generation failed'));
335
-
336
- // When
337
- try {
338
- await passwordHasher.hash(password);
339
- } catch {
340
- // Expected to fail
341
- }
342
-
343
- // Then
344
- expect(mockCryptoProvider.argon2Hash).not.toHaveBeenCalled();
345
- });
346
-
347
- it('should only call crypto provider once per operation', async () => {
348
- // Given
349
- const password = 'securePassword123!';
350
-
351
- // When
352
- await passwordHasher.hash(password);
353
-
354
- // Then
355
- expect(mockCryptoProvider.generateSalt).toHaveBeenCalledTimes(1);
356
- expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledTimes(1);
357
- });
358
- });
359
- });
1
+ /**
2
+ * V3 Claude-Flow Password Hasher Unit Tests
3
+ *
4
+ * London School TDD - Behavior Verification
5
+ * Tests password hashing and verification behavior
6
+ */
7
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
8
+ import { createMock, createMockWithBehavior, type MockedInterface } from '../helpers/create-mock';
9
+ import { securityConfigs } from '../fixtures/configurations';
10
+
11
+ /**
12
+ * Password hasher interface (to be implemented)
13
+ */
14
+ interface IPasswordHasher {
15
+ hash(password: string): Promise<string>;
16
+ verify(password: string, hash: string): Promise<boolean>;
17
+ needsRehash(hash: string): boolean;
18
+ }
19
+
20
+ /**
21
+ * Crypto provider interface (collaborator)
22
+ */
23
+ interface ICryptoProvider {
24
+ argon2Hash(password: string, options: Argon2Options): Promise<string>;
25
+ argon2Verify(hash: string, password: string): Promise<boolean>;
26
+ generateSalt(length: number): Promise<string>;
27
+ }
28
+
29
+ interface Argon2Options {
30
+ memoryCost: number;
31
+ timeCost: number;
32
+ parallelism: number;
33
+ salt?: string;
34
+ }
35
+
36
+ /**
37
+ * Password hasher implementation for testing
38
+ */
39
+ class PasswordHasher implements IPasswordHasher {
40
+ constructor(
41
+ private readonly cryptoProvider: ICryptoProvider,
42
+ private readonly config: typeof securityConfigs.strict.hashing
43
+ ) {}
44
+
45
+ async hash(password: string): Promise<string> {
46
+ if (!password || password.length < 8) {
47
+ throw new Error('Password must be at least 8 characters');
48
+ }
49
+
50
+ const salt = await this.cryptoProvider.generateSalt(16);
51
+
52
+ return this.cryptoProvider.argon2Hash(password, {
53
+ memoryCost: this.config.memoryCost ?? 65536,
54
+ timeCost: this.config.timeCost ?? 3,
55
+ parallelism: this.config.parallelism ?? 4,
56
+ salt,
57
+ });
58
+ }
59
+
60
+ async verify(password: string, hash: string): Promise<boolean> {
61
+ if (!password || !hash) {
62
+ return false;
63
+ }
64
+
65
+ return this.cryptoProvider.argon2Verify(hash, password);
66
+ }
67
+
68
+ needsRehash(hash: string): boolean {
69
+ // Check if hash uses current algorithm and parameters
70
+ const currentVersion = `$argon2id$v=19$m=${this.config.memoryCost},t=${this.config.timeCost},p=${this.config.parallelism}`;
71
+ return !hash.startsWith(currentVersion);
72
+ }
73
+ }
74
+
75
+ describe('PasswordHasher', () => {
76
+ let mockCryptoProvider: MockedInterface<ICryptoProvider>;
77
+ let passwordHasher: PasswordHasher;
78
+ const hashingConfig = securityConfigs.strict.hashing;
79
+
80
+ beforeEach(() => {
81
+ mockCryptoProvider = createMock<ICryptoProvider>();
82
+
83
+ // Configure default mock behavior
84
+ mockCryptoProvider.generateSalt.mockResolvedValue('randomsalt16byte');
85
+ mockCryptoProvider.argon2Hash.mockResolvedValue(
86
+ '$argon2id$v=19$m=65536,t=3,p=4$cmFuZG9tc2FsdA$hashedpassword'
87
+ );
88
+ mockCryptoProvider.argon2Verify.mockResolvedValue(true);
89
+
90
+ passwordHasher = new PasswordHasher(mockCryptoProvider, hashingConfig);
91
+ });
92
+
93
+ describe('hash', () => {
94
+ it('should generate salt before hashing', async () => {
95
+ // Given
96
+ const password = 'securePassword123!';
97
+
98
+ // When
99
+ await passwordHasher.hash(password);
100
+
101
+ // Then - verify interaction
102
+ expect(mockCryptoProvider.generateSalt).toHaveBeenCalledWith(16);
103
+ expect(mockCryptoProvider.generateSalt).toHaveBeenCalledBefore(
104
+ mockCryptoProvider.argon2Hash
105
+ );
106
+ });
107
+
108
+ it('should hash password with configured parameters', async () => {
109
+ // Given
110
+ const password = 'securePassword123!';
111
+
112
+ // When
113
+ await passwordHasher.hash(password);
114
+
115
+ // Then - verify interaction with correct parameters
116
+ expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledWith(
117
+ password,
118
+ expect.objectContaining({
119
+ memoryCost: 65536,
120
+ timeCost: 3,
121
+ parallelism: 4,
122
+ })
123
+ );
124
+ });
125
+
126
+ it('should return hash from crypto provider', async () => {
127
+ // Given
128
+ const password = 'securePassword123!';
129
+ const expectedHash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
130
+ mockCryptoProvider.argon2Hash.mockResolvedValue(expectedHash);
131
+
132
+ // When
133
+ const result = await passwordHasher.hash(password);
134
+
135
+ // Then
136
+ expect(result).toBe(expectedHash);
137
+ });
138
+
139
+ it('should reject passwords shorter than 8 characters', async () => {
140
+ // Given
141
+ const shortPassword = 'short';
142
+
143
+ // When/Then
144
+ await expect(passwordHasher.hash(shortPassword)).rejects.toThrow(
145
+ 'Password must be at least 8 characters'
146
+ );
147
+
148
+ // Verify no interaction with crypto provider
149
+ expect(mockCryptoProvider.generateSalt).not.toHaveBeenCalled();
150
+ expect(mockCryptoProvider.argon2Hash).not.toHaveBeenCalled();
151
+ });
152
+
153
+ it('should reject empty passwords', async () => {
154
+ // Given
155
+ const emptyPassword = '';
156
+
157
+ // When/Then
158
+ await expect(passwordHasher.hash(emptyPassword)).rejects.toThrow(
159
+ 'Password must be at least 8 characters'
160
+ );
161
+ });
162
+
163
+ it('should use generated salt in hash options', async () => {
164
+ // Given
165
+ const password = 'securePassword123!';
166
+ const generatedSalt = 'unique-salt-value';
167
+ mockCryptoProvider.generateSalt.mockResolvedValue(generatedSalt);
168
+
169
+ // When
170
+ await passwordHasher.hash(password);
171
+
172
+ // Then
173
+ expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledWith(
174
+ password,
175
+ expect.objectContaining({
176
+ salt: generatedSalt,
177
+ })
178
+ );
179
+ });
180
+ });
181
+
182
+ describe('verify', () => {
183
+ it('should delegate verification to crypto provider', async () => {
184
+ // Given
185
+ const password = 'securePassword123!';
186
+ const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
187
+
188
+ // When
189
+ await passwordHasher.verify(password, hash);
190
+
191
+ // Then - verify interaction
192
+ expect(mockCryptoProvider.argon2Verify).toHaveBeenCalledWith(hash, password);
193
+ });
194
+
195
+ it('should return true for valid password', async () => {
196
+ // Given
197
+ const password = 'securePassword123!';
198
+ const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
199
+ mockCryptoProvider.argon2Verify.mockResolvedValue(true);
200
+
201
+ // When
202
+ const result = await passwordHasher.verify(password, hash);
203
+
204
+ // Then
205
+ expect(result).toBe(true);
206
+ });
207
+
208
+ it('should return false for invalid password', async () => {
209
+ // Given
210
+ const password = 'wrongPassword123!';
211
+ const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
212
+ mockCryptoProvider.argon2Verify.mockResolvedValue(false);
213
+
214
+ // When
215
+ const result = await passwordHasher.verify(password, hash);
216
+
217
+ // Then
218
+ expect(result).toBe(false);
219
+ });
220
+
221
+ it('should return false for empty password', async () => {
222
+ // Given
223
+ const emptyPassword = '';
224
+ const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
225
+
226
+ // When
227
+ const result = await passwordHasher.verify(emptyPassword, hash);
228
+
229
+ // Then
230
+ expect(result).toBe(false);
231
+ expect(mockCryptoProvider.argon2Verify).not.toHaveBeenCalled();
232
+ });
233
+
234
+ it('should return false for empty hash', async () => {
235
+ // Given
236
+ const password = 'securePassword123!';
237
+ const emptyHash = '';
238
+
239
+ // When
240
+ const result = await passwordHasher.verify(password, emptyHash);
241
+
242
+ // Then
243
+ expect(result).toBe(false);
244
+ expect(mockCryptoProvider.argon2Verify).not.toHaveBeenCalled();
245
+ });
246
+ });
247
+
248
+ describe('needsRehash', () => {
249
+ it('should return false for hash with current parameters', () => {
250
+ // Given
251
+ const currentHash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
252
+
253
+ // When
254
+ const result = passwordHasher.needsRehash(currentHash);
255
+
256
+ // Then
257
+ expect(result).toBe(false);
258
+ });
259
+
260
+ it('should return true for hash with different memory cost', () => {
261
+ // Given
262
+ const oldHash = '$argon2id$v=19$m=32768,t=3,p=4$salt$hash';
263
+
264
+ // When
265
+ const result = passwordHasher.needsRehash(oldHash);
266
+
267
+ // Then
268
+ expect(result).toBe(true);
269
+ });
270
+
271
+ it('should return true for hash with different time cost', () => {
272
+ // Given
273
+ const oldHash = '$argon2id$v=19$m=65536,t=2,p=4$salt$hash';
274
+
275
+ // When
276
+ const result = passwordHasher.needsRehash(oldHash);
277
+
278
+ // Then
279
+ expect(result).toBe(true);
280
+ });
281
+
282
+ it('should return true for hash with different parallelism', () => {
283
+ // Given
284
+ const oldHash = '$argon2id$v=19$m=65536,t=3,p=2$salt$hash';
285
+
286
+ // When
287
+ const result = passwordHasher.needsRehash(oldHash);
288
+
289
+ // Then
290
+ expect(result).toBe(true);
291
+ });
292
+
293
+ it('should return true for bcrypt hash', () => {
294
+ // Given
295
+ const bcryptHash = '$2b$10$salt.hash';
296
+
297
+ // When
298
+ const result = passwordHasher.needsRehash(bcryptHash);
299
+
300
+ // Then
301
+ expect(result).toBe(true);
302
+ });
303
+ });
304
+
305
+ describe('error handling', () => {
306
+ it('should propagate crypto provider errors on hash', async () => {
307
+ // Given
308
+ const password = 'securePassword123!';
309
+ const cryptoError = new Error('Crypto operation failed');
310
+ mockCryptoProvider.argon2Hash.mockRejectedValue(cryptoError);
311
+
312
+ // When/Then
313
+ await expect(passwordHasher.hash(password)).rejects.toThrow('Crypto operation failed');
314
+ });
315
+
316
+ it('should propagate crypto provider errors on verify', async () => {
317
+ // Given
318
+ const password = 'securePassword123!';
319
+ const hash = '$argon2id$v=19$m=65536,t=3,p=4$salt$hash';
320
+ const cryptoError = new Error('Verification failed');
321
+ mockCryptoProvider.argon2Verify.mockRejectedValue(cryptoError);
322
+
323
+ // When/Then
324
+ await expect(passwordHasher.verify(password, hash)).rejects.toThrow(
325
+ 'Verification failed'
326
+ );
327
+ });
328
+ });
329
+
330
+ describe('interaction verification', () => {
331
+ it('should not call argon2Hash if salt generation fails', async () => {
332
+ // Given
333
+ const password = 'securePassword123!';
334
+ mockCryptoProvider.generateSalt.mockRejectedValue(new Error('Salt generation failed'));
335
+
336
+ // When
337
+ try {
338
+ await passwordHasher.hash(password);
339
+ } catch {
340
+ // Expected to fail
341
+ }
342
+
343
+ // Then
344
+ expect(mockCryptoProvider.argon2Hash).not.toHaveBeenCalled();
345
+ });
346
+
347
+ it('should only call crypto provider once per operation', async () => {
348
+ // Given
349
+ const password = 'securePassword123!';
350
+
351
+ // When
352
+ await passwordHasher.hash(password);
353
+
354
+ // Then
355
+ expect(mockCryptoProvider.generateSalt).toHaveBeenCalledTimes(1);
356
+ expect(mockCryptoProvider.argon2Hash).toHaveBeenCalledTimes(1);
357
+ });
358
+ });
359
+ });