Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails truncating of parameters with '.' in urls

I am trying to send an IP address as a parameter to a Rails method destroy in a url. There is a problem if the parameter includes .; I get not found errors, this is the log generated:

Started DELETE "/admin/user/stefan-admin/whitelist/4.3.2.1" for 127.0.0.1 at 2013-07-17 09:31:18 +0100
Processing by ErrorsController#error_404 as 
  Parameters: {"not_found"=>"admin/user/stefan-admin/whitelist/4.3.2"}
WARNING: Can't verify CSRF token authenticity
Session: {:user=>"admin", :role=>:admin, :user_id=>"stefan-admin"}
Completed 404 Not Found in 30ms (Views: 1.1ms | ActiveRecord: 0.0ms)

The not found message has a truncated ip address. If I use a parameter without ., e.g. abc, I don't get the not found error, and the destroy method is called.

Rails received the url, but then mangled it internally, possibly because it is processing . as an extension. Is there some way to turn off this behaviour or escape the url to avoid it?

like image 258
Stefan Avatar asked Jul 17 '13 08:07

Stefan


2 Answers

You need to add a constraint to the routes to allow dots in params

resources :whitelists, :constraints => { :id => /[0-9.]+/ }

Or something of that kind in your routes.rb, it depends on how you write your routes but the constraints part stay the same

like image 108
yannick Avatar answered Nov 03 '22 07:11

yannick


The reason for the "truncated" ip address is the (optional) (.:format) that resources, 'get', 'match' etc. generate on every route.

You can specify a dedicate route without the format like:

match '/admin/user/:id/whitelist/*ip', to: 'controller#action', format: false

pay attention to the * at the last parameter. It collects the whole rest of the url.

like image 41
Martin M Avatar answered Nov 03 '22 07:11

Martin M