Skip to content

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.rb
index a6504c9cee0..f85765af1e0 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -266,6 +266,7 @@ class ApplicationController < ActionController::Base
custom_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],
+ }
+ end
end
end
diff --git a/lib/current_user.rb b/lib/current_user.rb
index 6a8b684a95d..6730343af66 100644
--- a/lib/current_user.rb
+++ b/lib/current_user.rb
@@ -42,6 +42,9 @@ module CurrentUser
def current_user
current_user_provider.current_user
+ rescue Discourse::InvalidAccess => e
+ e.opts[:authentication_failed] = true
+ raise
end
def impersonation_acting_user
diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rb
index 52c285698dc..33308164dd4 100644
--- a/spec/requests/application_controller_spec.rb
+++ b/spec/requests/application_controller_spec.rb
@@ -717,6 +717,33 @@ RSpec.describe ApplicationController do
expect(response.body).to include(topic2.title)
end
end
+
+ 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
+ end
end
describe "#handle_theme" do