Rails: ignore InvalidPage exception in production #271

Open
graudeejs opened this Issue Oct 1, 2012 · 8 comments

Comments

Projects
None yet
8 participants

in our logs we found this problem

ArgumentError: invalid value for Integer(): 'xoforvfmy' in "[GEM_ROOT]/gems/will_paginate-3.0.2/lib/will_paginate/page_number.rb" on line 16.

Some web crawler requested page=xoforvfmy

I tried to find best place to fix this, but failed.

relavent backtrace:

[GEM_ROOT]/gems/will_paginate-3.0.2/lib/will_paginate/page_number.rb:16→ Integer
[GEM_ROOT]/gems/will_paginate-3.0.2/lib/will_paginate/page_number.rb:16→ initialize
[GEM_ROOT]/gems/will_paginate-3.0.2/lib/will_paginate/page_number.rb:54→ new
[GEM_ROOT]/gems/will_paginate-3.0.2/lib/will_paginate/page_number.rb:54→ PageNumber
[GEM_ROOT]/gems/will_paginate-3.0.2/lib/will_paginate/active_record.rb:142→ page

quainjn commented Oct 5, 2012

I've been getting the same error periodically for the last couple weeks.

ArgumentError: invalid value for Integer(): "vvxvpgwxofhmvf"

(and: "jqhscznljnhlxr", "rixhuvnazokxf", "rksoidrehnh", "lbnbyiqvta")

[GEM_ROOT]/gems/will_paginate-3.0.3/lib/will_paginate/page_number.rb:16:in 'Integer'
[GEM_ROOT]/gems/will_paginate-3.0.3/lib/will_paginate/page_number.rb:16:in 'initialize'
[GEM_ROOT]/gems/will_paginate-3.0.3/lib/will_paginate/page_number.rb:54:in 'new'
[GEM_ROOT]/gems/will_paginate-3.0.3/lib/will_paginate/page_number.rb:54:in 'PageNumber'
[GEM_ROOT]/gems/will_paginate-3.0.3/lib/will_paginate/active_record.rb:142:in 'page'
[GEM_ROOT]/gems/will_paginate-3.0.3/lib/will_paginate/active_record.rb:133:in 'paginate'

best practice would be to check if your params id is a number and greater than 0, I also changed this in my fork and made a pull-reques

params[:page] = 'foobar'
posts = Post.where(:published => true).page(params[:page])
#posts.current_page = 1

# set default_page if necessary 
posts = Post.where(:published => true).page(params[:page], :default_page => 10)
#posts.current_page = 10
# this raises no exception, it trys to return the default_page

have a look at #276

Owner

mislav commented Jan 10, 2013

The exception is deliberate. However, in production will_paginate configures Rails to swallow these exceptions and return 404s. You shouldn't be seening those in the exception tracker.

If you see these exceptions in production, tell me your Rails version and output of rake middleware

mislav closed this Jan 10, 2013

@mislav I'm seeing this exception in production also.
Rails Version: 3.1.10

Exception:

invalid value for Integer(): "6/"
will_paginate (3.0.3) lib/will_paginate/page_number.rb:16:in Integer' will_paginate (3.0.3) lib/will_paginate/page_number.rb:16:ininitialize'
will_paginate (3.0.3) lib/will_paginate/page_number.rb:54:in new' will_paginate (3.0.3) lib/will_paginate/page_number.rb:54:inPageNumber'
will_paginate (3.0.3) lib/will_paginate/active_record.rb:142:in page' will_paginate (3.0.3) lib/will_paginate/active_record.rb:133:inpaginate'

Owner

mislav commented Feb 10, 2013

OK, my bad. will_paginate just configures Rails to display a 404 for that exception in production, but the exception is still thrown internally, and exception trackers will log it.

I'll have to rethink this for future versions

mislav reopened this Feb 10, 2013

j15e commented Feb 4, 2014

Hi there,

I got this issue in production on website with a lot of traffic (always get strange wrong queries).

I think a quick & clean solution can be to add a rails route constraint to avoid hitting your controller with wrong values (and yeah otherwise it should raise, but maybe a custom exception would be nicer to catch it & render a 404. Oh, seems like it now the case, nvm.).

Anyway, here it goes for my solution on the routes level :

# lib/will_paginate_constraint.rb
class WillPaginateConstraint
  def matches?(request)
    request.params[:page].nil? || /^\d+$/ =~ request.params[:page]
  end
end
# config/routes.rb
match '/', :action => :index, :controller => :publications,
  :constraints => WillPaginateConstraint.new

@mislav mislav added todo and removed invalid labels Jun 18, 2014

Any update on this? It's been a todo for a year and a half. I'm still seeing this logged in our production exception logger. I'd ignore it in my exception tracker but it's an exception coming from ruby and not will paginate.

sintro commented May 4, 2016

What if I want to handle the InvalidPage exception manually in my ApplicationController? As I can assume now, it is handled by middleware and outputs the 404 static page, without any possibility to override this behavior (without monkeypatching of gem`s source).

hisayohorie referenced this issue in openaustralia/planningalerts May 4, 2017

Merged

Issue#669 bad page number parameters causing ArgumentError #1144

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment