Module: Homebrew::Vulns::OsvExport Private
- Defined in:
- vulns/osv_export.rb
Overview
This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.
Emits OSV-schema records for the Homebrew ecosystem describing CVEs that
homebrew-core formulae resolve via shipped patches.
One record is written per (formula, vulnerability id) pair found in
serialized_patches[].resolves. The record states that the formula was
affected up to (but not including) the currently shipped version+revision;
this is a "fixed at or before what we ship today" approximation, since the
precise fix boundary requires homebrew-core git archaeology.
Record shape follows the OSV 1.7 schema and mirrors the Debian DSA layout
(upstream listing the source CVE, affected[].ranges of type
ECOSYSTEM, ecosystem_specific carrying the resolving patch detail).
See DevCmd::GenerateVulnsAdvisories for the entry point and https://github.com/Homebrew/advisory-database for the published feed.
Constant Summary collapse
- SCHEMA_VERSION =
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.
https://ossf.github.io/osv-schema/ — value of the emitted
schema_versionfield, pinning the OSV schema release these records target.HomebrewandBREWwere registered in that schema in ossf/osv-schema#576. "1.7.3"- ECOSYSTEM =
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.
"Homebrew"- ID_PREFIX =
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.
"BREW"- PatchRef =
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.
T.type_alias { T::Hash[Symbol, T.any(String, T::Array[String])] }
Class Method Summary collapse
- .affected_entry(formula, vuln_id, patches, fixed) ⇒ Hash{Symbol => T.untyped} private
-
.fetch_upstream(vuln_id) ⇒ Hash{String => T.untyped}, Symbol
private
Returns
:failed(notnil) on error so callers can distinguish a transient outage from a successful fetch that returned no enrichment. -
.fixed_follows?(ranges, fixed) ⇒ Boolean
private
True when
fixedcan close every reviewed open Homebrew ecosystem range. -
.merge_existing(path, record, close_open_ranges: false, initial_introduction: false) ⇒ Hash{Symbol => T.untyped}?
private
If a record already exists at
path, carry forward itspublishedtimestamp andaffected[].ranges(so a terminal boundary does not drift to today'spkg_version), and skip the write entirely when nothing else has changed. - .patch_ref(patch) ⇒ PatchRef? private
- .patches_resolving(serialized_patches, vuln_id) ⇒ Array<Hash{String => T.untyped}> private
- .purl(name) ⇒ String private
- .ranges_open?(ranges) ⇒ Boolean private
-
.ranges_terminal?(ranges) ⇒ Boolean
private
True only when every range has a terminal
fixed,last_affected, orlimitevent. - .record_for(formula, vuln_id, patches: formula.serialized_patches, fixed: formula.pkg_version.to_s, upstream: nil, now: Time.now.utc) ⇒ Hash{Symbol => T.untyped} private
- .record_id(formula, vuln_id) ⇒ String private
-
.reintroduction_follows?(ranges, introduced) ⇒ Boolean
private
True when
introducedis compatible with every reviewed Homebrew ecosystem range. -
.run(annotated, dir, first_fixed: nil, now: Time.now.utc) ⇒ Array<String>
private
annotatedis a list of[formula, serialized_patches]pairs.
Class Method Details
.affected_entry(formula, vuln_id, patches, fixed) ⇒ Hash{Symbol => 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.
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'vulns/osv_export.rb', line 413 def self.affected_entry(formula, vuln_id, patches, fixed) { package: { ecosystem: ECOSYSTEM, name: formula.name, purl: purl(formula.name), }, ranges: [ { type: "ECOSYSTEM", events: [{ introduced: "0" }, { fixed: }], }, ], ecosystem_specific: { fix: "patch", patches: patches_resolving(patches, vuln_id).filter_map { |p| patch_ref(p) }, }, } end |
.fetch_upstream(vuln_id) ⇒ Hash{String => T.untyped}, 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.
Returns :failed (not nil) on error so callers can distinguish a
transient outage from a successful fetch that returned no enrichment.
471 472 473 474 475 |
# File 'vulns/osv_export.rb', line 471 def self.fetch_upstream(vuln_id) OSV.vulnerability(vuln_id) rescue OSV::Error :failed end |
.fixed_follows?(ranges, fixed) ⇒ 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.
True when fixed can close every reviewed open Homebrew ecosystem
range. Terminal ranges are unchanged; invalid ranges remain eligible
for the existing repair path.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'vulns/osv_export.rb', line 180 def self.fixed_follows?(ranges, fixed) fixed_version = PkgVersion.parse(fixed) Array(ranges).all? do |range| next true unless range.is_a?(Hash) next true if (range["type"] || range[:type]) != "ECOSYSTEM" next true if range_state(range) != :open event = Array(range["events"] || range[:events]).rfind do |item| item.is_a?(Hash) && (item.key?("introduced") || item.key?(:introduced)) end introduced = event&.[]("introduced") || event&.[](:introduced) introduced.is_a?(String) && fixed_version > PkgVersion.parse(introduced) rescue ArgumentError false end rescue ArgumentError false end |
.merge_existing(path, record, close_open_ranges: false, initial_introduction: false) ⇒ Hash{Symbol => 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.
If a record already exists at path, carry forward its published
timestamp and affected[].ranges (so a terminal boundary does not
drift to today's pkg_version), and skip the write entirely when
nothing else has changed. close_open_ranges lets the matcher merge a
newly discovered fixed or reintroduced event into an existing range
while preserving its reviewed history. initial_introduction repairs
unreviewed ranges without reopening valid terminal ranges. Records for
annotations no longer in core are simply not visited, so they persist.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'vulns/osv_export.rb', line 112 def self.merge_existing(path, record, close_open_ranges: false, initial_introduction: false) return record unless File.file?(path) existing = JSON.parse(File.read(path)) # Records written before `published` was introduced only have # `modified`; use it as the migration value so `published` does not # jump forward to today on first rewrite. if (existing_published = existing["published"] || existing["modified"]) record[:published] = existing_published end affected_records = Array(record[:affected]) invalid_transition = affected_records.each_with_index.any? do |affected, index| existing_ranges = existing.dig("affected", index, "ranges") next false unless existing_ranges close_open_ranges && ( ((fixed = explicit_fixed(affected[:ranges])) && !fixed_follows?(existing_ranges, fixed)) || (!initial_introduction && (introduced = explicit_reintroduction(affected[:ranges])) && !reintroduction_follows?(existing_ranges, introduced)) ) end return if invalid_transition affected_records.each_with_index do |affected, index| existing_ranges = existing.dig("affected", index, "ranges") next unless existing_ranges affected[:ranges] = if close_open_ranges merge_range_transitions(existing_ranges, affected[:ranges], initial_introduction:) else existing_ranges end end # Compare as parsed structures so key ordering (which JSON does not # define but Ruby serialisation preserves) does not cause spurious # rewrites of a hand-formatted or differently-serialised existing file. return if JSON.parse(JSON.generate(record)).except("modified") == existing.except("modified") record rescue JSON::ParserError record end |
.patch_ref(patch) ⇒ PatchRef?
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.
459 460 461 462 463 464 465 466 |
# File 'vulns/osv_export.rb', line 459 def self.patch_ref(patch) ref = T.let({}, PatchRef) ref[:type] = patch["type"] if patch["type"] ref[:url] = patch["url"] if patch["url"] ref[:file] = patch["file"] if patch["file"] ref[:apply] = patch["apply"] if patch["apply"] ref.presence end |
.patches_resolving(serialized_patches, vuln_id) ⇒ 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.
449 450 451 452 453 454 |
# File 'vulns/osv_export.rb', line 449 def self.patches_resolving(serialized_patches, vuln_id) target = vuln_id.upcase serialized_patches.select do |p| Array(p["resolves"]).any? { |r| r.is_a?(Hash) && r["type"] == "security" && r["id"].to_s.upcase == target } end end |
.purl(name) ⇒ 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.
441 442 443 |
# File 'vulns/osv_export.rb', line 441 def self.purl(name) "pkg:brew/#{name.gsub(/[@+]/, PURL_NAME_ENCODE)}" end |
.ranges_open?(ranges) ⇒ 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.
168 169 170 171 172 173 174 |
# File 'vulns/osv_export.rb', line 168 def self.ranges_open?(ranges) return false unless ranges.is_a?(Array) return false if ranges.empty? states = ranges.map { |range| range_state(range) } states.exclude?(:invalid) && states.include?(:open) end |
.ranges_terminal?(ranges) ⇒ 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.
True only when every range has a terminal fixed, last_affected, or
limit event. Invalid/empty ranges deliberately return false so the
selective matcher repairs them instead of treating them as reviewed.
160 161 162 163 164 165 |
# File 'vulns/osv_export.rb', line 160 def self.ranges_terminal?(ranges) return false unless ranges.is_a?(Array) return false if ranges.empty? !!ranges.all? { |range| range_state(range) == :terminal } end |
.record_for(formula, vuln_id, patches: formula.serialized_patches, fixed: formula.pkg_version.to_s, upstream: nil, now: Time.now.utc) ⇒ Hash{Symbol => 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.
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 398 399 400 401 402 |
# File 'vulns/osv_export.rb', line 372 def self.record_for(formula, vuln_id, patches: formula.serialized_patches, fixed: formula.pkg_version.to_s, upstream: nil, now: Time.now.utc) = now.strftime("%Y-%m-%dT%H:%M:%SZ") record = T.let({ schema_version: SCHEMA_VERSION, id: record_id(formula, vuln_id), published: , modified: , upstream: [vuln_id], affected: [affected_entry(formula, vuln_id, patches, fixed)], database_specific: { source: "generated" }, }, T::Hash[Symbol, T.untyped]) if upstream record[:summary] = upstream["summary"] if upstream["summary"] record[:details] = upstream["details"] if upstream["details"] record[:severity] = upstream["severity"] if upstream["severity"] record[:upstream] = ([vuln_id] + Array(upstream["aliases"])).uniq if (refs = upstream["references"]) # OSV.dev merges NVD and cve.org reference lists without normalising # percent-encoding, so the same URL can appear twice (e.g. `%40` vs # `@`). Collapse those while keeping the same URL under distinct # `type` values, which the schema allows and which carries meaning. record[:references] = refs.uniq do |r| [r["type"], URI::RFC2396_PARSER.unescape(r["url"].to_s)] end end end record end |
.record_id(formula, vuln_id) ⇒ 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.
405 406 407 |
# File 'vulns/osv_export.rb', line 405 def self.record_id(formula, vuln_id) "#{ID_PREFIX}-#{formula.name}-#{vuln_id}" end |
.reintroduction_follows?(ranges, introduced) ⇒ 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.
True when introduced is compatible with every reviewed Homebrew
ecosystem range. Open ranges are already affected and remain unchanged;
terminal ranges must end before the new boundary. Other OSV range types
(notably GIT commit ranges) are ignored.
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 235 236 237 238 |
# File 'vulns/osv_export.rb', line 204 def self.reintroduction_follows?(ranges, introduced) ecosystem_ranges = Array(ranges).select do |range| range.is_a?(Hash) && (range["type"] || range[:type]) == "ECOSYSTEM" end return false if ecosystem_ranges.empty? introduced_version = PkgVersion.parse(introduced) ecosystem_ranges.all? do |range| state = range_state(range) next true if state == :open next false if state != :terminal terminal = terminal_event(range) next false unless terminal terminal_key = TERMINAL_EVENT_KEYS.find do |key| terminal.key?(key) || terminal.key?(key.to_sym) end next false unless terminal_key terminal_version = terminal[terminal_key] || terminal[terminal_key.to_sym] next false unless terminal_version.is_a?(String) terminal_version = PkgVersion.parse(terminal_version) if terminal_key == "limit" introduced_version >= terminal_version else introduced_version > terminal_version end rescue ArgumentError false end rescue ArgumentError false end |
.run(annotated, dir, first_fixed: nil, now: Time.now.utc) ⇒ 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.
annotated is a list of [formula, serialized_patches] pairs. The
patches are passed in rather than read from the formula so callers can
supply the union across OS/architecture variations (a patch inside an
on_linux/on_intel block only appears in Formula#serialized_patches
under the matching SimulateSystem).
first_fixed, when given, is called (formula, vuln_id) -> String? for
records with no existing file to derive an accurate fixed boundary
(e.g. via FormulaVersions git history). It may return
:history_unavailable to skip a new record when no boundary can be
verified; existing records preserve their on-disk ranges regardless.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'vulns/osv_export.rb', line 60 def self.run(annotated, dir, first_fixed: nil, now: Time.now.utc) FileUtils.mkdir_p(dir) written = [] upstream_cache = T.let({}, T::Hash[String, T.any(T::Hash[String, T.untyped], Symbol)]) annotated.each do |formula, patches| Scanner.resolved_ids(patches).each do |vuln_id| upstream = upstream_cache.fetch(vuln_id) { upstream_cache[vuln_id] = fetch_upstream(vuln_id) } path = File.join(dir, "#{record_id(formula, vuln_id)}.json") existing = File.file?(path) # A transient OSV outage would otherwise strip summary/severity/etc. # from an existing enriched record; leave it untouched instead. next if upstream == :failed && existing fixed_result = T.let(nil, T.nilable(T.any(String, Symbol))) fixed_result = first_fixed.call(formula, vuln_id) if first_fixed && !existing fixed = case fixed_result when String fixed_result when nil formula.pkg_version.to_s when :history_unavailable next else raise TypeError, "unexpected first-fixed result: #{fixed_result.inspect}" end record = record_for(formula, vuln_id, patches:, fixed:, upstream: upstream.is_a?(Hash) ? upstream : nil, now:) merged = merge_existing(path, record) next if merged.nil? File.write(path, "#{JSON.pretty_generate(merged)}\n") written << path end end written end |