Class: Homebrew::Vulns::Vulnerability Private
- Includes:
- Utils::Output::Mixin
- Defined in:
- vulns/vulnerability.rb
Overview
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.
Wraps a single OSV vulnerability record. See https://ossf.github.io/osv-schema/ for the field definitions.
Defined Under Namespace
Classes: RangeStatus, Uncomparable
Instance Attribute Summary collapse
- #affected ⇒ Array<Hash{String => T.untyped}> readonly private
- #aliases ⇒ Array<String> readonly private
- #details ⇒ String? readonly private
- #id ⇒ String readonly private
- #references ⇒ Array<Hash{String => T.untyped}> readonly private
- #related ⇒ Array<String> readonly private
- #severity ⇒ Symbol? readonly private
- #severity_entries ⇒ Array<Hash{String => T.untyped}> readonly private
- #summary ⇒ String? readonly private
- #upstream ⇒ Array<String> readonly private
Class Method Summary collapse
Instance Method Summary collapse
- #advisory_url ⇒ String? private
- #affected_entries_for(ecosystem, name) ⇒ Array<Hash{String => T.untyped}> private
-
#affects_version?(version) ⇒ Boolean
private
OSV has already matched this record against the queried version.
- #comparator_for(range_type) ⇒ T.proc.params(a: String, b: String).returns(Integer) private
- #cve_ids ⇒ Array<String> private
- #extract_severity(data) ⇒ Symbol? private
- #fix_urls ⇒ Array<String> private
- #fixed_versions ⇒ Array<String> private
-
#identifiers ⇒ Array<String>
private
Only
idandaliasesname this vulnerability. - #in_interval?(target, lower, upper, upper_inclusive, cmp) ⇒ Boolean private
- #in_semver_range?(target, events) ⇒ Boolean private
- #initialize(data) ⇒ void constructor private
- #intervals(events) ⇒ Array<Array<([String, nil], [String, nil], Boolean)>> private
- #normalize_severity(value) ⇒ Symbol? private
- #normalize_version(version) ⇒ String private
-
#range_status(ecosystem, name, version) ⇒ RangeStatus?
private
Evaluates
versionagainst everyaffected[]entry whosepackagematches{ecosystem, name}(an advisory can carry several disjoint entries for the same package, e.g. GHSA-jfh8-c2jp-5v3q's three log4j-core ranges), honouring rangetype:. - #semver_compare!(left, right) ⇒ Integer private
- #severity_display ⇒ String private
- #severity_level ⇒ Integer private
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
#initialize(data) ⇒ 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.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'vulns/vulnerability.rb', line 42 def initialize(data) @id = T.let(data.fetch("id"), String) @summary = T.let(data["summary"], T.nilable(String)) @details = T.let(data["details"], T.nilable(String)) @aliases = T.let(Array(data["aliases"]), T::Array[String]) @upstream = T.let(Array(data["upstream"]), T::Array[String]) @related = T.let(Array(data["related"]), T::Array[String]) @references = T.let(Array(data["references"]), T::Array[T::Hash[String, T.untyped]]) @affected = T.let(Array(data["affected"]), T::Array[T::Hash[String, T.untyped]]) @severity_entries = T.let(Array(data["severity"]), T::Array[T::Hash[String, T.untyped]]) @severity = T.let(extract_severity(data), T.nilable(Symbol)) end |
Instance Attribute Details
#affected ⇒ Array<Hash{String => T.untyped}> (readonly)
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.
39 40 41 |
# File 'vulns/vulnerability.rb', line 39 def affected @affected end |
#aliases ⇒ Array<String> (readonly)
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.
36 37 38 |
# File 'vulns/vulnerability.rb', line 36 def aliases @aliases end |
#details ⇒ String? (readonly)
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.
30 31 32 |
# File 'vulns/vulnerability.rb', line 30 def details @details end |
#id ⇒ String (readonly)
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.
27 28 29 |
# File 'vulns/vulnerability.rb', line 27 def id @id end |
#references ⇒ Array<Hash{String => T.untyped}> (readonly)
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.
39 40 41 |
# File 'vulns/vulnerability.rb', line 39 def references @references end |
#related ⇒ Array<String> (readonly)
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.
36 37 38 |
# File 'vulns/vulnerability.rb', line 36 def @related end |
#severity ⇒ Symbol? (readonly)
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.
33 34 35 |
# File 'vulns/vulnerability.rb', line 33 def severity @severity end |
#severity_entries ⇒ Array<Hash{String => T.untyped}> (readonly)
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.
39 40 41 |
# File 'vulns/vulnerability.rb', line 39 def severity_entries @severity_entries end |
#summary ⇒ String? (readonly)
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.
30 31 32 |
# File 'vulns/vulnerability.rb', line 30 def summary @summary end |
#upstream ⇒ Array<String> (readonly)
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.
36 37 38 |
# File 'vulns/vulnerability.rb', line 36 def upstream @upstream end |
Class Method Details
.from_osv_list(list) ⇒ 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.
56 57 58 |
# File 'vulns/vulnerability.rb', line 56 def self.from_osv_list(list) list.map { |data| new(data) } end |
Instance Method Details
#advisory_url ⇒ 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.
89 90 91 |
# File 'vulns/vulnerability.rb', line 89 def advisory_url references.find { |r| r["type"] == "ADVISORY" }&.dig("url") end |
#affected_entries_for(ecosystem, name) ⇒ Array<Hash{String => T.untyped}>
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.
179 180 181 182 183 184 |
# File 'vulns/vulnerability.rb', line 179 def affected_entries_for(ecosystem, name) affected.select do |aff| pkg = aff["package"] pkg.is_a?(Hash) && pkg["ecosystem"] == ecosystem && pkg["name"] == name end end |
#affects_version?(version) ⇒ 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.
OSV has already matched this record against the queried version. This
method only overrides that with false when every affected entry can
be evaluated locally (explicit versions list or SEMVER range) and
none of them contains the target. Any uncheckable range or comparison
failure keeps the match.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'vulns/vulnerability.rb', line 218 def affects_version?(version) return true if affected.empty? target = normalize_version(version) checkable = T.let(false, T::Boolean) affected.each do |aff| versions = Array(aff["versions"]) if versions.any? checkable = true return true if versions.any? { |v| normalize_version(v.to_s) == target } end Array(aff["ranges"]).each do |range| return true if range["type"] != "SEMVER" checkable = true return true if in_semver_range?(target, Array(range["events"])) end end !checkable end |
#comparator_for(range_type) ⇒ T.proc.params(a: String, b: String).returns(Integer)
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.
187 188 189 190 191 192 193 |
# File 'vulns/vulnerability.rb', line 187 def comparator_for(range_type) if range_type == "SEMVER" ->(a, b) { Semver.compare(a, b) || raise(Uncomparable) } else ->(a, b) { Version.new(a) <=> Version.new(b) || raise(Uncomparable) } end end |
#cve_ids ⇒ 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.
84 85 86 |
# File 'vulns/vulnerability.rb', line 84 def cve_ids identifiers.select { |i| i.start_with?("CVE-") } end |
#extract_severity(data) ⇒ Symbol?
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.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'vulns/vulnerability.rb', line 243 def extract_severity(data) Array(data["severity"]) .select { |s| CVSS_TYPE_PRIORITY.key?(s["type"]) } .sort_by { |s| -CVSS_TYPE_PRIORITY.fetch(s["type"]) } .each do |s| sev = CVSS.severity(s["score"].to_s) return sev if sev end top = normalize_severity(data.dig("database_specific", "severity")) return top if top affected.each do |aff| eco = normalize_severity(aff.dig("ecosystem_specific", "severity")) return eco if eco db = normalize_severity(aff.dig("database_specific", "severity")) return db if db end nil end |
#fix_urls ⇒ 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.
94 95 96 |
# File 'vulns/vulnerability.rb', line 94 def fix_urls references.select { |r| r["type"] == "FIX" }.filter_map { |r| r["url"] } end |
#fixed_versions ⇒ 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.
99 100 101 102 103 104 105 |
# File 'vulns/vulnerability.rb', line 99 def fixed_versions affected.flat_map do |aff| Array(aff["ranges"]).flat_map do |range| Array(range["events"]).filter_map { |e| e["fixed"] } end end.uniq end |
#identifiers ⇒ 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.
Only id and aliases name this vulnerability. upstream is a
directed reference (a distro advisory pointing at one-or-more source
CVEs) and related links to different vulnerabilities; neither is safe
to treat as an identity of this record. Match follows upstream
explicitly and re-attributes the hit to each CVE it names.
79 80 81 |
# File 'vulns/vulnerability.rb', line 79 def identifiers [id, *aliases].uniq end |
#in_interval?(target, lower, upper, upper_inclusive, cmp) ⇒ 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.
200 201 202 203 204 205 206 207 208 209 210 |
# File 'vulns/vulnerability.rb', line 200 def in_interval?(target, lower, upper, upper_inclusive, cmp) above = lower.nil? || cmp.call(target, lower) >= 0 below = if upper.nil? true elsif upper_inclusive cmp.call(target, upper) <= 0 else cmp.call(target, upper).negative? end above && below end |
#in_semver_range?(target, events) ⇒ 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.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'vulns/vulnerability.rb', line 285 def in_semver_range?(target, events) return false if events.empty? intervals(events).any? do |lower, upper, upper_inclusive| above = lower.nil? || semver_compare!(target, lower) >= 0 below = if upper.nil? true elsif upper_inclusive semver_compare!(target, upper) <= 0 else semver_compare!(target, upper).negative? end above && below end rescue => e odebug "Failed to check #{target} against SEMVER range for #{id}: #{e}" true end |
#intervals(events) ⇒ Array<Array<([String, nil], [String, nil], 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.
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'vulns/vulnerability.rb', line 313 def intervals(events) result = [] lower = T.let(nil, T.nilable(String)) events.each do |event| if (intro = event["introduced"]) result << [lower, nil, false] if lower intro = normalize_version(intro.to_s) lower = (intro == "0") ? nil : intro elsif (fixed = event["fixed"]) result << [lower, normalize_version(fixed.to_s), false] lower = nil elsif (last = event["last_affected"]) result << [lower, normalize_version(last.to_s), true] lower = nil elsif (limit = event["limit"]) limit = limit.to_s upper = (limit == "*") ? nil : normalize_version(limit) result << [lower, upper, false] lower = nil end end result << [lower, nil, false] if lower || result.empty? result end |
#normalize_severity(value) ⇒ Symbol?
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.
267 268 269 270 271 272 273 274 |
# File 'vulns/vulnerability.rb', line 267 def normalize_severity(value) case value&.downcase when "critical" then :critical when "high" then :high when "medium", "moderate" then :medium when "low" then :low end end |
#normalize_version(version) ⇒ 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.
277 278 279 |
# File 'vulns/vulnerability.rb', line 277 def normalize_version(version) version.sub(/\Av/i, "") end |
#range_status(ecosystem, name, version) ⇒ RangeStatus?
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.
Evaluates version against every affected[] entry whose package
matches {ecosystem, name} (an advisory can carry several disjoint
entries for the same package, e.g. GHSA-jfh8-c2jp-5v3q's three
log4j-core ranges), honouring range type:
SEMVERranges compare with Semver.ECOSYSTEMranges compare with Version (best-effort; the record's own ecosystem defines the ordering, but callers only invoke this for ecosystems whose versions are broadlyVersion-comparable).GITranges are commit hashes and are skipped as uncomparable.
Returns nil when no entry matches the package or no comparable range
exists in the matching entries, so callers can distinguish
:not_applicable/:fixed from "could not check".
136 137 138 139 140 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 |
# File 'vulns/vulnerability.rb', line 136 def range_status(ecosystem, name, version) entries = affected_entries_for(ecosystem, name) return if entries.empty? target = normalize_version(version) checked = T.let(false, T::Boolean) past_fixes = T.let([], T::Array[String]) entries.each do |entry| Array(entry["ranges"]).each do |range| type = range["type"] next if type == "GIT" cmp = comparator_for(type) intervals(Array(range["events"])).each do |lower, upper, upper_inclusive| inside = in_interval?(target, lower, upper, upper_inclusive, cmp) checked = true return RangeStatus.new(state: :affected, fixed_in: upper_inclusive ? nil : upper).freeze if inside next unless upper rel = cmp.call(target, upper) past_fixes << upper if upper_inclusive ? rel.positive? : rel >= 0 rescue Uncomparable next end end versions = Array(entry["versions"]) next if versions.empty? checked = true if versions.any? { |v| normalize_version(v.to_s) == target } return RangeStatus.new(state: :affected, fixed_in: nil).freeze end end return unless checked state = past_fixes.any? ? :fixed : :not_applicable RangeStatus.new(state:, fixed_in: past_fixes.max_by { |v| Version.new(v) }).freeze end |
#semver_compare!(left, right) ⇒ Integer
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.
305 306 307 |
# File 'vulns/vulnerability.rb', line 305 def semver_compare!(left, right) Semver.compare(left, right) || raise(Uncomparable, "cannot compare #{left.inspect} with #{right.inspect}") end |
#severity_display ⇒ 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.
61 62 63 |
# File 'vulns/vulnerability.rb', line 61 def severity_display severity&.to_s&.upcase || "UNKNOWN" end |
#severity_level ⇒ Integer
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.
66 67 68 69 70 71 |
# File 'vulns/vulnerability.rb', line 66 def severity_level sev = severity return 0 if sev.nil? SEVERITY_LEVEL.fetch(sev, 0) end |