From e8de80de98e8dce33b4cf33a9b7b70324f6f2b2e Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 25 Feb 2016 11:32:16 +0800 Subject: [PATCH] FIX: Default to first page when page params is an array. --- app/controllers/topics_controller.rb | 8 ++++++-- lib/topic_view.rb | 7 ++----- spec/controllers/topics_controller_spec.rb | 5 +++++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index d30f1008c..fb6465bb3 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -49,6 +49,10 @@ class TopicsController < ApplicationController # existing installs. return wordpress if params[:best].present? + # work around people somehow sending in arrays, + # arrays are not supported + params[:page] = params[:page].to_i rescue 1 + opts = params.slice(:username_filters, :filter, :page, :post_number, :show_deleted) username_filters = opts[:username_filters] @@ -65,7 +69,7 @@ class TopicsController < ApplicationController raise Discourse::NotFound end - page = params[:page].to_i + page = params[:page] if (page < 0) || ((page - 1) * @topic_view.chunk_size > @topic_view.topic.highest_post_number) raise Discourse::NotFound end @@ -529,7 +533,7 @@ class TopicsController < ApplicationController url << "/#{post_number}" if post_number.to_i > 0 url << ".json" if request.format.json? - page = params[:page].to_i + page = params[:page] url << "?page=#{page}" if page != 0 redirect_to url, status: 301 diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 752c04b46..97b903ec5 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -43,10 +43,7 @@ class TopicView self.instance_variable_set("@#{key}".to_sym, value) end - # work around people somehow sending in arrays, - # arrays are not supported - @page = @page.to_i rescue 1 - @page = 1 if @page.zero? + @page = 1 if (!@page || @page.zero?) @chunk_size = options[:slow_platform] ? TopicView.slow_chunk_size : TopicView.chunk_size @limit ||= @chunk_size @@ -183,7 +180,7 @@ class TopicView return filter_posts_by_ids(opts[:post_ids]) if opts[:post_ids].present? return filter_best(opts[:best], opts) if opts[:best].present? - filter_posts_paged(opts[:page].to_i) + filter_posts_paged(@page) end def primary_group_names diff --git a/spec/controllers/topics_controller_spec.rb b/spec/controllers/topics_controller_spec.rb index 69281fca3..92f36ba5f 100644 --- a/spec/controllers/topics_controller_spec.rb +++ b/spec/controllers/topics_controller_spec.rb @@ -563,6 +563,11 @@ describe TopicsController do expect(response).to redirect_to(topic.relative_url + "/42?page=123") end + it 'does not accept page params as an array' do + xhr :get, :show, id: topic.slug, post_number: 42, page: [2] + expect(response).to redirect_to("#{topic.relative_url}/42?page=1") + end + it 'returns 404 when an invalid slug is given and no id' do xhr :get, :show, id: 'nope-nope' expect(response.status).to eq(404)