Class: Homebrew::DevCmd::AdvisoryMatch::DirEmitter Private

Inherits:
Emitter show all
Defined in:
dev-cmd/advisory-match.rb

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.

Instance Method Summary collapse

Methods inherited from Emitter

#range_basis, #subject_identity, #unversioned_key

Constructor Details

#initialize(dir, verbose:, close_open_ranges:, reconcile_history: false, formula_names: nil) ⇒ 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:

  • dir (String)
  • verbose (Boolean)
  • close_open_ranges (Boolean)
  • reconcile_history (Boolean) (defaults to: false)
  • formula_names (Array<String>, nil) (defaults to: nil)


696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'dev-cmd/advisory-match.rb', line 696

def initialize(dir, verbose:, close_open_ranges:, reconcile_history: false, formula_names: nil)
  super()
  FileUtils.mkdir_p(dir)
  @dir = dir
  @verbose = verbose
  @close_open_ranges = close_open_ranges
  @reconcile_history = reconcile_history
  @formula_names = formula_names
  @deleted = T.let(0, Integer)
  @reconciliation_skips = T.let({}, T::Hash[Symbol, Integer])
  @reconciliation_seen = T.let({}, T::Hash[String, T::Boolean])
  @written = T.let(0, Integer)
  @unchanged = T.let(0, Integer)
  @skipped_generated = T.let(0, Integer)
  @basis_changed = T.let(0, Integer)
  @history_walks = T.let(0, Integer)
  @history_unavailable_by_formula = T.let({}, T::Hash[String, Integer])
  @alias_targets = T.let({}, T::Hash[String, T::Array[String]])
  @protected_aliases = T.let({}, T::Hash[String, T::Boolean])
  @alias_records = T.let({}, T::Hash[String, T.untyped])
  @identity_paths = T.let({}, T::Hash[String, T::Array[String]])
  @path_identities = T.let({}, T::Hash[String, T::Array[String]])
  @alias_index_loaded = T.let(false, T::Boolean)
end

Instance Method Details

#affected_formula_names(record) ⇒ 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.

Parameters:

Returns:



1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
# File 'dev-cmd/advisory-match.rb', line 1203

def affected_formula_names(record)
  Array(record["affected"]).filter_map do |entry|
    next unless entry.is_a?(Hash)

    package = entry["package"]
    next unless package.is_a?(Hash)
    next if package["ecosystem"] != Homebrew::Vulns::OsvExport::ECOSYSTEM

    package["name"]
  end.uniq
end

#alias_protected?(record_id) ⇒ 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)


819
820
821
# File 'dev-cmd/advisory-match.rb', line 819

def alias_protected?(record_id)
  @protected_aliases.fetch(record_id, false)
end

#alias_record(path) ⇒ 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:

  • (T.untyped)


1194
1195
1196
1197
1198
1199
1200
# File 'dev-cmd/advisory-match.rb', line 1194

def alias_record(path)
  return @alias_records[path] if @alias_records.key?(path)

  @alias_records[path] = JSON.parse(File.read(path))
rescue JSON::ParserError
  @alias_records[path] = :malformed
end

#alias_target_paths(record_id) ⇒ 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.

Parameters:

Returns:



1189
1190
1191
# File 'dev-cmd/advisory-match.rb', line 1189

def alias_target_paths(record_id)
  @alias_targets.fetch(record_id) { [record_path(record_id)] }
end

#emit(record, revalidated: false, initial_introduction: false) ⇒ 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.

This method returns an undefined value.

Parameters:

  • record (Hash{Symbol => T.untyped})
  • revalidated (Boolean) (defaults to: false)
  • initial_introduction (Boolean) (defaults to: false)


1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
# File 'dev-cmd/advisory-match.rb', line 1039

def emit(record, revalidated: false, initial_introduction: false)
  updates = alias_target_paths(record.fetch(:id)).filter_map do |path|
    candidate = record.deep_dup
    existing = alias_record(path) if File.file?(path)
    if existing.is_a?(Hash)
      candidate[:id] = existing.fetch("id")
      candidate[:upstream] = (Array(existing["upstream"]) + Array(candidate[:upstream])).uniq
      existing_basis = range_basis(existing)
      candidate_basis = range_basis(candidate)
      if existing_basis != candidate_basis
        ranges = JSON.parse(JSON.generate(candidate)).dig("affected", 0, "ranges")
        existing_ranges = existing.dig("affected", 0, "ranges")
        no_reviewed_ranges = Array(existing_ranges).none? do |range|
          Homebrew::Vulns::OsvExport.ranges_open?([range]) ||
            Homebrew::Vulns::OsvExport.ranges_terminal?([range])
        end
        compatible = ranges == existing_ranges || (@close_open_ranges && no_reviewed_ranges)
        if !compatible || (!revalidated && !Homebrew::Vulns::OsvExport.ranges_open?(ranges))
          fields = (existing_basis.keys | candidate_basis.keys).reject do |key|
            existing_basis[key] == candidate_basis[key]
          end
          Utils::Output.onoe "#{candidate[:id]}: reviewed range basis changed (#{fields.join(", ")}); " \
                             "leaving it unchanged.\n" \
                             "Run advisory-match for this formula with --json and history enabled,\n" \
                             "then review and update its ranges and provenance together."
          @basis_changed += 1
          Homebrew.failed = true
          next
        end
      end
    elsif File.file?(path)
      candidate[:id] = File.basename(path, ".json")
    end
    merged = Homebrew::Vulns::OsvExport.merge_existing(
      path, candidate, close_open_ranges: @close_open_ranges, initial_introduction:
    )
    [path, merged]
  end

  updates.each do |path, merged|
    if merged.nil?
      @unchanged += 1
      next
    end
    File.write(path, "#{JSON.pretty_generate(merged)}\n")
    puts "  wrote #{path}" if @verbose
    @written += 1
  end
end

#ensure_alias_index ⇒ 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.

This method returns an undefined value.



1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
# File 'dev-cmd/advisory-match.rb', line 1216

def ensure_alias_index
  return if @alias_index_loaded

  Dir.glob(File.join(@dir, "BREW-*.json")).each do |path|
    existing = alias_record(path)
    next unless existing.is_a?(Hash)

    formula_names = affected_formula_names(existing)
    next unless formula_names.one?

    identities = Array(existing["upstream"]).grep(String).map do |id|
      "BREW-#{formula_names.fetch(0)}-#{id}"
    end.uniq
    @path_identities[path] = identities
    identities.each { |identity| (@identity_paths[identity] ||= []) << path }
  end
  @alias_index_loaded = true
end

#finish ⇒ 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.

This method returns an undefined value.



1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
# File 'dev-cmd/advisory-match.rb', line 1290

def finish
  history_unavailable = @history_unavailable_by_formula.values.sum
  Utils::Output.ohai "#{@written} records written to #{@dir} " \
                     "(#{@unchanged} unchanged, #{@skipped_generated} generated left as-is, " \
                     "#{@history_walks} history walks, #{history_unavailable} history-unavailable skips, " \
                     "#{@basis_changed} range-basis skips)"
  if @reconcile_history
    ensure_alias_index
    formula_names = @formula_names
    unmatched = @alias_records.count do |path, record|
      next false unless record.is_a?(Hash)
      next false if formula_names&.exclude?(record.dig("affected", 0, "package", "name"))

      record.dig("database_specific", "source") == "matched" && !@reconciliation_seen[path]
    end
    puts "  Reconciliation: #{@deleted} deleted; #{unmatched} matched records not revisited"
    @reconciliation_skips.sort.each { |reason, count| puts "    #{reason}: #{count}" }
  end
  return if @history_unavailable_by_formula.empty?

  puts "  Unavailable history by formula:"
  @history_unavailable_by_formula.sort.each { |formula, count| puts "    #{formula}: #{count}" }
end

#fixed_boundary_valid?(record_id, boundary) ⇒ 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)


1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
# File 'dev-cmd/advisory-match.rb', line 1109

def fixed_boundary_valid?(record_id, boundary)
  alias_target_paths(record_id).all? do |path|
    next true unless File.file?(path)

    existing = alias_record(path)
    next false unless existing.is_a?(Hash)

    affected = existing["affected"]
    next false unless affected.is_a?(Array)

    affected.all? do |entry|
      entry.is_a?(Hash) &&
        Homebrew::Vulns::OsvExport.fixed_follows?(homebrew_ranges(entry["ranges"]), boundary)
    end
  end
end

#history_required?(record_id) ⇒ 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)


1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
# File 'dev-cmd/advisory-match.rb', line 1090

def history_required?(record_id)
  alias_target_paths(record_id).any? do |path|
    next true unless File.file?(path)

    existing = alias_record(path)
    next true unless existing.is_a?(Hash)

    affected = existing["affected"]
    next true unless affected.is_a?(Array)
    next true if affected.empty?

    affected.any? do |entry|
      !entry.is_a?(Hash) ||
        !Homebrew::Vulns::OsvExport.ranges_terminal?(homebrew_ranges(entry["ranges"]))
    end
  end
end

#homebrew_ranges(ranges) ⇒ Array<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:

  • ranges (T.untyped)

Returns:



1283
1284
1285
1286
1287
# File 'dev-cmd/advisory-match.rb', line 1283

def homebrew_ranges(ranges)
  Array(ranges).select do |range|
    range.is_a?(Hash) && range["type"] == "ECOSYSTEM"
  end
end

#matching_alias_paths(record_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.

Parameters:

Returns:



1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
# File 'dev-cmd/advisory-match.rb', line 1236

def matching_alias_paths(record_ids)
  pending = record_ids.dup
  identities = T.let({}, T::Hash[String, T::Boolean])
  paths = T.let({}, T::Hash[String, T::Boolean])
  until pending.empty?
    identity = pending.shift
    next if identity.nil? || identities[identity]

    identities[identity] = true
    direct = record_path(identity)
    linked = [direct, *@identity_paths.fetch(identity, [])]
    linked.each do |path|
      next unless File.file?(path)
      next if paths[path]

      paths[path] = true
      pending.concat(@path_identities.fetch(path, []))
    end
  end
  paths.keys.sort
end

#prepare_aliases(formula_name, groups) ⇒ 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.

Parameters:

Returns:



725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'dev-cmd/advisory-match.rb', line 725

def prepare_aliases(formula_name, groups)
  ensure_alias_index
  errors = T.let([], T::Array[String])
  targets = T.let({}, T::Hash[String, T::Array[String]])
  protected = T.let({}, T::Hash[String, T::Boolean])
  owners = T.let({}, T::Hash[String, String])
  identity_owners = T.let({}, T::Hash[String, String])
  generated_paths = T.let([], T::Array[String])

  groups.each do |canonical_id, record_ids|
    record_ids.each do |record_id|
      if (owner = identity_owners[record_id]) && owner != canonical_id
        errors << "#{canonical_id}: identity also belongs to #{owner}; leaving both unchanged"
      else
        identity_owners[record_id] = canonical_id
      end
    end
    paths = matching_alias_paths(record_ids)
    paths.each do |path|
      if (owner = owners[path]) && owner != canonical_id
        errors << "#{canonical_id}: alias family also belongs to #{owner}; leaving both unchanged"
      else
        owners[path] = canonical_id
      end
    end

    writable = T.let([], T::Array[String])
    generated = T.let([], T::Array[String])
    paths.each do |path|
      existing = alias_record(path)
      if existing == :malformed
        errors << "#{canonical_id}: malformed alias #{path}; leaving family unchanged"
        next
      end
      unless existing.is_a?(Hash)
        errors << "#{canonical_id}: invalid alias #{path}; leaving family unchanged"
        next
      end
      if existing["id"] != File.basename(path, ".json")
        errors << "#{canonical_id}: #{path} has a mismatched id; leaving family unchanged"
        next
      end

      affected = existing["affected"]
      names = affected_formula_names(existing)
      if !affected.is_a?(Array) || !affected.one? || names != [formula_name]
        errors << "#{canonical_id}: #{path} has unsupported affected entries for " \
                  "#{formula_name}; leaving family unchanged"
        next
      end

      database_specific = existing["database_specific"]
      source = database_specific["source"] if database_specific.is_a?(Hash)
      if source == "generated"
        generated << path
        next
      end
      if source != "matched"
        errors << "#{canonical_id}: #{path} has unsupported source " \
                  "#{source.inspect}; leaving family unchanged"
        next
      end

      writable << path
    end

    if generated.any?
      targets[canonical_id] = []
      protected[canonical_id] = true
      generated_paths.concat(generated)
      next
    end
    if writable.length > 1
      errors << "#{canonical_id}: multiple alias records found; consolidate the family before matching"
      next
    end

    canonical_path = record_path(canonical_id)
    targets[canonical_id] = if writable.one?
      writable
    else
      [canonical_path]
    end
    protected[canonical_id] = false
  end
  return errors.uniq if errors.any?

  @alias_targets.merge!(targets)
  @protected_aliases.merge!(protected)
  @skipped_generated += generated_paths.uniq.length
  []
end

#provenance_matches?(existing, record) ⇒ 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.

The stored record must still be reached through the same upstream identifiers with evidence and a range basis the candidate reproduces.

Parameters:

Returns:

  • (Boolean)


949
950
951
952
953
954
955
956
# File 'dev-cmd/advisory-match.rb', line 949

def provenance_matches?(existing, record)
  upstream = existing["upstream"]
  return false if !upstream.is_a?(Array) || upstream.empty?
  return false if (upstream - record.fetch(:upstream)).any?
  return false if Array(existing.dig("database_specific", "upstream_evidence")).empty?

  range_basis(existing) == range_basis(record)
end

#range_basis_changed?(record) ⇒ 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)


1027
1028
1029
1030
1031
1032
1033
1034
# File 'dev-cmd/advisory-match.rb', line 1027

def range_basis_changed?(record)
  alias_target_paths(record.fetch(:id)).any? do |path|
    next false unless File.file?(path)

    existing = alias_record(path)
    existing.is_a?(Hash) && range_basis(existing) != range_basis(record)
  end
end

#reconcilable_record?(existing) ⇒ 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.

Only matched, bump-fixed records are reconciled; generated patch fixes and withdrawn records keep their annotation-based ranges.

Parameters:

  • existing (T.untyped)

Returns:

  • (Boolean)


899
900
901
902
903
904
# File 'dev-cmd/advisory-match.rb', line 899

def reconcilable_record?(existing)
  existing.is_a?(Hash) &&
    existing.dig("database_specific", "source") == "matched" &&
    existing["withdrawn"].blank? &&
    existing.dig("affected", 0, "ecosystem_specific", "fix") == "bump"
end

#reconcile(record, result) ⇒ 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.

This method returns an undefined value.

Parameters:



965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'dev-cmd/advisory-match.rb', line 965

def reconcile(record, result)
  existing = reconciliation_record(record)
  return unless existing

  id = record.fetch(:id)
  path = alias_target_paths(id).fetch(0)
  updated = existing.deep_dup
  if result.state == :range
    if result.intervals.empty? || result.intervals.any? { |interval| !interval.valid? }
      skip_reconciliation(id, [:invalid_interval])
      return
    end
    if result.intervals.any? { |interval| interval.fixed.nil? }
      skip_reconciliation(id, [:reopened])
      return
    end
    reviewed = terminal_intervals(existing)
    return unless reviewed

    if result.intervals.any? do |interval|
      reviewed.none? do |old|
        fixed = interval.fixed
        old_fixed = old.fixed
        fixed && old_fixed && PkgVersion.parse(interval.introduced) >= PkgVersion.parse(old.introduced) &&
        PkgVersion.parse(fixed) <= PkgVersion.parse(old_fixed)
      end
    end
      skip_reconciliation(id, [:range_expansion])
      return
    end
    if result.events == reviewed.flat_map(&:events)
      @unchanged += 1
      return
    end
    updated.fetch("affected").fetch(0)["ranges"] = [
      { "type" => "ECOSYSTEM", "events" => result.events },
    ]
    updated["modified"] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
  elsif result.state != :never_affected
    skip_reconciliation(id, result.reasons.presence || [:unsupported_result])
    return
  end

  # A full walk can be slow. Do not overwrite an intervening review edit.
  if !File.file?(path) || JSON.parse(File.read(path)) != existing
    skip_reconciliation(id, [:record_changed])
    return
  end
  if result.state == :never_affected
    File.unlink(path)
    @deleted += 1
    puts "  deleted #{path}" if @verbose
  else
    File.write(path, "#{JSON.pretty_generate(updated)}\n")
    @written += 1
    puts "  reconciled #{path}" if @verbose
  end
rescue JSON::ParserError
  skip_reconciliation(record.fetch(:id), [:record_changed])
end

#reconciliation_record(record) ⇒ 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.

Reconciliation never creates a record or changes its matching provenance.

Parameters:

Returns:



864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
# File 'dev-cmd/advisory-match.rb', line 864

def reconciliation_record(record)
  id = record.fetch(:id)
  if alias_protected?(id)
    skip_reconciliation(id, [:alias_protected])
    return
  end

  paths = alias_target_paths(id)
  unless paths.one?
    skip_reconciliation(id, [:ambiguous_alias_paths])
    return
  end
  return unless File.file?(paths.fetch(0))

  path = paths.fetch(0)
  @reconciliation_seen[path] = true
  existing = alias_record(path)
  unless reconcilable_record?(existing)
    skip_reconciliation(id, [:unsupported_record])
    return
  end
  unless terminal_intervals(existing)
    skip_reconciliation(id, [:unsupported_range])
    return
  end
  unless provenance_matches?(existing, record)
    skip_reconciliation(id, [:provenance_changed])
    return
  end
  existing
end

#reconciliation_resource_records(formula_name) ⇒ Array<Homebrew::Vulns::Match::ResourceRecord>

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 pure registry-resource records can supply fallback query hints. Mixed or incomplete provenance retains the ordinary discovery guard.

Parameters:

Returns:



826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# File 'dev-cmd/advisory-match.rb', line 826

def reconciliation_resource_records(formula_name)
  fields = %w[ecosystem name resource subject_version key]
  Dir.glob(File.join(@dir, "BREW-#{formula_name}-*.json")).filter_map do |path|
    record = alias_record(path)
    next unless record.is_a?(Hash)
    next unless reconcilable_record?(record)
    next unless terminal_intervals(record)
    next if affected_formula_names(record) != [formula_name]
    next if record["id"] != File.basename(path, ".json")

    upstream = record["upstream"]
    next unless upstream.is_a?(Array)
    next if upstream.empty? || !upstream.all?(String)

    rows = record.dig("database_specific", "upstream_evidence")
    next unless rows.is_a?(Array)
    next if rows.empty?

    evidence = rows.filter_map do |row|
      next unless row.is_a?(Hash)
      next if row["strategy"] != "registry"
      next unless fields.all? do |field|
        row[field].is_a?(String) && row[field].present?
      end

      Homebrew::Vulns::Match::Evidence.new(
        strategy: :registry, ecosystem: row.fetch("ecosystem"), name: row.fetch("name"),
        resource: row.fetch("resource"), subject_version: row.fetch("subject_version"), key: row.fetch("key")
      ).freeze
    end
    next if evidence.length != rows.length

    Homebrew::Vulns::Match::ResourceRecord.new(id: record.fetch("id"), upstream:, evidence: evidence.uniq)
  end
end

#record_history_unavailable(formula_name) ⇒ 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.

This method returns an undefined value.

Parameters:



1165
1166
1167
1168
# File 'dev-cmd/advisory-match.rb', line 1165

def record_history_unavailable(formula_name)
  count = @history_unavailable_by_formula.fetch(formula_name, 0)
  @history_unavailable_by_formula[formula_name] = count + 1
end

#record_history_walk ⇒ 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.

This method returns an undefined value.



1159
1160
1161
1162
# File 'dev-cmd/advisory-match.rb', line 1159

def record_history_walk
  @history_walks += 1
  puts "  #{@history_walks} history walks" if @verbose && (@history_walks % 100).zero?
end

#record_path(record_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.

Parameters:

Returns:



1184
1185
1186
# File 'dev-cmd/advisory-match.rb', line 1184

def record_path(record_id)
  File.join(@dir, "#{record_id}.json")
end

#record_upstream_unavailable(formula_name) ⇒ 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.

This method returns an undefined value.

Parameters:



1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
# File 'dev-cmd/advisory-match.rb', line 1171

def record_upstream_unavailable(formula_name)
  ensure_alias_index
  @alias_records.each do |path, record|
    next unless record.is_a?(Hash)
    next if record.dig("database_specific", "source") != "matched"
    next unless affected_formula_names(record).include?(formula_name)

    @reconciliation_seen[path] = true
    skip_reconciliation(record.fetch("id"), [:upstream_unavailable])
  end
end

#reintroduction_boundary_valid?(record_id, boundary) ⇒ 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)


1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
# File 'dev-cmd/advisory-match.rb', line 1139

def reintroduction_boundary_valid?(record_id, boundary)
  alias_target_paths(record_id).all? do |path|
    next true unless File.file?(path)

    existing = alias_record(path)
    next false unless existing.is_a?(Hash)

    affected = existing["affected"]
    next false unless affected.is_a?(Array)

    affected.all? do |entry|
      next false unless entry.is_a?(Hash)

      ranges = homebrew_ranges(entry["ranges"])
      Homebrew::Vulns::OsvExport.reintroduction_follows?(ranges, boundary)
    end
  end
end

#reviewed_range_state(record_id) ⇒ 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:



1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'dev-cmd/advisory-match.rb', line 1127

def reviewed_range_state(record_id)
  paths = alias_target_paths(record_id).select { |path| File.file?(path) }
  states_by_path = paths.map { |path| reviewed_states(path) }
  return if states_by_path.any?(&:nil?)

  states = states_by_path.flatten
  return if states.empty?

  states.fetch(0)
end

#reviewed_states(path) ⇒ Array<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:



1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
# File 'dev-cmd/advisory-match.rb', line 1259

def reviewed_states(path)
  existing = alias_record(path)
  return unless existing.is_a?(Hash)

  affected = existing["affected"]
  return unless affected.is_a?(Array)
  return if affected.empty?

  states = affected.filter_map do |entry|
    next unless entry.is_a?(Hash)

    ranges = homebrew_ranges(entry["ranges"])
    if Homebrew::Vulns::OsvExport.ranges_terminal?(ranges)
      :terminal
    elsif Homebrew::Vulns::OsvExport.ranges_open?(ranges)
      :open
    end
  end
  return if states.length != affected.length

  states
end

#skip_reconciliation(record_id, reasons) ⇒ 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.

This method returns an undefined value.

Parameters:



959
960
961
962
# File 'dev-cmd/advisory-match.rb', line 959

def skip_reconciliation(record_id, reasons)
  reasons.each { |reason| @reconciliation_skips[reason] = @reconciliation_skips.fetch(reason, 0) + 1 }
  Utils::Output.opoo "#{record_id}: #{reasons.join(", ")}; leaving it unchanged" if @verbose
end

#terminal_intervals(existing) ⇒ Array<Homebrew::Vulns::Match::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.

Only complete introduced/fixed pairs can be automatically narrowed. Other event types and range metadata remain protected for review.

Parameters:

Returns:



912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
# File 'dev-cmd/advisory-match.rb', line 912

def terminal_intervals(existing)
  ranges = existing.dig("affected", 0, "ranges")
  return if !ranges.is_a?(Array) || ranges.empty?

  intervals = T.let([], T::Array[Homebrew::Vulns::Match::Interval])
  ranges.each do |range|
    return nil if !range.is_a?(Hash) || range["type"] != "ECOSYSTEM"
    return nil if (range.keys - %w[type events]).any?

    events = range["events"]
    return nil if !events.is_a?(Array) || events.empty? || events.length.odd?

    previous_fixed = T.let(nil, T.nilable(String))
    events.each_slice(2) do |introduced, fixed|
      return nil if !introduced.is_a?(Hash) || introduced.keys != ["introduced"]
      return nil if !fixed.is_a?(Hash) || fixed.keys != ["fixed"]

      first = introduced["introduced"]
      last = fixed["fixed"]
      return nil if !first.is_a?(String) || !last.is_a?(String)

      interval = Homebrew::Vulns::Match::Interval.new(introduced: first, fixed: last)
      return nil unless interval.valid?
      return nil if previous_fixed && PkgVersion.parse(first) < PkgVersion.parse(previous_fixed)

      intervals << interval
      previous_fixed = last
    end
  end
  Homebrew::Vulns::Match::Interval.union(intervals)
rescue ArgumentError
  nil
end