Skip to content

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

SECURITY: Topic existence oracle in bookmark endpoints (#41152)

Samdf82183bab32
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index a832403e133..f6a4dbbca0d 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -717,7 +717,8 @@ class TopicsController < ApplicationController
end
def remove_bookmarks
- topic = Topic.find(params[:topic_id].to_i)
+ topic = find_visible_topic_from_topic_id
+
BookmarkManager.new(current_user).destroy_for_topic(topic)
render body: nil
end
@@ -769,7 +770,7 @@ class TopicsController < ApplicationController
end
def bookmark
- topic = Topic.find(params[:topic_id].to_i)
+ topic = find_visible_topic_from_topic_id
bookmark_manager = BookmarkManager.new(current_user)
bookmark_manager.create_for(bookmarkable_id: topic.id, bookmarkable_type: "Topic")
@@ -1407,6 +1408,13 @@ class TopicsController < ApplicationController
params.permit(:topic_id, :topic_time, timings: {})
end
+ def find_visible_topic_from_topic_id
+ topic = Topic.find_by(id: params[:topic_id].to_i)
+ raise Discourse::NotFound unless guardian.can_see?(topic)
+
+ topic
+ end
+
def fetch_topic_view(options)
if (username_filters = params[:username_filters]).present?
options[:username_filters] = username_filters.split(",")
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 54e73b6e27d..3082d9159bd 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -5378,6 +5378,25 @@ RSpec.describe TopicsController do
expect(response.status).to eq(403)
end
+ it "returns 404 for inaccessible private messages" do
+ sign_in(user_2)
+ private_message =
+ create_post(
+ user: user,
+ archetype: "private_message",
+ target_usernames: [user.username],
+ ).topic
+ missing_topic_id = Topic.maximum(:id) + 1
+
+ put "/t/#{private_message.id}/remove_bookmarks.json"
+ expect(response.status).to eq(404)
+ expect(response.parsed_body["error_type"]).to eq("not_found")
+
+ put "/t/#{missing_topic_id}/remove_bookmarks.json"
+ expect(response.status).to eq(404)
+ expect(response.parsed_body["error_type"]).to eq("not_found")
+ end
+
it "should remove bookmarks properly from non first post" do
sign_in(user)
@@ -5390,14 +5409,6 @@ RSpec.describe TopicsController do
expect(Bookmark.where(user: user).count).to eq(0)
end
- it "should disallow bookmarks on posts you have no access to" do
- sign_in(Fabricate(:user))
- pm = create_post(user: user, archetype: "private_message", target_usernames: [user.username])
-
- put "/t/#{pm.topic_id}/bookmark.json"
- expect(response).to be_forbidden
- end
-
context "with bookmarks with reminders" do
it "deletes all the bookmarks for the user in the topic" do
sign_in(user)
@@ -5412,6 +5423,25 @@ RSpec.describe TopicsController do
describe "#bookmark" do
before { sign_in(user) }
+ it "returns 404 for inaccessible private messages" do
+ sign_in(user_2)
+ private_message =
+ create_post(
+ user: user,
+ archetype: "private_message",
+ target_usernames: [user.username],
+ ).topic
+ missing_topic_id = Topic.maximum(:id) + 1
+
+ put "/t/#{private_message.id}/bookmark.json"
+ expect(response.status).to eq(404)
+ expect(response.parsed_body["error_type"]).to eq("not_found")
+
+ put "/t/#{missing_topic_id}/bookmark.json"
+ expect(response.status).to eq(404)
+ expect(response.parsed_body["error_type"]).to eq("not_found")
+ end
+
it "should create a new bookmark for the topic" do
post = create_post
_post2 = create_post(topic_id: post.topic_id)