Skip to content

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

SECURITY: fix is_warning type coercion bypass in PostActionsController

Alan Guo Xiang Tand3cb203feabc
diff --git a/app/controllers/post_actions_controller.rb b/app/controllers/post_actions_controller.rb
index 333f8258ea4..a8a1c273692 100644
--- a/app/controllers/post_actions_controller.rb
+++ b/app/controllers/post_actions_controller.rb
@@ -14,7 +14,7 @@ class PostActionsController < ApplicationController
current_user,
@post,
@post_action_type_id,
- is_warning: params[:is_warning],
+ is_warning: ActiveModel::Type::Boolean.new.cast(params[:is_warning]),
message: params[:message],
take_action: params[:take_action] == "true",
flag_topic: params[:flag_topic] == "true",
diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rb
index 934e8489404..be024b94865 100644
--- a/lib/guardian/post_guardian.rb
+++ b/lib/guardian/post_guardian.rb
@@ -28,11 +28,7 @@ module PostGuardian
return false if !(can_see_post.nil? && can_see_post?(post)) && !can_see_post
# no warnings except for staff
- if action_key == :notify_user &&
- (
- post.user.blank? ||
- (!is_staff? && opts[:is_warning].present? && opts[:is_warning] == "true")
- )
+ if action_key == :notify_user && (post.user.blank? || (!is_staff? && opts[:is_warning]))
return false
end
diff --git a/spec/lib/post_action_creator_spec.rb b/spec/lib/post_action_creator_spec.rb
index a3c4be90442..934557624c1 100644
--- a/spec/lib/post_action_creator_spec.rb
+++ b/spec/lib/post_action_creator_spec.rb
@@ -167,6 +167,21 @@ RSpec.describe PostActionCreator do
expect(event[:params].first).to be_instance_of(PostAction)
expect(event[:params].second).to be_instance_of(PostActionCreator)
end
+
+ it "forbids non-staff from creating warning post actions" do
+ target_post = Fabricate(:post)
+
+ result =
+ PostActionCreator.new(
+ user,
+ target_post,
+ PostActionType.types[:notify_user],
+ is_warning: true,
+ message: "this is a test",
+ ).perform
+
+ expect(result.forbidden).to eq(true)
+ end
end
describe "flags" do
diff --git a/spec/requests/post_actions_controller_spec.rb b/spec/requests/post_actions_controller_spec.rb
index 92e64b9a1ff..54498a98283 100644
--- a/spec/requests/post_actions_controller_spec.rb
+++ b/spec/requests/post_actions_controller_spec.rb
@@ -138,6 +138,56 @@ RSpec.describe PostActionsController do
)
end
+ describe "as a non-staff user" do
+ fab!(:target_post) { Fabricate(:post, user: coding_horror) }
+
+ before { sign_in(Fabricate(:user, refresh_auto_groups: true)) }
+
+ [true, "true"].each do |value|
+ it "forbids sending warnings when the is_warning query param is #{value.inspect}" do
+ SiteSetting.personal_message_enabled_groups = Group::AUTO_GROUPS[:trust_level_0]
+
+ post "/post_actions.json",
+ params: {
+ id: target_post.id,
+ post_action_type_id: PostActionType.types[:notify_user],
+ message: "action message goes here",
+ is_warning: value,
+ },
+ as: :json
+
+ expect(response.status).to eq(403)
+ end
+ end
+
+ [false, "false"].each do |value|
+ it "allows notifying a user when the is_warning query param is #{value.inspect}" do
+ SiteSetting.personal_message_enabled_groups = Group::AUTO_GROUPS[:trust_level_0]
+
+ message = "action message goes here"
+
+ expect do
+ post "/post_actions.json",
+ params: {
+ id: target_post.id,
+ post_action_type_id: PostActionType.types[:notify_user],
+ message: message,
+ is_warning: value,
+ },
+ as: :json
+ end.to change { PostAction.count }.by(1)
+
+ expect(response.status).to eq(200)
+
+ post_action = PostAction.last
+
+ expect(post_action.post_id).to eq(target_post.id)
+ expect(post_action.related_post.raw).to include(message)
+ expect(post_action.related_post.topic.is_official_warning?).to eq(false)
+ end
+ end
+ end
+
describe "as a moderator" do
fab!(:user) { Fabricate(:moderator, refresh_auto_groups: true) }
fab!(:post_1) { Fabricate(:post, user: coding_horror) }
@@ -243,20 +293,6 @@ RSpec.describe PostActionsController do
expect(post.topic.is_official_warning?).to eq(true)
end
- it "doesn't create message as a warning if the user isn't staff" do
- sign_in(Fabricate(:user))
-
- post "/post_actions.json",
- params: {
- id: post_1.id,
- post_action_type_id: PostActionType.types[:notify_user],
- message: "action message goes here",
- is_warning: true,
- }
-
- expect(response.status).to eq(403)
- end
-
it "passes take_action through" do
post "/post_actions.json",
params: {