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

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 (150) 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__/keychain-adapter.test.ts +60 -0
  12. package/__tests__/oauth-browser.test.ts +13 -0
  13. package/__tests__/oauth-export-surface.test.ts +21 -0
  14. package/__tests__/oauth-pkce.test.ts +37 -0
  15. package/__tests__/password-hasher.test.ts +239 -239
  16. package/__tests__/path-validator.test.ts +302 -302
  17. package/__tests__/plugin-integrity-verifier.test.ts +199 -0
  18. package/__tests__/safe-executor.test.ts +292 -292
  19. package/__tests__/token-generator.test.ts +371 -371
  20. package/__tests__/tool-output-guardrail.test.ts +256 -0
  21. package/__tests__/unit/credential-generator.test.ts +182 -182
  22. package/__tests__/unit/password-hasher.test.ts +359 -359
  23. package/__tests__/unit/path-validator.test.ts +509 -509
  24. package/__tests__/unit/safe-executor.test.ts +667 -667
  25. package/__tests__/unit/token-generator.test.ts +310 -310
  26. package/dist/CVE-REMEDIATION.d.ts +17 -6
  27. package/dist/CVE-REMEDIATION.d.ts.map +1 -1
  28. package/dist/CVE-REMEDIATION.js +328 -12
  29. package/dist/CVE-REMEDIATION.js.map +1 -1
  30. package/dist/authorization/propagator.d.ts +148 -0
  31. package/dist/authorization/propagator.d.ts.map +1 -0
  32. package/dist/authorization/propagator.js +182 -0
  33. package/dist/authorization/propagator.js.map +1 -0
  34. package/dist/credential-generator.js +9 -9
  35. package/dist/domain/services/security-domain-service.js +1 -1
  36. package/dist/domain/services/security-domain-service.js.map +1 -1
  37. package/dist/index.d.ts +9 -1
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +21 -1
  40. package/dist/index.js.map +1 -1
  41. package/dist/keychain-adapter.d.ts +48 -0
  42. package/dist/keychain-adapter.d.ts.map +1 -0
  43. package/dist/keychain-adapter.js +0 -0
  44. package/dist/keychain-adapter.js.map +1 -0
  45. package/dist/oauth/browser.d.ts +31 -0
  46. package/dist/oauth/browser.d.ts.map +1 -0
  47. package/dist/oauth/browser.js +55 -0
  48. package/dist/oauth/browser.js.map +1 -0
  49. package/dist/oauth/callback-server.d.ts +37 -0
  50. package/dist/oauth/callback-server.d.ts.map +1 -0
  51. package/dist/oauth/callback-server.js +86 -0
  52. package/dist/oauth/callback-server.js.map +1 -0
  53. package/dist/oauth/client.d.ts +57 -0
  54. package/dist/oauth/client.d.ts.map +1 -0
  55. package/dist/oauth/client.js +120 -0
  56. package/dist/oauth/client.js.map +1 -0
  57. package/dist/oauth/pkce.d.ts +25 -0
  58. package/dist/oauth/pkce.d.ts.map +1 -0
  59. package/dist/oauth/pkce.js +36 -0
  60. package/dist/oauth/pkce.js.map +1 -0
  61. package/dist/password-hasher.d.ts.map +1 -1
  62. package/dist/password-hasher.js +8 -1
  63. package/dist/password-hasher.js.map +1 -1
  64. package/dist/plugins/integrity-verifier.d.ts +125 -0
  65. package/dist/plugins/integrity-verifier.d.ts.map +1 -0
  66. package/dist/plugins/integrity-verifier.js +186 -0
  67. package/dist/plugins/integrity-verifier.js.map +1 -0
  68. package/dist/safe-executor.d.ts +7 -0
  69. package/dist/safe-executor.d.ts.map +1 -1
  70. package/dist/safe-executor.js +21 -1
  71. package/dist/safe-executor.js.map +1 -1
  72. package/dist/tool-output-guardrail.d.ts +126 -0
  73. package/dist/tool-output-guardrail.d.ts.map +1 -0
  74. package/dist/tool-output-guardrail.js +257 -0
  75. package/dist/tool-output-guardrail.js.map +1 -0
  76. package/node_modules-incomplete-release-test/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  77. package/package.json +33 -28
  78. package/scripts/verify-oauth-exports.mjs +29 -0
  79. package/src/CVE-REMEDIATION.ts +591 -251
  80. package/src/application/index.ts +10 -10
  81. package/src/application/services/security-application-service.ts +193 -193
  82. package/src/authorization/propagator.ts +279 -0
  83. package/src/credential-generator.ts +368 -368
  84. package/src/domain/entities/security-context.ts +173 -173
  85. package/src/domain/index.ts +17 -17
  86. package/src/domain/services/security-domain-service.ts +296 -296
  87. package/src/index.ts +340 -271
  88. package/src/input-validator.ts +466 -466
  89. package/src/keychain-adapter.ts +0 -0
  90. package/src/oauth/browser.ts +58 -0
  91. package/src/oauth/callback-server.ts +97 -0
  92. package/src/oauth/client.ts +153 -0
  93. package/src/oauth/pkce.ts +45 -0
  94. package/src/password-hasher.ts +277 -270
  95. package/src/path-validator.ts +525 -525
  96. package/src/plugins/integrity-verifier.ts +265 -0
  97. package/src/safe-executor.ts +546 -525
  98. package/src/token-generator.ts +463 -463
  99. package/src/tool-output-guardrail.ts +360 -0
  100. package/tmp.json +0 -0
  101. package/tsconfig.json +9 -9
  102. package/tsconfig.tsbuildinfo +1 -1
  103. package/vitest.config.ts +20 -0
  104. package/.agentic-flow/intelligence.json +0 -16
  105. package/__tests__/coverage/base.css +0 -224
  106. package/__tests__/coverage/block-navigation.js +0 -87
  107. package/__tests__/coverage/coverage-final.json +0 -11
  108. package/__tests__/coverage/favicon.png +0 -0
  109. package/__tests__/coverage/index.html +0 -161
  110. package/__tests__/coverage/lcov-report/base.css +0 -224
  111. package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
  112. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  113. package/__tests__/coverage/lcov-report/index.html +0 -161
  114. package/__tests__/coverage/lcov-report/prettify.css +0 -1
  115. package/__tests__/coverage/lcov-report/prettify.js +0 -2
  116. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  117. package/__tests__/coverage/lcov-report/sorter.js +0 -210
  118. package/__tests__/coverage/lcov-report/src/CVE-REMEDIATION.ts.html +0 -838
  119. package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
  120. package/__tests__/coverage/lcov-report/src/application/services/security-application-service.ts.html +0 -664
  121. package/__tests__/coverage/lcov-report/src/credential-generator.ts.html +0 -1189
  122. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
  123. package/__tests__/coverage/lcov-report/src/domain/entities/security-context.ts.html +0 -604
  124. package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
  125. package/__tests__/coverage/lcov-report/src/domain/services/security-domain-service.ts.html +0 -973
  126. package/__tests__/coverage/lcov-report/src/index.html +0 -206
  127. package/__tests__/coverage/lcov-report/src/input-validator.ts.html +0 -1483
  128. package/__tests__/coverage/lcov-report/src/password-hasher.ts.html +0 -895
  129. package/__tests__/coverage/lcov-report/src/path-validator.ts.html +0 -1660
  130. package/__tests__/coverage/lcov-report/src/safe-executor.ts.html +0 -1660
  131. package/__tests__/coverage/lcov-report/src/token-generator.ts.html +0 -1474
  132. package/__tests__/coverage/lcov.info +0 -3650
  133. package/__tests__/coverage/prettify.css +0 -1
  134. package/__tests__/coverage/prettify.js +0 -2
  135. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  136. package/__tests__/coverage/sorter.js +0 -210
  137. package/__tests__/coverage/src/CVE-REMEDIATION.ts.html +0 -838
  138. package/__tests__/coverage/src/application/services/index.html +0 -116
  139. package/__tests__/coverage/src/application/services/security-application-service.ts.html +0 -664
  140. package/__tests__/coverage/src/credential-generator.ts.html +0 -1189
  141. package/__tests__/coverage/src/domain/entities/index.html +0 -116
  142. package/__tests__/coverage/src/domain/entities/security-context.ts.html +0 -604
  143. package/__tests__/coverage/src/domain/services/index.html +0 -116
  144. package/__tests__/coverage/src/domain/services/security-domain-service.ts.html +0 -973
  145. package/__tests__/coverage/src/index.html +0 -206
  146. package/__tests__/coverage/src/input-validator.ts.html +0 -1483
  147. package/__tests__/coverage/src/password-hasher.ts.html +0 -895
  148. package/__tests__/coverage/src/path-validator.ts.html +0 -1660
  149. package/__tests__/coverage/src/safe-executor.ts.html +0 -1660
  150. package/__tests__/coverage/src/token-generator.ts.html +0 -1474
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Keychain adapter tests. `SessionOnlyKeychainAdapter` is fully covered
3
+ * offline (no native backend needed). `createKeychainAdapter()`'s native
4
+ * path is exercised only behind a real `isAvailable()` canary probe — CI
5
+ * environments with no reachable keychain backend skip gracefully rather
6
+ * than faking success, per ADR-306's degrade-to-session-only contract.
7
+ */
8
+
9
+ import { describe, it, expect } from 'vitest';
10
+ import { createKeychainAdapter, SessionOnlyKeychainAdapter } from '../src/keychain-adapter.js';
11
+
12
+ describe('SessionOnlyKeychainAdapter', () => {
13
+ it('round-trips a secret in memory', async () => {
14
+ const adapter = new SessionOnlyKeychainAdapter();
15
+ await adapter.setSecret('svc', 'acct', 'topsecret');
16
+ expect(await adapter.getSecret('svc', 'acct')).toBe('topsecret');
17
+ });
18
+
19
+ it('returns null for a secret that was never set', async () => {
20
+ const adapter = new SessionOnlyKeychainAdapter();
21
+ expect(await adapter.getSecret('svc', 'nobody')).toBeNull();
22
+ });
23
+
24
+ it('deletes a secret', async () => {
25
+ const adapter = new SessionOnlyKeychainAdapter();
26
+ await adapter.setSecret('svc', 'acct', 'topsecret');
27
+ await adapter.deleteSecret('svc', 'acct');
28
+ expect(await adapter.getSecret('svc', 'acct')).toBeNull();
29
+ });
30
+
31
+ it('is always available', async () => {
32
+ expect(await new SessionOnlyKeychainAdapter().isAvailable()).toBe(true);
33
+ });
34
+
35
+ it('keeps distinct (service, account) pairs isolated', async () => {
36
+ const adapter = new SessionOnlyKeychainAdapter();
37
+ await adapter.setSecret('svc-a', 'acct', 'secret-a');
38
+ await adapter.setSecret('svc-b', 'acct', 'secret-b');
39
+ expect(await adapter.getSecret('svc-a', 'acct')).toBe('secret-a');
40
+ expect(await adapter.getSecret('svc-b', 'acct')).toBe('secret-b');
41
+ });
42
+ });
43
+
44
+ describe('createKeychainAdapter', () => {
45
+ it('resolves to an adapter that is always available (native or session-only fallback)', async () => {
46
+ const adapter = await createKeychainAdapter();
47
+ expect(await adapter.isAvailable()).toBe(true);
48
+ });
49
+
50
+ it('round-trips a canary secret through whatever backend was selected', async () => {
51
+ const adapter = await createKeychainAdapter();
52
+ const account = `ruflo-test-${Date.now()}`;
53
+ await adapter.setSecret('ruflo-security-test', account, 'round-trip-value');
54
+ try {
55
+ expect(await adapter.getSecret('ruflo-security-test', account)).toBe('round-trip-value');
56
+ } finally {
57
+ await adapter.deleteSecret('ruflo-security-test', account);
58
+ }
59
+ });
60
+ });
@@ -0,0 +1,13 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { browserCommand } from '../src/oauth/browser.js';
3
+
4
+ describe('OAuth browser launcher', () => {
5
+ it('preserves the complete OAuth query string on Windows without cmd.exe', () => {
6
+ const url = 'https://auth.cognitum.one/oauth/authorize?response_type=code&client_id=meta-proxy&state=test';
7
+ const launch = browserCommand(url, 'win32');
8
+
9
+ expect(launch.cmd).toBe('rundll32.exe');
10
+ expect(launch.args).toEqual(['url.dll,FileProtocolHandler', url]);
11
+ expect(launch.args[1]).toContain('&client_id=meta-proxy&');
12
+ });
13
+ });
@@ -0,0 +1,21 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import * as security from '../src/index.js';
3
+
4
+ describe('OAuth public export surface', () => {
5
+ it('exports every primitive required by ruflo auth', () => {
6
+ for (const name of [
7
+ 'authorizeUrl',
8
+ 'exchangeCode',
9
+ 'refreshToken',
10
+ 'exchangeManualCode',
11
+ 'generatePkce',
12
+ 'CallbackServer',
13
+ 'openBrowser',
14
+ 'createKeychainAdapter',
15
+ 'OAuthError',
16
+ ] as const) {
17
+ expect(security[name]).toBeTypeOf('function');
18
+ }
19
+ expect(security.OOB_REDIRECT_URI).toBeTypeOf('string');
20
+ });
21
+ });
@@ -0,0 +1,37 @@
1
+ /**
2
+ * PKCE (RFC 7636) generation tests — the port's correctness check against
3
+ * the same values meta-proxy's Rust `pkce.rs` test suite verifies.
4
+ */
5
+
6
+ import { describe, it, expect } from 'vitest';
7
+ import { generate, challengeFromVerifier } from '../src/oauth/pkce.js';
8
+
9
+ describe('oauth/pkce', () => {
10
+ it('generates a verifier within RFC 7636 length bounds (43-128 chars)', () => {
11
+ const req = generate();
12
+ expect(req.codeVerifier.length).toBeGreaterThanOrEqual(43);
13
+ expect(req.codeVerifier.length).toBeLessThanOrEqual(128);
14
+ });
15
+
16
+ it('produces a deterministic challenge for a given verifier', () => {
17
+ const req = generate();
18
+ expect(challengeFromVerifier(req.codeVerifier)).toBe(req.codeChallenge);
19
+ });
20
+
21
+ it('never collides across two generations', () => {
22
+ const a = generate();
23
+ const b = generate();
24
+ expect(a.state).not.toBe(b.state);
25
+ expect(a.codeVerifier).not.toBe(b.codeVerifier);
26
+ expect(a.codeChallenge).not.toBe(b.codeChallenge);
27
+ });
28
+
29
+ it('matches the RFC 7636 Appendix B worked example', () => {
30
+ // The exact vector meta-proxy's pkce.rs test suite uses — confirms this
31
+ // port's encoding (base64url, no padding) and hash (SHA-256) match byte
32
+ // for byte, since a verifier generated here must validate against the
33
+ // same identity server meta-proxy already talks to successfully.
34
+ const verifier = 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk';
35
+ expect(challengeFromVerifier(verifier)).toBe('E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM');
36
+ });
37
+ });
@@ -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 '../../security/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
- });
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
+ });