@@ -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