Skip to content

A platform for community discussion. Free, open, simple.

SECURITY: Unauthorized Post Data Exposure in discourse-user-notes Plugin

Isaac Janzen58f56d37fc1b
diff --git a/plugins/discourse-user-notes/app/controllers/discourse_user_notes/user_notes_controller.rb b/plugins/discourse-user-notes/app/controllers/discourse_user_notes/user_notes_controller.rb
index 2f781341af3..e20a2941d91 100644
--- a/plugins/discourse-user-notes/app/controllers/discourse_user_notes/user_notes_controller.rb
+++ b/plugins/discourse-user-notes/app/controllers/discourse_user_notes/user_notes_controller.rb
@@ -19,7 +19,8 @@ module DiscourseUserNotes
raise Discourse::NotFound if user.blank?
extras = {}
if post_id = params[:user_note][:post_id]
- extras[:post_id] = post_id
+ post = Post.with_deleted.find_by(id: post_id)
+ extras[:post_id] = post_id if post && guardian.can_see_post?(post)
end
if reviewable_id = params[:user_note][:reviewable_id]
extras[:reviewable_id] = reviewable_id
@@ -52,11 +53,13 @@ module DiscourseUserNotes
Post.with_deleted.where(id: obj.map { |o| o[:post_id] }).each { |p| posts_by_id[p.id] = p }
obj.each do |o|
o[:created_by] = users_by_id[o[:created_by].to_i]
- o[:post] = posts_by_id[o[:post_id].to_i]
+ post = posts_by_id[o[:post_id].to_i]
+ o[:post] = post if post && guardian.can_see_post?(post)
end
else
obj[:created_by] = User.where(id: obj[:created_by]).first
- obj[:post] = Post.with_deleted.where(id: obj[:post_id]).first
+ post = Post.with_deleted.where(id: obj[:post_id]).first
+ obj[:post] = post if post && guardian.can_see_post?(post)
end
serialize_data(obj, ::UserNoteSerializer)
diff --git a/plugins/discourse-user-notes/app/serializers/user_note_serializer.rb b/plugins/discourse-user-notes/app/serializers/user_note_serializer.rb
index 76247a27603..783a981c267 100644
--- a/plugins/discourse-user-notes/app/serializers/user_note_serializer.rb
+++ b/plugins/discourse-user-notes/app/serializers/user_note_serializer.rb
@@ -42,6 +42,10 @@ class ::UserNoteSerializer < ApplicationSerializer
object[:post_id]
end
+ def include_post_id?
+ object[:post].present?
+ end
+
def post_url
url = object[:post].try(:url)
@@ -51,10 +55,18 @@ class ::UserNoteSerializer < ApplicationSerializer
"#{Discourse.base_path}#{url}"
end
+ def include_post_url?
+ object[:post].present?
+ end
+
def post_title
object[:post].try(:title)
end
+ def include_post_title?
+ object[:post].present?
+ end
+
def topic_id
object[:topic_id]
end
diff --git a/plugins/discourse-user-notes/spec/requests/user_notes_controller_spec.rb b/plugins/discourse-user-notes/spec/requests/user_notes_controller_spec.rb
new file mode 100644
index 00000000000..7e146085f69
--- /dev/null
+++ b/plugins/discourse-user-notes/spec/requests/user_notes_controller_spec.rb
@@ -0,0 +1,78 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+describe DiscourseUserNotes::UserNotesController do
+ fab!(:moderator)
+ fab!(:user)
+ fab!(:admin)
+
+ before { SiteSetting.user_notes_enabled = true }
+
+ describe "#create" do
+ context "when post_id references a PM the moderator cannot see" do
+ fab!(:pm_topic) do
+ Fabricate(
+ :private_message_topic,
+ user: admin,
+ topic_allowed_users: [Fabricate.build(:topic_allowed_user, user: admin)],
+ )
+ end
+ fab!(:pm_post) { Fabricate(:post, topic: pm_topic, user: admin) }
+
+ it "does not leak post_url or post_title for the inaccessible post" do
+ sign_in(moderator)
+
+ post "/user_notes",
+ params: {
+ user_note: {
+ user_id: user.id,
+ raw: "probe note",
+ post_id: pm_post.id,
+ },
+ },
+ headers: {
+ "ACCEPT" => "application/json",
+ }
+
+ expect(response.status).to eq(200)
+ json = response.parsed_body
+ serialized = json.is_a?(Array) ? json.first : json
+
+ expect(serialized["post_url"]).to be_nil
+ expect(serialized["post_title"]).to be_nil
+
+ notes = DiscourseUserNotes.notes_for(user.id)
+ expect(notes.last["post_id"]).to be_nil
+ end
+ end
+ end
+
+ describe "#index" do
+ context "when a note references a PM the moderator cannot see" do
+ fab!(:pm_topic) do
+ Fabricate(
+ :private_message_topic,
+ user: admin,
+ topic_allowed_users: [Fabricate.build(:topic_allowed_user, user: admin)],
+ )
+ end
+ fab!(:pm_post) { Fabricate(:post, topic: pm_topic, user: admin) }
+
+ before { DiscourseUserNotes.add_note(user, "old note", admin.id, post_id: pm_post.id) }
+
+ it "does not leak post_url or post_title for inaccessible posts" do
+ sign_in(moderator)
+
+ get "/user_notes", params: { user_id: user.id }, headers: { "ACCEPT" => "application/json" }
+
+ expect(response.status).to eq(200)
+ json = response.parsed_body
+ note = json["user_notes"].first
+
+ expect(note["post_url"]).to be_nil
+ expect(note["post_title"]).to be_nil
+ end
+ end
+ end
+end