@claude-flow/security 3.0.0-alpha.1
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/.agentic-flow/intelligence.json +16 -0
- package/README.md +234 -0
- package/__tests__/acceptance/security-compliance.test.ts +674 -0
- package/__tests__/coverage/base.css +224 -0
- package/__tests__/coverage/block-navigation.js +87 -0
- package/__tests__/coverage/coverage-final.json +11 -0
- package/__tests__/coverage/favicon.png +0 -0
- package/__tests__/coverage/index.html +161 -0
- package/__tests__/coverage/lcov-report/base.css +224 -0
- package/__tests__/coverage/lcov-report/block-navigation.js +87 -0
- package/__tests__/coverage/lcov-report/favicon.png +0 -0
- package/__tests__/coverage/lcov-report/index.html +161 -0
- package/__tests__/coverage/lcov-report/prettify.css +1 -0
- package/__tests__/coverage/lcov-report/prettify.js +2 -0
- package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/lcov-report/sorter.js +210 -0
- package/__tests__/coverage/lcov-report/src/CVE-REMEDIATION.ts.html +838 -0
- package/__tests__/coverage/lcov-report/src/application/services/index.html +116 -0
- package/__tests__/coverage/lcov-report/src/application/services/security-application-service.ts.html +664 -0
- package/__tests__/coverage/lcov-report/src/credential-generator.ts.html +1189 -0
- package/__tests__/coverage/lcov-report/src/domain/entities/index.html +116 -0
- package/__tests__/coverage/lcov-report/src/domain/entities/security-context.ts.html +604 -0
- package/__tests__/coverage/lcov-report/src/domain/services/index.html +116 -0
- package/__tests__/coverage/lcov-report/src/domain/services/security-domain-service.ts.html +973 -0
- package/__tests__/coverage/lcov-report/src/index.html +206 -0
- package/__tests__/coverage/lcov-report/src/input-validator.ts.html +1483 -0
- package/__tests__/coverage/lcov-report/src/password-hasher.ts.html +895 -0
- package/__tests__/coverage/lcov-report/src/path-validator.ts.html +1660 -0
- package/__tests__/coverage/lcov-report/src/safe-executor.ts.html +1660 -0
- package/__tests__/coverage/lcov-report/src/token-generator.ts.html +1474 -0
- package/__tests__/coverage/lcov.info +3650 -0
- package/__tests__/coverage/prettify.css +1 -0
- package/__tests__/coverage/prettify.js +2 -0
- package/__tests__/coverage/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/sorter.js +210 -0
- package/__tests__/coverage/src/CVE-REMEDIATION.ts.html +838 -0
- package/__tests__/coverage/src/application/services/index.html +116 -0
- package/__tests__/coverage/src/application/services/security-application-service.ts.html +664 -0
- package/__tests__/coverage/src/credential-generator.ts.html +1189 -0
- package/__tests__/coverage/src/domain/entities/index.html +116 -0
- package/__tests__/coverage/src/domain/entities/security-context.ts.html +604 -0
- package/__tests__/coverage/src/domain/services/index.html +116 -0
- package/__tests__/coverage/src/domain/services/security-domain-service.ts.html +973 -0
- package/__tests__/coverage/src/index.html +206 -0
- package/__tests__/coverage/src/input-validator.ts.html +1483 -0
- package/__tests__/coverage/src/password-hasher.ts.html +895 -0
- package/__tests__/coverage/src/path-validator.ts.html +1660 -0
- package/__tests__/coverage/src/safe-executor.ts.html +1660 -0
- package/__tests__/coverage/src/token-generator.ts.html +1474 -0
- package/__tests__/credential-generator.test.ts +310 -0
- package/__tests__/fixtures/configurations.ts +419 -0
- package/__tests__/fixtures/index.ts +21 -0
- package/__tests__/helpers/create-mock.ts +469 -0
- package/__tests__/helpers/index.ts +32 -0
- package/__tests__/input-validator.test.ts +381 -0
- package/__tests__/integration/security-flow.test.ts +606 -0
- package/__tests__/password-hasher.test.ts +239 -0
- package/__tests__/path-validator.test.ts +302 -0
- package/__tests__/safe-executor.test.ts +292 -0
- package/__tests__/token-generator.test.ts +371 -0
- package/__tests__/unit/credential-generator.test.ts +182 -0
- package/__tests__/unit/password-hasher.test.ts +359 -0
- package/__tests__/unit/path-validator.test.ts +509 -0
- package/__tests__/unit/safe-executor.test.ts +667 -0
- package/__tests__/unit/token-generator.test.ts +310 -0
- package/dist/CVE-REMEDIATION.d.ts +86 -0
- package/dist/CVE-REMEDIATION.d.ts.map +1 -0
- package/dist/CVE-REMEDIATION.js +221 -0
- package/dist/CVE-REMEDIATION.js.map +1 -0
- package/dist/application/index.d.ts +7 -0
- package/dist/application/index.d.ts.map +1 -0
- package/dist/application/index.js +7 -0
- package/dist/application/index.js.map +1 -0
- package/dist/application/services/security-application-service.d.ts +71 -0
- package/dist/application/services/security-application-service.d.ts.map +1 -0
- package/dist/application/services/security-application-service.js +153 -0
- package/dist/application/services/security-application-service.js.map +1 -0
- package/dist/credential-generator.d.ts +176 -0
- package/dist/credential-generator.d.ts.map +1 -0
- package/dist/credential-generator.js +272 -0
- package/dist/credential-generator.js.map +1 -0
- package/dist/domain/entities/security-context.d.ts +68 -0
- package/dist/domain/entities/security-context.d.ts.map +1 -0
- package/dist/domain/entities/security-context.js +132 -0
- package/dist/domain/entities/security-context.js.map +1 -0
- package/dist/domain/index.d.ts +8 -0
- package/dist/domain/index.d.ts.map +1 -0
- package/dist/domain/index.js +8 -0
- package/dist/domain/index.js.map +1 -0
- package/dist/domain/services/security-domain-service.d.ts +71 -0
- package/dist/domain/services/security-domain-service.d.ts.map +1 -0
- package/dist/domain/services/security-domain-service.js +237 -0
- package/dist/domain/services/security-domain-service.js.map +1 -0
- package/dist/index.d.ts +119 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +145 -0
- package/dist/index.js.map +1 -0
- package/dist/input-validator.d.ts +306 -0
- package/dist/input-validator.d.ts.map +1 -0
- package/dist/input-validator.js +393 -0
- package/dist/input-validator.js.map +1 -0
- package/dist/password-hasher.d.ts +128 -0
- package/dist/password-hasher.d.ts.map +1 -0
- package/dist/password-hasher.js +183 -0
- package/dist/password-hasher.js.map +1 -0
- package/dist/path-validator.d.ts +148 -0
- package/dist/path-validator.d.ts.map +1 -0
- package/dist/path-validator.js +421 -0
- package/dist/path-validator.js.map +1 -0
- package/dist/safe-executor.d.ts +173 -0
- package/dist/safe-executor.d.ts.map +1 -0
- package/dist/safe-executor.js +370 -0
- package/dist/safe-executor.js.map +1 -0
- package/dist/token-generator.d.ts +224 -0
- package/dist/token-generator.d.ts.map +1 -0
- package/dist/token-generator.js +351 -0
- package/dist/token-generator.js.map +1 -0
- package/package.json +28 -0
- package/src/CVE-REMEDIATION.ts +251 -0
- package/src/application/index.ts +10 -0
- package/src/application/services/security-application-service.ts +193 -0
- package/src/credential-generator.ts +368 -0
- package/src/domain/entities/security-context.ts +173 -0
- package/src/domain/index.ts +17 -0
- package/src/domain/services/security-domain-service.ts +296 -0
- package/src/index.ts +271 -0
- package/src/input-validator.ts +466 -0
- package/src/password-hasher.ts +270 -0
- package/src/path-validator.ts +525 -0
- package/src/safe-executor.ts +525 -0
- package/src/token-generator.ts +463 -0
- package/tsconfig.json +9 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,71 @@
|
|
|
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
|
+
import { SecurityContext } from '../../domain/entities/security-context.js';
|
|
9
|
+
import { ValidationResult, ThreatDetectionResult } from '../../domain/services/security-domain-service.js';
|
|
10
|
+
/**
|
|
11
|
+
* Security audit result
|
|
12
|
+
*/
|
|
13
|
+
export interface SecurityAuditResult {
|
|
14
|
+
passed: boolean;
|
|
15
|
+
score: number;
|
|
16
|
+
checks: Array<{
|
|
17
|
+
name: string;
|
|
18
|
+
passed: boolean;
|
|
19
|
+
severity: 'low' | 'medium' | 'high' | 'critical';
|
|
20
|
+
message: string;
|
|
21
|
+
}>;
|
|
22
|
+
recommendations: string[];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Security Application Service
|
|
26
|
+
*/
|
|
27
|
+
export declare class SecurityApplicationService {
|
|
28
|
+
private readonly domainService;
|
|
29
|
+
private readonly contexts;
|
|
30
|
+
constructor();
|
|
31
|
+
/**
|
|
32
|
+
* Create and register security context for agent
|
|
33
|
+
*/
|
|
34
|
+
createAgentContext(agentId: string, role: string): SecurityContext;
|
|
35
|
+
/**
|
|
36
|
+
* Get security context
|
|
37
|
+
*/
|
|
38
|
+
getContext(principalId: string): SecurityContext | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Remove security context
|
|
41
|
+
*/
|
|
42
|
+
removeContext(principalId: string): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Validate path access
|
|
45
|
+
*/
|
|
46
|
+
validatePath(path: string, principalId: string): ValidationResult;
|
|
47
|
+
/**
|
|
48
|
+
* Validate command execution
|
|
49
|
+
*/
|
|
50
|
+
validateCommand(command: string, principalId: string): ValidationResult;
|
|
51
|
+
/**
|
|
52
|
+
* Validate user input
|
|
53
|
+
*/
|
|
54
|
+
validateInput(input: string): ValidationResult;
|
|
55
|
+
/**
|
|
56
|
+
* Detect threats in content
|
|
57
|
+
*/
|
|
58
|
+
detectThreats(content: string): ThreatDetectionResult;
|
|
59
|
+
/**
|
|
60
|
+
* Run security audit on codebase
|
|
61
|
+
*/
|
|
62
|
+
auditCodebase(files: Array<{
|
|
63
|
+
path: string;
|
|
64
|
+
content: string;
|
|
65
|
+
}>): Promise<SecurityAuditResult>;
|
|
66
|
+
/**
|
|
67
|
+
* Check if operation is allowed
|
|
68
|
+
*/
|
|
69
|
+
isOperationAllowed(principalId: string, operation: 'path' | 'command', target: string): boolean;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=security-application-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"security-application-service.d.ts","sourceRoot":"","sources":["../../../src/application/services/security-application-service.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,eAAe,EAAmB,MAAM,2CAA2C,CAAC;AAC7F,OAAO,EAAyB,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,kDAAkD,CAAC;AAElI;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IAC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
2
|
+
* Security Application Service - Application Layer
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates security operations and provides simplified interface.
|
|
5
|
+
*
|
|
6
|
+
* @module v3/security/application/services
|
|
7
|
+
*/
|
|
8
|
+
import { SecurityDomainService } from '../../domain/services/security-domain-service.js';
|
|
9
|
+
/**
|
|
10
|
+
* Security Application Service
|
|
11
|
+
*/
|
|
12
|
+
export class SecurityApplicationService {
|
|
13
|
+
domainService;
|
|
14
|
+
contexts = new Map();
|
|
15
|
+
constructor() {
|
|
16
|
+
this.domainService = new SecurityDomainService();
|
|
17
|
+
}
|
|
18
|
+
// ============================================================================
|
|
19
|
+
// Context Management
|
|
20
|
+
// ============================================================================
|
|
21
|
+
/**
|
|
22
|
+
* Create and register security context for agent
|
|
23
|
+
*/
|
|
24
|
+
createAgentContext(agentId, role) {
|
|
25
|
+
const context = this.domainService.createAgentContext(agentId, role);
|
|
26
|
+
this.contexts.set(agentId, context);
|
|
27
|
+
return context;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get security context
|
|
31
|
+
*/
|
|
32
|
+
getContext(principalId) {
|
|
33
|
+
return this.contexts.get(principalId);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Remove security context
|
|
37
|
+
*/
|
|
38
|
+
removeContext(principalId) {
|
|
39
|
+
return this.contexts.delete(principalId);
|
|
40
|
+
}
|
|
41
|
+
// ============================================================================
|
|
42
|
+
// Validation
|
|
43
|
+
// ============================================================================
|
|
44
|
+
/**
|
|
45
|
+
* Validate path access
|
|
46
|
+
*/
|
|
47
|
+
validatePath(path, principalId) {
|
|
48
|
+
const context = this.contexts.get(principalId);
|
|
49
|
+
if (!context) {
|
|
50
|
+
return {
|
|
51
|
+
valid: false,
|
|
52
|
+
errors: ['Security context not found'],
|
|
53
|
+
warnings: [],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return this.domainService.validatePath(path, context);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Validate command execution
|
|
60
|
+
*/
|
|
61
|
+
validateCommand(command, principalId) {
|
|
62
|
+
const context = this.contexts.get(principalId);
|
|
63
|
+
if (!context) {
|
|
64
|
+
return {
|
|
65
|
+
valid: false,
|
|
66
|
+
errors: ['Security context not found'],
|
|
67
|
+
warnings: [],
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return this.domainService.validateCommand(command, context);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Validate user input
|
|
74
|
+
*/
|
|
75
|
+
validateInput(input) {
|
|
76
|
+
return this.domainService.validateInput(input);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Detect threats in content
|
|
80
|
+
*/
|
|
81
|
+
detectThreats(content) {
|
|
82
|
+
return this.domainService.detectThreats(content);
|
|
83
|
+
}
|
|
84
|
+
// ============================================================================
|
|
85
|
+
// Audit
|
|
86
|
+
// ============================================================================
|
|
87
|
+
/**
|
|
88
|
+
* Run security audit on codebase
|
|
89
|
+
*/
|
|
90
|
+
async auditCodebase(files) {
|
|
91
|
+
const checks = [];
|
|
92
|
+
const recommendations = [];
|
|
93
|
+
let criticalCount = 0;
|
|
94
|
+
let highCount = 0;
|
|
95
|
+
for (const file of files) {
|
|
96
|
+
const threats = this.domainService.detectThreats(file.content);
|
|
97
|
+
for (const threat of threats.threats) {
|
|
98
|
+
checks.push({
|
|
99
|
+
name: `${threat.type} in ${file.path}`,
|
|
100
|
+
passed: false,
|
|
101
|
+
severity: threat.severity,
|
|
102
|
+
message: threat.description,
|
|
103
|
+
});
|
|
104
|
+
if (threat.severity === 'critical')
|
|
105
|
+
criticalCount++;
|
|
106
|
+
if (threat.severity === 'high')
|
|
107
|
+
highCount++;
|
|
108
|
+
}
|
|
109
|
+
if (threats.safe) {
|
|
110
|
+
checks.push({
|
|
111
|
+
name: `Security check: ${file.path}`,
|
|
112
|
+
passed: true,
|
|
113
|
+
severity: 'low',
|
|
114
|
+
message: 'No threats detected',
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// Generate recommendations
|
|
119
|
+
if (criticalCount > 0) {
|
|
120
|
+
recommendations.push('Address critical security issues immediately');
|
|
121
|
+
}
|
|
122
|
+
if (highCount > 0) {
|
|
123
|
+
recommendations.push('Review and fix high-severity findings');
|
|
124
|
+
}
|
|
125
|
+
recommendations.push('Run regular security scans');
|
|
126
|
+
recommendations.push('Keep dependencies updated');
|
|
127
|
+
// Calculate score
|
|
128
|
+
const totalChecks = checks.length;
|
|
129
|
+
const passedChecks = checks.filter((c) => c.passed).length;
|
|
130
|
+
const score = totalChecks > 0 ? Math.round((passedChecks / totalChecks) * 100) : 100;
|
|
131
|
+
return {
|
|
132
|
+
passed: criticalCount === 0 && highCount === 0,
|
|
133
|
+
score,
|
|
134
|
+
checks,
|
|
135
|
+
recommendations,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Check if operation is allowed
|
|
140
|
+
*/
|
|
141
|
+
isOperationAllowed(principalId, operation, target) {
|
|
142
|
+
const context = this.contexts.get(principalId);
|
|
143
|
+
if (!context || context.isExpired())
|
|
144
|
+
return false;
|
|
145
|
+
if (operation === 'path') {
|
|
146
|
+
return context.canAccessPath(target);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
return context.canExecuteCommand(target);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=security-application-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"security-application-service.js","sourceRoot":"","sources":["../../../src/application/services/security-application-service.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,qBAAqB,EAA2C,MAAM,kDAAkD,CAAC;AAiBlI;;GAEG;AACH,MAAM,OAAO,0BAA0B;IACpB,aAAa,CAAwB;IACrC,QAAQ,GAAiC,IAAI,GAAG,EAAE,CAAC;IAEpE;QACE,IAAI,CAAC,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,IAAI,SAAS,KAAK,MAAM,EAAE,CAAC;YACzB,OAAO,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;QACvC,CAAC;aAAM,CAAC;YACN,OAAO,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;QAC3C,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credential Generator - CVE-3 Remediation
|
|
3
|
+
*
|
|
4
|
+
* Fixes hardcoded default credentials by providing secure random
|
|
5
|
+
* credential generation for installation and runtime.
|
|
6
|
+
*
|
|
7
|
+
* Security Properties:
|
|
8
|
+
* - Uses crypto.randomBytes for cryptographically secure randomness
|
|
9
|
+
* - Configurable entropy levels
|
|
10
|
+
* - No hardcoded defaults stored in code
|
|
11
|
+
* - Secure credential storage recommendations
|
|
12
|
+
*
|
|
13
|
+
* @module v3/security/credential-generator
|
|
14
|
+
*/
|
|
15
|
+
export interface CredentialConfig {
|
|
16
|
+
/**
|
|
17
|
+
* Length of generated passwords.
|
|
18
|
+
* Default: 32 characters
|
|
19
|
+
*/
|
|
20
|
+
passwordLength?: number;
|
|
21
|
+
/**
|
|
22
|
+
* Length of generated API keys.
|
|
23
|
+
* Default: 48 characters
|
|
24
|
+
*/
|
|
25
|
+
apiKeyLength?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Length of generated secrets (JWT, session, etc.).
|
|
28
|
+
* Default: 64 characters
|
|
29
|
+
*/
|
|
30
|
+
secretLength?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Character set for password generation.
|
|
33
|
+
* Default: alphanumeric + special
|
|
34
|
+
*/
|
|
35
|
+
passwordCharset?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Character set for API key generation.
|
|
38
|
+
* Default: alphanumeric only (URL-safe)
|
|
39
|
+
*/
|
|
40
|
+
apiKeyCharset?: string;
|
|
41
|
+
}
|
|
42
|
+
export interface GeneratedCredentials {
|
|
43
|
+
adminPassword: string;
|
|
44
|
+
servicePassword: string;
|
|
45
|
+
jwtSecret: string;
|
|
46
|
+
sessionSecret: string;
|
|
47
|
+
encryptionKey: string;
|
|
48
|
+
generatedAt: Date;
|
|
49
|
+
expiresAt?: Date;
|
|
50
|
+
}
|
|
51
|
+
export interface ApiKeyCredential {
|
|
52
|
+
key: string;
|
|
53
|
+
prefix: string;
|
|
54
|
+
keyId: string;
|
|
55
|
+
createdAt: Date;
|
|
56
|
+
}
|
|
57
|
+
export declare class CredentialGeneratorError extends Error {
|
|
58
|
+
readonly code: string;
|
|
59
|
+
constructor(message: string, code: string);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Secure credential generator.
|
|
63
|
+
*
|
|
64
|
+
* This class provides cryptographically secure credential generation
|
|
65
|
+
* to replace hardcoded default credentials.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const generator = new CredentialGenerator();
|
|
70
|
+
* const credentials = generator.generateInstallationCredentials();
|
|
71
|
+
* // Store credentials securely (environment variables, secrets manager)
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare class CredentialGenerator {
|
|
75
|
+
private readonly config;
|
|
76
|
+
constructor(config?: CredentialConfig);
|
|
77
|
+
/**
|
|
78
|
+
* Validates configuration parameters.
|
|
79
|
+
*/
|
|
80
|
+
private validateConfig;
|
|
81
|
+
/**
|
|
82
|
+
* Generates a cryptographically secure random string using rejection sampling
|
|
83
|
+
* to eliminate modulo bias.
|
|
84
|
+
*
|
|
85
|
+
* @param length - Length of the string to generate
|
|
86
|
+
* @param charset - Character set to use
|
|
87
|
+
* @returns Random string
|
|
88
|
+
*/
|
|
89
|
+
private generateSecureString;
|
|
90
|
+
/**
|
|
91
|
+
* Generates a secure random password.
|
|
92
|
+
*
|
|
93
|
+
* @param length - Optional custom length (default from config)
|
|
94
|
+
* @returns Secure random password
|
|
95
|
+
*/
|
|
96
|
+
generatePassword(length?: number): string;
|
|
97
|
+
/**
|
|
98
|
+
* Checks if password has required character types.
|
|
99
|
+
*/
|
|
100
|
+
private hasRequiredCharacterTypes;
|
|
101
|
+
/**
|
|
102
|
+
* Generates a secure API key.
|
|
103
|
+
*
|
|
104
|
+
* @param prefix - Optional prefix for the key (e.g., 'cf_')
|
|
105
|
+
* @returns API key credential with metadata
|
|
106
|
+
*/
|
|
107
|
+
generateApiKey(prefix?: string): ApiKeyCredential;
|
|
108
|
+
/**
|
|
109
|
+
* Generates a secure secret for JWT, sessions, etc.
|
|
110
|
+
*
|
|
111
|
+
* @param length - Optional custom length (default from config)
|
|
112
|
+
* @returns Hex-encoded secret
|
|
113
|
+
*/
|
|
114
|
+
generateSecret(length?: number): string;
|
|
115
|
+
/**
|
|
116
|
+
* Generates an encryption key suitable for AES-256.
|
|
117
|
+
*
|
|
118
|
+
* @returns 32-byte key encoded as hex (64 characters)
|
|
119
|
+
*/
|
|
120
|
+
generateEncryptionKey(): string;
|
|
121
|
+
/**
|
|
122
|
+
* Generates a complete set of installation credentials.
|
|
123
|
+
*
|
|
124
|
+
* These should be stored securely (environment variables,
|
|
125
|
+
* secrets manager, etc.) and NEVER committed to version control.
|
|
126
|
+
*
|
|
127
|
+
* @param expirationDays - Optional expiration period in days
|
|
128
|
+
* @returns Complete credential set
|
|
129
|
+
*/
|
|
130
|
+
generateInstallationCredentials(expirationDays?: number): GeneratedCredentials;
|
|
131
|
+
/**
|
|
132
|
+
* Generates a secure session token.
|
|
133
|
+
*
|
|
134
|
+
* @returns URL-safe session token
|
|
135
|
+
*/
|
|
136
|
+
generateSessionToken(): string;
|
|
137
|
+
/**
|
|
138
|
+
* Generates a secure CSRF token.
|
|
139
|
+
*
|
|
140
|
+
* @returns CSRF token
|
|
141
|
+
*/
|
|
142
|
+
generateCsrfToken(): string;
|
|
143
|
+
/**
|
|
144
|
+
* Generates a secure nonce for one-time use.
|
|
145
|
+
*
|
|
146
|
+
* @returns Unique nonce value
|
|
147
|
+
*/
|
|
148
|
+
generateNonce(): string;
|
|
149
|
+
/**
|
|
150
|
+
* Creates a setup script output for secure credential deployment.
|
|
151
|
+
*
|
|
152
|
+
* @param credentials - Generated credentials
|
|
153
|
+
* @returns Environment variable export script
|
|
154
|
+
*/
|
|
155
|
+
createEnvScript(credentials: GeneratedCredentials): string;
|
|
156
|
+
/**
|
|
157
|
+
* Creates a JSON configuration output for secure credential deployment.
|
|
158
|
+
*
|
|
159
|
+
* @param credentials - Generated credentials
|
|
160
|
+
* @returns JSON configuration (for secrets manager import)
|
|
161
|
+
*/
|
|
162
|
+
createJsonConfig(credentials: GeneratedCredentials): string;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Factory function to create a production credential generator.
|
|
166
|
+
*
|
|
167
|
+
* @returns Configured CredentialGenerator instance
|
|
168
|
+
*/
|
|
169
|
+
export declare function createCredentialGenerator(): CredentialGenerator;
|
|
170
|
+
/**
|
|
171
|
+
* Quick credential generation for CLI usage.
|
|
172
|
+
*
|
|
173
|
+
* @returns Generated installation credentials
|
|
174
|
+
*/
|
|
175
|
+
export declare function generateCredentials(): GeneratedCredentials;
|
|
176
|
+
//# sourceMappingURL=credential-generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-generator.d.ts","sourceRoot":"","sources":["../src/credential-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,oBAAoB;IACnC,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,MAAM,CAAC;IACxB,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AE,oBAAoB,GAAG,MAAM;CAW5D;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,IAAI,mBAAmB,CAE/D;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAI,oBAAoB,CAG1D"}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credential Generator - CVE-3 Remediation
|
|
3
|
+
*
|
|
4
|
+
* Fixes hardcoded default credentials by providing secure random
|
|
5
|
+
* credential generation for installation and runtime.
|
|
6
|
+
*
|
|
7
|
+
* Security Properties:
|
|
8
|
+
* - Uses crypto.randomBytes for cryptographically secure randomness
|
|
9
|
+
* - Configurable entropy levels
|
|
10
|
+
* - No hardcoded defaults stored in code
|
|
11
|
+
* - Secure credential storage recommendations
|
|
12
|
+
*
|
|
13
|
+
* @module v3/security/credential-generator
|
|
14
|
+
*/
|
|
15
|
+
import { randomBytes, randomUUID } from 'crypto';
|
|
16
|
+
export class CredentialGeneratorError extends Error {
|
|
17
|
+
code;
|
|
18
|
+
constructor(message, code) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.code = code;
|
|
21
|
+
this.name = 'CredentialGeneratorError';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Character sets for credential generation
|
|
26
|
+
*/
|
|
27
|
+
const CHARSETS = {
|
|
28
|
+
UPPERCASE: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
29
|
+
LOWERCASE: 'abcdefghijklmnopqrstuvwxyz',
|
|
30
|
+
DIGITS: '0123456789',
|
|
31
|
+
SPECIAL: '!@#$%^&*()_+-=[]{}|;:,.<>?',
|
|
32
|
+
// URL-safe characters for API keys
|
|
33
|
+
URL_SAFE: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
|
|
34
|
+
// Hex characters for secrets
|
|
35
|
+
HEX: '0123456789abcdef',
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Secure credential generator.
|
|
39
|
+
*
|
|
40
|
+
* This class provides cryptographically secure credential generation
|
|
41
|
+
* to replace hardcoded default credentials.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const generator = new CredentialGenerator();
|
|
46
|
+
* const credentials = generator.generateInstallationCredentials();
|
|
47
|
+
* // Store credentials securely (environment variables, secrets manager)
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export class CredentialGenerator {
|
|
51
|
+
config;
|
|
52
|
+
constructor(config = {}) {
|
|
53
|
+
this.config = {
|
|
54
|
+
passwordLength: config.passwordLength ?? 32,
|
|
55
|
+
apiKeyLength: config.apiKeyLength ?? 48,
|
|
56
|
+
secretLength: config.secretLength ?? 64,
|
|
57
|
+
passwordCharset: config.passwordCharset ??
|
|
58
|
+
CHARSETS.UPPERCASE + CHARSETS.LOWERCASE + CHARSETS.DIGITS + CHARSETS.SPECIAL,
|
|
59
|
+
apiKeyCharset: config.apiKeyCharset ?? CHARSETS.URL_SAFE,
|
|
60
|
+
};
|
|
61
|
+
this.validateConfig();
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Validates configuration parameters.
|
|
65
|
+
*/
|
|
66
|
+
validateConfig() {
|
|
67
|
+
if (this.config.passwordLength < 16) {
|
|
68
|
+
throw new CredentialGeneratorError('Password length must be at least 16 characters', 'INVALID_PASSWORD_LENGTH');
|
|
69
|
+
}
|
|
70
|
+
if (this.config.apiKeyLength < 32) {
|
|
71
|
+
throw new CredentialGeneratorError('API key length must be at least 32 characters', 'INVALID_API_KEY_LENGTH');
|
|
72
|
+
}
|
|
73
|
+
if (this.config.secretLength < 32) {
|
|
74
|
+
throw new CredentialGeneratorError('Secret length must be at least 32 characters', 'INVALID_SECRET_LENGTH');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Generates a cryptographically secure random string using rejection sampling
|
|
79
|
+
* to eliminate modulo bias.
|
|
80
|
+
*
|
|
81
|
+
* @param length - Length of the string to generate
|
|
82
|
+
* @param charset - Character set to use
|
|
83
|
+
* @returns Random string
|
|
84
|
+
*/
|
|
85
|
+
generateSecureString(length, charset) {
|
|
86
|
+
const charsetLength = charset.length;
|
|
87
|
+
const result = new Array(length);
|
|
88
|
+
// Calculate rejection threshold to eliminate modulo bias
|
|
89
|
+
// For a byte (0-255), we reject values >= (256 - (256 % charsetLength))
|
|
90
|
+
// This ensures uniform distribution over charset indices
|
|
91
|
+
const maxValidValue = 256 - (256 % charsetLength);
|
|
92
|
+
let i = 0;
|
|
93
|
+
while (i < length) {
|
|
94
|
+
// Generate more random bytes than needed to reduce iterations
|
|
95
|
+
const randomBuffer = randomBytes(Math.max(length - i, 16));
|
|
96
|
+
for (let j = 0; j < randomBuffer.length && i < length; j++) {
|
|
97
|
+
const randomValue = randomBuffer[j];
|
|
98
|
+
// Rejection sampling: only accept values below threshold
|
|
99
|
+
if (randomValue < maxValidValue) {
|
|
100
|
+
result[i] = charset[randomValue % charsetLength];
|
|
101
|
+
i++;
|
|
102
|
+
}
|
|
103
|
+
// Values >= maxValidValue are rejected to avoid bias
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return result.join('');
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Generates a secure random password.
|
|
110
|
+
*
|
|
111
|
+
* @param length - Optional custom length (default from config)
|
|
112
|
+
* @returns Secure random password
|
|
113
|
+
*/
|
|
114
|
+
generatePassword(length) {
|
|
115
|
+
const len = length ?? this.config.passwordLength;
|
|
116
|
+
// Ensure password contains at least one of each required character type
|
|
117
|
+
const password = this.generateSecureString(len, this.config.passwordCharset);
|
|
118
|
+
// Validate the generated password meets requirements
|
|
119
|
+
if (!this.hasRequiredCharacterTypes(password)) {
|
|
120
|
+
// Regenerate if requirements not met (rare case)
|
|
121
|
+
return this.generatePassword(length);
|
|
122
|
+
}
|
|
123
|
+
return password;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Checks if password has required character types.
|
|
127
|
+
*/
|
|
128
|
+
hasRequiredCharacterTypes(password) {
|
|
129
|
+
const hasUppercase = /[A-Z]/.test(password);
|
|
130
|
+
const hasLowercase = /[a-z]/.test(password);
|
|
131
|
+
const hasDigit = /\d/.test(password);
|
|
132
|
+
const hasSpecial = /[!@#$%^&*()_+\-=\[\]{}|;:,.<>?]/.test(password);
|
|
133
|
+
return hasUppercase && hasLowercase && hasDigit && hasSpecial;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Generates a secure API key.
|
|
137
|
+
*
|
|
138
|
+
* @param prefix - Optional prefix for the key (e.g., 'cf_')
|
|
139
|
+
* @returns API key credential with metadata
|
|
140
|
+
*/
|
|
141
|
+
generateApiKey(prefix = 'cf_') {
|
|
142
|
+
const keyBody = this.generateSecureString(this.config.apiKeyLength - prefix.length, this.config.apiKeyCharset);
|
|
143
|
+
const key = `${prefix}${keyBody}`;
|
|
144
|
+
const keyId = randomUUID();
|
|
145
|
+
return {
|
|
146
|
+
key,
|
|
147
|
+
prefix,
|
|
148
|
+
keyId,
|
|
149
|
+
createdAt: new Date(),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Generates a secure secret for JWT, sessions, etc.
|
|
154
|
+
*
|
|
155
|
+
* @param length - Optional custom length (default from config)
|
|
156
|
+
* @returns Hex-encoded secret
|
|
157
|
+
*/
|
|
158
|
+
generateSecret(length) {
|
|
159
|
+
const len = length ?? this.config.secretLength;
|
|
160
|
+
// Generate raw bytes and encode as hex for consistent storage
|
|
161
|
+
return randomBytes(Math.ceil(len / 2)).toString('hex').slice(0, len);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Generates an encryption key suitable for AES-256.
|
|
165
|
+
*
|
|
166
|
+
* @returns 32-byte key encoded as hex (64 characters)
|
|
167
|
+
*/
|
|
168
|
+
generateEncryptionKey() {
|
|
169
|
+
return randomBytes(32).toString('hex');
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Generates a complete set of installation credentials.
|
|
173
|
+
*
|
|
174
|
+
* These should be stored securely (environment variables,
|
|
175
|
+
* secrets manager, etc.) and NEVER committed to version control.
|
|
176
|
+
*
|
|
177
|
+
* @param expirationDays - Optional expiration period in days
|
|
178
|
+
* @returns Complete credential set
|
|
179
|
+
*/
|
|
180
|
+
generateInstallationCredentials(expirationDays) {
|
|
181
|
+
const now = new Date();
|
|
182
|
+
const expiresAt = expirationDays
|
|
183
|
+
? new Date(now.getTime() + expirationDays * 24 * 60 * 60 * 1000)
|
|
184
|
+
: undefined;
|
|
185
|
+
return {
|
|
186
|
+
adminPassword: this.generatePassword(),
|
|
187
|
+
servicePassword: this.generatePassword(),
|
|
188
|
+
jwtSecret: this.generateSecret(64),
|
|
189
|
+
sessionSecret: this.generateSecret(64),
|
|
190
|
+
encryptionKey: this.generateEncryptionKey(),
|
|
191
|
+
generatedAt: now,
|
|
192
|
+
expiresAt,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Generates a secure session token.
|
|
197
|
+
*
|
|
198
|
+
* @returns URL-safe session token
|
|
199
|
+
*/
|
|
200
|
+
generateSessionToken() {
|
|
201
|
+
return this.generateSecureString(64, CHARSETS.URL_SAFE);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Generates a secure CSRF token.
|
|
205
|
+
*
|
|
206
|
+
* @returns CSRF token
|
|
207
|
+
*/
|
|
208
|
+
generateCsrfToken() {
|
|
209
|
+
return randomBytes(32).toString('base64url');
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Generates a secure nonce for one-time use.
|
|
213
|
+
*
|
|
214
|
+
* @returns Unique nonce value
|
|
215
|
+
*/
|
|
216
|
+
generateNonce() {
|
|
217
|
+
return randomBytes(16).toString('hex');
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Creates a setup script output for secure credential deployment.
|
|
221
|
+
*
|
|
222
|
+
* @param credentials - Generated credentials
|
|
223
|
+
* @returns Environment variable export script
|
|
224
|
+
*/
|
|
225
|
+
createEnvScript(credentials) {
|
|
226
|
+
return `# Claude Flow V3 - Generated Credentials
|
|
227
|
+
# Generated: ${credentials.generatedAt.toISOString()}
|
|
228
|
+
# IMPORTANT: Store these securely and delete this file after use
|
|
229
|
+
|
|
230
|
+
export CLAUDE_FLOW_ADMIN_PASSWORD="${credentials.adminPassword}"
|
|
231
|
+
export CLAUDE_FLOW_SERVICE_PASSWORD="${credentials.servicePassword}"
|
|
232
|
+
export CLAUDE_FLOW_JWT_SECRET="${credentials.jwtSecret}"
|
|
233
|
+
export CLAUDE_FLOW_SESSION_SECRET="${credentials.sessionSecret}"
|
|
234
|
+
export CLAUDE_FLOW_ENCRYPTION_KEY="${credentials.encryptionKey}"
|
|
235
|
+
`;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Creates a JSON configuration output for secure credential deployment.
|
|
239
|
+
*
|
|
240
|
+
* @param credentials - Generated credentials
|
|
241
|
+
* @returns JSON configuration (for secrets manager import)
|
|
242
|
+
*/
|
|
243
|
+
createJsonConfig(credentials) {
|
|
244
|
+
return JSON.stringify({
|
|
245
|
+
'claude-flow/admin-password': credentials.adminPassword,
|
|
246
|
+
'claude-flow/service-password': credentials.servicePassword,
|
|
247
|
+
'claude-flow/jwt-secret': credentials.jwtSecret,
|
|
248
|
+
'claude-flow/session-secret': credentials.sessionSecret,
|
|
249
|
+
'claude-flow/encryption-key': credentials.encryptionKey,
|
|
250
|
+
'claude-flow/generated-at': credentials.generatedAt.toISOString(),
|
|
251
|
+
'claude-flow/expires-at': credentials.expiresAt?.toISOString() ?? null,
|
|
252
|
+
}, null, 2);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Factory function to create a production credential generator.
|
|
257
|
+
*
|
|
258
|
+
* @returns Configured CredentialGenerator instance
|
|
259
|
+
*/
|
|
260
|
+
export function createCredentialGenerator() {
|
|
261
|
+
return new CredentialGenerator();
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Quick credential generation for CLI usage.
|
|
265
|
+
*
|
|
266
|
+
* @returns Generated installation credentials
|
|
267
|
+
*/
|
|
268
|
+
export function generateCredentials() {
|
|
269
|
+
const generator = new CredentialGenerator();
|
|
270
|
+
return generator.generateInstallationCredentials();
|
|
271
|
+
}
|
|
272
|
+
//# sourceMappingURL=credential-generator.js.map
|