@claude-flow/security 3.0.0-alpha.1 → 3.0.0-alpha.7
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/README.md +234 -234
- package/__tests__/acceptance/security-compliance.test.ts +674 -674
- package/__tests__/credential-generator.test.ts +310 -310
- package/__tests__/fixtures/configurations.ts +425 -419
- package/__tests__/fixtures/index.ts +21 -21
- package/__tests__/helpers/create-mock.ts +469 -469
- package/__tests__/helpers/index.ts +32 -32
- package/__tests__/input-validator.test.ts +382 -381
- package/__tests__/integration/security-flow.test.ts +606 -606
- package/__tests__/password-hasher.test.ts +239 -239
- package/__tests__/path-validator.test.ts +302 -302
- package/__tests__/safe-executor.test.ts +292 -292
- package/__tests__/token-generator.test.ts +371 -371
- package/__tests__/unit/credential-generator.test.ts +182 -182
- package/__tests__/unit/password-hasher.test.ts +359 -359
- package/__tests__/unit/path-validator.test.ts +509 -509
- package/__tests__/unit/safe-executor.test.ts +667 -667
- package/__tests__/unit/token-generator.test.ts +310 -310
- package/dist/credential-generator.js +9 -9
- package/dist/domain/services/security-domain-service.js +1 -1
- package/dist/domain/services/security-domain-service.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/password-hasher.d.ts.map +1 -1
- package/dist/password-hasher.js +8 -1
- package/dist/password-hasher.js.map +1 -1
- package/dist/safe-executor.d.ts +7 -0
- package/dist/safe-executor.d.ts.map +1 -1
- package/dist/safe-executor.js +21 -1
- package/dist/safe-executor.js.map +1 -1
- package/package.json +27 -28
- package/src/CVE-REMEDIATION.ts +251 -251
- package/src/application/index.ts +10 -10
- package/src/application/services/security-application-service.ts +193 -193
- package/src/credential-generator.ts +368 -368
- package/src/domain/entities/security-context.ts +173 -173
- package/src/domain/index.ts +17 -17
- package/src/domain/services/security-domain-service.ts +296 -296
- package/src/index.ts +271 -271
- package/src/input-validator.ts +466 -466
- package/src/password-hasher.ts +277 -270
- package/src/path-validator.ts +525 -525
- package/src/safe-executor.ts +546 -525
- package/src/token-generator.ts +463 -463
- package/tmp.json +0 -0
- package/tsconfig.json +9 -9
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +20 -0
- package/.agentic-flow/intelligence.json +0 -16
- package/__tests__/coverage/base.css +0 -224
- package/__tests__/coverage/block-navigation.js +0 -87
- package/__tests__/coverage/coverage-final.json +0 -11
- package/__tests__/coverage/favicon.png +0 -0
- package/__tests__/coverage/index.html +0 -161
- package/__tests__/coverage/lcov-report/base.css +0 -224
- package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
- package/__tests__/coverage/lcov-report/favicon.png +0 -0
- package/__tests__/coverage/lcov-report/index.html +0 -161
- package/__tests__/coverage/lcov-report/prettify.css +0 -1
- package/__tests__/coverage/lcov-report/prettify.js +0 -2
- package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/lcov-report/sorter.js +0 -210
- package/__tests__/coverage/lcov-report/src/CVE-REMEDIATION.ts.html +0 -838
- package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/application/services/security-application-service.ts.html +0 -664
- package/__tests__/coverage/lcov-report/src/credential-generator.ts.html +0 -1189
- package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/domain/entities/security-context.ts.html +0 -604
- package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/domain/services/security-domain-service.ts.html +0 -973
- package/__tests__/coverage/lcov-report/src/index.html +0 -206
- package/__tests__/coverage/lcov-report/src/input-validator.ts.html +0 -1483
- package/__tests__/coverage/lcov-report/src/password-hasher.ts.html +0 -895
- package/__tests__/coverage/lcov-report/src/path-validator.ts.html +0 -1660
- package/__tests__/coverage/lcov-report/src/safe-executor.ts.html +0 -1660
- package/__tests__/coverage/lcov-report/src/token-generator.ts.html +0 -1474
- package/__tests__/coverage/lcov.info +0 -3650
- package/__tests__/coverage/prettify.css +0 -1
- package/__tests__/coverage/prettify.js +0 -2
- package/__tests__/coverage/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/sorter.js +0 -210
- package/__tests__/coverage/src/CVE-REMEDIATION.ts.html +0 -838
- package/__tests__/coverage/src/application/services/index.html +0 -116
- package/__tests__/coverage/src/application/services/security-application-service.ts.html +0 -664
- package/__tests__/coverage/src/credential-generator.ts.html +0 -1189
- package/__tests__/coverage/src/domain/entities/index.html +0 -116
- package/__tests__/coverage/src/domain/entities/security-context.ts.html +0 -604
- package/__tests__/coverage/src/domain/services/index.html +0 -116
- package/__tests__/coverage/src/domain/services/security-domain-service.ts.html +0 -973
- package/__tests__/coverage/src/index.html +0 -206
- package/__tests__/coverage/src/input-validator.ts.html +0 -1483
- package/__tests__/coverage/src/password-hasher.ts.html +0 -895
- package/__tests__/coverage/src/path-validator.ts.html +0 -1660
- package/__tests__/coverage/src/safe-executor.ts.html +0 -1660
- package/__tests__/coverage/src/token-generator.ts.html +0 -1474
|
@@ -1,239 +1,239 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Password Hasher Tests - CVE-2 Remediation Validation
|
|
3
|
-
*
|
|
4
|
-
* Tests verify:
|
|
5
|
-
* - bcrypt is used instead of SHA-256
|
|
6
|
-
* - 12 rounds minimum
|
|
7
|
-
* - Password validation rules
|
|
8
|
-
* - Timing-safe comparison
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { describe, it, expect, beforeEach } from 'vitest';
|
|
12
|
-
import {
|
|
13
|
-
PasswordHasher,
|
|
14
|
-
PasswordHashError,
|
|
15
|
-
createPasswordHasher,
|
|
16
|
-
} from '
|
|
17
|
-
|
|
18
|
-
describe('PasswordHasher', () => {
|
|
19
|
-
let hasher: PasswordHasher;
|
|
20
|
-
|
|
21
|
-
beforeEach(() => {
|
|
22
|
-
hasher = new PasswordHasher({ rounds: 12 });
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
describe('Configuration', () => {
|
|
26
|
-
it('should create hasher with default 12 rounds', () => {
|
|
27
|
-
const defaultHasher = new PasswordHasher();
|
|
28
|
-
expect(defaultHasher.getConfig().rounds).toBe(12);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('should reject rounds below 10', () => {
|
|
32
|
-
expect(() => new PasswordHasher({ rounds: 8 })).toThrow(PasswordHashError);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('should reject rounds above 20', () => {
|
|
36
|
-
expect(() => new PasswordHasher({ rounds: 22 })).toThrow(PasswordHashError);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('should reject minimum password length below 8', () => {
|
|
40
|
-
expect(() => new PasswordHasher({ minLength: 4 })).toThrow(PasswordHashError);
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
describe('Password Validation', () => {
|
|
45
|
-
it('should reject empty password', () => {
|
|
46
|
-
const result = hasher.validate('');
|
|
47
|
-
expect(result.isValid).toBe(false);
|
|
48
|
-
expect(result.errors).toContain('Password is required');
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('should reject password shorter than minimum', () => {
|
|
52
|
-
const result = hasher.validate('short');
|
|
53
|
-
expect(result.isValid).toBe(false);
|
|
54
|
-
expect(result.errors.some(e => e.includes('at least'))).toBe(true);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('should reject password without uppercase', () => {
|
|
58
|
-
const result = hasher.validate('password123');
|
|
59
|
-
expect(result.isValid).toBe(false);
|
|
60
|
-
expect(result.errors.some(e => e.includes('uppercase'))).toBe(true);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('should reject password without lowercase', () => {
|
|
64
|
-
const result = hasher.validate('PASSWORD123');
|
|
65
|
-
expect(result.isValid).toBe(false);
|
|
66
|
-
expect(result.errors.some(e => e.includes('lowercase'))).toBe(true);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('should reject password without digit', () => {
|
|
70
|
-
const result = hasher.validate('PasswordNoDigit');
|
|
71
|
-
expect(result.isValid).toBe(false);
|
|
72
|
-
expect(result.errors.some(e => e.includes('digit'))).toBe(true);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('should accept valid password', () => {
|
|
76
|
-
const result = hasher.validate('SecurePass123');
|
|
77
|
-
expect(result.isValid).toBe(true);
|
|
78
|
-
expect(result.errors).toHaveLength(0);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('should optionally require special character', () => {
|
|
82
|
-
const strictHasher = new PasswordHasher({ requireSpecial: true });
|
|
83
|
-
const result = strictHasher.validate('SecurePass123');
|
|
84
|
-
expect(result.isValid).toBe(false);
|
|
85
|
-
expect(result.errors.some(e => e.includes('special'))).toBe(true);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('should accept password with special character when required', () => {
|
|
89
|
-
const strictHasher = new PasswordHasher({ requireSpecial: true });
|
|
90
|
-
const result = strictHasher.validate('SecurePass123!');
|
|
91
|
-
expect(result.isValid).toBe(true);
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
describe('Password Hashing', () => {
|
|
96
|
-
it('should hash password with bcrypt', async () => {
|
|
97
|
-
const hash = await hasher.hash('SecurePass123');
|
|
98
|
-
|
|
99
|
-
// bcrypt hashes start with $2a$, $2b$, or $2y$
|
|
100
|
-
expect(hash).toMatch(/^\$2[aby]\$\d{2}\$/);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('should produce different hashes for same password', async () => {
|
|
104
|
-
const hash1 = await hasher.hash('SecurePass123');
|
|
105
|
-
const hash2 = await hasher.hash('SecurePass123');
|
|
106
|
-
|
|
107
|
-
expect(hash1).not.toBe(hash2); // Different salts
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it('should include rounds in hash', async () => {
|
|
111
|
-
const hash = await hasher.hash('SecurePass123');
|
|
112
|
-
|
|
113
|
-
// Hash format: $2b$12$...
|
|
114
|
-
expect(hash).toContain('$12$');
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('should throw for invalid password', async () => {
|
|
118
|
-
await expect(hasher.hash('short')).rejects.toThrow(PasswordHashError);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('should throw for empty password', async () => {
|
|
122
|
-
await expect(hasher.hash('')).rejects.toThrow(PasswordHashError);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
describe('Password Verification', () => {
|
|
127
|
-
it('should verify correct password', async () => {
|
|
128
|
-
const password = 'SecurePass123';
|
|
129
|
-
const hash = await hasher.hash(password);
|
|
130
|
-
|
|
131
|
-
const isValid = await hasher.verify(password, hash);
|
|
132
|
-
expect(isValid).toBe(true);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
it('should reject incorrect password', async () => {
|
|
136
|
-
const hash = await hasher.hash('SecurePass123');
|
|
137
|
-
|
|
138
|
-
const isValid = await hasher.verify('WrongPass123', hash);
|
|
139
|
-
expect(isValid).toBe(false);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('should return false for empty password', async () => {
|
|
143
|
-
const hash = await hasher.hash('SecurePass123');
|
|
144
|
-
|
|
145
|
-
const isValid = await hasher.verify('', hash);
|
|
146
|
-
expect(isValid).toBe(false);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it('should return false for empty hash', async () => {
|
|
150
|
-
const isValid = await hasher.verify('SecurePass123', '');
|
|
151
|
-
expect(isValid).toBe(false);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it('should return false for invalid hash format', async () => {
|
|
155
|
-
const isValid = await hasher.verify('SecurePass123', 'invalid-hash');
|
|
156
|
-
expect(isValid).toBe(false);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it('should return false for SHA-256 hash (old format)', async () => {
|
|
160
|
-
// SHA-256 hash format (what we're replacing)
|
|
161
|
-
const sha256Hash = 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3';
|
|
162
|
-
|
|
163
|
-
const isValid = await hasher.verify('password', sha256Hash);
|
|
164
|
-
expect(isValid).toBe(false);
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
describe('Rehash Detection', () => {
|
|
169
|
-
it('should not need rehash for current rounds', async () => {
|
|
170
|
-
const hash = await hasher.hash('SecurePass123');
|
|
171
|
-
expect(hasher.needsRehash(hash)).toBe(false);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it('should need rehash for lower rounds', () => {
|
|
175
|
-
// Hash with 10 rounds
|
|
176
|
-
const lowRoundsHash = '$2b$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy';
|
|
177
|
-
expect(hasher.needsRehash(lowRoundsHash)).toBe(true);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it('should need rehash for invalid format', () => {
|
|
181
|
-
expect(hasher.needsRehash('invalid-hash')).toBe(true);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it('should need rehash for SHA-256 hash', () => {
|
|
185
|
-
const sha256Hash = 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3';
|
|
186
|
-
expect(hasher.needsRehash(sha256Hash)).toBe(true);
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
describe('Factory Function', () => {
|
|
191
|
-
it('should create hasher with specified rounds', () => {
|
|
192
|
-
const hasher12 = createPasswordHasher(12);
|
|
193
|
-
expect(hasher12.getConfig().rounds).toBe(12);
|
|
194
|
-
|
|
195
|
-
const hasher14 = createPasswordHasher(14);
|
|
196
|
-
expect(hasher14.getConfig().rounds).toBe(14);
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it('should use default 12 rounds', () => {
|
|
200
|
-
const hasher = createPasswordHasher();
|
|
201
|
-
expect(hasher.getConfig().rounds).toBe(12);
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
describe('CVE-2 Security Verification', () => {
|
|
206
|
-
it('should NOT use hardcoded salt', async () => {
|
|
207
|
-
// Generate multiple hashes and verify they have different salts
|
|
208
|
-
const hashes = await Promise.all([
|
|
209
|
-
hasher.hash('SecurePass123'),
|
|
210
|
-
hasher.hash('SecurePass123'),
|
|
211
|
-
hasher.hash('SecurePass123'),
|
|
212
|
-
]);
|
|
213
|
-
|
|
214
|
-
// Extract salts (22 chars after $2b$12$)
|
|
215
|
-
const salts = hashes.map(h => h.substring(7, 29));
|
|
216
|
-
|
|
217
|
-
// All salts should be unique
|
|
218
|
-
const uniqueSalts = new Set(salts);
|
|
219
|
-
expect(uniqueSalts.size).toBe(3);
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
it('should NOT produce same hash for same input (unlike SHA-256)', async () => {
|
|
223
|
-
// SHA-256 with hardcoded salt would produce identical output
|
|
224
|
-
const hash1 = await hasher.hash('SecurePass123');
|
|
225
|
-
const hash2 = await hasher.hash('SecurePass123');
|
|
226
|
-
|
|
227
|
-
expect(hash1).not.toBe(hash2);
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
it('should produce hash that takes time to compute', async () => {
|
|
231
|
-
const start = Date.now();
|
|
232
|
-
await hasher.hash('SecurePass123');
|
|
233
|
-
const duration = Date.now() - start;
|
|
234
|
-
|
|
235
|
-
// bcrypt with 12 rounds should take measurable time (>10ms typically)
|
|
236
|
-
expect(duration).toBeGreaterThan(5);
|
|
237
|
-
});
|
|
238
|
-
});
|
|
239
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* Password Hasher Tests - CVE-2 Remediation Validation
|
|
3
|
+
*
|
|
4
|
+
* Tests verify:
|
|
5
|
+
* - bcrypt is used instead of SHA-256
|
|
6
|
+
* - 12 rounds minimum
|
|
7
|
+
* - Password validation rules
|
|
8
|
+
* - Timing-safe comparison
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
12
|
+
import {
|
|
13
|
+
PasswordHasher,
|
|
14
|
+
PasswordHashError,
|
|
15
|
+
createPasswordHasher,
|
|
16
|
+
} from '../src/password-hasher.js';
|
|
17
|
+
|
|
18
|
+
describe('PasswordHasher', () => {
|
|
19
|
+
let hasher: PasswordHasher;
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
hasher = new PasswordHasher({ rounds: 12 });
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('Configuration', () => {
|
|
26
|
+
it('should create hasher with default 12 rounds', () => {
|
|
27
|
+
const defaultHasher = new PasswordHasher();
|
|
28
|
+
expect(defaultHasher.getConfig().rounds).toBe(12);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should reject rounds below 10', () => {
|
|
32
|
+
expect(() => new PasswordHasher({ rounds: 8 })).toThrow(PasswordHashError);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should reject rounds above 20', () => {
|
|
36
|
+
expect(() => new PasswordHasher({ rounds: 22 })).toThrow(PasswordHashError);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should reject minimum password length below 8', () => {
|
|
40
|
+
expect(() => new PasswordHasher({ minLength: 4 })).toThrow(PasswordHashError);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('Password Validation', () => {
|
|
45
|
+
it('should reject empty password', () => {
|
|
46
|
+
const result = hasher.validate('');
|
|
47
|
+
expect(result.isValid).toBe(false);
|
|
48
|
+
expect(result.errors).toContain('Password is required');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should reject password shorter than minimum', () => {
|
|
52
|
+
const result = hasher.validate('short');
|
|
53
|
+
expect(result.isValid).toBe(false);
|
|
54
|
+
expect(result.errors.some(e => e.includes('at least'))).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should reject password without uppercase', () => {
|
|
58
|
+
const result = hasher.validate('password123');
|
|
59
|
+
expect(result.isValid).toBe(false);
|
|
60
|
+
expect(result.errors.some(e => e.includes('uppercase'))).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should reject password without lowercase', () => {
|
|
64
|
+
const result = hasher.validate('PASSWORD123');
|
|
65
|
+
expect(result.isValid).toBe(false);
|
|
66
|
+
expect(result.errors.some(e => e.includes('lowercase'))).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should reject password without digit', () => {
|
|
70
|
+
const result = hasher.validate('PasswordNoDigit');
|
|
71
|
+
expect(result.isValid).toBe(false);
|
|
72
|
+
expect(result.errors.some(e => e.includes('digit'))).toBe(true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should accept valid password', () => {
|
|
76
|
+
const result = hasher.validate('SecurePass123');
|
|
77
|
+
expect(result.isValid).toBe(true);
|
|
78
|
+
expect(result.errors).toHaveLength(0);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('should optionally require special character', () => {
|
|
82
|
+
const strictHasher = new PasswordHasher({ requireSpecial: true });
|
|
83
|
+
const result = strictHasher.validate('SecurePass123');
|
|
84
|
+
expect(result.isValid).toBe(false);
|
|
85
|
+
expect(result.errors.some(e => e.includes('special'))).toBe(true);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should accept password with special character when required', () => {
|
|
89
|
+
const strictHasher = new PasswordHasher({ requireSpecial: true });
|
|
90
|
+
const result = strictHasher.validate('SecurePass123!');
|
|
91
|
+
expect(result.isValid).toBe(true);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('Password Hashing', () => {
|
|
96
|
+
it('should hash password with bcrypt', async () => {
|
|
97
|
+
const hash = await hasher.hash('SecurePass123');
|
|
98
|
+
|
|
99
|
+
// bcrypt hashes start with $2a$, $2b$, or $2y$
|
|
100
|
+
expect(hash).toMatch(/^\$2[aby]\$\d{2}\$/);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should produce different hashes for same password', async () => {
|
|
104
|
+
const hash1 = await hasher.hash('SecurePass123');
|
|
105
|
+
const hash2 = await hasher.hash('SecurePass123');
|
|
106
|
+
|
|
107
|
+
expect(hash1).not.toBe(hash2); // Different salts
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should include rounds in hash', async () => {
|
|
111
|
+
const hash = await hasher.hash('SecurePass123');
|
|
112
|
+
|
|
113
|
+
// Hash format: $2b$12$...
|
|
114
|
+
expect(hash).toContain('$12$');
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('should throw for invalid password', async () => {
|
|
118
|
+
await expect(hasher.hash('short')).rejects.toThrow(PasswordHashError);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('should throw for empty password', async () => {
|
|
122
|
+
await expect(hasher.hash('')).rejects.toThrow(PasswordHashError);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe('Password Verification', () => {
|
|
127
|
+
it('should verify correct password', async () => {
|
|
128
|
+
const password = 'SecurePass123';
|
|
129
|
+
const hash = await hasher.hash(password);
|
|
130
|
+
|
|
131
|
+
const isValid = await hasher.verify(password, hash);
|
|
132
|
+
expect(isValid).toBe(true);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should reject incorrect password', async () => {
|
|
136
|
+
const hash = await hasher.hash('SecurePass123');
|
|
137
|
+
|
|
138
|
+
const isValid = await hasher.verify('WrongPass123', hash);
|
|
139
|
+
expect(isValid).toBe(false);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should return false for empty password', async () => {
|
|
143
|
+
const hash = await hasher.hash('SecurePass123');
|
|
144
|
+
|
|
145
|
+
const isValid = await hasher.verify('', hash);
|
|
146
|
+
expect(isValid).toBe(false);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('should return false for empty hash', async () => {
|
|
150
|
+
const isValid = await hasher.verify('SecurePass123', '');
|
|
151
|
+
expect(isValid).toBe(false);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('should return false for invalid hash format', async () => {
|
|
155
|
+
const isValid = await hasher.verify('SecurePass123', 'invalid-hash');
|
|
156
|
+
expect(isValid).toBe(false);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('should return false for SHA-256 hash (old format)', async () => {
|
|
160
|
+
// SHA-256 hash format (what we're replacing)
|
|
161
|
+
const sha256Hash = 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3';
|
|
162
|
+
|
|
163
|
+
const isValid = await hasher.verify('password', sha256Hash);
|
|
164
|
+
expect(isValid).toBe(false);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
describe('Rehash Detection', () => {
|
|
169
|
+
it('should not need rehash for current rounds', async () => {
|
|
170
|
+
const hash = await hasher.hash('SecurePass123');
|
|
171
|
+
expect(hasher.needsRehash(hash)).toBe(false);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should need rehash for lower rounds', () => {
|
|
175
|
+
// Hash with 10 rounds
|
|
176
|
+
const lowRoundsHash = '$2b$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy';
|
|
177
|
+
expect(hasher.needsRehash(lowRoundsHash)).toBe(true);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('should need rehash for invalid format', () => {
|
|
181
|
+
expect(hasher.needsRehash('invalid-hash')).toBe(true);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should need rehash for SHA-256 hash', () => {
|
|
185
|
+
const sha256Hash = 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3';
|
|
186
|
+
expect(hasher.needsRehash(sha256Hash)).toBe(true);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe('Factory Function', () => {
|
|
191
|
+
it('should create hasher with specified rounds', () => {
|
|
192
|
+
const hasher12 = createPasswordHasher(12);
|
|
193
|
+
expect(hasher12.getConfig().rounds).toBe(12);
|
|
194
|
+
|
|
195
|
+
const hasher14 = createPasswordHasher(14);
|
|
196
|
+
expect(hasher14.getConfig().rounds).toBe(14);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('should use default 12 rounds', () => {
|
|
200
|
+
const hasher = createPasswordHasher();
|
|
201
|
+
expect(hasher.getConfig().rounds).toBe(12);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe('CVE-2 Security Verification', () => {
|
|
206
|
+
it('should NOT use hardcoded salt', async () => {
|
|
207
|
+
// Generate multiple hashes and verify they have different salts
|
|
208
|
+
const hashes = await Promise.all([
|
|
209
|
+
hasher.hash('SecurePass123'),
|
|
210
|
+
hasher.hash('SecurePass123'),
|
|
211
|
+
hasher.hash('SecurePass123'),
|
|
212
|
+
]);
|
|
213
|
+
|
|
214
|
+
// Extract salts (22 chars after $2b$12$)
|
|
215
|
+
const salts = hashes.map(h => h.substring(7, 29));
|
|
216
|
+
|
|
217
|
+
// All salts should be unique
|
|
218
|
+
const uniqueSalts = new Set(salts);
|
|
219
|
+
expect(uniqueSalts.size).toBe(3);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('should NOT produce same hash for same input (unlike SHA-256)', async () => {
|
|
223
|
+
// SHA-256 with hardcoded salt would produce identical output
|
|
224
|
+
const hash1 = await hasher.hash('SecurePass123');
|
|
225
|
+
const hash2 = await hasher.hash('SecurePass123');
|
|
226
|
+
|
|
227
|
+
expect(hash1).not.toBe(hash2);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('should produce hash that takes time to compute', async () => {
|
|
231
|
+
const start = Date.now();
|
|
232
|
+
await hasher.hash('SecurePass123');
|
|
233
|
+
const duration = Date.now() - start;
|
|
234
|
+
|
|
235
|
+
// bcrypt with 12 rounds should take measurable time (>10ms typically)
|
|
236
|
+
expect(duration).toBeGreaterThan(5);
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
});
|