From e9a0efbbf2aabc915723db49b2f064b8437783a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Min=C3=A1rik?= Date: Mon, 24 Feb 2020 19:41:06 +0100 Subject: [PATCH] Fix broken StoriesController#show article with bad chars (#6248) [deploy] --- app/controllers/application_controller.rb | 4 ++++ app/controllers/stories_controller.rb | 2 ++ spec/requests/stories_show_spec.rb | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 881a47251..155b32225 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -20,6 +20,10 @@ class ApplicationController < ActionController::Base raise NotAuthorizedError, "Unauthorized" end + def bad_request + render json: "Error: Bad Request", status: :bad_request + end + def authenticate_user! return if current_user diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 75defbcd6..fff5474a0 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -14,6 +14,8 @@ class StoriesController < ApplicationController before_action :authenticate_user!, except: %i[index search show] before_action :set_cache_control_headers, only: %i[index search show] + rescue_from ArgumentError, with: :bad_request + def index @page = (params[:page] || 1).to_i @article_index = true diff --git a/spec/requests/stories_show_spec.rb b/spec/requests/stories_show_spec.rb index 08f847f3d..ca3dce732 100644 --- a/spec/requests/stories_show_spec.rb +++ b/spec/requests/stories_show_spec.rb @@ -119,6 +119,13 @@ RSpec.describe "StoriesShow", type: :request do get article.path expect(response.body).not_to include('"canonical" href="' + article.canonical_url.to_s + '"') end + + it "handles invalid slug characters" do + allow(Article).to receive(:find_by).and_raise(ArgumentError) + get article.path + + expect(response.status).to be(400) + end end describe "GET /:username (org)" do