Skip to content

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

FIX: Authorize topic visibility before recording or deleting read timings (#42176)

Isaac Janzen1cbd5b6ba8f2
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 620fdc0ee2a..51866bee090 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -366,7 +366,8 @@ class TopicsController < ApplicationController
end
def destroy_timings
- topic_id = params[:topic_id].to_i
+ topic = find_visible_topic_from_topic_id
+ topic_id = topic.id
if params[:last].to_s == "1"
PostTiming.destroy_last_for(current_user, topic_id: topic_id)
@@ -1109,7 +1110,7 @@ class TopicsController < ApplicationController
def timings
allowed_params = topic_params
- topic_id = allowed_params[:topic_id].to_i
+ topic_id = find_visible_topic_from_topic_id.id
topic_time = allowed_params[:topic_time].to_i
timings = allowed_params[:timings].to_h || {}
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 8de29baf148..36b16f99e01 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -1787,6 +1787,29 @@ RSpec.describe TopicsController do
expect(response.status).to eq(403)
end
+ it "does not delete timings for a topic the user cannot see" do
+ private_category = Fabricate(:private_category, group: Fabricate(:group))
+ private_topic = Fabricate(:topic, category: private_category)
+ private_post = Fabricate(:post, topic: private_topic)
+ PostTiming.create!(
+ topic: private_topic,
+ user: user,
+ post_number: private_post.post_number,
+ msecs: 1000,
+ )
+ TopicUser.create!(topic: private_topic, user: user)
+
+ sign_in(user)
+ delete "/t/#{private_topic.id}/timings.json"
+
+ aggregate_failures do
+ expect(response.status).to eq(404)
+ expect(response.parsed_body["error_type"]).to eq("not_found")
+ expect(PostTiming.where(topic: private_topic, user: user)).to exist
+ expect(TopicUser.where(topic: private_topic, user: user)).to exist
+ end
+ end
+
def topic_user_post_timings_count(user, topic)
[TopicUser, PostTiming].map { |klass| klass.where(user: user, topic: topic).count }
end
@@ -6517,6 +6540,28 @@ RSpec.describe TopicsController do
expect(tu.last_read_post_number).to eq(whisper.post_number)
end
+ it "does not record timings for a topic the user cannot see" do
+ private_category = Fabricate(:private_category, group: Fabricate(:group))
+ private_topic = Fabricate(:topic, category: private_category)
+ private_post = Fabricate(:post, topic: private_topic)
+
+ sign_in(user)
+ post "/t/#{private_post.topic_id}/timings.json",
+ params: {
+ topic_time: 5,
+ timings: {
+ private_post.post_number => 2,
+ },
+ }
+
+ aggregate_failures do
+ expect(response.status).to eq(404)
+ expect(response.parsed_body["error_type"]).to eq("not_found")
+ expect(PostTiming.where(topic: private_post.topic, user: user)).to be_empty
+ expect(user.user_stat.reload.posts_read_count).to eq(0)
+ end
+ end
+
it "should record the timing" do
sign_in(user)