diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb
index 9652dc2b545..2f6417f8949 100644
--- a/lib/pretty_text.rb
+++ b/lib/pretty_text.rb
@@ -242,8 +242,9 @@ module PrettyText
site_uri ||= URI(Discourse.base_url)
if !uri.host.present? ||
- uri.host.ends_with?(site_uri.host) ||
- whitelist.any?{|u| uri.host.ends_with?(u)}
+ uri.host == site_uri.host ||
+ uri.host.ends_with?("." << site_uri.host) ||
+ whitelist.any?{|u| uri.host == u || uri.host.ends_with?("." << u)}
# we are good no need for nofollow
else
l["rel"] = "nofollow"
diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb
index 4f6a0f6f45a..66a64a404b3 100644
--- a/spec/components/pretty_text_spec.rb
+++ b/spec/components/pretty_text_spec.rb
@@ -69,10 +69,18 @@ describe PrettyText do
expect(PrettyText.cook("cnn") !~ /nofollow/).to eq(true)
end
+ it "should inject nofollow in all non subdomain links" do
+ expect(PrettyText.cook("cnn")).to match(/nofollow/)
+ end
+
it "should not inject nofollow for foo.com" do
expect(PrettyText.cook("cnn") !~ /nofollow/).to eq(true)
end
+ it "should inject nofollow for afoo.com" do
+ expect(PrettyText.cook("cnn")).to match(/nofollow/)
+ end
+
it "should not inject nofollow for bar.foo.com" do
expect(PrettyText.cook("cnn") !~ /nofollow/).to eq(true)
end