CVE-2021-41140 (published 2021-10-19T18:15Z)

Discourse-reactions is a plugin for the Discourse platform that allows user to add their reactions to the post. In affected versions reactions given by user to secure topics and private messages are visible. This issue is patched in version 0.2 of discourse-reaction. Users who are unable to update are advised to disable the Discourse-reactions plugin in admin panel.

Severity: MEDIUM
CVSS v2 base score: 5.0 (AV:N/AC:L/Au:N/C:P/I:N/A:N)
CVSS v3 base score: 5.3, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N)

Weakness information:
cwe_id: CWE-200
cwe_name: Exposure of Sensitive Information to an Unauthorized Actor
description: The product exposes sensitive information to an actor that is not explicitly authorized to have access to that information.
extended_description: 
	   There are many different kinds of mistakes that introduce information exposures. The severity of the error can range widely, depending on the context in which the product operates, the type of sensitive information that is revealed, and the benefits it may provide to an attacker.  Some kinds of sensitive information include:
       
         private, personal information, such as personal messages, financial data, health records, geographic location, or contact details
         system status and environment, such as the operating system and installed packages
		 business secrets and intellectual property
		 network status and configuration
		 the product's own code or internal state
         metadata, e.g. logging of connections or message headers
		 indirect information, such as a discrepancy between two internal operations that can be observed by an outsider
       
	    Information might be sensitive to different parties, each of which may have their own expectations for whether the information should be protected.  These parties include:
               
                  the product's own users
                  people or organizations whose information is created or used by the product, even if they are not direct product users
		  the product's administrators, including the admins of the system(s) and/or networks on which the product operates
                  the developer
               
	    Information exposures can occur in different ways:
               
                  the code explicitly inserts sensitive information into resources or messages that are intentionally made accessible to unauthorized actors, but should not contain the information - i.e., the information should have been "scrubbed" or "sanitized"
                  a different weakness or mistake indirectly inserts the sensitive information into resources, such as a web script error revealing the full system path of the program.
                  the code manages resources that intentionally contain sensitive information, but the resources are unintentionally made accessible to unauthorized actors. In this case, the information exposure is resultant - i.e., a different weakness enabled the access to the information in the first place.
               
	    It is common practice to describe any loss of confidentiality as an "information exposure," but this can lead to overuse of CWE-200 in CWE mapping. From the CWE perspective, loss of confidentiality is a technical impact that can arise from dozens of different weaknesses, such as insecure file permissions or out-of-bounds read.  CWE-200 and its lower-level descendants are intended to cover the mistakes that occur in behaviors that explicitly manage, store, transfer, or cleanse sensitive information.
         
         

Fix information:
repo_url: https://github.com/discourse/discourse-reactions
commit_details: repo_url: https://github.com/discourse/discourse-reactions
author: Krzysztof Kotlarek
author_date: 2021-10-11 13:03:38+11:00
msg: SECURITY: leaking PMs and secure categories topics (#110)

Ensure that user has access to private messages and categories of he topics
repository: repo_url: https://github.com/discourse/discourse-reactions
repo_name: discourse/discourse-reactions
repo_language: Ruby
owner: discourse
file_changes: filename: custom_reactions_controller.rb
change_type: ModificationType.MODIFY
diff: @@ -32,11 +32,14 @@ module DiscourseReactions
       raise Discourse::NotFound unless guardian.can_see_profile?(user)
 
       reaction_users = DiscourseReactions::ReactionUser
-        .joins(:reaction, :post)
+        .joins(:reaction, post: :topic)
+        .joins("LEFT JOIN categories ON categories.id = topics.category_id")
         .includes(:user, :post, :reaction)
         .where(user_id: user.id)
         .where('discourse_reactions_reactions.reaction_users_count IS NOT NULL')
 
+      reaction_users = secure_reaction_users!(reaction_users)
+
       if params[:before_reaction_user_id]
         reaction_users = reaction_users
           .where('discourse_reactions_reaction_users.id < ?', params[:before_reaction_user_id].to_i)
@@ -187,5 +190,40 @@ module DiscourseReactions
 
       MessageBus.publish("/topic/#{post.topic.id}/reactions", message)
     end
+
+    def secure_reaction_users!(reaction_users)
+      if !guardian.can_see_private_messages?(current_user.id) || !guardian.user
+        reaction_users = reaction_users.where("topics.archetype <> :private_message", private_message: archetype::private_message)
+      else
+        unless guardian.is_admin?
+          sql = <<~SQL
+            topics.archetype <> :private_message OR
+            EXISTS (
+              SELECT 1 FROM topic_allowed_users tu WHERE tu.topic_id = topics.id AND tu.user_id = :current_user_id
+            ) OR
+            EXISTS (
+              SELECT 1 FROM topic_allowed_groups tg WHERE tg.topic_id = topics.id AND tg.group_id IN (
+                SELECT group_id FROM group_users gu WHERE gu.user_id = :current_user_id
+              )
+            )
+          SQL
+
+          reaction_users = reaction_users.where(sql, private_message: Archetype::private_message, current_user_id: guardian.user.id)
+        end
+      end
+
+      unless guardian.is_admin?
+        allowed = guardian.secure_category_ids
+        if allowed.present?
+          reaction_users = reaction_users.where("(categories.read_restricted IS NULL OR
+                         NOT categories.read_restricted OR
+                        (categories.read_restricted and categories.id in (:categories)) )", categories: guardian.secure_category_ids)
+        else
+          reaction_users = reaction_users.where("(categories.read_restricted IS NULL OR NOT categories.read_restricted)")
+        end
+      end
+
+      reaction_users
+    end
   end
 end

programming_language: Ruby
filename: custom_reactions_controller_spec.rb
change_type: ModificationType.MODIFY
diff: @@ -1,6 +1,8 @@
 # frozen_string_literal: true
 
 require 'rails_helper'
+require_relative '../fabricators/reaction_fabricator.rb'
+require_relative '../fabricators/reaction_user_fabricator.rb'
 
 describe DiscourseReactions::CustomReactionsController do
   fab!(:post_1) { Fabricate(:post) }
@@ -109,6 +111,17 @@ describe DiscourseReactions::CustomReactionsController do
   end
 
   context '#reactions_given' do
+    fab!(:private_topic) { Fabricate(:private_message_topic, user: user_2) }
+    fab!(:private_post) { Fabricate(:post, topic: private_topic) }
+    fab!(:secure_group) { Fabricate(:group) }
+    fab!(:secure_category) { Fabricate(:private_category, group: secure_group) }
+    fab!(:secure_topic) { Fabricate(:topic, category: secure_category) }
+    fab!(:secure_post) { Fabricate(:post, topic: secure_topic) }
+    fab!(:private_reaction) { Fabricate(:reaction, post: private_post, reaction_value: "hugs") }
+    fab!(:secure_reaction) { Fabricate(:reaction, post: secure_post, reaction_value: "hugs") }
+    fab!(:private_topic_reaction_user) { Fabricate(:reaction_user, reaction: private_reaction, user: user_2, post: private_post) }
+    fab!(:secure_topic_reaction_user) { Fabricate(:reaction_user, reaction: secure_reaction, user: user_2, post: secure_post) }
+
     it 'returns reactions given by a user' do
       sign_in(user_1)
 
@@ -121,6 +134,40 @@ describe DiscourseReactions::CustomReactionsController do
       expect(parsed[0]['reaction']['id']).to eq(reaction_1.id)
     end
 
+    it 'does not return reactions for private messages' do
+      sign_in(user_1)
+
+      get "/discourse-reactions/posts/reactions.json", params: { username: user_2.username }
+      parsed = response.parsed_body
+      expect(response.parsed_body.map { |reaction| reaction["post_id"] }).not_to include(private_post.id)
+
+      sign_in(user_2)
+
+      get "/discourse-reactions/posts/reactions.json", params: { username: user_2.username }
+      parsed = response.parsed_body
+      expect(response.parsed_body.map { |reaction| reaction["post_id"] }).to include(private_post.id)
+    end
+
+    it 'does not return reactions for secure categories' do
+      secure_group.add(user_2)
+      sign_in(user_1)
+
+      get "/discourse-reactions/posts/reactions.json", params: { username: user_2.username }
+      parsed = response.parsed_body
+      expect(response.parsed_body.map { |reaction| reaction["post_id"] }).not_to include(secure_post.id)
+
+      secure_group.add(user_1)
+      get "/discourse-reactions/posts/reactions.json", params: { username: user_2.username }
+      parsed = response.parsed_body
+      expect(response.parsed_body.map { |reaction| reaction["post_id"] }).to include(secure_post.id)
+
+      sign_in(user_2)
+
+      get "/discourse-reactions/posts/reactions.json", params: { username: user_2.username }
+      parsed = response.parsed_body
+      expect(response.parsed_body.map { |reaction| reaction["post_id"] }).to include(secure_post.id)
+    end
+
     context 'a post with one of your reactions has been deleted' do
       fab!(:deleted_post) { Fabricate(:post) }
       fab!(:kept_post) { Fabricate(:post) }

programming_language: Ruby