Class: Homebrew::Cmd::Vulns Private

Inherits:
AbstractCommand show all
Defined in:
cmd/vulns.rb,
sorbet/rbi/dsl/homebrew/cmd/vulns.rbi

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: Args

Constant Summary collapse

SEVERITIES =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

%w[low medium high critical].freeze

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#argsHomebrew::Cmd::Vulns::Args

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



10
# File 'sorbet/rbi/dsl/homebrew/cmd/vulns.rbi', line 10

def args; end

#brewfile_path(value) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

A bare --brewfile (no =path) yields true from OptionParser at runtime; the generated RBI types it as T.nilable(String), so accept the wider type here and normalise true/"" to the nil default.

Parameters:

Returns:



110
111
112
# File 'cmd/vulns.rb', line 110

def brewfile_path(value)
  value.presence if value.is_a?(String)
end

#formulaeArray<Formula>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'cmd/vulns.rb', line 75

def formulae
  list = T.let([], T::Array[Formula])
  if (brewfile = args.brewfile)
    require "bundle/brewfile"
    list += Homebrew::Bundle::Brewfile.read(file: brewfile_path(brewfile)).entries
                                      .select { |e| e.type == :brew }
                                      .map { |e| Formulary.resolve(e.name) }
  end
  list += args.named.to_resolved_formulae if args.named.any?
  list = installed_formulae if !args.brewfile && args.no_named?
  list += list.flat_map { |f| f.recursive_dependencies.map(&:to_formula) } if args.deps?
  list.uniq(&:full_name)
end

#installed_formulaeArray<Formula>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



90
91
92
93
94
95
96
97
98
99
# File 'cmd/vulns.rb', line 90

def installed_formulae
  Formula.racks.filter_map do |rack|
    Formulary.from_rack(rack)
  rescue Homebrew::UntrustedTapError => e
    untrusted_skipped << e.message.lines.first.to_s.strip
    nil
  rescue
    nil
  end.uniq(&:name)
end

#max_summaryInteger

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

Raises:



126
127
128
129
130
131
132
133
# File 'cmd/vulns.rb', line 126

def max_summary
  raw = args.max_summary
  return Homebrew::Vulns::Output::DEFAULT_MAX_SUMMARY if raw.nil?

  raise UsageError, "`--max-summary` must be a non-negative integer" unless raw.match?(/\A\d+\z/)

  raw.to_i
end

#min_severitySymbol?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

Raises:



115
116
117
118
119
120
121
122
123
# File 'cmd/vulns.rb', line 115

def min_severity
  raw = args.severity
  return if raw.nil?

  raw = raw.downcase
  raise UsageError, "`--severity` must be one of: #{SEVERITIES.join(", ")}" unless SEVERITIES.include?(raw)

  raw.to_sym
end

#runvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'cmd/vulns.rb', line 36

def run
  require "vulns"

  summary_width = max_summary
  severity = min_severity

  results = Homebrew::Vulns::Scanner.new(
    formulae,
    ignore_patches: !args.no_ignore_patches?,
    min_severity:   severity,
  ).scan

  if args.json?
    Homebrew::Vulns::Output.json(results)
  else
    Homebrew::Vulns::Output.text(results, max_summary: summary_width)
  end

  if untrusted_skipped.any?
    kegs = Utils.pluralize("installed keg", untrusted_skipped.size, include_count: true)
    opoo <<~EOS
      #{kegs} from an untrusted tap not scanned:
        #{untrusted_skipped.join("\n  ")}
      Run `brew trust` on the formula or tap to include it in future scans.
    EOS
    Homebrew.failed = true
  end
  if results.outdated_without_sbom.any?
    opoo <<~EOS
      The installed source of #{results.outdated_without_sbom.sort.join(", ")} could not be determined
      (older than the current formula and no SBOM was written at install time). Results above reflect
      the current formula version, not what is installed. Run `brew upgrade` for accurate results.
    EOS
    Homebrew.failed = true
  end
  Homebrew.failed = true if results.any_open?
end

#untrusted_skippedArray<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



102
103
104
# File 'cmd/vulns.rb', line 102

def untrusted_skipped
  @untrusted_skipped ||= T.let([], T.nilable(T::Array[String]))
end