@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,270 +1,277 @@
1
- /**
2
- * Password Hasher - CVE-2 Remediation
3
- *
4
- * Fixes weak password hashing by replacing SHA-256 with hardcoded salt
5
- * with bcrypt using 12 rounds (configurable).
6
- *
7
- * Security Properties:
8
- * - bcrypt with adaptive cost factor (12 rounds)
9
- * - Automatic salt generation per password
10
- * - Timing-safe comparison
11
- * - Minimum password length enforcement
12
- *
13
- * @module v3/security/password-hasher
14
- */
15
-
16
- import * as bcrypt from 'bcrypt';
17
-
18
- export interface PasswordHasherConfig {
19
- /**
20
- * Number of bcrypt rounds (cost factor).
21
- * Default: 12 (recommended minimum for production)
22
- * Each increment doubles the computation time.
23
- */
24
- rounds?: number;
25
-
26
- /**
27
- * Minimum password length.
28
- * Default: 8 characters
29
- */
30
- minLength?: number;
31
-
32
- /**
33
- * Maximum password length.
34
- * Default: 128 characters (bcrypt limit is 72 bytes)
35
- */
36
- maxLength?: number;
37
-
38
- /**
39
- * Require at least one uppercase letter.
40
- * Default: true
41
- */
42
- requireUppercase?: boolean;
43
-
44
- /**
45
- * Require at least one lowercase letter.
46
- * Default: true
47
- */
48
- requireLowercase?: boolean;
49
-
50
- /**
51
- * Require at least one digit.
52
- * Default: true
53
- */
54
- requireDigit?: boolean;
55
-
56
- /**
57
- * Require at least one special character.
58
- * Default: false
59
- */
60
- requireSpecial?: boolean;
61
- }
62
-
63
- export interface PasswordValidationResult {
64
- isValid: boolean;
65
- errors: string[];
66
- }
67
-
68
- export class PasswordHashError extends Error {
69
- constructor(
70
- message: string,
71
- public readonly code: string,
72
- ) {
73
- super(message);
74
- this.name = 'PasswordHashError';
75
- }
76
- }
77
-
78
- /**
79
- * Secure password hasher using bcrypt.
80
- *
81
- * This class replaces the vulnerable SHA-256 + hardcoded salt implementation
82
- * with industry-standard bcrypt hashing.
83
- *
84
- * @example
85
- * ```typescript
86
- * const hasher = new PasswordHasher({ rounds: 12 });
87
- * const hash = await hasher.hash('securePassword123');
88
- * const isValid = await hasher.verify('securePassword123', hash);
89
- * ```
90
- */
91
- export class PasswordHasher {
92
- private readonly config: Required<PasswordHasherConfig>;
93
-
94
- constructor(config: PasswordHasherConfig = {}) {
95
- this.config = {
96
- rounds: config.rounds ?? 12,
97
- minLength: config.minLength ?? 8,
98
- maxLength: config.maxLength ?? 128,
99
- requireUppercase: config.requireUppercase ?? true,
100
- requireLowercase: config.requireLowercase ?? true,
101
- requireDigit: config.requireDigit ?? true,
102
- requireSpecial: config.requireSpecial ?? false,
103
- };
104
-
105
- // Validate configuration
106
- if (this.config.rounds < 10 || this.config.rounds > 20) {
107
- throw new PasswordHashError(
108
- 'Bcrypt rounds must be between 10 and 20 for security and performance balance',
109
- 'INVALID_ROUNDS'
110
- );
111
- }
112
-
113
- if (this.config.minLength < 8) {
114
- throw new PasswordHashError(
115
- 'Minimum password length must be at least 8 characters',
116
- 'INVALID_MIN_LENGTH'
117
- );
118
- }
119
- }
120
-
121
- /**
122
- * Validates password against configured requirements.
123
- *
124
- * @param password - The password to validate
125
- * @returns Validation result with errors if any
126
- */
127
- validate(password: string): PasswordValidationResult {
128
- const errors: string[] = [];
129
-
130
- if (!password) {
131
- errors.push('Password is required');
132
- return { isValid: false, errors };
133
- }
134
-
135
- if (password.length < this.config.minLength) {
136
- errors.push(`Password must be at least ${this.config.minLength} characters`);
137
- }
138
-
139
- if (password.length > this.config.maxLength) {
140
- errors.push(`Password must not exceed ${this.config.maxLength} characters`);
141
- }
142
-
143
- if (this.config.requireUppercase && !/[A-Z]/.test(password)) {
144
- errors.push('Password must contain at least one uppercase letter');
145
- }
146
-
147
- if (this.config.requireLowercase && !/[a-z]/.test(password)) {
148
- errors.push('Password must contain at least one lowercase letter');
149
- }
150
-
151
- if (this.config.requireDigit && !/\d/.test(password)) {
152
- errors.push('Password must contain at least one digit');
153
- }
154
-
155
- if (this.config.requireSpecial && !/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) {
156
- errors.push('Password must contain at least one special character');
157
- }
158
-
159
- return {
160
- isValid: errors.length === 0,
161
- errors,
162
- };
163
- }
164
-
165
- /**
166
- * Hashes a password using bcrypt.
167
- *
168
- * @param password - The plaintext password to hash
169
- * @returns The bcrypt hash
170
- * @throws PasswordHashError if password is invalid
171
- */
172
- async hash(password: string): Promise<string> {
173
- const validation = this.validate(password);
174
-
175
- if (!validation.isValid) {
176
- throw new PasswordHashError(
177
- validation.errors.join('; '),
178
- 'VALIDATION_FAILED'
179
- );
180
- }
181
-
182
- try {
183
- // bcrypt automatically generates a random salt per hash
184
- return await bcrypt.hash(password, this.config.rounds);
185
- } catch (error) {
186
- throw new PasswordHashError(
187
- 'Failed to hash password',
188
- 'HASH_FAILED'
189
- );
190
- }
191
- }
192
-
193
- /**
194
- * Verifies a password against a bcrypt hash.
195
- * Uses timing-safe comparison internally.
196
- *
197
- * @param password - The plaintext password to verify
198
- * @param hash - The bcrypt hash to compare against
199
- * @returns True if password matches, false otherwise
200
- */
201
- async verify(password: string, hash: string): Promise<boolean> {
202
- if (!password || !hash) {
203
- return false;
204
- }
205
-
206
- // Validate hash format (bcrypt hashes start with $2a$, $2b$, or $2y$)
207
- if (!this.isValidBcryptHash(hash)) {
208
- return false;
209
- }
210
-
211
- try {
212
- // bcrypt.compare uses timing-safe comparison
213
- return await bcrypt.compare(password, hash);
214
- } catch (error) {
215
- // Return false on any error to prevent timing attacks
216
- return false;
217
- }
218
- }
219
-
220
- /**
221
- * Checks if a hash needs to be rehashed with updated parameters.
222
- * Useful for upgrading hash strength over time.
223
- *
224
- * @param hash - The bcrypt hash to check
225
- * @returns True if hash should be updated
226
- */
227
- needsRehash(hash: string): boolean {
228
- if (!this.isValidBcryptHash(hash)) {
229
- return true;
230
- }
231
-
232
- // Extract rounds from hash (format: $2b$XX$...)
233
- const match = hash.match(/^\$2[aby]\$(\d{2})\$/);
234
- if (!match) {
235
- return true;
236
- }
237
-
238
- const hashRounds = parseInt(match[1], 10);
239
- return hashRounds < this.config.rounds;
240
- }
241
-
242
- /**
243
- * Validates bcrypt hash format.
244
- *
245
- * @param hash - The hash to validate
246
- * @returns True if valid bcrypt hash format
247
- */
248
- private isValidBcryptHash(hash: string): boolean {
249
- // bcrypt hash format: $2a$XX$22charsSalt31charsHash
250
- // Total length: 60 characters
251
- return /^\$2[aby]\$\d{2}\$[./A-Za-z0-9]{53}$/.test(hash);
252
- }
253
-
254
- /**
255
- * Returns current configuration (without sensitive defaults).
256
- */
257
- getConfig(): Readonly<Omit<Required<PasswordHasherConfig>, never>> {
258
- return { ...this.config };
259
- }
260
- }
261
-
262
- /**
263
- * Factory function to create a production-ready password hasher.
264
- *
265
- * @param rounds - Bcrypt rounds (default: 12)
266
- * @returns Configured PasswordHasher instance
267
- */
268
- export function createPasswordHasher(rounds = 12): PasswordHasher {
269
- return new PasswordHasher({ rounds });
270
- }
1
+ /**
2
+ * Password Hasher - CVE-2 Remediation
3
+ *
4
+ * Fixes weak password hashing by replacing SHA-256 with hardcoded salt
5
+ * with bcrypt using 12 rounds (configurable).
6
+ *
7
+ * Security Properties:
8
+ * - bcrypt with adaptive cost factor (12 rounds)
9
+ * - Automatic salt generation per password
10
+ * - Timing-safe comparison
11
+ * - Minimum password length enforcement
12
+ *
13
+ * @module v3/security/password-hasher
14
+ */
15
+
16
+ // #1608 — switched from `bcrypt` to `bcryptjs` to drop the
17
+ // `@mapbox/node-pre-gyp → tar <=7.5.10` transitive chain that pulled in
18
+ // 6 HIGH-severity CVEs (GHSA-34x7-hfp2-rc4v, GHSA-8qq5-rm4j-mr97, etc.).
19
+ // `bcryptjs` is a pure-JS implementation that produces the same `$2a$` /
20
+ // `$2b$` hash format and exposes the same `hash()` / `compare()` API
21
+ // surface this module uses, so the swap is transparent to callers and to
22
+ // any persisted hashes.
23
+ import * as bcrypt from 'bcryptjs';
24
+
25
+ export interface PasswordHasherConfig {
26
+ /**
27
+ * Number of bcrypt rounds (cost factor).
28
+ * Default: 12 (recommended minimum for production)
29
+ * Each increment doubles the computation time.
30
+ */
31
+ rounds?: number;
32
+
33
+ /**
34
+ * Minimum password length.
35
+ * Default: 8 characters
36
+ */
37
+ minLength?: number;
38
+
39
+ /**
40
+ * Maximum password length.
41
+ * Default: 128 characters (bcrypt limit is 72 bytes)
42
+ */
43
+ maxLength?: number;
44
+
45
+ /**
46
+ * Require at least one uppercase letter.
47
+ * Default: true
48
+ */
49
+ requireUppercase?: boolean;
50
+
51
+ /**
52
+ * Require at least one lowercase letter.
53
+ * Default: true
54
+ */
55
+ requireLowercase?: boolean;
56
+
57
+ /**
58
+ * Require at least one digit.
59
+ * Default: true
60
+ */
61
+ requireDigit?: boolean;
62
+
63
+ /**
64
+ * Require at least one special character.
65
+ * Default: false
66
+ */
67
+ requireSpecial?: boolean;
68
+ }
69
+
70
+ export interface PasswordValidationResult {
71
+ isValid: boolean;
72
+ errors: string[];
73
+ }
74
+
75
+ export class PasswordHashError extends Error {
76
+ constructor(
77
+ message: string,
78
+ public readonly code: string,
79
+ ) {
80
+ super(message);
81
+ this.name = 'PasswordHashError';
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Secure password hasher using bcrypt.
87
+ *
88
+ * This class replaces the vulnerable SHA-256 + hardcoded salt implementation
89
+ * with industry-standard bcrypt hashing.
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * const hasher = new PasswordHasher({ rounds: 12 });
94
+ * const hash = await hasher.hash('securePassword123');
95
+ * const isValid = await hasher.verify('securePassword123', hash);
96
+ * ```
97
+ */
98
+ export class PasswordHasher {
99
+ private readonly config: Required<PasswordHasherConfig>;
100
+
101
+ constructor(config: PasswordHasherConfig = {}) {
102
+ this.config = {
103
+ rounds: config.rounds ?? 12,
104
+ minLength: config.minLength ?? 8,
105
+ maxLength: config.maxLength ?? 128,
106
+ requireUppercase: config.requireUppercase ?? true,
107
+ requireLowercase: config.requireLowercase ?? true,
108
+ requireDigit: config.requireDigit ?? true,
109
+ requireSpecial: config.requireSpecial ?? false,
110
+ };
111
+
112
+ // Validate configuration
113
+ if (this.config.rounds < 10 || this.config.rounds > 20) {
114
+ throw new PasswordHashError(
115
+ 'Bcrypt rounds must be between 10 and 20 for security and performance balance',
116
+ 'INVALID_ROUNDS'
117
+ );
118
+ }
119
+
120
+ if (this.config.minLength < 8) {
121
+ throw new PasswordHashError(
122
+ 'Minimum password length must be at least 8 characters',
123
+ 'INVALID_MIN_LENGTH'
124
+ );
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Validates password against configured requirements.
130
+ *
131
+ * @param password - The password to validate
132
+ * @returns Validation result with errors if any
133
+ */
134
+ validate(password: string): PasswordValidationResult {
135
+ const errors: string[] = [];
136
+
137
+ if (!password) {
138
+ errors.push('Password is required');
139
+ return { isValid: false, errors };
140
+ }
141
+
142
+ if (password.length < this.config.minLength) {
143
+ errors.push(`Password must be at least ${this.config.minLength} characters`);
144
+ }
145
+
146
+ if (password.length > this.config.maxLength) {
147
+ errors.push(`Password must not exceed ${this.config.maxLength} characters`);
148
+ }
149
+
150
+ if (this.config.requireUppercase && !/[A-Z]/.test(password)) {
151
+ errors.push('Password must contain at least one uppercase letter');
152
+ }
153
+
154
+ if (this.config.requireLowercase && !/[a-z]/.test(password)) {
155
+ errors.push('Password must contain at least one lowercase letter');
156
+ }
157
+
158
+ if (this.config.requireDigit && !/\d/.test(password)) {
159
+ errors.push('Password must contain at least one digit');
160
+ }
161
+
162
+ if (this.config.requireSpecial && !/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) {
163
+ errors.push('Password must contain at least one special character');
164
+ }
165
+
166
+ return {
167
+ isValid: errors.length === 0,
168
+ errors,
169
+ };
170
+ }
171
+
172
+ /**
173
+ * Hashes a password using bcrypt.
174
+ *
175
+ * @param password - The plaintext password to hash
176
+ * @returns The bcrypt hash
177
+ * @throws PasswordHashError if password is invalid
178
+ */
179
+ async hash(password: string): Promise<string> {
180
+ const validation = this.validate(password);
181
+
182
+ if (!validation.isValid) {
183
+ throw new PasswordHashError(
184
+ validation.errors.join('; '),
185
+ 'VALIDATION_FAILED'
186
+ );
187
+ }
188
+
189
+ try {
190
+ // bcrypt automatically generates a random salt per hash
191
+ return await bcrypt.hash(password, this.config.rounds);
192
+ } catch (error) {
193
+ throw new PasswordHashError(
194
+ 'Failed to hash password',
195
+ 'HASH_FAILED'
196
+ );
197
+ }
198
+ }
199
+
200
+ /**
201
+ * Verifies a password against a bcrypt hash.
202
+ * Uses timing-safe comparison internally.
203
+ *
204
+ * @param password - The plaintext password to verify
205
+ * @param hash - The bcrypt hash to compare against
206
+ * @returns True if password matches, false otherwise
207
+ */
208
+ async verify(password: string, hash: string): Promise<boolean> {
209
+ if (!password || !hash) {
210
+ return false;
211
+ }
212
+
213
+ // Validate hash format (bcrypt hashes start with $2a$, $2b$, or $2y$)
214
+ if (!this.isValidBcryptHash(hash)) {
215
+ return false;
216
+ }
217
+
218
+ try {
219
+ // bcrypt.compare uses timing-safe comparison
220
+ return await bcrypt.compare(password, hash);
221
+ } catch (error) {
222
+ // Return false on any error to prevent timing attacks
223
+ return false;
224
+ }
225
+ }
226
+
227
+ /**
228
+ * Checks if a hash needs to be rehashed with updated parameters.
229
+ * Useful for upgrading hash strength over time.
230
+ *
231
+ * @param hash - The bcrypt hash to check
232
+ * @returns True if hash should be updated
233
+ */
234
+ needsRehash(hash: string): boolean {
235
+ if (!this.isValidBcryptHash(hash)) {
236
+ return true;
237
+ }
238
+
239
+ // Extract rounds from hash (format: $2b$XX$...)
240
+ const match = hash.match(/^\$2[aby]\$(\d{2})\$/);
241
+ if (!match) {
242
+ return true;
243
+ }
244
+
245
+ const hashRounds = parseInt(match[1], 10);
246
+ return hashRounds < this.config.rounds;
247
+ }
248
+
249
+ /**
250
+ * Validates bcrypt hash format.
251
+ *
252
+ * @param hash - The hash to validate
253
+ * @returns True if valid bcrypt hash format
254
+ */
255
+ private isValidBcryptHash(hash: string): boolean {
256
+ // bcrypt hash format: $2a$XX$22charsSalt31charsHash
257
+ // Total length: 60 characters
258
+ return /^\$2[aby]\$\d{2}\$[./A-Za-z0-9]{53}$/.test(hash);
259
+ }
260
+
261
+ /**
262
+ * Returns current configuration (without sensitive defaults).
263
+ */
264
+ getConfig(): Readonly<Omit<Required<PasswordHasherConfig>, never>> {
265
+ return { ...this.config };
266
+ }
267
+ }
268
+
269
+ /**
270
+ * Factory function to create a production-ready password hasher.
271
+ *
272
+ * @param rounds - Bcrypt rounds (default: 12)
273
+ * @returns Configured PasswordHasher instance
274
+ */
275
+ export function createPasswordHasher(rounds = 12): PasswordHasher {
276
+ return new PasswordHasher({ rounds });
277
+ }