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].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/brew/vulns/cli.rb', line 15

def initialize(args)
  @args = args
  @formula_names = parse_formula_names(args)
  @all = args.include?("--all")
  @include_deps = args.include?("--deps") || args.include?("-d")
  @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)
end

Class Method Details

.run(args) ⇒ Object



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

def self.run(args)
  new(args).run
end

Instance Method Details

#parse_brewfile_path(args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/brew/vulns/cli.rb', line 73

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/brew/vulns/cli.rb', line 29

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



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/brew/vulns/cli.rb', line 48

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_severity(args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/brew/vulns/cli.rb', line 60

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/brew/vulns/cli.rb', line 86

def run
  if @help
    print_help
    return 0
  end

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

  queryable = formulae.select(&:supported_forge?).select(&:tag)
  skipped = formulae.size - queryable.size

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

  results = scan_vulnerabilities(queryable)
  output_results(results, 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