Backo Backo - 5 months ago 13x
Ruby Question

Is it safe to accept URL parameters for populating the `url_for` method?

I am using Ruby on Rails 4.1.1 and I am thinking to accept parameters (through URL query strings) that are passed directly to the

method, this way:

# URL in the browser[controller]=users&redirect_to[action]=show&redirect_to[id]=1

# Controller
redirect_to url_for(params[:redirect_to].merge(:only_path => true))

Adopting the above approach users can be redirected after performing an action. However, I think people can enter arbitrary
that can lead to security issues...

Is it safe to accept URL parameters for populating the
method? What are pitfalls? What can happen in the worst case?

By logging
during requests to my application I noted Rails adds always
parameters. Maybe that confirms
can be used the above way since it is protected internally and works as-like Rails is intended to.


This it is safe internally as Ruby On Rails will only be issuing a HTTP redirect response.

As you are using only_path this will protect you from an Open redirect vulnerability. This is where an email is sent by an attacker containing a link in the following format (say your site is

As the user checks the URL and sees it is on the domain they beleive it is safe so click the link. However, if there's an open redirect then the user ends up on which could ask for their password without the user noticing.

Redirecting to a relative path only on your site fixes any vulnerability.

In your case you are giving users control of your controller, action and parameters. As long as your GET methods are safe (i.e. no side-effects), an attacker could not use this by creating a crafted link that the user opens.

In summary, from the information provided I don't see any risk from phishing URLs to your application.