Class: Homebrew::Vulns::Vulnerability Private

Inherits:
Object
  • Object
show all
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: Interval, RangeStatus, Uncomparable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:



49
50
51
52
53
54
55
56
57
58
59
60
# File 'vulns/vulnerability.rb', line 49

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

#affectedArray<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.

Returns:



46
47
48
# File 'vulns/vulnerability.rb', line 46

def affected
  @affected
end

#aliasesArray<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.

Returns:



43
44
45
# File 'vulns/vulnerability.rb', line 43

def aliases
  @aliases
end

#detailsString? (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.

Returns:



37
38
39
# File 'vulns/vulnerability.rb', line 37

def details
  @details
end

#idString (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.

Returns:



34
35
36
# File 'vulns/vulnerability.rb', line 34

def id
  @id
end

#referencesArray<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.

Returns:



46
47
48
# File 'vulns/vulnerability.rb', line 46

def references
  @references
end

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:



43
44
45
# File 'vulns/vulnerability.rb', line 43

def related
  @related
end

#severitySymbol? (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.

Returns:



40
41
42
# File 'vulns/vulnerability.rb', line 40

def severity
  @severity
end

#severity_entriesArray<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.

Returns:



46
47
48
# File 'vulns/vulnerability.rb', line 46

def severity_entries
  @severity_entries
end

#summaryString? (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.

Returns:



37
38
39
# File 'vulns/vulnerability.rb', line 37

def summary
  @summary
end

#upstreamArray<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.

Returns:



43
44
45
# File 'vulns/vulnerability.rb', line 43

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.

Parameters:

Returns:



63
64
65
# File 'vulns/vulnerability.rb', line 63

def self.from_osv_list(list)
  list.map { |data| new(data) }
end

Instance Method Details

#advisory_urlString?

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:



96
97
98
# File 'vulns/vulnerability.rb', line 96

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.

Parameters:

Returns:



190
191
192
193
194
195
# File 'vulns/vulnerability.rb', line 190

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

#affected_entry_relevant?(aff, repo_url) ⇒ 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.

Parameters:

Returns:

  • (Boolean)


400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'vulns/vulnerability.rb', line 400

def affected_entry_relevant?(aff, repo_url)
  return true if repo_url.nil?

  git_ranges = Array(aff["ranges"]).select { |r| r["type"] == "GIT" }
  if git_ranges.any?
    normalized_repo = repo_url.downcase.chomp("/").delete_suffix(".git").chomp("/")
    return git_ranges.any? do |r|
      range_repo = r["repo"].to_s.downcase.chomp("/").delete_suffix(".git").chomp("/")
      range_repo.empty? || range_repo == normalized_repo
    end
  end

  # For entries without GIT ranges (e.g. SEMVER ranges or version lists),
  # check that the package ecosystem is GIT or unspecified.
  ecosystem = aff.dig("package", "ecosystem")
  ecosystem.blank? || ecosystem == "GIT"
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.

Parameters:

Returns:

  • (Boolean)


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'vulns/vulnerability.rb', line 229

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.

Parameters:

Returns:



198
199
200
201
202
203
204
# File 'vulns/vulnerability.rb', line 198

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_idsArray<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:



91
92
93
# File 'vulns/vulnerability.rb', line 91

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.

Parameters:

Returns:



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'vulns/vulnerability.rb', line 254

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_available?(version, repo_url = 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.

Parameters:

  • version (String)
  • repo_url (String, nil) (defaults to: nil)

Returns:

  • (Boolean)


419
420
421
# File 'vulns/vulnerability.rb', line 419

def fix_available?(version, repo_url = nil)
  fix_available_of_type?(version, repo_url, :any)
end

#fix_available_of_type?(version, repo_url, fix_type) ⇒ 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.

Parameters:

Returns:

  • (Boolean)


434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'vulns/vulnerability.rb', line 434

def fix_available_of_type?(version, repo_url, fix_type)
  return false if affected.empty?

  target = normalize_version(version)

  affected.each do |aff|
    next unless affected_entry_relevant?(aff, repo_url)

    version_matched = false
    ranges = Array(aff["ranges"])
    semver_ranges, non_semver_ranges = ranges.partition { |r| r["type"] == "SEMVER" }

    versions = Array(aff["versions"])
    version_matched = true if versions.any? { |v| normalize_version(v.to_s) == target }

    version_matched = true if non_semver_ranges.any?

    matched_semver_range = T.let(false, T::Boolean)
    fix_found = T.let(false, T::Boolean)

    semver_ranges.each do |range|
      intervals(Array(range["events"])).each do |interval|
        result = in_interval_strict?(target, interval)
        next if result.nil?

        next unless result

        matched_semver_range = true
        fix = interval.fix
        next if fix.blank?

        fix_found = true if fix_type != :patch
      end
    end

    version_matched ||= matched_semver_range

    next unless version_matched

    return true if fix_found

    non_semver_ranges.each do |range|
      return true if non_semver_fix_available_of_type?(target, range, fix_type)
    end
  end

  false
end

#fix_urlsArray<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:



101
102
103
# File 'vulns/vulnerability.rb', line 101

def fix_urls
  references.select { |r| r["type"] == "FIX" }.filter_map { |r| r["url"] }
end

#fixed_versionsArray<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:



106
107
108
109
110
111
112
# File 'vulns/vulnerability.rb', line 106

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

#identifiersArray<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.

Returns:



86
87
88
# File 'vulns/vulnerability.rb', line 86

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.

Parameters:

Returns:

  • (Boolean)


211
212
213
214
215
216
217
218
219
220
221
# File 'vulns/vulnerability.rb', line 211

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_interval_permissive?(target, interval) ⇒ 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.

Parameters:

Returns:

  • (Boolean)


320
321
322
323
324
325
# File 'vulns/vulnerability.rb', line 320

def in_interval_permissive?(target, interval)
  in_interval_strict?(target, interval) != false
rescue => e
  odebug "Failed to check #{target} against interval #{interval.inspect} for #{id}: #{e}"
  true
end

#in_interval_strict?(target, interval) ⇒ 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.

Parameters:

Returns:

  • (Boolean, nil)


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'vulns/vulnerability.rb', line 303

def in_interval_strict?(target, interval)
  lower = interval.lower
  upper = interval.upper
  above = lower.nil? || semver_compare!(target, lower) >= 0
  below = if upper.nil?
    true
  elsif interval.upper_inclusive
    semver_compare!(target, upper) <= 0
  else
    semver_compare!(target, upper).negative?
  end
  above && below
rescue Uncomparable
  nil
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.

Parameters:

Returns:

  • (Boolean)


296
297
298
299
300
# File 'vulns/vulnerability.rb', line 296

def in_semver_range?(target, events)
  return false if events.empty?

  intervals(events).any? { |interval| in_interval_permissive?(target, interval) }
end

#intervals(events) ⇒ Array<Interval>

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.

Parameters:

Returns:



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'vulns/vulnerability.rb', line 336

def intervals(events)
  result = []
  lower = T.let(nil, T.nilable(String))

  events.each do |event|
    if (intro = event["introduced"])
      result << Interval.new(lower: lower, upper: nil, upper_inclusive: false, fix: nil) if lower
      intro = normalize_version(intro.to_s)
      lower = (intro == "0") ? nil : intro
    elsif (fixed = event["fixed"])
      fixed_norm = normalize_version(fixed.to_s)
      result << Interval.new(lower: lower, upper: fixed_norm, upper_inclusive: false, fix: fixed_norm)
      lower = nil
    elsif (last = event["last_affected"])
      last_norm = normalize_version(last.to_s)
      result << Interval.new(lower: lower, upper: last_norm, upper_inclusive: true, fix: nil)
      lower = nil
    elsif (limit = event["limit"])
      limit = limit.to_s
      upper = (limit == "*") ? nil : normalize_version(limit)
      result << Interval.new(lower: lower, upper: upper, upper_inclusive: false, fix: nil)
      lower = nil
    end
  end
  result << Interval.new(lower: lower, upper: nil, upper_inclusive: false, fix: nil) if lower || result.empty?
  result
end

#non_semver_fix_available_of_type?(target, range, fix_type) ⇒ 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.

Parameters:

Returns:

  • (Boolean)


365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'vulns/vulnerability.rb', line 365

def non_semver_fix_available_of_type?(target, range, fix_type)
  events = Array(range["events"])
  return false if events.empty?

  is_git = range["type"] == "GIT"
  range_intervals = intervals(events)
  uncomparable_seen = T.let(false, T::Boolean)
  range_intervals.each do |interval|
    result = in_interval_strict?(target, interval)
    if result.nil?
      uncomparable_seen = true
      next
    end

    next unless result

    fix = interval.fix
    return false if fix.blank?

    return (fix_type == :patch && is_git) || (fix_type == :released && !is_git) || fix_type == :any
  end

  return false unless uncomparable_seen

  last_event = events.last
  return false if last_event.nil?
  return false unless last_event.key?("fixed")

  fix = last_event["fixed"].to_s
  return false if fix.blank?

  (fix_type == :patch && is_git) || (fix_type == :released && !is_git) || fix_type == :any
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.

Parameters:

Returns:



278
279
280
281
282
283
284
285
# File 'vulns/vulnerability.rb', line 278

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.

Parameters:

Returns:



288
289
290
# File 'vulns/vulnerability.rb', line 288

def normalize_version(version)
  version.sub(/\Av/i, "")
end

#patch_fix_available?(version, repo_url = 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.

Parameters:

  • version (String)
  • repo_url (String, nil) (defaults to: nil)

Returns:

  • (Boolean)


429
430
431
# File 'vulns/vulnerability.rb', line 429

def patch_fix_available?(version, repo_url = nil)
  fix_available_of_type?(version, repo_url, :patch)
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:

  • SEMVER ranges compare with Semver.
  • ECOSYSTEM ranges compare with Version (best-effort; the record's own ecosystem defines the ordering, but callers only invoke this for ecosystems whose versions are broadly Version-comparable).
  • GIT ranges 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".

Parameters:

Returns:



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
# File 'vulns/vulnerability.rb', line 143

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 |interval|
        inside = in_interval?(target, interval.lower, interval.upper, interval.upper_inclusive, cmp)
        checked = true
        if inside
          fixed_in = interval.upper_inclusive ? nil : interval.upper
          return RangeStatus.new(state: :affected, fixed_in:).freeze
        end
        upper = interval.upper
        next unless upper

        rel = cmp.call(target, upper)
        past_fixes << upper if interval.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

#released_fix_available?(version, repo_url = 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.

Parameters:

  • version (String)
  • repo_url (String, nil) (defaults to: nil)

Returns:

  • (Boolean)


424
425
426
# File 'vulns/vulnerability.rb', line 424

def released_fix_available?(version, repo_url = nil)
  fix_available_of_type?(version, repo_url, :released)
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.

Parameters:

Returns:



328
329
330
# File 'vulns/vulnerability.rb', line 328

def semver_compare!(left, right)
  Semver.compare(left, right) || raise(Uncomparable, "cannot compare #{left.inspect} with #{right.inspect}")
end

#severity_displayString

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:



68
69
70
# File 'vulns/vulnerability.rb', line 68

def severity_display
  severity&.to_s&.upcase || "UNKNOWN"
end

#severity_levelInteger

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:



73
74
75
76
77
78
# File 'vulns/vulnerability.rb', line 73

def severity_level
  sev = severity
  return 0 if sev.nil?

  SEVERITY_LEVEL.fetch(sev, 0)
end