A platform for community discussion. Free, open, simple.
FIX: Whisper access bypass for removed users on single-post endpoints (#42323)
Isaac Janzen7e6c7362c25a
diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rbindex 0e5f966103d..7d56ccb7ac4 100644--- a/lib/guardian/post_guardian.rb+++ b/lib/guardian/post_guardian.rb@@ -183,6 +183,7 @@ module PostGuardianreturn false if !trusted_with_post_edits?if is_my_own?(post)+ return false if !is_staff? && !Topic.visible_post_types(@user).include?(post.post_type)return false if @user.silenced?return can_edit_hidden_post?(post) if post.hidden?@@ -310,7 +311,7 @@ module PostGuardianreturn false if post.blank?return true if is_admin?return false unless can_see_post_topic?(post)- return false unless is_my_own?(post) || Topic.visible_post_types(@user).include?(post.post_type)+ return false if Topic.visible_post_types(@user).exclude?(post.post_type)return true if is_moderator? || is_category_group_moderator?(post.topic.category)if (!post.trashed? || can_see_deleted_post?(post)) &&(!post.hidden? || can_see_hidden_post?(post))diff --git a/spec/lib/guardian/post_guardian_spec.rb b/spec/lib/guardian/post_guardian_spec.rbindex 694eba89878..55817e4fb1e 100644--- a/spec/lib/guardian/post_guardian_spec.rb+++ b/spec/lib/guardian/post_guardian_spec.rb@@ -1123,7 +1123,7 @@ RSpec.describe PostGuardian doexpect(Guardian.new(user2).can_see?(post)).to eq(false)end- it "respects whispers" do+ it "requires current whisper group membership" doSiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}|#{group.id}"regular_post = post@@ -1138,9 +1138,8 @@ RSpec.describe PostGuardian doexpect(regular_guardian.can_see?(regular_post)).to eq(true)expect(regular_guardian.can_see?(whisper_post)).to eq(false)- # can see your own whispersregular_whisper = Fabricate(:post, post_type: Post.types[:whisper], user: regular_user)- expect(regular_guardian.can_see?(regular_whisper)).to eq(true)+ expect(regular_guardian.can_see?(regular_whisper)).to eq(false)mod_guardian = Guardian.new(Fabricate(:moderator))expect(mod_guardian.can_see?(regular_post)).to eq(true)diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rbindex 817765a5646..1ac90697baa 100644--- a/spec/requests/posts_controller_spec.rb+++ b/spec/requests/posts_controller_spec.rb@@ -228,6 +228,47 @@ RSpec.describe PostsController doendend+ describe "whisper visibility" do+ it "blocks removed whisperers from their own whisper", :aggregate_failures do+ whisper_group = Fabricate(:group)+ whisper_author = Fabricate(:user, trust_level: TrustLevel[1])+ topic = Fabricate(:topic)+ Fabricate(:post, topic: topic)+ original_raw = "staff-only whisper body"+ edited_raw = "edited whisper body"+ whisper =+ Fabricate(+ :post,+ topic: topic,+ user: whisper_author,+ post_type: Post.types[:whisper],+ raw: original_raw,+ )++ SiteSetting.whispers_allowed_groups = whisper_group.id.to_s+ whisper_group.add(whisper_author)+ whisper_group.remove(whisper_author)+ sign_in(User.find(whisper_author.id))++ get "/posts/#{whisper.id}.json"+ expect(response).to be_forbidden+ expect(response.body).not_to include(whisper.raw)++ get "/posts/by_number/#{topic.id}/#{whisper.post_number}.json"+ expect(response).to be_forbidden+ expect(response.body).not_to include(whisper.raw)++ get "/raw/#{topic.id}/#{whisper.post_number}"+ expect(response.status).to eq(404)+ expect(response.body).not_to include(whisper.raw)++ put "/posts/#{whisper.id}.json", params: { post: { raw: edited_raw } }+ expect(response).to be_forbidden+ expect(response.body).not_to include(edited_raw)+ expect(whisper.reload.raw).to eq(original_raw)+ end+ end+describe "#by_date" doinclude_examples "finding and showing post" dolet(:url) { "/posts/by-date/#{post.topic_id}/#{post.created_at.strftime("%Y-%m-%d")}.json" }