@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.
- package/README.md +234 -234
- package/__tests__/acceptance/security-compliance.test.ts +674 -674
- package/__tests__/credential-generator.test.ts +310 -310
- package/__tests__/fixtures/configurations.ts +425 -419
- package/__tests__/fixtures/index.ts +21 -21
- package/__tests__/helpers/create-mock.ts +469 -469
- package/__tests__/helpers/index.ts +32 -32
- package/__tests__/input-validator.test.ts +382 -381
- package/__tests__/integration/security-flow.test.ts +606 -606
- package/__tests__/password-hasher.test.ts +239 -239
- package/__tests__/path-validator.test.ts +302 -302
- package/__tests__/safe-executor.test.ts +292 -292
- package/__tests__/token-generator.test.ts +371 -371
- package/__tests__/unit/credential-generator.test.ts +182 -182
- package/__tests__/unit/password-hasher.test.ts +359 -359
- package/__tests__/unit/path-validator.test.ts +509 -509
- package/__tests__/unit/safe-executor.test.ts +667 -667
- package/__tests__/unit/token-generator.test.ts +310 -310
- package/dist/credential-generator.js +9 -9
- package/dist/domain/services/security-domain-service.js +1 -1
- package/dist/domain/services/security-domain-service.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/password-hasher.d.ts.map +1 -1
- package/dist/password-hasher.js +8 -1
- package/dist/password-hasher.js.map +1 -1
- package/dist/safe-executor.d.ts +7 -0
- package/dist/safe-executor.d.ts.map +1 -1
- package/dist/safe-executor.js +21 -1
- package/dist/safe-executor.js.map +1 -1
- package/package.json +27 -28
- package/src/CVE-REMEDIATION.ts +251 -251
- package/src/application/index.ts +10 -10
- package/src/application/services/security-application-service.ts +193 -193
- package/src/credential-generator.ts +368 -368
- package/src/domain/entities/security-context.ts +173 -173
- package/src/domain/index.ts +17 -17
- package/src/domain/services/security-domain-service.ts +296 -296
- package/src/index.ts +271 -271
- package/src/input-validator.ts +466 -466
- package/src/password-hasher.ts +277 -270
- package/src/path-validator.ts +525 -525
- package/src/safe-executor.ts +546 -525
- package/src/token-generator.ts +463 -463
- package/tmp.json +0 -0
- package/tsconfig.json +9 -9
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +20 -0
- package/.agentic-flow/intelligence.json +0 -16
- package/__tests__/coverage/base.css +0 -224
- package/__tests__/coverage/block-navigation.js +0 -87
- package/__tests__/coverage/coverage-final.json +0 -11
- package/__tests__/coverage/favicon.png +0 -0
- package/__tests__/coverage/index.html +0 -161
- package/__tests__/coverage/lcov-report/base.css +0 -224
- package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
- package/__tests__/coverage/lcov-report/favicon.png +0 -0
- package/__tests__/coverage/lcov-report/index.html +0 -161
- package/__tests__/coverage/lcov-report/prettify.css +0 -1
- package/__tests__/coverage/lcov-report/prettify.js +0 -2
- package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/lcov-report/sorter.js +0 -210
- package/__tests__/coverage/lcov-report/src/CVE-REMEDIATION.ts.html +0 -838
- package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/application/services/security-application-service.ts.html +0 -664
- package/__tests__/coverage/lcov-report/src/credential-generator.ts.html +0 -1189
- package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/domain/entities/security-context.ts.html +0 -604
- package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/domain/services/security-domain-service.ts.html +0 -973
- package/__tests__/coverage/lcov-report/src/index.html +0 -206
- package/__tests__/coverage/lcov-report/src/input-validator.ts.html +0 -1483
- package/__tests__/coverage/lcov-report/src/password-hasher.ts.html +0 -895
- package/__tests__/coverage/lcov-report/src/path-validator.ts.html +0 -1660
- package/__tests__/coverage/lcov-report/src/safe-executor.ts.html +0 -1660
- package/__tests__/coverage/lcov-report/src/token-generator.ts.html +0 -1474
- package/__tests__/coverage/lcov.info +0 -3650
- package/__tests__/coverage/prettify.css +0 -1
- package/__tests__/coverage/prettify.js +0 -2
- package/__tests__/coverage/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/sorter.js +0 -210
- package/__tests__/coverage/src/CVE-REMEDIATION.ts.html +0 -838
- package/__tests__/coverage/src/application/services/index.html +0 -116
- package/__tests__/coverage/src/application/services/security-application-service.ts.html +0 -664
- package/__tests__/coverage/src/credential-generator.ts.html +0 -1189
- package/__tests__/coverage/src/domain/entities/index.html +0 -116
- package/__tests__/coverage/src/domain/entities/security-context.ts.html +0 -604
- package/__tests__/coverage/src/domain/services/index.html +0 -116
- package/__tests__/coverage/src/domain/services/security-domain-service.ts.html +0 -973
- package/__tests__/coverage/src/index.html +0 -206
- package/__tests__/coverage/src/input-validator.ts.html +0 -1483
- package/__tests__/coverage/src/password-hasher.ts.html +0 -895
- package/__tests__/coverage/src/path-validator.ts.html +0 -1660
- package/__tests__/coverage/src/safe-executor.ts.html +0 -1660
- package/__tests__/coverage/src/token-generator.ts.html +0 -1474
|
@@ -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
|
+
}
|