Class: Homebrew::Vulns::Scanner Private
- Includes:
- Utils::Output::Mixin
- Defined in:
- vulns/scanner.rb
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
Class Method Summary collapse
- .resolved_ids(serialized_patches) ⇒ Array<String> private
- .source_from_sbom(prefix) ⇒ Array<([String, nil], [String, nil])>? private
- .target_repo_url(source_url, head_url, homepage) ⇒ String? private
Instance Method Summary collapse
- #build_target(formula) ⇒ Target? private
- #fetch_vulnerabilities(ids) ⇒ Array<Vulnerability> private
- #fixed_in_release?(vuln, target) ⇒ Boolean private
- #initialize(formulae, ignore_patches: true, min_severity: nil, fix_type: nil) ⇒ void constructor private
- #partition_patched(formula, target, vulns) ⇒ Array<(Array<Vulnerability>, Array<Vulnerability>)> private
- #scan ⇒ Results private
- #stale_target?(formula) ⇒ Boolean private
- #target_for(formula) ⇒ Target? private
Methods included from Utils::Output::Mixin
#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_incompatible, #pretty_install_status, #pretty_installed, #pretty_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning
Constructor Details
#initialize(formulae, ignore_patches: true, min_severity: nil, fix_type: nil) ⇒ void
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.
107 108 109 110 111 112 113 114 |
# File 'vulns/scanner.rb', line 107 def initialize(formulae, ignore_patches: true, min_severity: nil, fix_type: nil) @formulae = formulae @ignore_patches = ignore_patches min_severity_level = min_severity ? SEVERITY_LEVELS.fetch(min_severity) : 0 @min_severity_level = T.let(min_severity_level, Integer) @fix_type = fix_type @patch_inclusion = T.let(GitHub::PatchInclusion.new, GitHub::PatchInclusion) end |
Class Method Details
.resolved_ids(serialized_patches) ⇒ Array<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.
51 52 53 54 55 56 57 |
# File 'vulns/scanner.rb', line 51 def self.resolved_ids(serialized_patches) serialized_patches .flat_map { |p| Array(p["resolves"]) } .select { |r| r.is_a?(Hash) && r["type"] == "security" } .map { |r| r["id"].to_s.upcase } .uniq end |
.source_from_sbom(prefix) ⇒ Array<([String, nil], [String, nil])>?
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.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'vulns/scanner.rb', line 31 def self.source_from_sbom(prefix) file = prefix/SBOM::FILENAME return unless file.file? data = JSON.parse(file.read) src = Array(data["packages"]).find { |p| p["SPDXID"].to_s.match?(SBOM_SRC_SPDXID) } return if src.nil? url = src["downloadLocation"] url = nil if url == "NOASSERTION" version = src["versionInfo"] version = nil if version == "NOASSERTION" return if url.nil? && version.nil? [url, version] rescue JSON::ParserError nil end |
.target_repo_url(source_url, head_url, homepage) ⇒ 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.
20 21 22 23 24 25 |
# File 'vulns/scanner.rb', line 20 def self.target_repo_url(source_url, head_url, homepage) url = Identify.repo_url(source_url, head_url, homepage) url ||= source_url if Identify.tag(source_url) url ||= head_url url end |
Instance Method Details
#build_target(formula) ⇒ Target?
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.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'vulns/scanner.rb', line 201 def build_target(formula) stable = formula.stable stable_url = stable&.url head_url = formula.head&.url homepage = formula.homepage if (prefix = formula.any_installed_prefix) installed_pkg_version = formula.any_installed_version version = installed_pkg_version&.version.to_s current_recipe_applies = installed_pkg_version == formula.pkg_version if (sbom = self.class.source_from_sbom(prefix)) sbom_url, sbom_version = sbom repo_url = self.class.target_repo_url(sbom_url, head_url, homepage) sbom_tag = Identify.tag(sbom_url) if repo_url && (tag = sbom_tag || sbom_version || version.presence) return Target.new(repo_url:, tag:, version:, current_recipe_applies:, from_installed_sbom: true, source_tag: sbom_tag, versioned_archive: Identify.versioned_archive?(sbom_url, tag)) end end else version = formula.version.to_s current_recipe_applies = true end repo_url = self.class.target_repo_url(stable_url, head_url, homepage) source_tag = Identify.tag(stable_url) || stable&.specs&.[](:tag) tag = source_tag || stable&.version&.to_s return if repo_url.nil? || tag.nil? Target.new(repo_url:, tag:, version:, source_tag:, from_installed_sbom: false, current_recipe_applies:, revision: stable&.specs&.[](:revision), versioned_archive: Identify.versioned_archive?(stable_url, tag)) end |
#fetch_vulnerabilities(ids) ⇒ Array<Vulnerability>
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.
269 270 271 272 273 274 275 276 |
# File 'vulns/scanner.rb', line 269 def fetch_vulnerabilities(ids) records = ids.each_slice(MAX_VULN_FETCH_THREADS).flat_map do |slice| slice .map { |v| Thread.new { OSV.vulnerability(v.fetch("id")) } } .map { |t| T.cast(t.value, T::Hash[String, T.untyped]) } end Vulnerability.from_osv_list(records) end |
#fixed_in_release?(vuln, target) ⇒ Boolean
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.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'vulns/scanner.rb', line 245 def fixed_in_release?(vuln, target) return false unless @patch_inclusion.available? return false unless (repository = target.repo_url[%r{\Ahttps://github\.com/([\w.-]+/[\w.-]+)\z}, 1]) fixes = vuln.git_fix_commits(target.repo_url) return false if fixes.empty? return false unless (tag = target.release_tag) return false if vuln.lists_version?(target.version, target.tag, tag) return false if target.revision && !target.revision.match?(/\A\h{40}\z/) ref = target.revision || "refs/tags/#{tag}" fixes.all? do |alternatives| alternatives.any? { |sha| @patch_inclusion.included_in_ref(repository, sha, ref:) } end rescue GitHub::API::Error, ErrorDuringExecution => e if @patch_inclusion.available? odebug "Could not check #{vuln.id} against #{target.repo_url} tag #{tag}: #{e.}" else opoo "GitHub commit checks stopped. Findings with unverified fixes remain open.\n#{e.}" end false end |
#partition_patched(formula, target, vulns) ⇒ Array<(Array<Vulnerability>, Array<Vulnerability>)>
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.
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'vulns/scanner.rb', line 282 def partition_patched(formula, target, vulns) return [vulns, []] unless @ignore_patches # The current formula's `serialized_patches` reflects the recipe on # disk. If the scanned keg was built from an older recipe it may lack a # patch the recipe has since gained, so its `resolves` must not # suppress findings. return [vulns, []] unless target.current_recipe_applies resolved = self.class.resolved_ids(formula.serialized_patches) return [vulns, []] if resolved.empty? patched, open = vulns.partition do |v| v.identifiers.any? { |id| resolved.include?(id.to_s.upcase) } end [open, patched] end |
#scan ⇒ Results
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.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'vulns/scanner.rb', line 141 def scan queryable, skipped_formulae = @formulae.partition { |f| target_for(f) } skipped_formulae = skipped_formulae.map(&:full_name) outdated_without_sbom = queryable.select { |f| stale_target?(f) }.map(&:full_name) return Results.new(findings: [], checked: 0, skipped_formulae:, outdated_without_sbom:) if queryable.empty? targets = queryable.filter_map { |f| target_for(f) } batch = OSV.query_batch(targets.map { |t| { ecosystem: "GIT", name: t.repo_url, version: t.tag } }) findings = queryable.each_with_index.filter_map do |formula, index| target = targets.fetch(index) ids = batch.fetch(index) next if ids.empty? vulns = fetch_vulnerabilities(ids) .select { |v| v.affects_version?(target.tag) } .select { |v| v.severity_level >= @min_severity_level } case @fix_type when :released vulns = vulns.select { |v| v.released_fix_available?(target.tag, target.repo_url) } when :patch vulns = vulns.select { |v| v.patch_fix_available?(target.tag, target.repo_url) } when :any vulns = vulns.select { |v| v.fix_available?(target.tag, target.repo_url) } when :none vulns = vulns.reject { |v| v.fix_available?(target.tag, target.repo_url) } when :unreleased vulns = vulns.reject { |v| v.released_fix_available?(target.tag, target.repo_url) } end # The current recipe can pin a different commit even at the installed version. if target.from_installed_sbom || formula.any_installed_prefix.nil? vulns.reject! { |v| fixed_in_release?(v, target) } end next if vulns.empty? open, patched = partition_patched(formula, target, vulns) next if open.empty? && patched.empty? Finding.new( name: formula.full_name, version: target.version, tag: target.tag, repo_url: target.repo_url, open:, patched:, ) end Results.new(findings:, checked: queryable.size, skipped_formulae:, outdated_without_sbom:) end |
#stale_target?(formula) ⇒ Boolean
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.
237 238 239 240 241 242 |
# File 'vulns/scanner.rb', line 237 def stale_target?(formula) target = target_for(formula) return false if target.nil? || target.from_installed_sbom !target.current_recipe_applies end |
#target_for(formula) ⇒ Target?
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.
193 194 195 196 197 198 |
# File 'vulns/scanner.rb', line 193 def target_for(formula) @targets ||= T.let({}, T.nilable(T::Hash[String, T.nilable(Target)])) @targets.fetch(formula.full_name) do @targets[formula.full_name] = build_target(formula) end end |