@@ -11,8 +11,8 @@ use tower_lsp::{
1111use crate :: infra:: parse_dockerfile;
1212
1313use super :: {
14- ImageBuilder , ImageScanResult , ImageScanner , InMemoryDocumentDatabase , LSPClient , VulnSeverity ,
15- lsp_server:: WithContext ,
14+ ImageBuilder , ImageScanResult , ImageScanner , InMemoryDocumentDatabase , LSPClient ,
15+ LayerScanResult , VulnSeverity , lsp_server:: WithContext ,
1616} ;
1717
1818pub struct CommandExecutor < C > {
@@ -246,53 +246,64 @@ pub fn diagnostics_for_layers(
246246 break ;
247247 }
248248
249- if layer. layer_text . contains ( & instr. arguments_str ) {
250- instr_idx = instr_idx. and_then ( |x| x. checked_sub ( 1 ) ) ;
251- layer_idx = layer_idx. and_then ( |x| x. checked_sub ( 1 ) ) ;
252-
253- let mut msg = String :: new ( ) ;
254- if layer. count_vulns_of_severity ( VulnSeverity :: Critical ) > 0 {
255- msg += & format ! (
256- "🟣 {} " ,
257- layer. count_vulns_of_severity( VulnSeverity :: Critical )
258- )
259- }
260- if layer. count_vulns_of_severity ( VulnSeverity :: High ) > 0 {
261- msg += & format ! ( "🔴 {} " , layer. count_vulns_of_severity( VulnSeverity :: High ) )
262- }
263- if layer. count_vulns_of_severity ( VulnSeverity :: Medium ) > 0 {
264- msg += & format ! (
265- "🟠 {} " ,
266- layer. count_vulns_of_severity( VulnSeverity :: Medium )
267- )
268- }
269- if layer. count_vulns_of_severity ( VulnSeverity :: Low ) > 0 {
270- msg += & format ! ( "🟡 {} " , layer. count_vulns_of_severity( VulnSeverity :: Low ) )
271- }
272- if layer. count_vulns_of_severity ( VulnSeverity :: Negligible ) > 0 {
273- msg += & format ! (
274- "⚪ {} " ,
275- layer. count_vulns_of_severity( VulnSeverity :: Negligible )
276- )
277- }
278-
249+ instr_idx = instr_idx. and_then ( |x| x. checked_sub ( 1 ) ) ;
250+ layer_idx = layer_idx. and_then ( |x| x. checked_sub ( 1 ) ) ;
251+
252+ if layer. has_vulnerabilities ( ) {
253+ let msg = format ! (
254+ "Vulnerabilities found in layer: {} Critical, {} High, {} Medium, {} Low, {} Negligible" ,
255+ layer. count_vulns_of_severity( VulnSeverity :: Critical ) ,
256+ layer. count_vulns_of_severity( VulnSeverity :: High ) ,
257+ layer. count_vulns_of_severity( VulnSeverity :: Medium ) ,
258+ layer. count_vulns_of_severity( VulnSeverity :: Low ) ,
259+ layer. count_vulns_of_severity( VulnSeverity :: Negligible ) ,
260+ ) ;
279261 let diagnostic = Diagnostic {
280262 range : instr. range ,
281263 severity : Some ( DiagnosticSeverity :: WARNING ) ,
282- source : Some ( "Sysdig" . to_string ( ) ) ,
283264 message : msg,
284265 ..Default :: default ( )
285266 } ;
286267
287268 diagnostics. push ( diagnostic) ;
288- } else {
289- layer_idx = layer_idx . and_then ( |x| x . checked_sub ( 1 ) ) ;
269+
270+ fill_vulnerability_hints_for_layer ( layer , instr . range , & mut diagnostics )
290271 }
291272 }
292273
293274 Ok ( diagnostics)
294275}
295276
277+ fn fill_vulnerability_hints_for_layer (
278+ layer : & LayerScanResult ,
279+ range : Range ,
280+ diagnostics : & mut Vec < Diagnostic > ,
281+ ) {
282+ let vulnerability_types = [
283+ VulnSeverity :: Critical ,
284+ VulnSeverity :: High ,
285+ VulnSeverity :: Medium ,
286+ VulnSeverity :: Low ,
287+ VulnSeverity :: Negligible ,
288+ ] ;
289+
290+ let vulns_per_severity = vulnerability_types
291+ . iter ( )
292+ . flat_map ( |sev| layer. vulnerabilities . iter ( ) . filter ( |l| l. severity == * sev) ) ;
293+
294+ // TODO(fede): eventually we would want to add here a .take() to truncate the number
295+ // of vulnerabilities shown as hint per layer.
296+ vulns_per_severity. for_each ( |vuln| {
297+ let url = format ! ( "https://nvd.nist.gov/vuln/detail/{}" , vuln. id) ;
298+ diagnostics. push ( Diagnostic {
299+ range,
300+ severity : Some ( DiagnosticSeverity :: HINT ) ,
301+ message : format ! ( "Vulnerability: {} ({:?}) {}" , vuln. id, vuln. severity, url) ,
302+ ..Default :: default ( )
303+ } ) ;
304+ } ) ;
305+ }
306+
296307fn diagnostic_for_image (
297308 line : u32 ,
298309 document_text : & str ,
@@ -319,7 +330,7 @@ fn diagnostic_for_image(
319330
320331 if scan_result. has_vulnerabilities ( ) {
321332 diagnostic. message = format ! (
322- "Vulnerabilities found: {} Critical, {} High, {} Medium, {} Low, {} Negligible" ,
333+ "Total vulnerabilities found: {} Critical, {} High, {} Medium, {} Low, {} Negligible" ,
323334 scan_result. count_vulns_of_severity( VulnSeverity :: Critical ) ,
324335 scan_result. count_vulns_of_severity( VulnSeverity :: High ) ,
325336 scan_result. count_vulns_of_severity( VulnSeverity :: Medium ) ,
0 commit comments