From 4180e207c31cfdb4fa3be127481b57db9a72a15a Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 23 Mar 2016 12:13:29 -0400 Subject: [PATCH] FIX: Crazy large ids should not raise exceptions --- app/controllers/application_controller.rb | 11 ++++++++++- spec/controllers/topics_controller_spec.rb | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 558ec2f6038..2db84795883 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -100,7 +100,16 @@ class ApplicationController < ActionController::Base class PluginDisabled < StandardError; end - rescue_from Discourse::NotFound, PluginDisabled do + # Handles requests for giant IDs that throw pg exceptions + rescue_from RangeError do |e| + if e.message =~ /ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer/ + rescue_discourse_actions(:not_found, 404) + else + raise e + end + end + + rescue_from Discourse::NotFound, PluginDisabled do rescue_discourse_actions(:not_found, 404) end diff --git a/spec/controllers/topics_controller_spec.rb b/spec/controllers/topics_controller_spec.rb index 92f36ba5f8f..d06b4ff1cd7 100644 --- a/spec/controllers/topics_controller_spec.rb +++ b/spec/controllers/topics_controller_spec.rb @@ -578,6 +578,11 @@ describe TopicsController do expect(response.status).to eq(404) end + it 'returns a 404 for an ID that is larger than postgres limits' do + xhr :get, :show, topic_id: 50142173232201640412, slug: 'topic-that-is-made-up' + expect(response.status).to eq(404) + end + context 'a topic with nil slug exists' do before do @nil_slug_topic = Fabricate(:topic)