A platform for community discussion. Free, open, simple.
FIX: Return 403 instead of 500 for invalid API key on topic JSON (#45215)
Krzysztof Kotlarekf0a2d01aaeea
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex a6504c9cee0..f85765af1e0 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -266,6 +266,7 @@ class ApplicationController < ActionController::Basecustom_message: e.custom_message,custom_message_params: e.custom_message_params,group: e.group,+ authentication_failed: e.opts[:authentication_failed],)end@@ -358,20 +359,23 @@ class ApplicationController < ActionController::Base}if show_json_errors+ authentication_failed = opts[:authentication_failed]opts = { type: type, status: status_code }- with_resolved_locale do- # Include error in HTML format for topics#show.- if (request.params[:controller] == "topics" && request.params[:action] == "show") ||- (- request.params[:controller] == "categories" &&- request.params[:action] == "find_by_slug"- )- opts[:extras] = {- title: I18n.t("page_not_found.page_title"),- html: build_not_found_page(error_page_opts),- group: error_page_opts[:group],- }+ if !authentication_failed+ with_resolved_locale do+ # Include error in HTML format for topics#show.+ if (request.params[:controller] == "topics" && request.params[:action] == "show") ||+ (+ request.params[:controller] == "categories" &&+ request.params[:action] == "find_by_slug"+ )+ opts[:extras] = {+ title: I18n.t("page_not_found.page_title"),+ html: build_not_found_page(error_page_opts),+ group: error_page_opts[:group],+ }+ endendenddiff --git a/lib/current_user.rb b/lib/current_user.rbindex 6a8b684a95d..6730343af66 100644--- a/lib/current_user.rb+++ b/lib/current_user.rb@@ -42,6 +42,9 @@ module CurrentUserdef current_usercurrent_user_provider.current_user+ rescue Discourse::InvalidAccess => e+ e.opts[:authentication_failed] = true+ raiseenddef impersonation_acting_userdiff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rbindex 52c285698dc..33308164dd4 100644--- a/spec/requests/application_controller_spec.rb+++ b/spec/requests/application_controller_spec.rb@@ -717,6 +717,33 @@ RSpec.describe ApplicationController doexpect(response.body).to include(topic2.title)endend++ describe "with invalid API credentials" do+ fab!(:topic)+ fab!(:category)++ let(:headers) { { "Api-Key" => "invalid-api-key", "Api-Username" => "system" } }++ it "returns 403 for topics#show" do+ get "/t/#{topic.id}.json", headers: headers++ expect(response.status).to eq(403)+ end++ it "returns 403 for categories#find_by_slug" do+ get "/c/#{category.slug}/find_by_slug.json", headers: headers++ expect(response.status).to eq(403)+ end++ it "returns 403 for a user API key without the required scope" do+ user_api_key = Fabricate(:bookmarks_calendar_user_api_key)++ get "/t/#{topic.id}.json", headers: { "User-Api-Key" => user_api_key.key }++ expect(response.status).to eq(403)+ end+ endenddescribe "#handle_theme" do