@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
package/README.md CHANGED
@@ -1,234 +1,292 @@
1
- # @claude-flow/security
2
-
3
- [![npm version](https://img.shields.io/npm/v/@claude-flow/security.svg)](https://www.npmjs.com/package/@claude-flow/security)
4
- [![npm downloads](https://img.shields.io/npm/dm/@claude-flow/security.svg)](https://www.npmjs.com/package/@claude-flow/security)
5
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
- [![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-blue.svg)](https://www.typescriptlang.org/)
7
- [![Security Audit](https://img.shields.io/badge/Security-Audited-green.svg)](https://github.com/ruvnet/claude-flow)
8
-
9
- > Comprehensive security module for Claude Flow V3 - CVE fixes, input validation, path security, and secure credential management.
10
-
11
- ## Features
12
-
13
- - **CVE Remediation** - Fixes for CVE-2 (Weak Password Hashing), CVE-3 (Hardcoded Credentials), HIGH-1 (Command Injection), HIGH-2 (Path Traversal)
14
- - **Password Hashing** - Secure bcrypt-based password hashing with configurable rounds (12+ recommended)
15
- - **Credential Generation** - Cryptographically secure credential and API key generation
16
- - **Safe Command Execution** - Allowlist-based command execution preventing injection attacks
17
- - **Path Validation** - Protection against path traversal and symlink attacks
18
- - **Input Validation** - Zod-based schema validation for all input types
19
- - **Token Generation** - Secure token creation with HMAC signing
20
-
21
- ## Installation
22
-
23
- ```bash
24
- npm install @claude-flow/security
25
- ```
26
-
27
- ## Quick Start
28
-
29
- ```typescript
30
- import { createSecurityModule } from '@claude-flow/security';
31
-
32
- // Create a complete security module
33
- const security = createSecurityModule({
34
- projectRoot: '/workspaces/project',
35
- hmacSecret: process.env.HMAC_SECRET!,
36
- bcryptRounds: 12,
37
- allowedCommands: ['git', 'npm', 'npx', 'node']
38
- });
39
-
40
- // Hash a password
41
- const hash = await security.passwordHasher.hash('userPassword123');
42
-
43
- // Validate a path
44
- const pathResult = await security.pathValidator.validate('/workspaces/project/src/file.ts');
45
-
46
- // Execute command safely
47
- const output = await security.safeExecutor.execute('git', ['status']);
48
-
49
- // Generate secure credentials
50
- const creds = await security.credentialGenerator.generate();
51
- ```
52
-
53
- ## API Reference
54
-
55
- ### Password Hashing (CVE-2 Fix)
56
-
57
- ```typescript
58
- import { PasswordHasher, createPasswordHasher } from '@claude-flow/security';
59
-
60
- const hasher = createPasswordHasher({ rounds: 12 });
61
-
62
- // Hash password
63
- const hash = await hasher.hash('password');
64
-
65
- // Verify password
66
- const isValid = await hasher.verify('password', hash);
67
-
68
- // Check if hash needs rehashing
69
- const needsRehash = hasher.needsRehash(hash);
70
- ```
71
-
72
- ### Credential Generation (CVE-3 Fix)
73
-
74
- ```typescript
75
- import { CredentialGenerator, generateCredentials } from '@claude-flow/security';
76
-
77
- const generator = new CredentialGenerator();
78
-
79
- // Generate API key
80
- const apiKey = await generator.generateApiKey({
81
- prefix: 'cf',
82
- length: 32
83
- });
84
-
85
- // Generate complete credentials
86
- const creds = generateCredentials({
87
- includeApiKey: true,
88
- includeSecret: true
89
- });
90
- ```
91
-
92
- ### Safe Command Execution (HIGH-1 Fix)
93
-
94
- ```typescript
95
- import { SafeExecutor, createDevelopmentExecutor } from '@claude-flow/security';
96
-
97
- const executor = createDevelopmentExecutor();
98
-
99
- // Execute allowed command
100
- const result = await executor.execute('git', ['status']);
101
-
102
- // With timeout
103
- const result2 = await executor.execute('npm', ['install'], {
104
- timeout: 60000,
105
- cwd: '/workspaces/project'
106
- });
107
- ```
108
-
109
- ### Path Validation (HIGH-2 Fix)
110
-
111
- ```typescript
112
- import { PathValidator, createProjectPathValidator } from '@claude-flow/security';
113
-
114
- const validator = createProjectPathValidator('/workspaces/project');
115
-
116
- // Validate path
117
- const result = await validator.validate('../../../etc/passwd');
118
- // { valid: false, reason: 'Path traversal detected' }
119
-
120
- // Safe path
121
- const result2 = await validator.validate('/workspaces/project/src/index.ts');
122
- // { valid: true, normalized: '/workspaces/project/src/index.ts' }
123
- ```
124
-
125
- ### Input Validation
126
-
127
- ```typescript
128
- import {
129
- InputValidator,
130
- SafeStringSchema,
131
- EmailSchema,
132
- PasswordSchema,
133
- SpawnAgentSchema
134
- } from '@claude-flow/security';
135
-
136
- // Validate email
137
- const email = EmailSchema.parse('user@example.com');
138
-
139
- // Validate password
140
- const password = PasswordSchema.parse('SecurePass123!');
141
-
142
- // Validate agent spawn request
143
- const agentRequest = SpawnAgentSchema.parse({
144
- type: 'coder',
145
- name: 'code-agent-1'
146
- });
147
-
148
- // Sanitize HTML
149
- import { sanitizeHtml } from '@claude-flow/security';
150
- const safe = sanitizeHtml('<script>alert("xss")</script>Hello');
151
- // 'Hello'
152
- ```
153
-
154
- ### Token Generation
155
-
156
- ```typescript
157
- import { TokenGenerator, quickGenerate } from '@claude-flow/security';
158
-
159
- const generator = new TokenGenerator({
160
- hmacSecret: process.env.HMAC_SECRET!
161
- });
162
-
163
- // Generate signed token
164
- const token = await generator.generate({
165
- type: 'session',
166
- expiresIn: 3600
167
- });
168
-
169
- // Verify token
170
- const verified = await generator.verify(token);
171
-
172
- // Quick generation
173
- const sessionToken = quickGenerate.sessionToken();
174
- const verificationCode = quickGenerate.verificationCode();
175
- ```
176
-
177
- ## Security Constants
178
-
179
- ```typescript
180
- import {
181
- MIN_BCRYPT_ROUNDS, // 12
182
- MAX_BCRYPT_ROUNDS, // 14
183
- MIN_PASSWORD_LENGTH, // 8
184
- MAX_PASSWORD_LENGTH, // 72 (bcrypt limit)
185
- DEFAULT_TOKEN_EXPIRATION, // 3600 (1 hour)
186
- DEFAULT_SESSION_EXPIRATION // 86400 (24 hours)
187
- } from '@claude-flow/security';
188
- ```
189
-
190
- ## Security Audit
191
-
192
- ```typescript
193
- import { auditSecurityConfig } from '@claude-flow/security';
194
-
195
- const warnings = auditSecurityConfig({
196
- bcryptRounds: 10,
197
- hmacSecret: 'short'
198
- });
199
-
200
- // ['bcryptRounds (10) below recommended minimum (12)',
201
- // 'hmacSecret should be at least 32 characters']
202
- ```
203
-
204
- ## Validation Schemas
205
-
206
- | Schema | Description |
207
- |--------|-------------|
208
- | `SafeStringSchema` | Basic safe string with length limits |
209
- | `IdentifierSchema` | Alphanumeric identifiers |
210
- | `FilenameSchema` | Safe filenames |
211
- | `EmailSchema` | Email addresses |
212
- | `PasswordSchema` | Secure passwords |
213
- | `UUIDSchema` | UUID v4 format |
214
- | `HttpsUrlSchema` | HTTPS URLs only |
215
- | `SemverSchema` | Semantic versions |
216
- | `PortSchema` | Valid port numbers |
217
- | `IPv4Schema` | IPv4 addresses |
218
- | `SpawnAgentSchema` | Agent spawn requests |
219
- | `TaskInputSchema` | Task definitions |
220
- | `SecurityConfigSchema` | Security configuration |
221
-
222
- ## Dependencies
223
-
224
- - `bcrypt` - Password hashing
225
- - `zod` - Schema validation
226
-
227
- ## Related Packages
228
-
229
- - [@claude-flow/shared](../shared) - Shared types and utilities
230
- - [@claude-flow/swarm](../swarm) - Swarm coordination (secure agent spawning)
231
-
232
- ## License
233
-
234
- MIT
1
+ # @claude-flow/security
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@claude-flow/security.svg)](https://www.npmjs.com/package/@claude-flow/security)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@claude-flow/security.svg)](https://www.npmjs.com/package/@claude-flow/security)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-blue.svg)](https://www.typescriptlang.org/)
7
+ [![Security Audit](https://img.shields.io/badge/Security-Audited-green.svg)](https://github.com/ruvnet/claude-flow)
8
+
9
+ > Comprehensive security module for Claude Flow V3 - CVE fixes, input validation, path security, and secure credential management.
10
+
11
+ ## Features
12
+
13
+ - **CVE Remediation** - Fixes for CVE-2 (Weak Password Hashing), CVE-3 (Hardcoded Credentials), HIGH-1 (Command Injection), HIGH-2 (Path Traversal)
14
+ - **Password Hashing** - Secure bcrypt-based password hashing with configurable rounds (12+ recommended)
15
+ - **Credential Generation** - Cryptographically secure credential and API key generation
16
+ - **Safe Command Execution** - Allowlist-based command execution preventing injection attacks
17
+ - **Path Validation** - Protection against path traversal and symlink attacks
18
+ - **Input Validation** - Zod-based schema validation for all input types
19
+ - **Token Generation** - Secure token creation with HMAC signing
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ npm install @claude-flow/security
25
+ ```
26
+
27
+ ## Standalone use (without the Ruflo CLI)
28
+
29
+ This is a plain library — every primitive (`PasswordHasher`,
30
+ `PathValidator`, `SafeExecutor`, `InputValidator`, `CredentialGenerator`)
31
+ is independently importable. No CLI, no MCP server, no daemon. The
32
+ primitives close CVE-2 (weak hashing), CVE-3 (default credentials),
33
+ HIGH-1 (command injection), and HIGH-2 (path traversal) — drop them
34
+ into any Node app that needs to validate at a system boundary.
35
+
36
+ ### Recipe — Validate input, hash a password, generate an API key
37
+
38
+ ```typescript
39
+ // recipe.mjs
40
+ import {
41
+ InputValidator,
42
+ EmailSchema,
43
+ PasswordHasher,
44
+ createCredentialGenerator,
45
+ } from '@claude-flow/security';
46
+
47
+ // 1. Validate user input at the boundary (Zod schemas, no surprises)
48
+ const email = InputValidator.validate(EmailSchema, 'user@example.com');
49
+ // ↳ throws if invalid; returns the typed value otherwise
50
+
51
+ // 2. Hash a password with bcrypt at the recommended cost (CVE-2)
52
+ // Default policy: ≥8 chars, ≥1 upper, ≥1 lower, ≥1 digit.
53
+ // Tune with new PasswordHasher({ rounds, requireUppercase, ... }).
54
+ const hasher = new PasswordHasher({ rounds: 12 });
55
+ const hash = await hasher.hash('CorrectHorseBatteryStaple9');
56
+ const ok = await hasher.verify('CorrectHorseBatteryStaple9', hash); // true
57
+
58
+ // 3. Generate a high-entropy API key (CVE-3) — uses crypto.randomBytes
59
+ // under the hood and refuses to emit keys below 32 bytes of entropy.
60
+ const creds = createCredentialGenerator();
61
+ const apiKey = creds.generateApiKey('ck_live_');
62
+ console.log(apiKey.keyId, apiKey.key.slice(0, 16) + '…');
63
+ // → 7e2b1a9c-… ck_live_xX9aQ…
64
+ ```
65
+
66
+ ### Recipe — Refuse command injection + path traversal
67
+
68
+ ```typescript
69
+ import {
70
+ createDevelopmentExecutor,
71
+ createProjectPathValidator,
72
+ } from '@claude-flow/security';
73
+
74
+ // SafeExecutor — only the allow-listed commands ever run (HIGH-1)
75
+ const exec = createDevelopmentExecutor({ projectRoot: process.cwd() });
76
+ const { stdout } = await exec.execute('git', ['status', '--porcelain']);
77
+
78
+ // PathValidator — refuses traversal and symlinks out of the project (HIGH-2)
79
+ const paths = createProjectPathValidator(process.cwd());
80
+ const result = await paths.validate('../../etc/passwd');
81
+ if (!result.isValid) console.log('blocked:', result.errors.join('; '));
82
+ // → blocked: Path traversal pattern detected
83
+ ```
84
+
85
+ ## Quick Start
86
+
87
+ ```typescript
88
+ import { createSecurityModule } from '@claude-flow/security';
89
+
90
+ // Create a complete security module
91
+ const security = createSecurityModule({
92
+ projectRoot: '/workspaces/project',
93
+ hmacSecret: process.env.HMAC_SECRET!,
94
+ bcryptRounds: 12,
95
+ allowedCommands: ['git', 'npm', 'node']
96
+ });
97
+
98
+ // Hash a password
99
+ const hash = await security.passwordHasher.hash('userPassword123');
100
+
101
+ // Validate a path
102
+ const pathResult = await security.pathValidator.validate('/workspaces/project/src/file.ts');
103
+
104
+ // Execute command safely
105
+ const output = await security.safeExecutor.execute('git', ['status']);
106
+
107
+ // Generate secure credentials
108
+ const creds = await security.credentialGenerator.generate();
109
+ ```
110
+
111
+ ## API Reference
112
+
113
+ ### Password Hashing (CVE-2 Fix)
114
+
115
+ ```typescript
116
+ import { PasswordHasher, createPasswordHasher } from '@claude-flow/security';
117
+
118
+ const hasher = createPasswordHasher({ rounds: 12 });
119
+
120
+ // Hash password
121
+ const hash = await hasher.hash('password');
122
+
123
+ // Verify password
124
+ const isValid = await hasher.verify('password', hash);
125
+
126
+ // Check if hash needs rehashing
127
+ const needsRehash = hasher.needsRehash(hash);
128
+ ```
129
+
130
+ ### Credential Generation (CVE-3 Fix)
131
+
132
+ ```typescript
133
+ import { CredentialGenerator, generateCredentials } from '@claude-flow/security';
134
+
135
+ const generator = new CredentialGenerator();
136
+
137
+ // Generate API key
138
+ const apiKey = await generator.generateApiKey({
139
+ prefix: 'cf',
140
+ length: 32
141
+ });
142
+
143
+ // Generate complete credentials
144
+ const creds = generateCredentials({
145
+ includeApiKey: true,
146
+ includeSecret: true
147
+ });
148
+ ```
149
+
150
+ ### Safe Command Execution (HIGH-1 Fix)
151
+
152
+ ```typescript
153
+ import { SafeExecutor, createDevelopmentExecutor } from '@claude-flow/security';
154
+
155
+ const executor = createDevelopmentExecutor();
156
+
157
+ // Execute allowed command
158
+ const result = await executor.execute('git', ['status']);
159
+
160
+ // With timeout
161
+ const result2 = await executor.execute('npm', ['install'], {
162
+ timeout: 60000,
163
+ cwd: '/workspaces/project'
164
+ });
165
+ ```
166
+
167
+ ### Path Validation (HIGH-2 Fix)
168
+
169
+ ```typescript
170
+ import { PathValidator, createProjectPathValidator } from '@claude-flow/security';
171
+
172
+ const validator = createProjectPathValidator('/workspaces/project');
173
+
174
+ // Validate path
175
+ const result = await validator.validate('../../../etc/passwd');
176
+ // { valid: false, reason: 'Path traversal detected' }
177
+
178
+ // Safe path
179
+ const result2 = await validator.validate('/workspaces/project/src/index.ts');
180
+ // { valid: true, normalized: '/workspaces/project/src/index.ts' }
181
+ ```
182
+
183
+ ### Input Validation
184
+
185
+ ```typescript
186
+ import {
187
+ InputValidator,
188
+ SafeStringSchema,
189
+ EmailSchema,
190
+ PasswordSchema,
191
+ SpawnAgentSchema
192
+ } from '@claude-flow/security';
193
+
194
+ // Validate email
195
+ const email = EmailSchema.parse('user@example.com');
196
+
197
+ // Validate password
198
+ const password = PasswordSchema.parse('SecurePass123!');
199
+
200
+ // Validate agent spawn request
201
+ const agentRequest = SpawnAgentSchema.parse({
202
+ type: 'coder',
203
+ name: 'code-agent-1'
204
+ });
205
+
206
+ // Sanitize HTML
207
+ import { sanitizeHtml } from '@claude-flow/security';
208
+ const safe = sanitizeHtml('<script>alert("xss")</script>Hello');
209
+ // 'Hello'
210
+ ```
211
+
212
+ ### Token Generation
213
+
214
+ ```typescript
215
+ import { TokenGenerator, quickGenerate } from '@claude-flow/security';
216
+
217
+ const generator = new TokenGenerator({
218
+ hmacSecret: process.env.HMAC_SECRET!
219
+ });
220
+
221
+ // Generate signed token
222
+ const token = await generator.generate({
223
+ type: 'session',
224
+ expiresIn: 3600
225
+ });
226
+
227
+ // Verify token
228
+ const verified = await generator.verify(token);
229
+
230
+ // Quick generation
231
+ const sessionToken = quickGenerate.sessionToken();
232
+ const verificationCode = quickGenerate.verificationCode();
233
+ ```
234
+
235
+ ## Security Constants
236
+
237
+ ```typescript
238
+ import {
239
+ MIN_BCRYPT_ROUNDS, // 12
240
+ MAX_BCRYPT_ROUNDS, // 14
241
+ MIN_PASSWORD_LENGTH, // 8
242
+ MAX_PASSWORD_LENGTH, // 72 (bcrypt limit)
243
+ DEFAULT_TOKEN_EXPIRATION, // 3600 (1 hour)
244
+ DEFAULT_SESSION_EXPIRATION // 86400 (24 hours)
245
+ } from '@claude-flow/security';
246
+ ```
247
+
248
+ ## Security Audit
249
+
250
+ ```typescript
251
+ import { auditSecurityConfig } from '@claude-flow/security';
252
+
253
+ const warnings = auditSecurityConfig({
254
+ bcryptRounds: 10,
255
+ hmacSecret: 'short'
256
+ });
257
+
258
+ // ['bcryptRounds (10) below recommended minimum (12)',
259
+ // 'hmacSecret should be at least 32 characters']
260
+ ```
261
+
262
+ ## Validation Schemas
263
+
264
+ | Schema | Description |
265
+ |--------|-------------|
266
+ | `SafeStringSchema` | Basic safe string with length limits |
267
+ | `IdentifierSchema` | Alphanumeric identifiers |
268
+ | `FilenameSchema` | Safe filenames |
269
+ | `EmailSchema` | Email addresses |
270
+ | `PasswordSchema` | Secure passwords |
271
+ | `UUIDSchema` | UUID v4 format |
272
+ | `HttpsUrlSchema` | HTTPS URLs only |
273
+ | `SemverSchema` | Semantic versions |
274
+ | `PortSchema` | Valid port numbers |
275
+ | `IPv4Schema` | IPv4 addresses |
276
+ | `SpawnAgentSchema` | Agent spawn requests |
277
+ | `TaskInputSchema` | Task definitions |
278
+ | `SecurityConfigSchema` | Security configuration |
279
+
280
+ ## Dependencies
281
+
282
+ - `bcrypt` - Password hashing
283
+ - `zod` - Schema validation
284
+
285
+ ## Related Packages
286
+
287
+ - [@claude-flow/shared](../shared) - Shared types and utilities
288
+ - [@claude-flow/swarm](../swarm) - Swarm coordination (secure agent spawning)
289
+
290
+ ## License
291
+
292
+ MIT