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) ⇒ Hash{Symbol => T.untyped}?
private
If a record already exists at
path, carry forward itspublishedtimestamp andaffected[].ranges(so thefixedboundary reflects when the annotation was first observed rather than drifting 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
- .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
-
.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.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'vulns/osv_export.rb', line 157 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.
215 216 217 218 219 |
# File 'vulns/osv_export.rb', line 215 def self.fetch_upstream(vuln_id) OSV.vulnerability(vuln_id) rescue OSV::Error :failed end |
.merge_existing(path, record) ⇒ 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 the fixed boundary reflects when
the annotation was first observed rather than drifting to today's
pkg_version), and skip the write entirely when nothing else has
changed. Records for annotations no longer in core are simply not
visited, so they persist.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'vulns/osv_export.rb', line 91 def self.merge_existing(path, record) 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") affected[:ranges] = existing_ranges if existing_ranges 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.
203 204 205 206 207 208 209 210 |
# File 'vulns/osv_export.rb', line 203 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.
193 194 195 196 197 198 |
# File 'vulns/osv_export.rb', line 193 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.
185 186 187 |
# File 'vulns/osv_export.rb', line 185 def self.purl(name) "pkg:brew/#{name.gsub(/[@+]/, PURL_NAME_ENCODE)}" 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.
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 |
# File 'vulns/osv_export.rb', line 121 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: "#{ID_PREFIX}-#{formula.name}-#{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 |
.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.
54 55 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 |
# File 'vulns/osv_export.rb', line 54 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, "#{ID_PREFIX}-#{formula.name}-#{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 |