@claude-flow/security 3.0.0-alpha.7 → 3.0.0-alpha.8

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 (37) hide show
  1. package/README.md +292 -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 -425
  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 -382
  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/package.json +27 -27
  21. package/ruvector.db +0 -0
  22. package/src/CVE-REMEDIATION.ts +251 -251
  23. package/src/application/index.ts +10 -10
  24. package/src/application/services/security-application-service.ts +193 -193
  25. package/src/credential-generator.ts +368 -368
  26. package/src/domain/entities/security-context.ts +173 -173
  27. package/src/domain/index.ts +17 -17
  28. package/src/domain/services/security-domain-service.ts +296 -296
  29. package/src/index.ts +271 -271
  30. package/src/input-validator.ts +466 -466
  31. package/src/password-hasher.ts +277 -277
  32. package/src/path-validator.ts +525 -525
  33. package/src/safe-executor.ts +546 -546
  34. package/src/token-generator.ts +463 -463
  35. package/tsconfig.json +9 -9
  36. package/tsconfig.tsbuildinfo +1 -1
  37. package/vitest.config.ts +20 -20
@@ -1,193 +1,193 @@
1
- /**
2
- * Security Application Service - Application Layer
3
- *
4
- * Orchestrates security operations and provides simplified interface.
5
- *
6
- * @module v3/security/application/services
7
- */
8
-
9
- import { SecurityContext, PermissionLevel } from '../../domain/entities/security-context.js';
10
- import { SecurityDomainService, ValidationResult, ThreatDetectionResult } from '../../domain/services/security-domain-service.js';
11
-
12
- /**
13
- * Security audit result
14
- */
15
- export interface SecurityAuditResult {
16
- passed: boolean;
17
- score: number;
18
- checks: Array<{
19
- name: string;
20
- passed: boolean;
21
- severity: 'low' | 'medium' | 'high' | 'critical';
22
- message: string;
23
- }>;
24
- recommendations: string[];
25
- }
26
-
27
- /**
28
- * Security Application Service
29
- */
30
- export class SecurityApplicationService {
31
- private readonly domainService: SecurityDomainService;
32
- private readonly contexts: Map<string, SecurityContext> = new Map();
33
-
34
- constructor() {
35
- this.domainService = new SecurityDomainService();
36
- }
37
-
38
- // ============================================================================
39
- // Context Management
40
- // ============================================================================
41
-
42
- /**
43
- * Create and register security context for agent
44
- */
45
- createAgentContext(agentId: string, role: string): SecurityContext {
46
- const context = this.domainService.createAgentContext(agentId, role);
47
- this.contexts.set(agentId, context);
48
- return context;
49
- }
50
-
51
- /**
52
- * Get security context
53
- */
54
- getContext(principalId: string): SecurityContext | undefined {
55
- return this.contexts.get(principalId);
56
- }
57
-
58
- /**
59
- * Remove security context
60
- */
61
- removeContext(principalId: string): boolean {
62
- return this.contexts.delete(principalId);
63
- }
64
-
65
- // ============================================================================
66
- // Validation
67
- // ============================================================================
68
-
69
- /**
70
- * Validate path access
71
- */
72
- validatePath(path: string, principalId: string): ValidationResult {
73
- const context = this.contexts.get(principalId);
74
- if (!context) {
75
- return {
76
- valid: false,
77
- errors: ['Security context not found'],
78
- warnings: [],
79
- };
80
- }
81
-
82
- return this.domainService.validatePath(path, context);
83
- }
84
-
85
- /**
86
- * Validate command execution
87
- */
88
- validateCommand(command: string, principalId: string): ValidationResult {
89
- const context = this.contexts.get(principalId);
90
- if (!context) {
91
- return {
92
- valid: false,
93
- errors: ['Security context not found'],
94
- warnings: [],
95
- };
96
- }
97
-
98
- return this.domainService.validateCommand(command, context);
99
- }
100
-
101
- /**
102
- * Validate user input
103
- */
104
- validateInput(input: string): ValidationResult {
105
- return this.domainService.validateInput(input);
106
- }
107
-
108
- /**
109
- * Detect threats in content
110
- */
111
- detectThreats(content: string): ThreatDetectionResult {
112
- return this.domainService.detectThreats(content);
113
- }
114
-
115
- // ============================================================================
116
- // Audit
117
- // ============================================================================
118
-
119
- /**
120
- * Run security audit on codebase
121
- */
122
- async auditCodebase(files: Array<{ path: string; content: string }>): Promise<SecurityAuditResult> {
123
- const checks: SecurityAuditResult['checks'] = [];
124
- const recommendations: string[] = [];
125
- let criticalCount = 0;
126
- let highCount = 0;
127
-
128
- for (const file of files) {
129
- const threats = this.domainService.detectThreats(file.content);
130
-
131
- for (const threat of threats.threats) {
132
- checks.push({
133
- name: `${threat.type} in ${file.path}`,
134
- passed: false,
135
- severity: threat.severity,
136
- message: threat.description,
137
- });
138
-
139
- if (threat.severity === 'critical') criticalCount++;
140
- if (threat.severity === 'high') highCount++;
141
- }
142
-
143
- if (threats.safe) {
144
- checks.push({
145
- name: `Security check: ${file.path}`,
146
- passed: true,
147
- severity: 'low',
148
- message: 'No threats detected',
149
- });
150
- }
151
- }
152
-
153
- // Generate recommendations
154
- if (criticalCount > 0) {
155
- recommendations.push('Address critical security issues immediately');
156
- }
157
- if (highCount > 0) {
158
- recommendations.push('Review and fix high-severity findings');
159
- }
160
- recommendations.push('Run regular security scans');
161
- recommendations.push('Keep dependencies updated');
162
-
163
- // Calculate score
164
- const totalChecks = checks.length;
165
- const passedChecks = checks.filter((c) => c.passed).length;
166
- const score = totalChecks > 0 ? Math.round((passedChecks / totalChecks) * 100) : 100;
167
-
168
- return {
169
- passed: criticalCount === 0 && highCount === 0,
170
- score,
171
- checks,
172
- recommendations,
173
- };
174
- }
175
-
176
- /**
177
- * Check if operation is allowed
178
- */
179
- isOperationAllowed(
180
- principalId: string,
181
- operation: 'path' | 'command',
182
- target: string
183
- ): boolean {
184
- const context = this.contexts.get(principalId);
185
- if (!context || context.isExpired()) return false;
186
-
187
- if (operation === 'path') {
188
- return context.canAccessPath(target);
189
- } else {
190
- return context.canExecuteCommand(target);
191
- }
192
- }
193
- }
1
+ /**
2
+ * Security Application Service - Application Layer
3
+ *
4
+ * Orchestrates security operations and provides simplified interface.
5
+ *
6
+ * @module v3/security/application/services
7
+ */
8
+
9
+ import { SecurityContext, PermissionLevel } from '../../domain/entities/security-context.js';
10
+ import { SecurityDomainService, ValidationResult, ThreatDetectionResult } from '../../domain/services/security-domain-service.js';
11
+
12
+ /**
13
+ * Security audit result
14
+ */
15
+ export interface SecurityAuditResult {
16
+ passed: boolean;
17
+ score: number;
18
+ checks: Array<{
19
+ name: string;
20
+ passed: boolean;
21
+ severity: 'low' | 'medium' | 'high' | 'critical';
22
+ message: string;
23
+ }>;
24
+ recommendations: string[];
25
+ }
26
+
27
+ /**
28
+ * Security Application Service
29
+ */
30
+ export class SecurityApplicationService {
31
+ private readonly domainService: SecurityDomainService;
32
+ private readonly contexts: Map<string, SecurityContext> = new Map();
33
+
34
+ constructor() {
35
+ this.domainService = new SecurityDomainService();
36
+ }
37
+
38
+ // ============================================================================
39
+ // Context Management
40
+ // ============================================================================
41
+
42
+ /**
43
+ * Create and register security context for agent
44
+ */
45
+ createAgentContext(agentId: string, role: string): SecurityContext {
46
+ const context = this.domainService.createAgentContext(agentId, role);
47
+ this.contexts.set(agentId, context);
48
+ return context;
49
+ }
50
+
51
+ /**
52
+ * Get security context
53
+ */
54
+ getContext(principalId: string): SecurityContext | undefined {
55
+ return this.contexts.get(principalId);
56
+ }
57
+
58
+ /**
59
+ * Remove security context
60
+ */
61
+ removeContext(principalId: string): boolean {
62
+ return this.contexts.delete(principalId);
63
+ }
64
+
65
+ // ============================================================================
66
+ // Validation
67
+ // ============================================================================
68
+
69
+ /**
70
+ * Validate path access
71
+ */
72
+ validatePath(path: string, principalId: string): ValidationResult {
73
+ const context = this.contexts.get(principalId);
74
+ if (!context) {
75
+ return {
76
+ valid: false,
77
+ errors: ['Security context not found'],
78
+ warnings: [],
79
+ };
80
+ }
81
+
82
+ return this.domainService.validatePath(path, context);
83
+ }
84
+
85
+ /**
86
+ * Validate command execution
87
+ */
88
+ validateCommand(command: string, principalId: string): ValidationResult {
89
+ const context = this.contexts.get(principalId);
90
+ if (!context) {
91
+ return {
92
+ valid: false,
93
+ errors: ['Security context not found'],
94
+ warnings: [],
95
+ };
96
+ }
97
+
98
+ return this.domainService.validateCommand(command, context);
99
+ }
100
+
101
+ /**
102
+ * Validate user input
103
+ */
104
+ validateInput(input: string): ValidationResult {
105
+ return this.domainService.validateInput(input);
106
+ }
107
+
108
+ /**
109
+ * Detect threats in content
110
+ */
111
+ detectThreats(content: string): ThreatDetectionResult {
112
+ return this.domainService.detectThreats(content);
113
+ }
114
+
115
+ // ============================================================================
116
+ // Audit
117
+ // ============================================================================
118
+
119
+ /**
120
+ * Run security audit on codebase
121
+ */
122
+ async auditCodebase(files: Array<{ path: string; content: string }>): Promise<SecurityAuditResult> {
123
+ const checks: SecurityAuditResult['checks'] = [];
124
+ const recommendations: string[] = [];
125
+ let criticalCount = 0;
126
+ let highCount = 0;
127
+
128
+ for (const file of files) {
129
+ const threats = this.domainService.detectThreats(file.content);
130
+
131
+ for (const threat of threats.threats) {
132
+ checks.push({
133
+ name: `${threat.type} in ${file.path}`,
134
+ passed: false,
135
+ severity: threat.severity,
136
+ message: threat.description,
137
+ });
138
+
139
+ if (threat.severity === 'critical') criticalCount++;
140
+ if (threat.severity === 'high') highCount++;
141
+ }
142
+
143
+ if (threats.safe) {
144
+ checks.push({
145
+ name: `Security check: ${file.path}`,
146
+ passed: true,
147
+ severity: 'low',
148
+ message: 'No threats detected',
149
+ });
150
+ }
151
+ }
152
+
153
+ // Generate recommendations
154
+ if (criticalCount > 0) {
155
+ recommendations.push('Address critical security issues immediately');
156
+ }
157
+ if (highCount > 0) {
158
+ recommendations.push('Review and fix high-severity findings');
159
+ }
160
+ recommendations.push('Run regular security scans');
161
+ recommendations.push('Keep dependencies updated');
162
+
163
+ // Calculate score
164
+ const totalChecks = checks.length;
165
+ const passedChecks = checks.filter((c) => c.passed).length;
166
+ const score = totalChecks > 0 ? Math.round((passedChecks / totalChecks) * 100) : 100;
167
+
168
+ return {
169
+ passed: criticalCount === 0 && highCount === 0,
170
+ score,
171
+ checks,
172
+ recommendations,
173
+ };
174
+ }
175
+
176
+ /**
177
+ * Check if operation is allowed
178
+ */
179
+ isOperationAllowed(
180
+ principalId: string,
181
+ operation: 'path' | 'command',
182
+ target: string
183
+ ): boolean {
184
+ const context = this.contexts.get(principalId);
185
+ if (!context || context.isExpired()) return false;
186
+
187
+ if (operation === 'path') {
188
+ return context.canAccessPath(target);
189
+ } else {
190
+ return context.canExecuteCommand(target);
191
+ }
192
+ }
193
+ }