@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.
Files changed (96) hide show
  1. package/README.md +234 -234
  2. package/__tests__/acceptance/security-compliance.test.ts +674 -674
  3. package/__tests__/credential-generator.test.ts +310 -310
  4. package/__tests__/fixtures/configurations.ts +425 -419
  5. package/__tests__/fixtures/index.ts +21 -21
  6. package/__tests__/helpers/create-mock.ts +469 -469
  7. package/__tests__/helpers/index.ts +32 -32
  8. package/__tests__/input-validator.test.ts +382 -381
  9. package/__tests__/integration/security-flow.test.ts +606 -606
  10. package/__tests__/password-hasher.test.ts +239 -239
  11. package/__tests__/path-validator.test.ts +302 -302
  12. package/__tests__/safe-executor.test.ts +292 -292
  13. package/__tests__/token-generator.test.ts +371 -371
  14. package/__tests__/unit/credential-generator.test.ts +182 -182
  15. package/__tests__/unit/password-hasher.test.ts +359 -359
  16. package/__tests__/unit/path-validator.test.ts +509 -509
  17. package/__tests__/unit/safe-executor.test.ts +667 -667
  18. package/__tests__/unit/token-generator.test.ts +310 -310
  19. package/dist/credential-generator.js +9 -9
  20. package/dist/domain/services/security-domain-service.js +1 -1
  21. package/dist/domain/services/security-domain-service.js.map +1 -1
  22. package/dist/index.d.ts +1 -1
  23. package/dist/index.js +1 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/password-hasher.d.ts.map +1 -1
  26. package/dist/password-hasher.js +8 -1
  27. package/dist/password-hasher.js.map +1 -1
  28. package/dist/safe-executor.d.ts +7 -0
  29. package/dist/safe-executor.d.ts.map +1 -1
  30. package/dist/safe-executor.js +21 -1
  31. package/dist/safe-executor.js.map +1 -1
  32. package/package.json +27 -28
  33. package/src/CVE-REMEDIATION.ts +251 -251
  34. package/src/application/index.ts +10 -10
  35. package/src/application/services/security-application-service.ts +193 -193
  36. package/src/credential-generator.ts +368 -368
  37. package/src/domain/entities/security-context.ts +173 -173
  38. package/src/domain/index.ts +17 -17
  39. package/src/domain/services/security-domain-service.ts +296 -296
  40. package/src/index.ts +271 -271
  41. package/src/input-validator.ts +466 -466
  42. package/src/password-hasher.ts +277 -270
  43. package/src/path-validator.ts +525 -525
  44. package/src/safe-executor.ts +546 -525
  45. package/src/token-generator.ts +463 -463
  46. package/tmp.json +0 -0
  47. package/tsconfig.json +9 -9
  48. package/tsconfig.tsbuildinfo +1 -1
  49. package/vitest.config.ts +20 -0
  50. package/.agentic-flow/intelligence.json +0 -16
  51. package/__tests__/coverage/base.css +0 -224
  52. package/__tests__/coverage/block-navigation.js +0 -87
  53. package/__tests__/coverage/coverage-final.json +0 -11
  54. package/__tests__/coverage/favicon.png +0 -0
  55. package/__tests__/coverage/index.html +0 -161
  56. package/__tests__/coverage/lcov-report/base.css +0 -224
  57. package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
  58. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  59. package/__tests__/coverage/lcov-report/index.html +0 -161
  60. package/__tests__/coverage/lcov-report/prettify.css +0 -1
  61. package/__tests__/coverage/lcov-report/prettify.js +0 -2
  62. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  63. package/__tests__/coverage/lcov-report/sorter.js +0 -210
  64. package/__tests__/coverage/lcov-report/src/CVE-REMEDIATION.ts.html +0 -838
  65. package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
  66. package/__tests__/coverage/lcov-report/src/application/services/security-application-service.ts.html +0 -664
  67. package/__tests__/coverage/lcov-report/src/credential-generator.ts.html +0 -1189
  68. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
  69. package/__tests__/coverage/lcov-report/src/domain/entities/security-context.ts.html +0 -604
  70. package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
  71. package/__tests__/coverage/lcov-report/src/domain/services/security-domain-service.ts.html +0 -973
  72. package/__tests__/coverage/lcov-report/src/index.html +0 -206
  73. package/__tests__/coverage/lcov-report/src/input-validator.ts.html +0 -1483
  74. package/__tests__/coverage/lcov-report/src/password-hasher.ts.html +0 -895
  75. package/__tests__/coverage/lcov-report/src/path-validator.ts.html +0 -1660
  76. package/__tests__/coverage/lcov-report/src/safe-executor.ts.html +0 -1660
  77. package/__tests__/coverage/lcov-report/src/token-generator.ts.html +0 -1474
  78. package/__tests__/coverage/lcov.info +0 -3650
  79. package/__tests__/coverage/prettify.css +0 -1
  80. package/__tests__/coverage/prettify.js +0 -2
  81. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  82. package/__tests__/coverage/sorter.js +0 -210
  83. package/__tests__/coverage/src/CVE-REMEDIATION.ts.html +0 -838
  84. package/__tests__/coverage/src/application/services/index.html +0 -116
  85. package/__tests__/coverage/src/application/services/security-application-service.ts.html +0 -664
  86. package/__tests__/coverage/src/credential-generator.ts.html +0 -1189
  87. package/__tests__/coverage/src/domain/entities/index.html +0 -116
  88. package/__tests__/coverage/src/domain/entities/security-context.ts.html +0 -604
  89. package/__tests__/coverage/src/domain/services/index.html +0 -116
  90. package/__tests__/coverage/src/domain/services/security-domain-service.ts.html +0 -973
  91. package/__tests__/coverage/src/index.html +0 -206
  92. package/__tests__/coverage/src/input-validator.ts.html +0 -1483
  93. package/__tests__/coverage/src/password-hasher.ts.html +0 -895
  94. package/__tests__/coverage/src/path-validator.ts.html +0 -1660
  95. package/__tests__/coverage/src/safe-executor.ts.html +0 -1660
  96. package/__tests__/coverage/src/token-generator.ts.html +0 -1474
@@ -1,310 +1,310 @@
1
- /**
2
- * Credential Generator Tests - CVE-3 Remediation Validation
3
- *
4
- * Tests verify:
5
- * - Cryptographically secure random generation
6
- * - No hardcoded defaults
7
- * - Proper entropy levels
8
- * - Unique credentials per generation
9
- */
10
-
11
- import { describe, it, expect, beforeEach } from 'vitest';
12
- import {
13
- CredentialGenerator,
14
- CredentialGeneratorError,
15
- createCredentialGenerator,
16
- generateCredentials,
17
- } from '../../security/credential-generator.js';
18
-
19
- describe('CredentialGenerator', () => {
20
- let generator: CredentialGenerator;
21
-
22
- beforeEach(() => {
23
- generator = new CredentialGenerator();
24
- });
25
-
26
- describe('Configuration', () => {
27
- it('should use default configuration', () => {
28
- expect(() => new CredentialGenerator()).not.toThrow();
29
- });
30
-
31
- it('should reject password length below 16', () => {
32
- expect(() => new CredentialGenerator({
33
- passwordLength: 8,
34
- })).toThrow(CredentialGeneratorError);
35
- });
36
-
37
- it('should reject API key length below 32', () => {
38
- expect(() => new CredentialGenerator({
39
- apiKeyLength: 16,
40
- })).toThrow(CredentialGeneratorError);
41
- });
42
-
43
- it('should reject secret length below 32', () => {
44
- expect(() => new CredentialGenerator({
45
- secretLength: 16,
46
- })).toThrow(CredentialGeneratorError);
47
- });
48
- });
49
-
50
- describe('Password Generation', () => {
51
- it('should generate password of specified length', () => {
52
- const password = generator.generatePassword();
53
- expect(password.length).toBeGreaterThanOrEqual(32);
54
- });
55
-
56
- it('should generate unique passwords', () => {
57
- const passwords = new Set<string>();
58
- for (let i = 0; i < 100; i++) {
59
- passwords.add(generator.generatePassword());
60
- }
61
- expect(passwords.size).toBe(100);
62
- });
63
-
64
- it('should include required character types', () => {
65
- for (let i = 0; i < 10; i++) {
66
- const password = generator.generatePassword();
67
- expect(password).toMatch(/[A-Z]/); // Uppercase
68
- expect(password).toMatch(/[a-z]/); // Lowercase
69
- expect(password).toMatch(/\d/); // Digit
70
- expect(password).toMatch(/[!@#$%^&*()_+\-=\[\]{}|;:,.<>?]/); // Special
71
- }
72
- });
73
- });
74
-
75
- describe('API Key Generation', () => {
76
- it('should generate API key with prefix', () => {
77
- const { key, prefix } = generator.generateApiKey('cf_');
78
- expect(key.startsWith('cf_')).toBe(true);
79
- expect(prefix).toBe('cf_');
80
- });
81
-
82
- it('should generate unique keys', () => {
83
- const keys = new Set<string>();
84
- for (let i = 0; i < 100; i++) {
85
- keys.add(generator.generateApiKey().key);
86
- }
87
- expect(keys.size).toBe(100);
88
- });
89
-
90
- it('should include keyId', () => {
91
- const { keyId } = generator.generateApiKey();
92
- // Should be a valid UUID
93
- expect(keyId).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i);
94
- });
95
-
96
- it('should include createdAt timestamp', () => {
97
- const { createdAt } = generator.generateApiKey();
98
- expect(createdAt).toBeInstanceOf(Date);
99
- });
100
- });
101
-
102
- describe('Secret Generation', () => {
103
- it('should generate hex-encoded secret', () => {
104
- const secret = generator.generateSecret();
105
- expect(secret).toMatch(/^[0-9a-f]+$/);
106
- });
107
-
108
- it('should generate secret of specified length', () => {
109
- const secret = generator.generateSecret(64);
110
- expect(secret.length).toBe(64);
111
- });
112
-
113
- it('should generate unique secrets', () => {
114
- const secrets = new Set<string>();
115
- for (let i = 0; i < 100; i++) {
116
- secrets.add(generator.generateSecret());
117
- }
118
- expect(secrets.size).toBe(100);
119
- });
120
- });
121
-
122
- describe('Encryption Key Generation', () => {
123
- it('should generate 32-byte (256-bit) key as hex', () => {
124
- const key = generator.generateEncryptionKey();
125
- // 32 bytes = 64 hex characters
126
- expect(key.length).toBe(64);
127
- expect(key).toMatch(/^[0-9a-f]+$/);
128
- });
129
-
130
- it('should generate unique keys', () => {
131
- const keys = new Set<string>();
132
- for (let i = 0; i < 100; i++) {
133
- keys.add(generator.generateEncryptionKey());
134
- }
135
- expect(keys.size).toBe(100);
136
- });
137
- });
138
-
139
- describe('Installation Credentials', () => {
140
- it('should generate complete credential set', () => {
141
- const credentials = generator.generateInstallationCredentials();
142
-
143
- expect(credentials.adminPassword).toBeDefined();
144
- expect(credentials.servicePassword).toBeDefined();
145
- expect(credentials.jwtSecret).toBeDefined();
146
- expect(credentials.sessionSecret).toBeDefined();
147
- expect(credentials.encryptionKey).toBeDefined();
148
- expect(credentials.generatedAt).toBeInstanceOf(Date);
149
- });
150
-
151
- it('should set expiration when specified', () => {
152
- const credentials = generator.generateInstallationCredentials(30);
153
-
154
- expect(credentials.expiresAt).toBeInstanceOf(Date);
155
-
156
- // Should be approximately 30 days in the future
157
- const expectedExpiration = Date.now() + 30 * 24 * 60 * 60 * 1000;
158
- const actualExpiration = credentials.expiresAt!.getTime();
159
- expect(Math.abs(actualExpiration - expectedExpiration)).toBeLessThan(1000);
160
- });
161
-
162
- it('should generate unique credentials each time', () => {
163
- const cred1 = generator.generateInstallationCredentials();
164
- const cred2 = generator.generateInstallationCredentials();
165
-
166
- expect(cred1.adminPassword).not.toBe(cred2.adminPassword);
167
- expect(cred1.servicePassword).not.toBe(cred2.servicePassword);
168
- expect(cred1.jwtSecret).not.toBe(cred2.jwtSecret);
169
- });
170
- });
171
-
172
- describe('Token Generation', () => {
173
- it('should generate session token', () => {
174
- const token = generator.generateSessionToken();
175
- expect(token).toBeDefined();
176
- expect(token.length).toBeGreaterThan(0);
177
- });
178
-
179
- it('should generate CSRF token', () => {
180
- const token = generator.generateCsrfToken();
181
- expect(token).toBeDefined();
182
- // Base64url encoded
183
- expect(token).toMatch(/^[A-Za-z0-9_-]+$/);
184
- });
185
-
186
- it('should generate nonce', () => {
187
- const nonce = generator.generateNonce();
188
- expect(nonce).toBeDefined();
189
- // Hex encoded, 16 bytes = 32 chars
190
- expect(nonce.length).toBe(32);
191
- expect(nonce).toMatch(/^[0-9a-f]+$/);
192
- });
193
- });
194
-
195
- describe('Environment Script Generation', () => {
196
- it('should generate valid shell script', () => {
197
- const credentials = generator.generateInstallationCredentials();
198
- const script = generator.createEnvScript(credentials);
199
-
200
- expect(script).toContain('export CLAUDE_FLOW_ADMIN_PASSWORD=');
201
- expect(script).toContain('export CLAUDE_FLOW_SERVICE_PASSWORD=');
202
- expect(script).toContain('export CLAUDE_FLOW_JWT_SECRET=');
203
- expect(script).toContain('export CLAUDE_FLOW_SESSION_SECRET=');
204
- expect(script).toContain('export CLAUDE_FLOW_ENCRYPTION_KEY=');
205
- });
206
-
207
- it('should include warning comment', () => {
208
- const credentials = generator.generateInstallationCredentials();
209
- const script = generator.createEnvScript(credentials);
210
-
211
- expect(script).toContain('Store these securely');
212
- });
213
- });
214
-
215
- describe('JSON Config Generation', () => {
216
- it('should generate valid JSON', () => {
217
- const credentials = generator.generateInstallationCredentials();
218
- const json = generator.createJsonConfig(credentials);
219
-
220
- expect(() => JSON.parse(json)).not.toThrow();
221
- });
222
-
223
- it('should include all credentials', () => {
224
- const credentials = generator.generateInstallationCredentials();
225
- const json = generator.createJsonConfig(credentials);
226
- const parsed = JSON.parse(json);
227
-
228
- expect(parsed['claude-flow/admin-password']).toBe(credentials.adminPassword);
229
- expect(parsed['claude-flow/service-password']).toBe(credentials.servicePassword);
230
- expect(parsed['claude-flow/jwt-secret']).toBe(credentials.jwtSecret);
231
- expect(parsed['claude-flow/session-secret']).toBe(credentials.sessionSecret);
232
- expect(parsed['claude-flow/encryption-key']).toBe(credentials.encryptionKey);
233
- });
234
- });
235
-
236
- describe('Factory Functions', () => {
237
- it('should create generator with factory function', () => {
238
- const gen = createCredentialGenerator();
239
- expect(gen).toBeInstanceOf(CredentialGenerator);
240
- });
241
-
242
- it('should generate credentials with quick function', () => {
243
- const credentials = generateCredentials();
244
- expect(credentials.adminPassword).toBeDefined();
245
- expect(credentials.jwtSecret).toBeDefined();
246
- });
247
- });
248
-
249
- describe('CVE-3 Security Verification', () => {
250
- it('should NOT produce hardcoded admin123 password', () => {
251
- for (let i = 0; i < 100; i++) {
252
- const credentials = generator.generateInstallationCredentials();
253
- expect(credentials.adminPassword).not.toBe('admin123');
254
- expect(credentials.servicePassword).not.toBe('service123');
255
- }
256
- });
257
-
258
- it('should NOT produce predictable patterns', () => {
259
- const passwords = new Set<string>();
260
- for (let i = 0; i < 1000; i++) {
261
- passwords.add(generator.generatePassword());
262
- }
263
-
264
- // All should be unique
265
- expect(passwords.size).toBe(1000);
266
-
267
- // None should match common patterns
268
- const commonPatterns = ['password', 'admin', 'service', '123456', 'qwerty'];
269
- for (const password of passwords) {
270
- for (const pattern of commonPatterns) {
271
- expect(password.toLowerCase()).not.toContain(pattern);
272
- }
273
- }
274
- });
275
-
276
- it('should use cryptographically secure random', () => {
277
- // Generate many passwords and verify entropy
278
- const passwords: string[] = [];
279
- for (let i = 0; i < 100; i++) {
280
- passwords.push(generator.generatePassword());
281
- }
282
-
283
- // Calculate average character distribution
284
- const charCounts = new Map<string, number>();
285
- for (const password of passwords) {
286
- for (const char of password) {
287
- charCounts.set(char, (charCounts.get(char) || 0) + 1);
288
- }
289
- }
290
-
291
- // Should have good distribution (many different characters)
292
- expect(charCounts.size).toBeGreaterThan(30);
293
- });
294
-
295
- it('should generate installation credentials with sufficient entropy', () => {
296
- const credentials = generator.generateInstallationCredentials();
297
-
298
- // Password should be at least 256 bits of entropy at minimum
299
- expect(credentials.adminPassword.length).toBeGreaterThanOrEqual(32);
300
- expect(credentials.servicePassword.length).toBeGreaterThanOrEqual(32);
301
-
302
- // Secrets should be 512 bits
303
- expect(credentials.jwtSecret.length).toBeGreaterThanOrEqual(64);
304
- expect(credentials.sessionSecret.length).toBeGreaterThanOrEqual(64);
305
-
306
- // Encryption key should be exactly 256 bits (64 hex chars)
307
- expect(credentials.encryptionKey.length).toBe(64);
308
- });
309
- });
310
- });
1
+ /**
2
+ * Credential Generator Tests - CVE-3 Remediation Validation
3
+ *
4
+ * Tests verify:
5
+ * - Cryptographically secure random generation
6
+ * - No hardcoded defaults
7
+ * - Proper entropy levels
8
+ * - Unique credentials per generation
9
+ */
10
+
11
+ import { describe, it, expect, beforeEach } from 'vitest';
12
+ import {
13
+ CredentialGenerator,
14
+ CredentialGeneratorError,
15
+ createCredentialGenerator,
16
+ generateCredentials,
17
+ } from '../src/credential-generator.js';
18
+
19
+ describe('CredentialGenerator', () => {
20
+ let generator: CredentialGenerator;
21
+
22
+ beforeEach(() => {
23
+ generator = new CredentialGenerator();
24
+ });
25
+
26
+ describe('Configuration', () => {
27
+ it('should use default configuration', () => {
28
+ expect(() => new CredentialGenerator()).not.toThrow();
29
+ });
30
+
31
+ it('should reject password length below 16', () => {
32
+ expect(() => new CredentialGenerator({
33
+ passwordLength: 8,
34
+ })).toThrow(CredentialGeneratorError);
35
+ });
36
+
37
+ it('should reject API key length below 32', () => {
38
+ expect(() => new CredentialGenerator({
39
+ apiKeyLength: 16,
40
+ })).toThrow(CredentialGeneratorError);
41
+ });
42
+
43
+ it('should reject secret length below 32', () => {
44
+ expect(() => new CredentialGenerator({
45
+ secretLength: 16,
46
+ })).toThrow(CredentialGeneratorError);
47
+ });
48
+ });
49
+
50
+ describe('Password Generation', () => {
51
+ it('should generate password of specified length', () => {
52
+ const password = generator.generatePassword();
53
+ expect(password.length).toBeGreaterThanOrEqual(32);
54
+ });
55
+
56
+ it('should generate unique passwords', () => {
57
+ const passwords = new Set<string>();
58
+ for (let i = 0; i < 100; i++) {
59
+ passwords.add(generator.generatePassword());
60
+ }
61
+ expect(passwords.size).toBe(100);
62
+ });
63
+
64
+ it('should include required character types', () => {
65
+ for (let i = 0; i < 10; i++) {
66
+ const password = generator.generatePassword();
67
+ expect(password).toMatch(/[A-Z]/); // Uppercase
68
+ expect(password).toMatch(/[a-z]/); // Lowercase
69
+ expect(password).toMatch(/\d/); // Digit
70
+ expect(password).toMatch(/[!@#$%^&*()_+\-=\[\]{}|;:,.<>?]/); // Special
71
+ }
72
+ });
73
+ });
74
+
75
+ describe('API Key Generation', () => {
76
+ it('should generate API key with prefix', () => {
77
+ const { key, prefix } = generator.generateApiKey('cf_');
78
+ expect(key.startsWith('cf_')).toBe(true);
79
+ expect(prefix).toBe('cf_');
80
+ });
81
+
82
+ it('should generate unique keys', () => {
83
+ const keys = new Set<string>();
84
+ for (let i = 0; i < 100; i++) {
85
+ keys.add(generator.generateApiKey().key);
86
+ }
87
+ expect(keys.size).toBe(100);
88
+ });
89
+
90
+ it('should include keyId', () => {
91
+ const { keyId } = generator.generateApiKey();
92
+ // Should be a valid UUID
93
+ expect(keyId).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i);
94
+ });
95
+
96
+ it('should include createdAt timestamp', () => {
97
+ const { createdAt } = generator.generateApiKey();
98
+ expect(createdAt).toBeInstanceOf(Date);
99
+ });
100
+ });
101
+
102
+ describe('Secret Generation', () => {
103
+ it('should generate hex-encoded secret', () => {
104
+ const secret = generator.generateSecret();
105
+ expect(secret).toMatch(/^[0-9a-f]+$/);
106
+ });
107
+
108
+ it('should generate secret of specified length', () => {
109
+ const secret = generator.generateSecret(64);
110
+ expect(secret.length).toBe(64);
111
+ });
112
+
113
+ it('should generate unique secrets', () => {
114
+ const secrets = new Set<string>();
115
+ for (let i = 0; i < 100; i++) {
116
+ secrets.add(generator.generateSecret());
117
+ }
118
+ expect(secrets.size).toBe(100);
119
+ });
120
+ });
121
+
122
+ describe('Encryption Key Generation', () => {
123
+ it('should generate 32-byte (256-bit) key as hex', () => {
124
+ const key = generator.generateEncryptionKey();
125
+ // 32 bytes = 64 hex characters
126
+ expect(key.length).toBe(64);
127
+ expect(key).toMatch(/^[0-9a-f]+$/);
128
+ });
129
+
130
+ it('should generate unique keys', () => {
131
+ const keys = new Set<string>();
132
+ for (let i = 0; i < 100; i++) {
133
+ keys.add(generator.generateEncryptionKey());
134
+ }
135
+ expect(keys.size).toBe(100);
136
+ });
137
+ });
138
+
139
+ describe('Installation Credentials', () => {
140
+ it('should generate complete credential set', () => {
141
+ const credentials = generator.generateInstallationCredentials();
142
+
143
+ expect(credentials.adminPassword).toBeDefined();
144
+ expect(credentials.servicePassword).toBeDefined();
145
+ expect(credentials.jwtSecret).toBeDefined();
146
+ expect(credentials.sessionSecret).toBeDefined();
147
+ expect(credentials.encryptionKey).toBeDefined();
148
+ expect(credentials.generatedAt).toBeInstanceOf(Date);
149
+ });
150
+
151
+ it('should set expiration when specified', () => {
152
+ const credentials = generator.generateInstallationCredentials(30);
153
+
154
+ expect(credentials.expiresAt).toBeInstanceOf(Date);
155
+
156
+ // Should be approximately 30 days in the future
157
+ const expectedExpiration = Date.now() + 30 * 24 * 60 * 60 * 1000;
158
+ const actualExpiration = credentials.expiresAt!.getTime();
159
+ expect(Math.abs(actualExpiration - expectedExpiration)).toBeLessThan(1000);
160
+ });
161
+
162
+ it('should generate unique credentials each time', () => {
163
+ const cred1 = generator.generateInstallationCredentials();
164
+ const cred2 = generator.generateInstallationCredentials();
165
+
166
+ expect(cred1.adminPassword).not.toBe(cred2.adminPassword);
167
+ expect(cred1.servicePassword).not.toBe(cred2.servicePassword);
168
+ expect(cred1.jwtSecret).not.toBe(cred2.jwtSecret);
169
+ });
170
+ });
171
+
172
+ describe('Token Generation', () => {
173
+ it('should generate session token', () => {
174
+ const token = generator.generateSessionToken();
175
+ expect(token).toBeDefined();
176
+ expect(token.length).toBeGreaterThan(0);
177
+ });
178
+
179
+ it('should generate CSRF token', () => {
180
+ const token = generator.generateCsrfToken();
181
+ expect(token).toBeDefined();
182
+ // Base64url encoded
183
+ expect(token).toMatch(/^[A-Za-z0-9_-]+$/);
184
+ });
185
+
186
+ it('should generate nonce', () => {
187
+ const nonce = generator.generateNonce();
188
+ expect(nonce).toBeDefined();
189
+ // Hex encoded, 16 bytes = 32 chars
190
+ expect(nonce.length).toBe(32);
191
+ expect(nonce).toMatch(/^[0-9a-f]+$/);
192
+ });
193
+ });
194
+
195
+ describe('Environment Script Generation', () => {
196
+ it('should generate valid shell script', () => {
197
+ const credentials = generator.generateInstallationCredentials();
198
+ const script = generator.createEnvScript(credentials);
199
+
200
+ expect(script).toContain('export CLAUDE_FLOW_ADMIN_PASSWORD=');
201
+ expect(script).toContain('export CLAUDE_FLOW_SERVICE_PASSWORD=');
202
+ expect(script).toContain('export CLAUDE_FLOW_JWT_SECRET=');
203
+ expect(script).toContain('export CLAUDE_FLOW_SESSION_SECRET=');
204
+ expect(script).toContain('export CLAUDE_FLOW_ENCRYPTION_KEY=');
205
+ });
206
+
207
+ it('should include warning comment', () => {
208
+ const credentials = generator.generateInstallationCredentials();
209
+ const script = generator.createEnvScript(credentials);
210
+
211
+ expect(script).toContain('Store these securely');
212
+ });
213
+ });
214
+
215
+ describe('JSON Config Generation', () => {
216
+ it('should generate valid JSON', () => {
217
+ const credentials = generator.generateInstallationCredentials();
218
+ const json = generator.createJsonConfig(credentials);
219
+
220
+ expect(() => JSON.parse(json)).not.toThrow();
221
+ });
222
+
223
+ it('should include all credentials', () => {
224
+ const credentials = generator.generateInstallationCredentials();
225
+ const json = generator.createJsonConfig(credentials);
226
+ const parsed = JSON.parse(json);
227
+
228
+ expect(parsed['claude-flow/admin-password']).toBe(credentials.adminPassword);
229
+ expect(parsed['claude-flow/service-password']).toBe(credentials.servicePassword);
230
+ expect(parsed['claude-flow/jwt-secret']).toBe(credentials.jwtSecret);
231
+ expect(parsed['claude-flow/session-secret']).toBe(credentials.sessionSecret);
232
+ expect(parsed['claude-flow/encryption-key']).toBe(credentials.encryptionKey);
233
+ });
234
+ });
235
+
236
+ describe('Factory Functions', () => {
237
+ it('should create generator with factory function', () => {
238
+ const gen = createCredentialGenerator();
239
+ expect(gen).toBeInstanceOf(CredentialGenerator);
240
+ });
241
+
242
+ it('should generate credentials with quick function', () => {
243
+ const credentials = generateCredentials();
244
+ expect(credentials.adminPassword).toBeDefined();
245
+ expect(credentials.jwtSecret).toBeDefined();
246
+ });
247
+ });
248
+
249
+ describe('CVE-3 Security Verification', () => {
250
+ it('should NOT produce hardcoded admin123 password', () => {
251
+ for (let i = 0; i < 100; i++) {
252
+ const credentials = generator.generateInstallationCredentials();
253
+ expect(credentials.adminPassword).not.toBe('admin123');
254
+ expect(credentials.servicePassword).not.toBe('service123');
255
+ }
256
+ });
257
+
258
+ it('should NOT produce predictable patterns', () => {
259
+ const passwords = new Set<string>();
260
+ for (let i = 0; i < 1000; i++) {
261
+ passwords.add(generator.generatePassword());
262
+ }
263
+
264
+ // All should be unique
265
+ expect(passwords.size).toBe(1000);
266
+
267
+ // None should match common patterns
268
+ const commonPatterns = ['password', 'admin', 'service', '123456', 'qwerty'];
269
+ for (const password of passwords) {
270
+ for (const pattern of commonPatterns) {
271
+ expect(password.toLowerCase()).not.toContain(pattern);
272
+ }
273
+ }
274
+ });
275
+
276
+ it('should use cryptographically secure random', () => {
277
+ // Generate many passwords and verify entropy
278
+ const passwords: string[] = [];
279
+ for (let i = 0; i < 100; i++) {
280
+ passwords.push(generator.generatePassword());
281
+ }
282
+
283
+ // Calculate average character distribution
284
+ const charCounts = new Map<string, number>();
285
+ for (const password of passwords) {
286
+ for (const char of password) {
287
+ charCounts.set(char, (charCounts.get(char) || 0) + 1);
288
+ }
289
+ }
290
+
291
+ // Should have good distribution (many different characters)
292
+ expect(charCounts.size).toBeGreaterThan(30);
293
+ });
294
+
295
+ it('should generate installation credentials with sufficient entropy', () => {
296
+ const credentials = generator.generateInstallationCredentials();
297
+
298
+ // Password should be at least 256 bits of entropy at minimum
299
+ expect(credentials.adminPassword.length).toBeGreaterThanOrEqual(32);
300
+ expect(credentials.servicePassword.length).toBeGreaterThanOrEqual(32);
301
+
302
+ // Secrets should be 512 bits
303
+ expect(credentials.jwtSecret.length).toBeGreaterThanOrEqual(64);
304
+ expect(credentials.sessionSecret.length).toBeGreaterThanOrEqual(64);
305
+
306
+ // Encryption key should be exactly 256 bits (64 hex chars)
307
+ expect(credentials.encryptionKey.length).toBe(64);
308
+ });
309
+ });
310
+ });