@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
package/src/index.ts CHANGED
@@ -1,271 +1,271 @@
1
- /**
2
- * V3 Security Module
3
- *
4
- * Comprehensive security module addressing all identified vulnerabilities:
5
- * - CVE-2: Weak Password Hashing (password-hasher.ts)
6
- * - CVE-3: Hardcoded Default Credentials (credential-generator.ts)
7
- * - HIGH-1: Command Injection (safe-executor.ts)
8
- * - HIGH-2: Path Traversal (path-validator.ts)
9
- *
10
- * Also provides:
11
- * - Input validation with Zod schemas
12
- * - Secure token generation
13
- *
14
- * @module v3/security
15
- */
16
-
17
- // Password Hashing (CVE-2 Fix)
18
- export {
19
- PasswordHasher,
20
- PasswordHashError,
21
- createPasswordHasher,
22
- type PasswordHasherConfig,
23
- type PasswordValidationResult,
24
- } from './password-hasher.js';
25
-
26
- // Credential Generation (CVE-3 Fix)
27
- export {
28
- CredentialGenerator,
29
- CredentialGeneratorError,
30
- createCredentialGenerator,
31
- generateCredentials,
32
- type CredentialConfig,
33
- type GeneratedCredentials,
34
- type ApiKeyCredential,
35
- } from './credential-generator.js';
36
-
37
- // Safe Command Execution (HIGH-1 Fix)
38
- export {
39
- SafeExecutor,
40
- SafeExecutorError,
41
- createDevelopmentExecutor,
42
- createReadOnlyExecutor,
43
- type ExecutorConfig,
44
- type ExecutionResult,
45
- type StreamingExecutor,
46
- } from './safe-executor.js';
47
-
48
- // Path Validation (HIGH-2 Fix)
49
- export {
50
- PathValidator,
51
- PathValidatorError,
52
- createProjectPathValidator,
53
- createFullProjectPathValidator,
54
- type PathValidatorConfig,
55
- type PathValidationResult,
56
- } from './path-validator.js';
57
-
58
- // Input Validation
59
- export {
60
- InputValidator,
61
- sanitizeString,
62
- sanitizeHtml,
63
- sanitizePath,
64
- // Base schemas
65
- SafeStringSchema,
66
- IdentifierSchema,
67
- FilenameSchema,
68
- EmailSchema,
69
- PasswordSchema,
70
- UUIDSchema,
71
- HttpsUrlSchema,
72
- UrlSchema,
73
- SemverSchema,
74
- PortSchema,
75
- IPv4Schema,
76
- IPSchema,
77
- // Auth schemas
78
- UserRoleSchema,
79
- PermissionSchema,
80
- LoginRequestSchema,
81
- CreateUserSchema,
82
- CreateApiKeySchema,
83
- // Agent & Task schemas
84
- AgentTypeSchema,
85
- SpawnAgentSchema,
86
- TaskInputSchema,
87
- // Command & Path schemas
88
- CommandArgumentSchema,
89
- PathSchema,
90
- // Config schemas
91
- SecurityConfigSchema,
92
- ExecutorConfigSchema,
93
- // Utilities
94
- PATTERNS,
95
- LIMITS,
96
- z,
97
- } from './input-validator.js';
98
-
99
- // Token Generation
100
- export {
101
- TokenGenerator,
102
- TokenGeneratorError,
103
- createTokenGenerator,
104
- getDefaultGenerator,
105
- quickGenerate,
106
- type TokenConfig,
107
- type Token,
108
- type SignedToken,
109
- type VerificationCode,
110
- } from './token-generator.js';
111
-
112
- // ============================================================================
113
- // Convenience Factory Functions
114
- // ============================================================================
115
-
116
- import { PasswordHasher } from './password-hasher.js';
117
- import { CredentialGenerator } from './credential-generator.js';
118
- import { SafeExecutor } from './safe-executor.js';
119
- import { PathValidator } from './path-validator.js';
120
- import { TokenGenerator } from './token-generator.js';
121
-
122
- /**
123
- * Security module configuration
124
- */
125
- export interface SecurityModuleConfig {
126
- /**
127
- * Project root directory for path validation
128
- */
129
- projectRoot: string;
130
-
131
- /**
132
- * HMAC secret for token signing
133
- */
134
- hmacSecret: string;
135
-
136
- /**
137
- * Bcrypt rounds for password hashing
138
- * Default: 12
139
- */
140
- bcryptRounds?: number;
141
-
142
- /**
143
- * Allowed commands for safe executor
144
- * Default: ['git', 'npm', 'npx', 'node']
145
- */
146
- allowedCommands?: string[];
147
- }
148
-
149
- /**
150
- * Complete security module instance
151
- */
152
- export interface SecurityModule {
153
- passwordHasher: PasswordHasher;
154
- credentialGenerator: CredentialGenerator;
155
- safeExecutor: SafeExecutor;
156
- pathValidator: PathValidator;
157
- tokenGenerator: TokenGenerator;
158
- }
159
-
160
- /**
161
- * Creates a complete security module with all components configured.
162
- *
163
- * @param config - Module configuration
164
- * @returns Complete security module
165
- *
166
- * @example
167
- * ```typescript
168
- * const security = createSecurityModule({
169
- * projectRoot: '/workspaces/project',
170
- * hmacSecret: process.env.HMAC_SECRET!,
171
- * });
172
- *
173
- * // Hash password
174
- * const hash = await security.passwordHasher.hash('password');
175
- *
176
- * // Validate path
177
- * const result = await security.pathValidator.validate('/workspaces/project/src/file.ts');
178
- *
179
- * // Execute command safely
180
- * const output = await security.safeExecutor.execute('git', ['status']);
181
- * ```
182
- */
183
- export function createSecurityModule(config: SecurityModuleConfig): SecurityModule {
184
- return {
185
- passwordHasher: new PasswordHasher({
186
- rounds: config.bcryptRounds ?? 12,
187
- }),
188
- credentialGenerator: new CredentialGenerator(),
189
- safeExecutor: new SafeExecutor({
190
- allowedCommands: config.allowedCommands ?? ['git', 'npm', 'npx', 'node'],
191
- }),
192
- pathValidator: new PathValidator({
193
- allowedPrefixes: [config.projectRoot],
194
- allowHidden: true,
195
- }),
196
- tokenGenerator: new TokenGenerator({
197
- hmacSecret: config.hmacSecret,
198
- }),
199
- };
200
- }
201
-
202
- // ============================================================================
203
- // Security Constants
204
- // ============================================================================
205
-
206
- /**
207
- * Minimum recommended bcrypt rounds for production
208
- */
209
- export const MIN_BCRYPT_ROUNDS = 12;
210
-
211
- /**
212
- * Maximum recommended bcrypt rounds (performance consideration)
213
- */
214
- export const MAX_BCRYPT_ROUNDS = 14;
215
-
216
- /**
217
- * Minimum password length
218
- */
219
- export const MIN_PASSWORD_LENGTH = 8;
220
-
221
- /**
222
- * Maximum password length (bcrypt limitation)
223
- */
224
- export const MAX_PASSWORD_LENGTH = 72;
225
-
226
- /**
227
- * Default token expiration in seconds (1 hour)
228
- */
229
- export const DEFAULT_TOKEN_EXPIRATION = 3600;
230
-
231
- /**
232
- * Default session expiration in seconds (24 hours)
233
- */
234
- export const DEFAULT_SESSION_EXPIRATION = 86400;
235
-
236
- // ============================================================================
237
- // Security Audit Helper
238
- // ============================================================================
239
-
240
- /**
241
- * Checks security configuration for common issues.
242
- *
243
- * @param config - Configuration to audit
244
- * @returns Array of security warnings
245
- */
246
- export function auditSecurityConfig(config: Partial<SecurityModuleConfig>): string[] {
247
- const warnings: string[] = [];
248
-
249
- if (config.bcryptRounds && config.bcryptRounds < MIN_BCRYPT_ROUNDS) {
250
- warnings.push(`bcryptRounds (${config.bcryptRounds}) below recommended minimum (${MIN_BCRYPT_ROUNDS})`);
251
- }
252
-
253
- if (config.hmacSecret && config.hmacSecret.length < 32) {
254
- warnings.push('hmacSecret should be at least 32 characters');
255
- }
256
-
257
- if (!config.projectRoot) {
258
- warnings.push('projectRoot not configured - path validation may be disabled');
259
- }
260
-
261
- if (config.allowedCommands && config.allowedCommands.length === 0) {
262
- warnings.push('No commands allowed - executor will reject all commands');
263
- }
264
-
265
- return warnings;
266
- }
267
-
268
- /**
269
- * Security module version
270
- */
271
- export const SECURITY_MODULE_VERSION = '3.0.0-alpha.1';
1
+ /**
2
+ * V3 Security Module
3
+ *
4
+ * Comprehensive security module addressing all identified vulnerabilities:
5
+ * - CVE-2: Weak Password Hashing (password-hasher.ts)
6
+ * - CVE-3: Hardcoded Default Credentials (credential-generator.ts)
7
+ * - HIGH-1: Command Injection (safe-executor.ts)
8
+ * - HIGH-2: Path Traversal (path-validator.ts)
9
+ *
10
+ * Also provides:
11
+ * - Input validation with Zod schemas
12
+ * - Secure token generation
13
+ *
14
+ * @module v3/security
15
+ */
16
+
17
+ // Password Hashing (CVE-2 Fix)
18
+ export {
19
+ PasswordHasher,
20
+ PasswordHashError,
21
+ createPasswordHasher,
22
+ type PasswordHasherConfig,
23
+ type PasswordValidationResult,
24
+ } from './password-hasher.js';
25
+
26
+ // Credential Generation (CVE-3 Fix)
27
+ export {
28
+ CredentialGenerator,
29
+ CredentialGeneratorError,
30
+ createCredentialGenerator,
31
+ generateCredentials,
32
+ type CredentialConfig,
33
+ type GeneratedCredentials,
34
+ type ApiKeyCredential,
35
+ } from './credential-generator.js';
36
+
37
+ // Safe Command Execution (HIGH-1 Fix)
38
+ export {
39
+ SafeExecutor,
40
+ SafeExecutorError,
41
+ createDevelopmentExecutor,
42
+ createReadOnlyExecutor,
43
+ type ExecutorConfig,
44
+ type ExecutionResult,
45
+ type StreamingExecutor,
46
+ } from './safe-executor.js';
47
+
48
+ // Path Validation (HIGH-2 Fix)
49
+ export {
50
+ PathValidator,
51
+ PathValidatorError,
52
+ createProjectPathValidator,
53
+ createFullProjectPathValidator,
54
+ type PathValidatorConfig,
55
+ type PathValidationResult,
56
+ } from './path-validator.js';
57
+
58
+ // Input Validation
59
+ export {
60
+ InputValidator,
61
+ sanitizeString,
62
+ sanitizeHtml,
63
+ sanitizePath,
64
+ // Base schemas
65
+ SafeStringSchema,
66
+ IdentifierSchema,
67
+ FilenameSchema,
68
+ EmailSchema,
69
+ PasswordSchema,
70
+ UUIDSchema,
71
+ HttpsUrlSchema,
72
+ UrlSchema,
73
+ SemverSchema,
74
+ PortSchema,
75
+ IPv4Schema,
76
+ IPSchema,
77
+ // Auth schemas
78
+ UserRoleSchema,
79
+ PermissionSchema,
80
+ LoginRequestSchema,
81
+ CreateUserSchema,
82
+ CreateApiKeySchema,
83
+ // Agent & Task schemas
84
+ AgentTypeSchema,
85
+ SpawnAgentSchema,
86
+ TaskInputSchema,
87
+ // Command & Path schemas
88
+ CommandArgumentSchema,
89
+ PathSchema,
90
+ // Config schemas
91
+ SecurityConfigSchema,
92
+ ExecutorConfigSchema,
93
+ // Utilities
94
+ PATTERNS,
95
+ LIMITS,
96
+ z,
97
+ } from './input-validator.js';
98
+
99
+ // Token Generation
100
+ export {
101
+ TokenGenerator,
102
+ TokenGeneratorError,
103
+ createTokenGenerator,
104
+ getDefaultGenerator,
105
+ quickGenerate,
106
+ type TokenConfig,
107
+ type Token,
108
+ type SignedToken,
109
+ type VerificationCode,
110
+ } from './token-generator.js';
111
+
112
+ // ============================================================================
113
+ // Convenience Factory Functions
114
+ // ============================================================================
115
+
116
+ import { PasswordHasher } from './password-hasher.js';
117
+ import { CredentialGenerator } from './credential-generator.js';
118
+ import { SafeExecutor } from './safe-executor.js';
119
+ import { PathValidator } from './path-validator.js';
120
+ import { TokenGenerator } from './token-generator.js';
121
+
122
+ /**
123
+ * Security module configuration
124
+ */
125
+ export interface SecurityModuleConfig {
126
+ /**
127
+ * Project root directory for path validation
128
+ */
129
+ projectRoot: string;
130
+
131
+ /**
132
+ * HMAC secret for token signing
133
+ */
134
+ hmacSecret: string;
135
+
136
+ /**
137
+ * Bcrypt rounds for password hashing
138
+ * Default: 12
139
+ */
140
+ bcryptRounds?: number;
141
+
142
+ /**
143
+ * Allowed commands for safe executor
144
+ * Default: ['git', 'npm', 'node']
145
+ */
146
+ allowedCommands?: string[];
147
+ }
148
+
149
+ /**
150
+ * Complete security module instance
151
+ */
152
+ export interface SecurityModule {
153
+ passwordHasher: PasswordHasher;
154
+ credentialGenerator: CredentialGenerator;
155
+ safeExecutor: SafeExecutor;
156
+ pathValidator: PathValidator;
157
+ tokenGenerator: TokenGenerator;
158
+ }
159
+
160
+ /**
161
+ * Creates a complete security module with all components configured.
162
+ *
163
+ * @param config - Module configuration
164
+ * @returns Complete security module
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * const security = createSecurityModule({
169
+ * projectRoot: '/workspaces/project',
170
+ * hmacSecret: process.env.HMAC_SECRET!,
171
+ * });
172
+ *
173
+ * // Hash password
174
+ * const hash = await security.passwordHasher.hash('password');
175
+ *
176
+ * // Validate path
177
+ * const result = await security.pathValidator.validate('/workspaces/project/src/file.ts');
178
+ *
179
+ * // Execute command safely
180
+ * const output = await security.safeExecutor.execute('git', ['status']);
181
+ * ```
182
+ */
183
+ export function createSecurityModule(config: SecurityModuleConfig): SecurityModule {
184
+ return {
185
+ passwordHasher: new PasswordHasher({
186
+ rounds: config.bcryptRounds ?? 12,
187
+ }),
188
+ credentialGenerator: new CredentialGenerator(),
189
+ safeExecutor: new SafeExecutor({
190
+ allowedCommands: config.allowedCommands ?? ['git', 'npm', 'node'],
191
+ }),
192
+ pathValidator: new PathValidator({
193
+ allowedPrefixes: [config.projectRoot],
194
+ allowHidden: true,
195
+ }),
196
+ tokenGenerator: new TokenGenerator({
197
+ hmacSecret: config.hmacSecret,
198
+ }),
199
+ };
200
+ }
201
+
202
+ // ============================================================================
203
+ // Security Constants
204
+ // ============================================================================
205
+
206
+ /**
207
+ * Minimum recommended bcrypt rounds for production
208
+ */
209
+ export const MIN_BCRYPT_ROUNDS = 12;
210
+
211
+ /**
212
+ * Maximum recommended bcrypt rounds (performance consideration)
213
+ */
214
+ export const MAX_BCRYPT_ROUNDS = 14;
215
+
216
+ /**
217
+ * Minimum password length
218
+ */
219
+ export const MIN_PASSWORD_LENGTH = 8;
220
+
221
+ /**
222
+ * Maximum password length (bcrypt limitation)
223
+ */
224
+ export const MAX_PASSWORD_LENGTH = 72;
225
+
226
+ /**
227
+ * Default token expiration in seconds (1 hour)
228
+ */
229
+ export const DEFAULT_TOKEN_EXPIRATION = 3600;
230
+
231
+ /**
232
+ * Default session expiration in seconds (24 hours)
233
+ */
234
+ export const DEFAULT_SESSION_EXPIRATION = 86400;
235
+
236
+ // ============================================================================
237
+ // Security Audit Helper
238
+ // ============================================================================
239
+
240
+ /**
241
+ * Checks security configuration for common issues.
242
+ *
243
+ * @param config - Configuration to audit
244
+ * @returns Array of security warnings
245
+ */
246
+ export function auditSecurityConfig(config: Partial<SecurityModuleConfig>): string[] {
247
+ const warnings: string[] = [];
248
+
249
+ if (config.bcryptRounds && config.bcryptRounds < MIN_BCRYPT_ROUNDS) {
250
+ warnings.push(`bcryptRounds (${config.bcryptRounds}) below recommended minimum (${MIN_BCRYPT_ROUNDS})`);
251
+ }
252
+
253
+ if (config.hmacSecret && config.hmacSecret.length < 32) {
254
+ warnings.push('hmacSecret should be at least 32 characters');
255
+ }
256
+
257
+ if (!config.projectRoot) {
258
+ warnings.push('projectRoot not configured - path validation may be disabled');
259
+ }
260
+
261
+ if (config.allowedCommands && config.allowedCommands.length === 0) {
262
+ warnings.push('No commands allowed - executor will reject all commands');
263
+ }
264
+
265
+ return warnings;
266
+ }
267
+
268
+ /**
269
+ * Security module version
270
+ */
271
+ export const SECURITY_MODULE_VERSION = '3.0.0-alpha.1';