A platform for community discussion. Free, open, simple.
FIX: Unauthenticated author spoofing via `discourse_username` during embed import (#37867)
Sam580b7ba67807
diff --git a/app/controllers/embed_controller.rb b/app/controllers/embed_controller.rbindex 558ca261478..0f6e4925661 100644--- a/app/controllers/embed_controller.rb+++ b/app/controllers/embed_controller.rb@@ -78,7 +78,6 @@ class EmbedController < ApplicationControllerdef commentsembed_url = params[:embed_url]- embed_username = params[:discourse_username]embed_topic_id = params[:topic_id]&.to_iunless embed_topic_id || EmbeddableHost.url_allowed?(embed_url)@@ -116,7 +115,6 @@ class EmbedController < ApplicationController:retrieve_topic,user_id: current_user.try(:id),embed_url: embed_url,- author_username: embed_username,referer: request.env["HTTP_REFERER"],)render "loading"diff --git a/lib/topic_retriever.rb b/lib/topic_retriever.rbindex 94da883250d..a156e70b39a 100644--- a/lib/topic_retriever.rb+++ b/lib/topic_retriever.rb@@ -34,17 +34,9 @@ class TopicRetriever# It's possible another process or job found the embed already. So if that happened bail out.return if TopicEmbed.where(embed_url: @embed_url).exists?- if @opts[:author_username].present?- Discourse.deprecate(- "discourse_username parameter has been deprecated. Prefer passing author name using a <meta> tag.",- since: "3.1.0.beta1",- drop_from: "3.2",- )- end-username =- @opts[:author_username].presence || SiteSetting.embed_by_username.presence ||- SiteSetting.site_contact_username.presence || Discourse.system_user.username+ SiteSetting.embed_by_username.presence || SiteSetting.site_contact_username.presence ||+ Discourse.system_user.usernameTopicEmbed.import_remote(@embed_url, user: User.find_by(username_lower: username.downcase))enddiff --git a/spec/requests/embed_controller_spec.rb b/spec/requests/embed_controller_spec.rbindex 60c1665a5ba..271f45e21d1 100644--- a/spec/requests/embed_controller_spec.rb+++ b/spec/requests/embed_controller_spec.rb@@ -3,7 +3,6 @@RSpec.describe EmbedController dolet(:embed_url) { "http://eviltrout.com/2013/02/10/why-discourse-uses-emberjs.html" }let(:embed_url_secure) { "https://eviltrout.com/2013/02/10/why-discourse-uses-emberjs.html" }- let(:discourse_username) { "eviltrout" }fab!(:topic)@@ -312,20 +311,29 @@ RSpec.describe EmbedController doexpect(response.body).to match("<span class='replies'>1 reply</span>")end- it "provides the topic retriever with the discourse username when provided" do- TopicRetriever.any_instance.expects(:retrieve).returns(nil)+ it "does not forward user-supplied discourse_username to TopicRetriever" do+ captured_opts = nil+ original_new = TopicRetriever.method(:new)+ TopicRetriever+ .stubs(:new)+ .with do |url, opts|+ captured_opts = opts+ true+ end+ .returns(stub(retrieve: nil))get "/embed/comments",params: {embed_url: embed_url,- discourse_username: discourse_username,+ discourse_username: "admin",},headers: {"REFERER" => embed_url,}expect(response.status).to eq(200)- expect(response.headers["X-Frame-Options"]).to be_nil+ expect(captured_opts).to be_present+ expect(captured_opts[:author_username]).to be_nilendendend