Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

FormulaURILoader: use regex to validate refs before attempting to cast #17766

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,20 @@ class FromURILoader < FormulaLoader
.returns(T.nilable(T.attached_class))
}
def self.try_new(ref, from: T.unsafe(nil), warn: false)
ref = ref.to_s
# Cache compiled regex
@uri_regex ||= begin
uri_regex = ::URI::DEFAULT_PARSER.make_regexp
Regexp.new("\\A#{uri_regex.source}\\Z", uri_regex.options)
end

uri = ref.to_s
return unless uri.match?(@uri_regex)

uri = URI(uri)
return unless uri.path
return unless uri.scheme.present?

new(ref, from:) if URI(ref).scheme.present?
new(uri, from:)
end

attr_reader :url
Expand Down
8 changes: 8 additions & 0 deletions Library/Homebrew/test/formulary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,14 @@ def formula_json_contents(extra_items = {})
end.not_to raise_error(UnsupportedInstallationMethod)
end
end

context "when passed ref with spaces" do
it "raises a FormulaUnavailableError error" do
expect do
described_class.factory("foo bar")
end.to raise_error(FormulaUnavailableError)
end
end
end

specify "::from_contents" do
Expand Down