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.rbindex 13be32d0535..65d86bd8263 100644--- a/app/controllers/post_actions_controller.rb+++ b/app/controllers/post_actions_controller.rb@@ -7,7 +7,7 @@ class PostActionsController < ApplicationControllerbefore_action :fetch_post_action_type_id_from_paramsdef 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.rbindex 5c422c04984..d3a75203de3 100644--- a/app/controllers/posts_controller.rb+++ b/app/controllers/posts_controller.rb@@ -1097,9 +1097,12 @@ class PostsController < ApplicationControllerenddef 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 < ApplicationControllerraise Discourse::NotFound unless guardian.can_moderate_topic?(post.topic)end- guardian.ensure_can_see!(post)+ raise Discourse::NotFound unless guardian.can_see?(post)postenddiff --git a/app/controllers/topic_view_stats_controller.rb b/app/controllers/topic_view_stats_controller.rbindex 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 < ApplicationControllerdef index- topic = Topic.find(params[:topic_id].to_i)+ topic = Topic.find_by(id: params[:topic_id].to_i)+ raise Discourse::NotFound unless topicfrom = 30.days.ago.to_dateto = Date.today@@ -15,7 +16,14 @@ class TopicViewStatsController < ApplicationControllerreturnend- 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+ endrows = []diff --git a/spec/requests/post_actions_controller_spec.rb b/spec/requests/post_actions_controller_spec.rbindex 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 doexpect(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" dosign_in(user)pm = Fabricate(:private_message_post, user: coding_horror)@@ -113,7 +113,16 @@ RSpec.describe PostActionsController dopost_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)endit "returns 404 when flagging a hidden topic" do@@ -234,7 +243,7 @@ RSpec.describe PostActionsController doexpect(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" dopost_1 = Fabricate(:private_message_post, user: Fabricate(:user))post "/post_actions.json",@@ -243,7 +252,7 @@ RSpec.describe PostActionsController dopost_action_type_id: PostActionType.types[:like],}- expect(response).to be_forbidden+ expect(response.status).to eq(404)endit "allows us to create an post action on a post" dodiff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rbindex 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" dotopic.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)endit "returns 200 for an accessible post" do@@ -100,6 +100,17 @@ RSpec.describe PostsController dolet(: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 testnew_post = create_post@@ -251,11 +262,11 @@ RSpec.describe PostsController dosign_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 dosign_in(user)delete "/posts/#{post.id}.json"- expect(response).to be_forbidden+ expect(response.status).to eq(404)endit "raises an error when the self deletions are disabled" do@@ -655,7 +666,7 @@ RSpec.describe PostsController dosign_in(user)put "/posts/#{post.id}/recover.json"- expect(response).to be_forbidden+ expect(response.status).to eq(404)endit "raises an error when self deletion/recovery is disabled" do@@ -3450,7 +3461,7 @@ RSpec.describe PostsController doit "throws an exception for users" dosign_in(user)get "/posts/#{post.id}/revisions/#{post_revision.number}.json"- expect(response.status).to eq(403)+ expect(response.status).to eq(404)endit "works for admins" dodiff --git a/spec/requests/topic_view_stats_controller_spec.rb b/spec/requests/topic_view_stats_controller_spec.rbindex 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 doexpect(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 = falsetopic.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."],+ )endit "returns errors for invalid parameters" do