From a653513e0a0d9c8a6c4bc673e9c3e819919a8689 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 5 Nov 2019 15:00:33 -0600 Subject: [PATCH] wiki pages: fix 404s for page titles containing dots. Bug: links like these returned 404s: * https://danbooru.donmai.us/wiki_pages/... * https://danbooru.donmai.us/wiki_pages/.hack// * https://danbooru.donmai.us/wiki_pages/ssss.gridman Cause: by default, Rails uses dots in route segments to separate the id from the format. For example, in /wiki_pages/ssss.gridman, the id is parsed as "ssss" and the format is "gridman" (as if "gridman" were a format like "json" or "xml"). We work around this by specifying the regex for the id param manually. The trick here is to use a non-greedy match-all combined with a positive lookahead to detect the extension but not include it in the match. --- config/routes.rb | 12 ++++-------- test/functional/wiki_pages_controller_test.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index abf2475f8..bc5d15dce 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -295,14 +295,10 @@ Rails.application.routes.draw do resource :user_upgrade, :only => [:new, :create, :show] resources :user_feedbacks resources :user_name_change_requests, only: [:new, :create, :show, :index] - resources :wiki_pages do - member do - put :revert - end - collection do - get :search - get :show_or_new - end + resources :wiki_pages, id: /.+?(?=\.json|\.xml|\.html)|.+/ do + put :revert, on: :member + get :search, on: :collection + get :show_or_new, on: :collection end resources :wiki_page_versions, :only => [:index, :show, :diff] do collection do diff --git a/test/functional/wiki_pages_controller_test.rb b/test/functional/wiki_pages_controller_test.rb index 9e7803f42..9de94df07 100644 --- a/test/functional/wiki_pages_controller_test.rb +++ b/test/functional/wiki_pages_controller_test.rb @@ -86,6 +86,19 @@ class WikiPagesControllerTest < ActionDispatch::IntegrationTest get wiki_page_path(@wiki_page.id) assert_redirected_to wiki_page_path(@wiki_page.title) end + + should "work for a title containing dots" do + as(@user) { create(:wiki_page, title: "...") } + + get wiki_page_path("...") + assert_response :success + + get wiki_page_path("....json") + assert_response :success + + get wiki_page_path("....xml") + assert_response :success + end end context "show_or_new action" do