Add sanity check for cask token

This commit is contained in:
apainintheneck 2023-01-30 21:56:40 -08:00
parent 4b3d64d307
commit 4917a35413

View File

@ -10,18 +10,25 @@ module Homebrew
class << self class << self
extend T::Sig extend T::Sig
CASK_TOKEN_REGEX = %r{^(homebrew/cask/)?[a-z0-9\-_]+$}.freeze
sig { params(token: String).returns(Hash) } sig { params(token: String).returns(Hash) }
def fetch(token) def fetch(token)
token = token.sub(%r{^homebrew/cask/}, "") token = token.delete_prefix("homebrew/cask/")
Homebrew::API.fetch "cask-source/#{token}.rb", json: false Homebrew::API.fetch "cask-source/#{token}.rb", json: false
end end
sig { params(token: String).returns(T::Boolean) } sig { params(token: String).returns(T::Boolean) }
def available?(token) def available?(token)
fetch token # Sanity check before hitting the API
true return false unless token.match?(CASK_TOKEN_REGEX)
rescue ArgumentError
false begin
fetch token
true
rescue ArgumentError
false
end
end end
end end
end end