Skip to content

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

FIX: Scope nested topic activity log with guardian properly (#39754)

Mark VanLandinghamcdd5601b1399
diff --git a/app/controllers/nested_topics_controller.rb b/app/controllers/nested_topics_controller.rb
index aa46227c0c8..81ecfcc8a2c 100644
--- a/app/controllers/nested_topics_controller.rb
+++ b/app/controllers/nested_topics_controller.rb
@@ -109,18 +109,20 @@ class NestedTopicsController < ApplicationController
]
actions.concat(
- posts.map do |post|
- {
- id: post.id,
- action_code: post.action_code,
- action_code_who: post.custom_fields["action_code_who"],
- action_code_path: post.custom_fields["action_code_path"],
- created_at: post.created_at,
- username: post.user&.username,
- avatar_template: post.user&.avatar_template,
- cooked: post.cooked,
- }
- end,
+ posts
+ .select { |post| guardian.can_see_post?(post) }
+ .map do |post|
+ {
+ id: post.id,
+ action_code: post.action_code,
+ action_code_who: post.custom_fields["action_code_who"],
+ action_code_path: post.custom_fields["action_code_path"],
+ created_at: post.created_at,
+ username: post.user&.username,
+ avatar_template: post.user&.avatar_template,
+ cooked: post.cooked,
+ }
+ end,
)
render json: { small_actions: actions }
diff --git a/spec/requests/nested_topics_controller_spec.rb b/spec/requests/nested_topics_controller_spec.rb
index 0e827388a94..8e007f98a67 100644
--- a/spec/requests/nested_topics_controller_spec.rb
+++ b/spec/requests/nested_topics_controller_spec.rb
@@ -1216,6 +1216,73 @@ RSpec.describe NestedTopicsController, type: :request do
expect(actions[1]["username"]).to eq(admin.username)
end
+ it "does not expose hidden small-action posts to users who cannot see them" do
+ visible_action =
+ Fabricate(
+ :small_action,
+ topic: topic,
+ user: admin,
+ action_code: "closed.enabled",
+ raw: "visible activity body",
+ )
+ hidden_action =
+ Fabricate(
+ :small_action,
+ topic: topic,
+ user: admin,
+ action_code: "opened.enabled",
+ raw: "hidden activity secret",
+ hidden: true,
+ hidden_reason_id: Post.hidden_reasons[:flag_threshold_reached],
+ )
+
+ sign_in(user)
+ get activity_url(topic)
+ expect(response.status).to eq(200)
+
+ actions = response.parsed_body["small_actions"]
+ ids = actions.map { |action| action["id"] }
+ expect(ids).to include(visible_action.id)
+ expect(ids).not_to include(hidden_action.id)
+ expect(response.body).not_to include("hidden activity secret")
+ end
+
+ it "does not expose hidden small-action posts to anonymous users" do
+ Fabricate(
+ :small_action,
+ topic: topic,
+ user: admin,
+ action_code: "opened.enabled",
+ raw: "hidden activity secret",
+ hidden: true,
+ hidden_reason_id: Post.hidden_reasons[:flag_threshold_reached],
+ )
+
+ get activity_url(topic)
+ expect(response.status).to eq(200)
+ expect(response.body).not_to include("hidden activity secret")
+ end
+
+ it "still exposes hidden small-action posts to staff" do
+ hidden_action =
+ Fabricate(
+ :small_action,
+ topic: topic,
+ user: admin,
+ action_code: "opened.enabled",
+ raw: "hidden activity secret",
+ hidden: true,
+ hidden_reason_id: Post.hidden_reasons[:flag_threshold_reached],
+ )
+
+ sign_in(admin)
+ get activity_url(topic)
+ expect(response.status).to eq(200)
+
+ actions = response.parsed_body["small_actions"]
+ expect(actions.map { |action| action["id"] }).to include(hidden_action.id)
+ end
+
it "excludes whisper action-code posts for non-whisperers" do
topic.add_moderator_post(
admin,