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. -
.merge_existing(path, record, close_open_ranges: 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_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
-
.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.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'vulns/osv_export.rb', line 235 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.
293 294 295 296 297 |
# File 'vulns/osv_export.rb', line 293 def self.fetch_upstream(vuln_id) OSV.vulnerability(vuln_id) rescue OSV::Error :failed end |
.merge_existing(path, record, close_open_ranges: 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 append a
newly discovered fixed event to an existing open range while
preserving its reviewed introduction events. Records for annotations
no longer in core are simply not visited, so they persist.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'vulns/osv_export.rb', line 95 def self.merge_existing(path, record, close_open_ranges: 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 Array(record[:affected]).each_with_index do |affected, index| existing_ranges = existing.dig("affected", index, "ranges") next unless existing_ranges affected[:ranges] = if close_open_ranges merge_open_ranges(existing_ranges, affected[:ranges]) 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.
281 282 283 284 285 286 287 288 |
# File 'vulns/osv_export.rb', line 281 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.
271 272 273 274 275 276 |
# File 'vulns/osv_export.rb', line 271 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.
263 264 265 |
# File 'vulns/osv_export.rb', line 263 def self.purl(name) "pkg:brew/#{name.gsub(/[@+]/, PURL_NAME_ENCODE)}" 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.
130 131 132 133 134 135 |
# File 'vulns/osv_export.rb', line 130 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.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'vulns/osv_export.rb', line 194 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.
227 228 229 |
# File 'vulns/osv_export.rb', line 227 def self.record_id(formula, vuln_id) "#{ID_PREFIX}-#{formula.name}-#{vuln_id}" 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); existing records preserve
their on-disk ranges regardless.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'vulns/osv_export.rb', line 56 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 = (first_fixed&.call(formula, vuln_id) unless existing) || formula.pkg_version.to_s 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 |