FIX: respect tl3 links no follow setting (#8232)

This commit is contained in:
Arpit Jalan
2019-10-22 22:41:04 +05:30
committed by GitHub
parent 51ecbeef4d
commit 1e9d9d9346
2 changed files with 38 additions and 2 deletions

View File

@ -28,6 +28,7 @@ class CookedPostProcessor
@size_cache = {} @size_cache = {}
@disable_loading_image = !!opts[:disable_loading_image] @disable_loading_image = !!opts[:disable_loading_image]
@omit_nofollow = post.omit_nofollow?
end end
def post_process(bypass_bump: false, new_post: false) def post_process(bypass_bump: false, new_post: false)
@ -580,7 +581,7 @@ class CookedPostProcessor
end end
end end
if @cooking_options[:omit_nofollow] || !SiteSetting.add_rel_nofollow_to_user_content if @omit_nofollow || !SiteSetting.add_rel_nofollow_to_user_content
@doc.css(".onebox-body a, .onebox a").each { |a| a.remove_attribute("rel") } @doc.css(".onebox-body a, .onebox a").each { |a| a.remove_attribute("rel") }
end end
end end
@ -617,7 +618,7 @@ class CookedPostProcessor
end end
def enforce_nofollow def enforce_nofollow
if !@cooking_options[:omit_nofollow] && SiteSetting.add_rel_nofollow_to_user_content if !@omit_nofollow && SiteSetting.add_rel_nofollow_to_user_content
PrettyText.add_rel_nofollow_to_user_content(@doc) PrettyText.add_rel_nofollow_to_user_content(@doc)
end end
end end

View File

@ -199,6 +199,12 @@ describe CookedPostProcessor do
RAW RAW
end end
let(:staff_post) do
Fabricate(:post, user: Fabricate(:admin), raw: <<~RAW)
This is a #{url_with_path} topic
RAW
end
before do before do
urls.each do |url| urls.each do |url|
stub_request(:get, url).to_return( stub_request(:get, url).to_return(
@ -247,6 +253,14 @@ describe CookedPostProcessor do
text: title, text: title,
count: 1 count: 1
) )
expect(html).to have_tag("a[rel='nofollow noopener']")
end
it 'removes nofollow if user is staff/tl3' do
cpp = CookedPostProcessor.new(staff_post, invalidate_oneboxes: true)
cpp.post_process
expect(cpp.html).to_not have_tag("a[rel='nofollow noopener']")
end end
end end
end end
@ -876,6 +890,27 @@ describe CookedPostProcessor do
end end
end end
context "#post_process_oneboxes removes nofollow if user is tl3" do
let(:post) { build(:post_with_youtube, id: 123) }
let(:cpp) { CookedPostProcessor.new(post, invalidate_oneboxes: true) }
before do
post.user.trust_level = TrustLevel[3]
post.user.save!
SiteSetting.add_rel_nofollow_to_user_content = true
SiteSetting.tl3_links_no_follow = false
Oneboxer.expects(:onebox)
.with("http://www.youtube.com/watch?v=9bZkp7q19f0", invalidate_oneboxes: true, user_id: nil, category_id: post.topic.category_id)
.returns('<aside class="onebox"><a href="https://www.youtube.com/watch?v=9bZkp7q19f0" rel="nofollow noopener">GANGNAM STYLE</a></aside>')
cpp.post_process_oneboxes
end
it "removes nofollow noopener from links" do
expect(cpp).to be_dirty
expect(cpp.html).to match_html '<aside class="onebox"><a href="https://www.youtube.com/watch?v=9bZkp7q19f0">GANGNAM STYLE</a></aside>'
end
end
context "#post_process_oneboxes with oneboxed image" do context "#post_process_oneboxes with oneboxed image" do
let(:post) { build(:post_with_youtube, id: 123) } let(:post) { build(:post_with_youtube, id: 123) }
let(:cpp) { CookedPostProcessor.new(post, invalidate_oneboxes: true) } let(:cpp) { CookedPostProcessor.new(post, invalidate_oneboxes: true) }