Skip to content

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

FIX: Normalize inaccessible content lookups to 404 (#43780)

Isaac Janzenbb402125dcf0
diff --git a/app/controllers/post_actions_controller.rb b/app/controllers/post_actions_controller.rb
index 13be32d0535..65d86bd8263 100644
--- a/app/controllers/post_actions_controller.rb
+++ b/app/controllers/post_actions_controller.rb
@@ -7,7 +7,7 @@ class PostActionsController < ApplicationController
before_action :fetch_post_action_type_id_from_params
def create
- raise Discourse::NotFound if @post.blank?
+ raise Discourse::NotFound unless guardian.can_see?(@post)
creator =
PostActionCreator.new(
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index 5c422c04984..d3a75203de3 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -1097,9 +1097,12 @@ class PostsController < ApplicationController
end
def find_post_from_params_by_date
+ topic = Topic.with_deleted.find_by(id: params[:topic_id])
+ raise Discourse::NotFound unless guardian.can_see?(topic)
+
by_date_finder =
TopicView
- .new(params[:topic_id], current_user)
+ .new(topic, current_user)
.filtered_posts
.where("created_at >= ?", Time.zone.parse(params[:date]))
.order("created_at ASC")
@@ -1119,7 +1122,7 @@ class PostsController < ApplicationController
raise Discourse::NotFound unless guardian.can_moderate_topic?(post.topic)
end
- guardian.ensure_can_see!(post)
+ raise Discourse::NotFound unless guardian.can_see?(post)
post
end
diff --git a/app/controllers/topic_view_stats_controller.rb b/app/controllers/topic_view_stats_controller.rb
index c61454b19d4..e24a93b140a 100644
--- a/app/controllers/topic_view_stats_controller.rb
+++ b/app/controllers/topic_view_stats_controller.rb
@@ -2,7 +2,8 @@
class TopicViewStatsController < ApplicationController
def index
- topic = Topic.find(params[:topic_id].to_i)
+ topic = Topic.find_by(id: params[:topic_id].to_i)
+ raise Discourse::NotFound unless topic
from = 30.days.ago.to_date
to = Date.today
@@ -15,7 +16,14 @@ class TopicViewStatsController < ApplicationController
return
end
- stats = TopicViewStatsQuery.call(topic:, guardian:, from:, to:)
+ stats =
+ begin
+ TopicViewStatsQuery.call(topic:, guardian:, from:, to:)
+ rescue Discourse::InvalidAccess
+ raise if SiteSetting.detailed_404
+
+ raise Discourse::NotFound
+ end
rows = []
diff --git a/spec/requests/post_actions_controller_spec.rb b/spec/requests/post_actions_controller_spec.rb
index ce0bdba0004..542542c2643 100644
--- a/spec/requests/post_actions_controller_spec.rb
+++ b/spec/requests/post_actions_controller_spec.rb
@@ -103,7 +103,7 @@ RSpec.describe PostActionsController do
expect(response.status).to eq(403)
end
- it "fails when the user does not have permission to see the post" do
+ it "does not reveal private post existence" do
sign_in(user)
pm = Fabricate(:private_message_post, user: coding_horror)
@@ -113,7 +113,16 @@ RSpec.describe PostActionsController do
post_action_type_id: PostActionType.types[:like],
}
- expect(response.status).to eq(403)
+ expect(response.status).to eq(404)
+ expect(response.body).not_to include(pm.raw)
+
+ post "/post_actions.json",
+ params: {
+ id: Post.maximum(:id) + 1,
+ post_action_type_id: PostActionType.types[:like],
+ }
+
+ expect(response.status).to eq(404)
end
it "returns 404 when flagging a hidden topic" do
@@ -234,7 +243,7 @@ RSpec.describe PostActionsController do
expect(response.status).to eq(400)
end
- it "fails when the user doesn't have permission to see the post" do
+ it "does not reveal private post existence" do
post_1 = Fabricate(:private_message_post, user: Fabricate(:user))
post "/post_actions.json",
@@ -243,7 +252,7 @@ RSpec.describe PostActionsController do
post_action_type_id: PostActionType.types[:like],
}
- expect(response).to be_forbidden
+ expect(response.status).to eq(404)
end
it "allows us to create an post action on a post" do
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index 8f525219736..5ee64ff4ebe 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -8,7 +8,7 @@ RSpec.shared_examples "finding and showing post" do
topic.convert_to_private_message(Discourse.system_user)
topic.remove_allowed_user(Discourse.system_user, user.username)
get url
- expect(response).to be_forbidden
+ expect(response.status).to eq(404)
end
it "returns 200 for an accessible post" do
@@ -100,6 +100,17 @@ RSpec.describe PostsController do
let(:url) { "/posts/#{post.id}.json" }
end
+ it "does not reveal private post existence to anonymous users" do
+ get "/posts/#{private_post.id}.json"
+
+ expect(response.status).to eq(404)
+ expect(response.body).not_to include(private_post.raw)
+
+ get "/posts/#{Post.maximum(:id) + 1}.json"
+
+ expect(response.status).to eq(404)
+ end
+
it "gets all the expected fields" do
# non fabricated test
new_post = create_post
@@ -251,11 +262,11 @@ RSpec.describe PostsController do
sign_in(User.find(whisper_author.id))
get "/posts/#{whisper.id}.json"
- expect(response).to be_forbidden
+ expect(response.status).to eq(404)
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.status).to eq(404)
expect(response.body).not_to include(whisper.raw)
get "/raw/#{topic.id}/#{whisper.post_number}"
@@ -404,7 +415,7 @@ RSpec.describe PostsController do
sign_in(user)
delete "/posts/#{post.id}.json"
- expect(response).to be_forbidden
+ expect(response.status).to eq(404)
end
it "raises an error when the self deletions are disabled" do
@@ -655,7 +666,7 @@ RSpec.describe PostsController do
sign_in(user)
put "/posts/#{post.id}/recover.json"
- expect(response).to be_forbidden
+ expect(response.status).to eq(404)
end
it "raises an error when self deletion/recovery is disabled" do
@@ -3450,7 +3461,7 @@ RSpec.describe PostsController do
it "throws an exception for users" do
sign_in(user)
get "/posts/#{post.id}/revisions/#{post_revision.number}.json"
- expect(response.status).to eq(403)
+ expect(response.status).to eq(404)
end
it "works for admins" do
diff --git a/spec/requests/topic_view_stats_controller_spec.rb b/spec/requests/topic_view_stats_controller_spec.rb
index 8ece25d6e43..e790dfe844d 100644
--- a/spec/requests/topic_view_stats_controller_spec.rb
+++ b/spec/requests/topic_view_stats_controller_spec.rb
@@ -9,11 +9,31 @@ describe TopicViewStatsController do
expect(response.status).to eq(403)
end
- it "does not allow access to private topics" do
+ it "returns the same not found response for inaccessible and nonexistent topics" do
+ SiteSetting.detailed_404 = false
topic.category.update!(read_restricted: true)
get "/t/#{topic.id}/view-stats.json"
+
+ expect(response.status).to eq(404)
+ inaccessible_topic_response = response.parsed_body
+
+ get "/t/#{topic.id + 1_000_000}/view-stats.json"
+
+ expect(response.status).to eq(404)
+ expect(response.parsed_body).to eq(inaccessible_topic_response)
+ end
+
+ it "returns an access error for private topics when detailed 404 errors are enabled" do
+ SiteSetting.detailed_404 = true
+ topic.category.update!(read_restricted: true)
+
+ get "/t/#{topic.id}/view-stats.json"
+
expect(response.status).to eq(403)
+ expect(response.parsed_body).to include(
+ "errors" => ["You are not permitted to view the requested resource."],
+ )
end
it "returns errors for invalid parameters" do