Skip to content

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

SECURITY: Authorization bypass in post revision endpoints via array id (#41014)

Osama Sayegh0a95cfcf2f9a
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index fd2d954f939..e143a2811e2 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -580,16 +580,15 @@ class PostsController < ApplicationController
def revert
raise Discourse::NotFound unless guardian.is_staff?
- post_id = params[:id] || params[:post_id]
revision = params[:revision].to_i
raise Discourse::InvalidParameters.new(:revision) if revision < 2
- post_revision = PostRevision.find_by(post_id: post_id, number: revision)
- raise Discourse::NotFound unless post_revision
-
post = find_post_from_params
raise Discourse::NotFound if post.blank?
+ post_revision = PostRevision.find_by(post_id: post.id, number: revision)
+ raise Discourse::NotFound unless post_revision
+
post_revision.post = post
guardian.ensure_can_see!(post_revision)
guardian.ensure_can_edit!(post)
@@ -808,29 +807,30 @@ class PostsController < ApplicationController
end
def find_post_revision_from_params
- post_id = params[:id] || params[:post_id]
revision = params[:revision].to_i
raise Discourse::InvalidParameters.new(:revision) if revision < 2
- post_revision = PostRevision.find_by(post_id: post_id, number: revision)
+ post = find_post_from_params
+
+ post_revision = PostRevision.find_by(post_id: post.id, number: revision)
raise Discourse::NotFound unless post_revision
- post_revision.post = find_post_from_params
+ post_revision.post = post
guardian.ensure_can_see!(post_revision)
post_revision
end
def find_latest_post_revision_from_params
- post_id = params[:id] || params[:post_id]
+ post = find_post_from_params
- finder = PostRevision.where(post_id: post_id).order(:number)
+ finder = PostRevision.where(post_id: post.id).order(:number)
finder = finder.where(hidden: false) unless guardian.is_staff?
post_revision = finder.last
raise Discourse::NotFound unless post_revision
- post_revision.post = find_post_from_params
+ post_revision.post = post
guardian.ensure_can_see!(post_revision)
post_revision
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index 82bb17e38e2..e943e96f0cb 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -2642,6 +2642,38 @@ RSpec.describe PostsController do
expect(response.status).to eq(400)
end
+ context "when the id is passed as an array" do
+ fab!(:accessible_post) { Fabricate(:post, user: moderator) }
+ fab!(:inaccessible_post, :private_message_post)
+ fab!(:inaccessible_revision) do
+ Fabricate(
+ :post_revision,
+ post: inaccessible_post,
+ number: 2,
+ modifications: {
+ "raw" => ["previous private body", "current private body"],
+ },
+ )
+ end
+
+ before { sign_in(moderator) }
+
+ it "does not return a revision the user cannot see on another post" do
+ expect(accessible_post.id).to be < inaccessible_post.id
+ expect(moderator.guardian.can_see?(inaccessible_post)).to eq(false)
+
+ previous_raw = inaccessible_revision.modifications["raw"][0]
+
+ get "/posts/#{accessible_post.id}/revisions/2.json",
+ params: {
+ id: [accessible_post.id, inaccessible_post.id],
+ }
+
+ expect(response.status).to eq(404)
+ expect(response.body).not_to include(previous_raw)
+ end
+ end
+
context "when diff generation exceeds the comparison budget" do
let(:diff_error) do
ONPDiff::DiffLimitExceeded.new(
@@ -3136,6 +3168,39 @@ RSpec.describe PostsController do
expect(post.reload.reply_to_post_number).to eq(earlier_post.post_number)
end
end
+
+ context "when the id is passed as an array" do
+ fab!(:accessible_post) { Fabricate(:post, user: moderator) }
+ fab!(:inaccessible_post, :private_message_post)
+ fab!(:inaccessible_revision) do
+ Fabricate(
+ :post_revision,
+ post: inaccessible_post,
+ number: 2,
+ modifications: {
+ "raw" => ["previous private body", "current private body"],
+ },
+ )
+ end
+
+ before { sign_in(moderator) }
+
+ it "does not revert using a revision the user cannot see on another post" do
+ expect(accessible_post.id).to be < inaccessible_post.id
+ expect(moderator.guardian.can_see?(inaccessible_post)).to eq(false)
+
+ previous_raw = inaccessible_revision.modifications["raw"][0]
+
+ put "/posts/#{accessible_post.id}/revisions/2/revert.json",
+ params: {
+ id: [accessible_post.id, inaccessible_post.id],
+ }
+
+ expect(response.status).to eq(404)
+ expect(response.body).not_to include(previous_raw)
+ expect(accessible_post.reload.raw).not_to include(previous_raw)
+ end
+ end
end
describe "#expand_embed" do