FEATURE: Remove user IDs from internal URLs. (#7406)

This commit is contained in:
Dan Ungureanu 2019-04-23 05:45:41 +03:00 committed by Sam
parent e0e12c63e8
commit b706a1b08d
2 changed files with 38 additions and 0 deletions

View File

@ -40,6 +40,7 @@ class CookedPostProcessor
post_process_images post_process_images
post_process_quotes post_process_quotes
optimize_urls optimize_urls
remove_user_ids
update_post_image update_post_image
enforce_nofollow enforce_nofollow
pull_hotlinked_images(bypass_bump) pull_hotlinked_images(bypass_bump)
@ -595,6 +596,19 @@ class CookedPostProcessor
end end
end end
def remove_user_ids
@doc.css("a[href]").each do |a|
uri = URI(a["href"])
next if uri.hostname != Discourse.current_hostname
query = Rack::Utils.parse_nested_query(uri.query)
next if !query.delete("u")
uri.query = query.map { |k, v| "#{k}=#{v}" }.join("&").presence
a["href"] = uri.to_s
end
end
def enforce_nofollow def enforce_nofollow
if !@cooking_options[:omit_nofollow] && SiteSetting.add_rel_nofollow_to_user_content if !@cooking_options[: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)

View File

@ -1037,6 +1037,30 @@ describe CookedPostProcessor do
end end
context "#remove_user_ids" do
let(:topic) { Fabricate(:topic) }
let(:post) do
Fabricate(:post, raw: <<~RAW)
link to a topic: #{topic.url}?u=foo
a tricky link to a topic: #{topic.url}?bob=bob;u=sam&jane=jane
link to an external topic: https://google.com/?u=bar
RAW
end
let(:cpp) { CookedPostProcessor.new(post, disable_loading_image: true) }
it "does remove user ids" do
cpp.remove_user_ids
expect(cpp.html).to have_tag('a', with: { href: topic.url })
expect(cpp.html).to have_tag('a', with: { href: "#{topic.url}?bob=bob&jane=jane" })
expect(cpp.html).to have_tag('a', with: { href: "https://google.com/?u=bar" })
end
end
context "#pull_hotlinked_images" do context "#pull_hotlinked_images" do
let(:post) { build(:post, created_at: 20.days.ago) } let(:post) { build(:post, created_at: 20.days.ago) }