A platform for community discussion. Free, open, simple.
FIX: stop logging every time invalid params are sent
Sam5b630f318867
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex e24baddfff7..ff5abffe881 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -252,7 +252,11 @@ class ApplicationController < ActionController::Baseif show_json_errors# HACK: do not use render_json_error for topics#showif request.params[:controller] == 'topics' && request.params[:action] == 'show'- return render status: status_code, layout: false, plain: (status_code == 404 || status_code == 410) ? build_not_found_page(status_code) : message+ return render(+ status: status_code,+ layout: false,+ plain: (status_code == 404 || status_code == 410) ? build_not_found_page(status_code) : message+ )endrender_json_error message, type: type, status: status_codediff --git a/lib/middleware/discourse_public_exceptions.rb b/lib/middleware/discourse_public_exceptions.rbindex 4770b427934..de34d61cb3e 100644--- a/lib/middleware/discourse_public_exceptions.rb+++ b/lib/middleware/discourse_public_exceptions.rb@@ -16,6 +16,10 @@ module Middlewareexception = env["action_dispatch.exception"]response = ActionDispatch::Response.new+ # Special handling for invalid params, in this case we can not re-dispatch+ # the Request object has a "broken" .params which can not be accessed+ exception = nil if Rack::QueryParser::InvalidParameterError === exception+if exceptionbeginfake_controller = ApplicationController.newdiff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rbindex fb64f91b0b5..03c5488f119 100644--- a/spec/requests/application_controller_spec.rb+++ b/spec/requests/application_controller_spec.rb@@ -15,6 +15,33 @@ RSpec.describe ApplicationController doendend+ describe 'invalid request params' do+ before do+ @old_logger = Rails.logger+ @logs = StringIO.new+ Rails.logger = Logger.new(@logs)+ end++ after do+ Rails.logger = @old_logger+ end++ it 'should not raise a 500 (nor should it log a warning) for bad params' do+ bad_str = "d\xDE".force_encoding('utf-8')+ expect(bad_str.valid_encoding?).to eq(false)++ get "/latest.json", params: { test: bad_str }++ expect(response.status).to eq(400)+ expect(@logs.string).not_to include('exception app middleware')++ expect(JSON.parse(response.body)).to eq(+ "status" => 400,+ "error" => "Bad Request"+ )+ end+ end+describe 'build_not_found_page' dodescribe 'topic not found' do