A platform for community discussion. Free, open, simple.
SECURITY: Check for no group assignments in data-explorer queries
Jarek Radosz43f80d7cc071
diff --git a/plugins/discourse-data-explorer/plugin.rb b/plugins/discourse-data-explorer/plugin.rbindex e249b413396..06f6794c9f5 100644--- a/plugins/discourse-data-explorer/plugin.rb+++ b/plugins/discourse-data-explorer/plugin.rb@@ -50,7 +50,7 @@ after_initialize doadd_to_class(:guardian, :user_can_access_query?) do |query|return false if !current_userreturn true if current_user.admin?- query.groups.blank? || query.groups.any? { |group| user_is_a_member_of_group?(group) }+ query.groups.any? { |group| user_is_a_member_of_group?(group) }endadd_to_class(:guardian, :group_and_user_can_access_query?) do |group, query|diff --git a/plugins/discourse-data-explorer/spec/guardian_spec.rb b/plugins/discourse-data-explorer/spec/guardian_spec.rbindex 4b83323bec2..d06270dc46a 100644--- a/plugins/discourse-data-explorer/spec/guardian_spec.rb+++ b/plugins/discourse-data-explorer/spec/guardian_spec.rb@@ -35,6 +35,30 @@ describe Guardian doendend+ describe "#user_can_access_query?" do+ it "is true when the user is an admin" do+ expect(Guardian.new(admin).user_can_access_query?(make_query)).to eq(true)+ end++ it "is false when the query has no group assignments" do+ expect(Guardian.new(user).user_can_access_query?(make_query)).to eq(false)+ end++ it "is true when the user is a member of a group assigned to the query" do+ query = make_query(["#{group.id}"])+ group.add(user)++ expect(Guardian.new(user).user_can_access_query?(query)).to eq(true)+ end++ it "is false when the user is not a member of any group assigned to the query" do+ other_group = Fabricate(:group)+ query = make_query(["#{other_group.id}"])++ expect(Guardian.new(user).user_can_access_query?(query)).to eq(false)+ end+ end+describe "#group_and_user_can_access_query?" doit "is true if the user is an admin" doexpect(Guardian.new(admin).group_and_user_can_access_query?(group, make_query)).to eq(true)diff --git a/plugins/discourse-data-explorer/spec/report_generator_spec.rb b/plugins/discourse-data-explorer/spec/report_generator_spec.rbindex 2e0b5892849..3afde8ff43e 100644--- a/plugins/discourse-data-explorer/spec/report_generator_spec.rb+++ b/plugins/discourse-data-explorer/spec/report_generator_spec.rb@@ -29,6 +29,7 @@ describe DiscourseDataExplorer::ReportGenerator doendit "returns a list of pms for authorised users" do+ Fabricate(:query_group, query: query, group: group)SiteSetting.personal_message_enabled_groups = group.idDiscourseDataExplorer::ResultToMarkdown.expects(:convert).returns("le table")freeze_time@@ -126,6 +127,7 @@ describe DiscourseDataExplorer::ReportGenerator doendit "works with duplicate recipients" do+ Fabricate(:query_group, query: query, group: group)DiscourseDataExplorer::ResultToMarkdown.expects(:convert).returns("le table")freeze_time@@ -231,6 +233,7 @@ describe DiscourseDataExplorer::ReportGenerator doendit "works with attached csv file" do+ Fabricate(:query_group, query: query, group: group)SiteSetting.personal_message_enabled_groups = group.idDiscourseDataExplorer::ResultToMarkdown.expects(:convert).returns("le table")freeze_timediff --git a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rbindex cb75514c1c6..9f977b60337 100644--- a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb+++ b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb@@ -710,6 +710,13 @@ describe DiscourseDataExplorer::QueryController doget "/data-explorer/queries/#{query.id}/run.json"expect(response.status).to eq(404)end++ it "returns 404 when the query has no group assignments" do+ query = make_query("SELECT 1 as value", {}, [])++ get "/data-explorer/queries/#{query.id}/run.json"+ expect(response.status).to eq(404)+ endendend