codeprobe-scanner 1.0.17 ā 1.0.20
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/package.json +1 -1
- package/src/cli/commands/scan.ts +35 -0
- package/src/cli/config.ts +3 -2
- package/src/engine/code-fixer.ts +169 -0
- package/src/engine/index.ts +1 -1
- package/src/engine/patcher.ts +14 -17
package/package.json
CHANGED
package/src/cli/commands/scan.ts
CHANGED
|
@@ -82,6 +82,20 @@ function displayReport(report: Report, json: boolean, durationMs: number): void
|
|
|
82
82
|
console.log(chalk.green(`Patches Available: ${patchCount}/${report.summary.total_cves}`));
|
|
83
83
|
console.log(`Duration: ${msToHuman(durationMs)}`);
|
|
84
84
|
|
|
85
|
+
// Display SAST findings if available
|
|
86
|
+
if ((report as any).code_vulnerabilities && (report as any).code_vulnerabilities.length > 0) {
|
|
87
|
+
const codeVulns = (report as any).code_vulnerabilities;
|
|
88
|
+
console.log(chalk.bold('\nš Source Code Vulnerabilities:'));
|
|
89
|
+
codeVulns.forEach((vuln: any) => {
|
|
90
|
+
const severity = vuln.severity === 'CRITICAL' ? chalk.red(vuln.severity)
|
|
91
|
+
: vuln.severity === 'HIGH' ? chalk.yellow(vuln.severity)
|
|
92
|
+
: vuln.severity === 'MEDIUM' ? chalk.cyan(vuln.severity)
|
|
93
|
+
: chalk.green(vuln.severity);
|
|
94
|
+
console.log(` ${severity} ${vuln.type} in ${vuln.file}:${vuln.line}`);
|
|
95
|
+
console.log(` ${vuln.description}`);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
85
99
|
if (report.scan.cves.length > 0) {
|
|
86
100
|
console.log(chalk.bold('\nCVE Details:'));
|
|
87
101
|
report.scan.cves.forEach((cve) => {
|
|
@@ -114,6 +128,27 @@ export async function scanCommand(args: string[]): Promise<void> {
|
|
|
114
128
|
// Run recursive scan to find all package.json files
|
|
115
129
|
const report = await engine.scanRecursive(repoPath);
|
|
116
130
|
|
|
131
|
+
// Also scan for source code vulnerabilities (SAST)
|
|
132
|
+
console.log("\nš Analyzing source code for vulnerabilities...");
|
|
133
|
+
const codeVulnerabilities = await engine.scanCodeVulnerabilities(repoPath);
|
|
134
|
+
|
|
135
|
+
// If --fix flag is set, apply fixes to source code
|
|
136
|
+
if (options.fix && codeVulnerabilities.length > 0) {
|
|
137
|
+
const { createCodeFixer } = await import("../../engine/code-fixer.js");
|
|
138
|
+
const fixer = createCodeFixer();
|
|
139
|
+
console.log("\nš§ Applying source code fixes...");
|
|
140
|
+
const fixes = await fixer.fixVulnerabilities(codeVulnerabilities);
|
|
141
|
+
console.log(` Applied ${fixes.length} code fixes`);
|
|
142
|
+
|
|
143
|
+
if (fixes.length > 0) {
|
|
144
|
+
console.log("\nš Fixed vulnerabilities:");
|
|
145
|
+
fixes.forEach((fix) => {
|
|
146
|
+
console.log(` - ${fix.file}:${fix.line}`);
|
|
147
|
+
console.log(` Type: ${fix.type}`);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
117
152
|
const duration = Date.now() - startTime;
|
|
118
153
|
|
|
119
154
|
// Save report
|
package/src/cli/config.ts
CHANGED
|
@@ -10,6 +10,7 @@ interface ConfigData {
|
|
|
10
10
|
bright_data_api_key?: string;
|
|
11
11
|
daytona_api_key?: string;
|
|
12
12
|
nosana_api_key?: string;
|
|
13
|
+
kimi_api_key?: string;
|
|
13
14
|
[key: string]: string | undefined;
|
|
14
15
|
}
|
|
15
16
|
|
|
@@ -138,7 +139,7 @@ export async function setConfig(key: string, value: string): Promise<void> {
|
|
|
138
139
|
const config = await loadConfig();
|
|
139
140
|
|
|
140
141
|
// Encrypt if it's a token/secret field (specific fields only)
|
|
141
|
-
const secretFields = ['github_token', 'bright_data_api_key', 'daytona_api_key', 'nosana_api_key'];
|
|
142
|
+
const secretFields = ['github_token', 'bright_data_api_key', 'daytona_api_key', 'nosana_api_key', 'kimi_api_key'];
|
|
142
143
|
if (secretFields.includes(key.toLowerCase())) {
|
|
143
144
|
config[key] = encryptToken(value);
|
|
144
145
|
} else {
|
|
@@ -156,7 +157,7 @@ export async function clearConfig(key: string): Promise<void> {
|
|
|
156
157
|
console.log(chalk.green(`ā Config cleared: ${key}`));
|
|
157
158
|
}
|
|
158
159
|
|
|
159
|
-
export async function getApiKey(service: 'BRIGHT_DATA' | 'DAYTONA' | 'NOSANA'): Promise<string> {
|
|
160
|
+
export async function getApiKey(service: 'BRIGHT_DATA' | 'DAYTONA' | 'NOSANA' | 'KIMI'): Promise<string> {
|
|
160
161
|
const envKey = process.env[`${service}_API_KEY`];
|
|
161
162
|
if (envKey) {
|
|
162
163
|
return envKey;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { readFile, writeFile } from "fs/promises";
|
|
2
|
+
import { CodeVulnerability } from "./sast";
|
|
3
|
+
|
|
4
|
+
export interface CodeFix {
|
|
5
|
+
file: string;
|
|
6
|
+
line: number;
|
|
7
|
+
original: string;
|
|
8
|
+
fixed: string;
|
|
9
|
+
type: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class CodeFixer {
|
|
13
|
+
async fixVulnerabilities(vulnerabilities: CodeVulnerability[]): Promise<CodeFix[]> {
|
|
14
|
+
const fixes: CodeFix[] = [];
|
|
15
|
+
const fileCache = new Map<string, { original: string; lines: string[] }>();
|
|
16
|
+
|
|
17
|
+
// Group vulnerabilities by file
|
|
18
|
+
const vulnsByFile = new Map<string, CodeVulnerability[]>();
|
|
19
|
+
for (const vuln of vulnerabilities) {
|
|
20
|
+
if (!vulnsByFile.has(vuln.file)) {
|
|
21
|
+
vulnsByFile.set(vuln.file, []);
|
|
22
|
+
}
|
|
23
|
+
vulnsByFile.get(vuln.file)!.push(vuln);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Process each file
|
|
27
|
+
for (const [filePath, fileVulns] of vulnsByFile) {
|
|
28
|
+
try {
|
|
29
|
+
const content = await readFile(filePath, "utf-8");
|
|
30
|
+
const lines = content.split("\n");
|
|
31
|
+
let modified = false;
|
|
32
|
+
|
|
33
|
+
for (const vuln of fileVulns) {
|
|
34
|
+
const fix = this.generateFixForVulnerability(vuln, lines);
|
|
35
|
+
if (fix) {
|
|
36
|
+
lines[vuln.line - 1] = fix.fixed;
|
|
37
|
+
fixes.push({ file: filePath, line: vuln.line, original: fix.original, fixed: fix.fixed, type: vuln.type });
|
|
38
|
+
modified = true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Write back if modified
|
|
43
|
+
if (modified) {
|
|
44
|
+
const updatedContent = lines.join("\n");
|
|
45
|
+
await writeFile(filePath, updatedContent, "utf-8");
|
|
46
|
+
console.log(` ā Fixed ${fileVulns.length} issues in ${filePath}`);
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.warn(` ā ļø Failed to fix ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return fixes;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private generateFixForVulnerability(vuln: CodeVulnerability, lines: string[]): { original: string; fixed: string } | null {
|
|
57
|
+
try {
|
|
58
|
+
const lineIndex = vuln.line - 1;
|
|
59
|
+
if (lineIndex < 0 || lineIndex >= lines.length) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const original = lines[lineIndex];
|
|
64
|
+
let fixed = original;
|
|
65
|
+
|
|
66
|
+
// Apply type-specific fixes
|
|
67
|
+
if (vuln.type === "Hardcoded Secret") {
|
|
68
|
+
fixed = this.fixHardcodedSecret(original);
|
|
69
|
+
} else if (vuln.type === "Command Injection") {
|
|
70
|
+
fixed = this.fixCommandInjection(original);
|
|
71
|
+
} else if (vuln.type === "Insecure Random") {
|
|
72
|
+
fixed = this.fixInsecureRandom(original);
|
|
73
|
+
} else if (vuln.type === "SQL Injection") {
|
|
74
|
+
fixed = this.fixSQLInjection(original);
|
|
75
|
+
} else if (vuln.type === "Cross-Site Scripting (XSS)") {
|
|
76
|
+
fixed = this.fixXSS(original);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (fixed !== original) {
|
|
80
|
+
return { original, fixed };
|
|
81
|
+
}
|
|
82
|
+
} catch (error) {
|
|
83
|
+
// Silent fail for individual fixes
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private fixHardcodedSecret(line: string): string {
|
|
90
|
+
// Replace hardcoded values with environment variable references
|
|
91
|
+
let fixed = line;
|
|
92
|
+
|
|
93
|
+
// Replace apiKey/api_key patterns
|
|
94
|
+
if (/api_?key\s*[:=]\s*["']/i.test(fixed)) {
|
|
95
|
+
const varName = fixed.match(/(\w+)\s*[:=]/)?.[1] || "apiKey";
|
|
96
|
+
fixed = fixed.replace(/api_?key\s*[:=]\s*["'][^"']*["']/i,
|
|
97
|
+
`${varName} = process.env.API_KEY || ""`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Replace password patterns
|
|
101
|
+
if (/password\s*[:=]\s*["']/i.test(fixed)) {
|
|
102
|
+
const varName = fixed.match(/(\w+)\s*[:=]/)?.[1] || "password";
|
|
103
|
+
fixed = fixed.replace(/password\s*[:=]\s*["'][^"']*["']/i,
|
|
104
|
+
`${varName} = process.env.PASSWORD || ""`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Replace token patterns
|
|
108
|
+
if (/token\s*[:=]\s*["']/i.test(fixed)) {
|
|
109
|
+
const varName = fixed.match(/(\w+)\s*[:=]/)?.[1] || "token";
|
|
110
|
+
fixed = fixed.replace(/token\s*[:=]\s*["'][^"']*["']/i,
|
|
111
|
+
`${varName} = process.env.TOKEN || ""`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Replace secret patterns
|
|
115
|
+
if (/secret\s*[:=]\s*["']/i.test(fixed)) {
|
|
116
|
+
const varName = fixed.match(/(\w+)\s*[:=]/)?.[1] || "secret";
|
|
117
|
+
fixed = fixed.replace(/secret\s*[:=]\s*["'][^"']*["']/i,
|
|
118
|
+
`${varName} = process.env.SECRET || ""`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return fixed;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private fixCommandInjection(line: string): string {
|
|
125
|
+
// Add escapeShellArg wrapper for unescaped variables
|
|
126
|
+
if (line.includes("execSync") || line.includes("exec")) {
|
|
127
|
+
return line.replace(/\$\{([^}]+)\}/g, (match, varName) => {
|
|
128
|
+
if (!line.includes("escapeShellArg")) {
|
|
129
|
+
return `\${escapeShellArg(${varName})}`;
|
|
130
|
+
}
|
|
131
|
+
return match;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return line;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private fixInsecureRandom(line: string): string {
|
|
138
|
+
// Replace Math.random with crypto.randomBytes
|
|
139
|
+
if (line.includes("Math.random()")) {
|
|
140
|
+
return line.replace(/Math\.random\(\)\.toString\(\d+\)\.slice\([^)]+\)/g, 'randomBytes(4).toString("hex")');
|
|
141
|
+
}
|
|
142
|
+
return line;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private fixSQLInjection(line: string): string {
|
|
146
|
+
// Suggest parameterized queries (manual fix needed)
|
|
147
|
+
if (line.includes("query") && line.includes("$")) {
|
|
148
|
+
return line.replace(/query\s*\([^)]*\$\{[^}]+\}[^)]*\)/, "query('...', [param1, param2])");
|
|
149
|
+
}
|
|
150
|
+
return line;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private fixXSS(line: string): string {
|
|
154
|
+
// Replace innerHTML with textContent
|
|
155
|
+
if (line.includes("innerHTML")) {
|
|
156
|
+
return line.replace(/\.innerHTML\s*=/, ".textContent =");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Replace dangerouslySetInnerHTML with safe alternative
|
|
160
|
+
if (line.includes("dangerouslySetInnerHTML")) {
|
|
161
|
+
return line.replace(/dangerouslySetInnerHTML=\{[^}]+\}/g, "children");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return line;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export const createCodeFixer = () => new CodeFixer();
|
package/src/engine/index.ts
CHANGED
|
@@ -132,7 +132,7 @@ export class CodeProbeEngine {
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
// Step 7: Generate patches for exploitable CVEs + any HIGH/CRITICAL with a known fix version
|
|
135
|
-
console.log("\x1b[33m[
|
|
135
|
+
console.log("\x1b[33m[Kimi]\x1b[0m š§ Generating patches with Kimi LLM...");
|
|
136
136
|
await this.patcher.loadPrebakedPatches();
|
|
137
137
|
const patchCandidates = matchedCves.filter((c) =>
|
|
138
138
|
c.exploitable || ((c.severity === "CRITICAL" || c.severity === "HIGH") && c.version_fixed)
|
package/src/engine/patcher.ts
CHANGED
|
@@ -67,38 +67,35 @@ export class PatchGenerator {
|
|
|
67
67
|
async generatePatch(cve: ScanCVE): Promise<string | null> {
|
|
68
68
|
await this.resolveKeys();
|
|
69
69
|
|
|
70
|
+
// First, check for prebaked patches (highest priority)
|
|
70
71
|
const prebakedPatch = this.patches.get(cve.id);
|
|
71
72
|
if (prebakedPatch) {
|
|
72
73
|
cve.patch_diff = prebakedPatch.diff;
|
|
73
74
|
return prebakedPatch.diff;
|
|
74
75
|
}
|
|
75
76
|
|
|
76
|
-
//
|
|
77
|
+
// Use Kimi LLM for patch generation (primary method)
|
|
77
78
|
if (this.kimiApiKey) {
|
|
78
79
|
try {
|
|
79
|
-
console.log(`[Kimi] Generating patch for ${cve.id}...`);
|
|
80
|
+
console.log(`[Kimi] š§ Generating patch for ${cve.id}...`);
|
|
80
81
|
const patch = await this.generatePatchWithKimi(cve);
|
|
81
82
|
if (patch) {
|
|
82
83
|
cve.patch_diff = patch;
|
|
84
|
+
console.log(`[Kimi] ā Patch generated for ${cve.id}`);
|
|
83
85
|
return patch;
|
|
84
86
|
}
|
|
85
87
|
} catch (error) {
|
|
86
|
-
console.warn(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (this.nosanaApiKey) {
|
|
92
|
-
try {
|
|
93
|
-
console.log(`[Kimi] Generating patch for ${cve.id}...`);
|
|
94
|
-
const patch = await this.generatePatchWithKimi(cve);
|
|
95
|
-
if (patch) {
|
|
96
|
-
cve.patch_diff = patch;
|
|
97
|
-
return patch;
|
|
98
|
-
}
|
|
99
|
-
} catch (error) {
|
|
100
|
-
console.warn(`[Kimi] Failed to generate patch: ${error instanceof Error ? error.message : String(error)}`);
|
|
88
|
+
console.warn(
|
|
89
|
+
`[Kimi] ā ļø Failed to generate patch for ${cve.id}: ${
|
|
90
|
+
error instanceof Error ? error.message : String(error)
|
|
91
|
+
}`
|
|
92
|
+
);
|
|
101
93
|
}
|
|
94
|
+
} else {
|
|
95
|
+
console.warn(
|
|
96
|
+
`[Kimi] ā ļø No Kimi API key configured. Set KIMI_API_KEY or run:\n` +
|
|
97
|
+
` codeprobe config set kimi_api_key <your-key>`
|
|
98
|
+
);
|
|
102
99
|
}
|
|
103
100
|
|
|
104
101
|
return null;
|