tap: define #allowed_by_env?

This commit is contained in:
Carlo Cabrera 2024-05-03 14:42:16 +01:00
parent 071dd93ef2
commit 6663516e79
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0
2 changed files with 37 additions and 26 deletions

View File

@ -1379,30 +1379,9 @@ on_request: installed_on_request?, options:)
EOS EOS
end end
sig { params(tap: Tap, allowed_taps: T::Set[Tap], forbidden_taps: T::Set[Tap]).returns(T::Boolean) }
def allowed_tap?(tap, allowed_taps, forbidden_taps)
(tap.official? || allowed_taps.blank? || allowed_taps.include?(tap)) && forbidden_taps.exclude?(tap)
end
sig { void } sig { void }
def forbidden_tap_check def forbidden_tap_check
forbidden_taps = Homebrew::EnvConfig.forbidden_taps.to_s.split return if Homebrew::EnvConfig.forbidden_taps.blank? && Homebrew::EnvConfig.allowed_taps.blank?
allowed_taps = Homebrew::EnvConfig.allowed_taps.to_s.split
return if forbidden_taps.blank? && allowed_taps.blank?
forbidden_taps_set = Set.new(forbidden_taps.filter_map do |tap|
Tap.fetch(tap)
rescue Tap::InvalidNameError
opoo "Invalid tap name in `HOMEBREW_FORBIDDEN_TAPS`: #{tap}"
nil
end)
allowed_taps_set = Set.new(allowed_taps.filter_map do |tap|
Tap.fetch(tap)
rescue Tap::InvalidNameError
opoo "Invalid tap name in `HOMEBREW_ALLOWED_TAPS`: #{tap}"
nil
end)
owner = Homebrew::EnvConfig.forbidden_owner owner = Homebrew::EnvConfig.forbidden_owner
owner_contact = if (contact = Homebrew::EnvConfig.forbidden_owner_contact.presence) owner_contact = if (contact = Homebrew::EnvConfig.forbidden_owner_contact.presence)
@ -1412,8 +1391,7 @@ on_request: installed_on_request?, options:)
unless ignore_deps? unless ignore_deps?
compute_dependencies.each do |(dep, _options)| compute_dependencies.each do |(dep, _options)|
dep_tap = dep.tap dep_tap = dep.tap
next if dep_tap.blank? next if dep_tap.blank? || dep_tap.allowed_by_env?
next if allowed_tap?(dep_tap, allowed_taps_set, forbidden_taps_set)
raise CannotInstallFormulaError, <<~EOS raise CannotInstallFormulaError, <<~EOS
The installation of #{formula.name} has a dependency #{dep.name} The installation of #{formula.name} has a dependency #{dep.name}
@ -1426,8 +1404,7 @@ on_request: installed_on_request?, options:)
return if only_deps? return if only_deps?
formula_tap = formula.tap formula_tap = formula.tap
return if formula_tap.blank? return if formula_tap.blank? || formula_tap.allowed_by_env?
return if allowed_tap?(formula_tap, allowed_taps_set, forbidden_taps_set)
raise CannotInstallFormulaError, <<~EOS raise CannotInstallFormulaError, <<~EOS
The installation of #{formula.full_name} has the tap #{formula_tap} The installation of #{formula.full_name} has the tap #{formula_tap}

View File

@ -132,6 +132,30 @@ class Tap
false false
end end
sig { returns(T::Set[Tap]) }
def self.allowed_taps
allowed_tap_list = Homebrew::EnvConfig.allowed_taps.to_s.split
Set.new(allowed_tap_list.filter_map do |tap|
Tap.fetch(tap)
rescue Tap::InvalidNameError
opoo "Invalid tap name in `HOMEBREW_ALLOWED_TAPS`: #{tap}"
nil
end)
end
sig { returns(T::Set[Tap]) }
def self.forbidden_taps
forbidden_tap_list = Homebrew::EnvConfig.forbidden_taps.to_s.split
Set.new(forbidden_tap_list.filter_map do |tap|
Tap.fetch(tap)
rescue Tap::InvalidNameError
opoo "Invalid tap name in `HOMEBREW_FORBIDDEN_TAPS`: #{tap}"
nil
end)
end
# @api public # @api public
extend Enumerable extend Enumerable
@ -1056,6 +1080,16 @@ class Tap
end end
end end
sig { returns(T::Boolean) }
def allowed_by_env?
@allowed_by_env ||= begin
allowed_taps = self.class.allowed_taps
forbidden_taps = self.class.forbidden_taps
(official? || allowed_taps.blank? || allowed_taps.include?(self)) && forbidden_taps.exclude?(self)
end
end
private private
sig { params(file: Pathname).returns(T.any(T::Array[String], Hash)) } sig { params(file: Pathname).returns(T.any(T::Array[String], Hash)) }