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.
This commit is contained in:
evazion
2019-11-05 15:00:33 -06:00
parent aeb5edaff6
commit a653513e0a
2 changed files with 17 additions and 8 deletions

View File

@@ -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

View File

@@ -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