Skip to content

Commit

Permalink
Merge pull request #3306 from manyfold3d/handle-invalid-domains
Browse files Browse the repository at this point in the history
Handle invalid domains safely in Link#site
  • Loading branch information
Floppy authored Dec 12, 2024
2 parents 8ea6022 + 062202a commit e3b7374
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/models/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ def host
end

def site
PublicSuffix.parse(host).sld.to_sym
PublicSuffix.parse(host).sld
rescue PublicSuffix::DomainInvalid
host
end

def self.ransackable_attributes(_auth_object = nil)
Expand Down
12 changes: 12 additions & 0 deletions spec/models/link_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "rails_helper"
RSpec.describe Link do
it "extracts site name from domain" do
link = described_class.new(url: "https://www.example.com")
expect(link.site).to eq "example"
end

it "extracts site fails safe with bad domain" do
link = described_class.new(url: "https://boop")
expect(link.site).to eq "boop"
end
end

0 comments on commit e3b7374

Please sign in to comment.