Skip to content

Commit 8840e46

Browse files
committed
feat:integrates with the fe
1 parent 667acc0 commit 8840e46

9 files changed

Lines changed: 264 additions & 188 deletions

File tree

api/src/index.js

Lines changed: 26 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/src/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/src/index.ts

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,68 @@
1-
import express from 'express';
2-
import mongoose from 'mongoose';
3-
import cors from 'cors';
4-
import dotenv from 'dotenv';
5-
import { ScanReport } from './models/ScanReport';
1+
import express from "express";
2+
import mongoose from "mongoose";
3+
import cors from "cors";
4+
import dotenv from "dotenv";
5+
import { ScanReport } from "./models/ScanReport";
66

77
dotenv.config();
88

99
const app = express();
10-
const port = process.env.PORT || 3000;
10+
const port = process.env.PORT || 4000;
1111

1212
// Middleware
1313
app.use(cors());
1414
app.use(express.json());
1515

1616
// Connect to MongoDB
17-
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/monkeycode')
18-
.then(() => console.log('Connected to MongoDB'))
19-
.catch(err => console.error('MongoDB connection error:', err));
17+
mongoose
18+
.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/monkeycode")
19+
.then(() => console.log("Connected to MongoDB"))
20+
.catch((err) => console.error("MongoDB connection error:", err));
2021

2122
// Store scan report endpoint
22-
app.post('/api/scan-reports', async (req, res) => {
23+
app.post("/api/scan-reports", async (req, res) => {
2324
try {
2425
const scanReport = new ScanReport(req.body);
2526
await scanReport.save();
26-
res.status(201).json({ message: 'Scan report stored successfully', scanId: scanReport.scanId });
27+
res
28+
.status(201)
29+
.json({
30+
message: "Scan report stored successfully",
31+
scanId: scanReport.scanId,
32+
});
2733
} catch (error: unknown) {
28-
console.error('Error storing scan report:', error);
34+
console.error("Error storing scan report:", error);
2935
if (error instanceof mongoose.Error.ValidationError) {
30-
res.status(400).json({ error: 'Invalid scan report data' });
31-
} else if (error && typeof error === 'object' && 'code' in error && error.code === 11000) {
32-
res.status(409).json({ error: 'Scan report with this ID already exists' });
36+
res.status(400).json({ error: "Invalid scan report data" });
37+
} else if (
38+
error &&
39+
typeof error === "object" &&
40+
"code" in error &&
41+
error.code === 11000
42+
) {
43+
res
44+
.status(409)
45+
.json({ error: "Scan report with this ID already exists" });
3346
} else {
34-
res.status(500).json({ error: 'Internal server error' });
47+
res.status(500).json({ error: "Internal server error" });
3548
}
3649
}
3750
});
3851

3952
// Get scan report by ID
40-
app.get('/api/scan-reports/:scanId', async (req, res) => {
53+
app.get("/api/scan-reports/:scanId", async (req, res) => {
4154
try {
4255
const scanReport = await ScanReport.findOne({ scanId: req.params.scanId });
4356
if (!scanReport) {
44-
return res.status(404).json({ error: 'Scan report not found' });
57+
return res.status(404).json({ error: "Scan report not found" });
4558
}
4659
res.json(scanReport);
4760
} catch (error) {
48-
console.error('Error retrieving scan report:', error);
49-
res.status(500).json({ error: 'Internal server error' });
61+
console.error("Error retrieving scan report:", error);
62+
res.status(500).json({ error: "Internal server error" });
5063
}
5164
});
5265

5366
app.listen(port, () => {
5467
console.log(`Server running on port ${port}`);
55-
});
68+
});

api/test-api.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/test-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios';
22

3-
const API_URL = 'http://localhost:3000';
3+
const API_URL = 'http://localhost:4000';
44

55
async function testApi() {
66
try {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
},
5858
"monkeycode.apiUrl": {
5959
"type": "string",
60-
"default": "http://localhost:3000",
60+
"default": "http://localhost:4000",
6161
"description": "URL of the MonkeyCode API server"
6262
}
6363
}

src/apiClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ScanReport } from './types';
44
export class ApiClient {
55
private baseUrl: string;
66

7-
constructor(baseUrl: string = 'http://localhost:3000') {
7+
constructor(baseUrl: string = 'http://localhost:4000') {
88
this.baseUrl = baseUrl;
99
}
1010

src/codeScanner.ts

Lines changed: 59 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -23,94 +23,81 @@ export class CodeScanner {
2323
vulnerableModules: Map<string, Vulnerability[]>
2424
): Promise<VulnerableUsage[]> {
2525
console.log('Starting code scan in:', workspaceRoot);
26-
console.log('Vulnerable modules to check:', Array.from(vulnerableModules.entries()).map(([path, vulns]) =>
27-
`${path} (${vulns.length} vulnerabilities)`
28-
));
2926

30-
const usages = new Map<string, VulnerableUsage>(); // Use Map to deduplicate
27+
const usages = new Map<string, VulnerableUsage>();
3128
const goFiles = await this.findGoFiles(workspaceRoot);
32-
console.log('Found Go files:', goFiles);
29+
console.log('Found', goFiles.length, 'Go files to scan');
3330

34-
for (const file of goFiles) {
35-
console.log('\nScanning file:', file);
36-
const fileContent = await vscode.workspace.fs.readFile(vscode.Uri.file(file));
37-
const content = Buffer.from(fileContent).toString('utf8');
38-
39-
// Skip test files for now (we can add them later if needed)
40-
if (file.endsWith('_test.go')) {
41-
console.log('Skipping test file');
42-
continue;
43-
}
31+
// Process files in batches to avoid blocking
32+
const batchSize = 10;
33+
for (let i = 0; i < goFiles.length; i += batchSize) {
34+
const batch = goFiles.slice(i, i + batchSize);
35+
await Promise.all(batch.map(async (file) => {
36+
try {
37+
// Skip test files
38+
if (file.endsWith('_test.go')) {
39+
return;
40+
}
4441

45-
// Find all imports and their locations
46-
const imports = this.findImports(content, file);
47-
console.log('Found imports:', Array.from(imports.keys()));
48-
49-
// Check each import against vulnerable modules
50-
for (const [importPath, locations] of imports) {
51-
const vulns = vulnerableModules.get(importPath);
52-
if (vulns) {
53-
console.log('Found vulnerable import:', importPath);
54-
for (const vuln of vulns) {
55-
console.log(' - Vulnerability:', vuln.id, vuln.severity);
56-
const key = `${importPath}:${vuln.id}`;
57-
const existingUsage = usages.get(key);
58-
59-
if (existingUsage) {
60-
console.log(' Adding to existing usage');
61-
existingUsage.locations.push(...locations);
62-
} else {
63-
console.log(' Creating new usage');
64-
usages.set(key, {
65-
module: { path: importPath, version: '', indirect: false },
66-
vulnerability: vuln,
67-
locations: locations
68-
});
42+
const fileContent = await vscode.workspace.fs.readFile(vscode.Uri.file(file));
43+
const content = Buffer.from(fileContent).toString('utf8');
44+
45+
// Find all imports and their locations
46+
const imports = this.findImports(content, file);
47+
48+
// Check each import against vulnerable modules
49+
for (const [importPath, locations] of imports) {
50+
const vulns = vulnerableModules.get(importPath);
51+
if (vulns) {
52+
for (const vuln of vulns) {
53+
const key = `${importPath}:${vuln.id}`;
54+
const existingUsage = usages.get(key);
55+
56+
if (existingUsage) {
57+
existingUsage.locations.push(...locations);
58+
} else {
59+
usages.set(key, {
60+
module: { path: importPath, version: '', indirect: false },
61+
vulnerability: vuln,
62+
locations: locations
63+
});
64+
}
65+
}
6966
}
7067
}
71-
} else {
72-
// Log when we find an import that's not in vulnerable modules
73-
console.log('Import not found in vulnerable modules:', importPath);
74-
}
75-
}
7668

77-
// Find function calls to vulnerable packages
78-
const functionCalls = this.findFunctionCalls(content, file, imports);
79-
console.log('Found function calls:', Array.from(functionCalls.keys()));
80-
81-
for (const [importPath, calls] of functionCalls) {
82-
const vulns = vulnerableModules.get(importPath);
83-
if (vulns) {
84-
console.log('Found vulnerable function calls in:', importPath);
85-
for (const vuln of vulns) {
86-
console.log(' - Vulnerability:', vuln.id, vuln.severity);
87-
const key = `${importPath}:${vuln.id}`;
88-
const existingUsage = usages.get(key);
89-
90-
if (existingUsage) {
91-
console.log(' Adding to existing usage');
92-
existingUsage.locations.push(...calls);
93-
} else {
94-
console.log(' Creating new usage');
95-
usages.set(key, {
96-
module: { path: importPath, version: '', indirect: false },
97-
vulnerability: vuln,
98-
locations: calls
99-
});
69+
// Find function calls to vulnerable packages
70+
const functionCalls = this.findFunctionCalls(content, file, imports);
71+
72+
for (const [importPath, calls] of functionCalls) {
73+
const vulns = vulnerableModules.get(importPath);
74+
if (vulns) {
75+
for (const vuln of vulns) {
76+
const key = `${importPath}:${vuln.id}`;
77+
const existingUsage = usages.get(key);
78+
79+
if (existingUsage) {
80+
existingUsage.locations.push(...calls);
81+
} else {
82+
usages.set(key, {
83+
module: { path: importPath, version: '', indirect: false },
84+
vulnerability: vuln,
85+
locations: calls
86+
});
87+
}
88+
}
10089
}
10190
}
102-
} else {
103-
// Log when we find function calls to a package that's not in vulnerable modules
104-
console.log('Function calls to package not found in vulnerable modules:', importPath);
91+
} catch (error) {
92+
console.error(`Error scanning file ${file}:`, error);
10593
}
106-
}
94+
}));
10795
}
10896

10997
const results = Array.from(usages.values());
11098
console.log('\nScan complete. Found usages:', results.length);
11199
results.forEach(usage => {
112100
console.log(`- ${usage.module.path}: ${usage.vulnerability.id} (${usage.locations.length} locations)`);
113-
// Log unique files where this vulnerability was found
114101
const uniqueFiles = new Set(usage.locations.map(l => l.file));
115102
console.log(` Found in ${uniqueFiles.size} files:`, Array.from(uniqueFiles));
116103
});

0 commit comments

Comments
 (0)