Class: Brew::Vulns::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/brew/vulns/cli.rb

Constant Summary collapse

DEFAULT_MAX_SUMMARY =
60
SEVERITY_LEVELS =
{ "low" => 1, "medium" => 2, "high" => 3, "critical" => 4 }.freeze
MAX_VULN_FETCH_THREADS =
15
FLAGS_WITH_VALUE =
%w[-b --brewfile -m --max-summary -s --severity --osv-export].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/brew/vulns/cli.rb', line 18

def initialize(args)
  @args = args
  @formula_names = parse_formula_names(args)
  @all = args.include?("--all")
  @include_deps = args.include?("--deps") || args.include?("-d")
  @ignore_patches = !args.include?("--no-ignore-patches")
  @json_output = args.include?("--json") || args.include?("-j")
  @sarif_output = args.include?("--sarif")
  @cyclonedx_output = args.include?("--cyclonedx")
  @help = args.include?("--help") || args.include?("-h")
  @max_summary = parse_max_summary(args)
  @min_severity = parse_severity(args)
  @brewfile = parse_brewfile_path(args)
  @osv_export_dir = parse_osv_export_dir(args)
  @force_ipv4 = args.include?("--ipv4")
end

Class Method Details

.run(args) ⇒ Object



6
7
8
9
10
11
# File 'lib/brew/vulns/cli.rb', line 6

def self.run(args)
  new(args).run
rescue Error => e
  $stderr.puts "Error: #{e.message}"
  2
end

Instance Method Details

#parse_brewfile_path(args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/brew/vulns/cli.rb', line 96

def parse_brewfile_path(args)
  args.each_with_index do |arg, idx|
    if arg == "--brewfile" || arg == "-b"
      value = args[idx + 1]
      return value if value && !value.start_with?("-")
      return "Brewfile"
    elsif arg.start_with?("--brewfile=")
      return arg.split("=", 2).last
    end
  end
  nil
end

#parse_formula_names(args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/brew/vulns/cli.rb', line 35

def parse_formula_names(args)
  names = []
  skip_next = false
  args.each do |arg|
    if skip_next
      skip_next = false
      next
    end
    if FLAGS_WITH_VALUE.include?(arg)
      skip_next = true
      next
    end
    next if arg.start_with?("-")

    names << arg
  end
  names
end

#parse_max_summary(args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/brew/vulns/cli.rb', line 54

def parse_max_summary(args)
  args.each_with_index do |arg, idx|
    if arg == "--max-summary" || arg == "-m"
      value = args[idx + 1]
      return value.to_i if value && !value.start_with?("-")
    elsif arg.start_with?("--max-summary=")
      return arg.split("=", 2).last.to_i
    end
  end
  DEFAULT_MAX_SUMMARY
end

#parse_osv_export_dir(args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/brew/vulns/cli.rb', line 79

def parse_osv_export_dir(args)
  args.each_with_index do |arg, idx|
    if arg == "--osv-export"
      value = args[idx + 1]
      return value if value && !value.empty? && !value.start_with?("-")

      raise Error, "--osv-export requires a directory argument"
    elsif arg.start_with?("--osv-export=")
      value = arg.split("=", 2).last
      raise Error, "--osv-export requires a directory argument" if value.empty?

      return value
    end
  end
  nil
end

#parse_severity(args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/brew/vulns/cli.rb', line 66

def parse_severity(args)
  args.each_with_index do |arg, idx|
    if arg == "--severity" || arg == "-s"
      value = args[idx + 1]
      return SEVERITY_LEVELS[value&.downcase] || 0 if value && !value.start_with?("-")
    elsif arg.start_with?("--severity=")
      value = arg.split("=", 2).last
      return SEVERITY_LEVELS[value&.downcase] || 0
    end
  end
  0
end

#runObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/brew/vulns/cli.rb', line 109

def run
  if @help
    print_help
    return 0
  end

  return run_osv_export if @osv_export_dir

  formulae = load_formulae
  if formulae.empty?
    puts "No formulae found."
    return 0
  end

  queryable = formulae.select(&:to_osv_query)
  skipped = formulae.size - queryable.size

  unless @json_output || @sarif_output || @cyclonedx_output
    puts "Checking #{queryable.size} packages for vulnerabilities..."
    puts "(#{skipped} packages skipped - no source repository URL or version)" if skipped > 0
    puts
  end

  results, patched = scan_vulnerabilities(queryable)
  output_results(results, patched, formulae)
rescue OsvClient::Error => e
  $stderr.puts "Error querying OSV: #{e.message}"
  2
rescue Error => e
  $stderr.puts "Error: #{e.message}"
  2
rescue JSON::ParserError => e
  $stderr.puts "Error parsing brew output: #{e.message}"
  2
end

#run_osv_exportObject

OSV export always operates on the full homebrew-core set (it generates an ecosystem-wide advisory feed), or on explicitly named formulae for testing. --brewfile / installed selection are intentionally ignored.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/brew/vulns/cli.rb', line 148

def run_osv_export
  formulae = if @formula_names.any?
    Formula.load_named(@formula_names)
  else
    Formula.load_all
  end

  annotated = formulae.select { |f| f.resolved_vulnerability_ids.any? }
  $stderr.puts "#{annotated.size} formulae with security `resolves` annotations"

  written = OsvExport.run(annotated, @osv_export_dir)
  written.each { |p| $stderr.puts "  wrote #{p}" }
  $stderr.puts "#{written.size} records written to #{@osv_export_dir}"
  0
rescue Error => e
  $stderr.puts "Error: #{e.message}"
  2
end