From 9b7525bb03a2965367f32d2aa786eccef6b70606 Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Fri, 20 Nov 2020 11:28:14 +0200 Subject: [PATCH] FIX: Handle uncaught exception (#11263) After the search term is parsed for advanced search filters, the term may become empty. Later, the same term will be passed to Discourse.route_for which will raise an ArgumentError. > URI(nil) ArgumentError: bad argument (expected URI object or URI string) --- lib/discourse.rb | 2 +- lib/search.rb | 11 ++++------- spec/requests/search_controller_spec.rb | 5 +++++ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/discourse.rb b/lib/discourse.rb index 636208587cd..948cf9ac8d0 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -412,7 +412,7 @@ module Discourse unless uri.is_a?(URI) uri = begin URI(uri) - rescue URI::Error + rescue ArgumentError, URI::Error end end diff --git a/lib/search.rb b/lib/search.rb index 7e7e5cfce48..c3f083b00fe 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -266,14 +266,11 @@ class Search if @term =~ /^\d+$/ single_topic(@term.to_i) else - begin - if route = Discourse.route_for(@term) - if route[:controller] == "topics" && route[:action] == "show" - topic_id = (route[:id] || route[:topic_id]).to_i - single_topic(topic_id) if topic_id > 0 - end + if route = Discourse.route_for(@term) + if route[:controller] == "topics" && route[:action] == "show" + topic_id = (route[:id] || route[:topic_id]).to_i + single_topic(topic_id) if topic_id > 0 end - rescue ActionController::RoutingError end end end diff --git a/spec/requests/search_controller_spec.rb b/spec/requests/search_controller_spec.rb index 5d7181f7a86..990bc15d97a 100644 --- a/spec/requests/search_controller_spec.rb +++ b/spec/requests/search_controller_spec.rb @@ -231,6 +231,11 @@ describe SearchController do expect(SearchLog.where(term: 'wookie')).to be_blank end + it "does not raise 500 with an empty term" do + get "/search/query.json", params: { term: "in:first", type_filter: "topic", search_for_id: true } + expect(response.status).to eq(200) + end + context 'rate limited' do it 'rate limits anon searches per user' do SiteSetting.rate_limit_search_anon_user = 2