Skip to content
Open redirect Disclosed report Intermediate

HTTP Host injection in redirect_to function

The redirect_to method in Ruby on Rails concatenates the request host with a user-supplied path, and the host is taken from the Host or X-Forwarded-Host header. An attacker can inject a malicious host to redirect users to an attacker-controlled site, potentially enabling password reset poisoning or web cache poisoning.

komang4130 Follow

Originally disclosed on HackerOne

  • Disclosed after 12 months

Hi team,

Here is the sample vulnerable code

Ruby
class TesttestController < ApplicationController
  def index
    redirect_to  "/demo/?your_reset_token=your_reset_token"
  end
end

The ** _compute_redirect_to_location** will take the input "/demo/?your_reset_token=your_reset_token" as options variables. File File action_controller\metal\redirecting.rb line 63

Ruby
    def redirect_to(options = {}, response_options = {})
      raise ActionControllerError.new("Cannot redirect to nil!") unless options
      raise AbstractController::DoubleRenderError if response_body

      self.status        = _extract_redirect_to_status(options, response_options)
      self.location      = _compute_redirect_to_location(request, options)
      self.response_body = "<html><body>You are being <a href=\"#{ERB::Util.unwrapped_html_escape(response.location)}\">redirected</a>.</body></html>"
    end

Then it will check if the options, because the input is String, so it will be the concatenate of request.protocol + request.host_with_port + options File action_controller\metal\redirecting.rb line 96

Ruby
    def _compute_redirect_to_location(request, options) #:nodoc:
      case options
      # The scheme name consist of a letter followed by any combination of
      # letters, digits, and the plus ("+"), period ("."), or hyphen ("-")
      # characters; and is terminated by a colon (":").
      # See https://tools.ietf.org/html/rfc3986#section-3.1
      # The protocol relative scheme starts with a double slash "//".
      when /\A([a-z][a-z\d\-+\.]*:|\/\/).*/i
        options
      when String
        request.protocol + request.host_with_port + options
      when Proc
        _compute_redirect_to_location request, instance_eval(&options)
      else
        url_for(options)
      end.delete("\0\r\n")
    end
    module_function :_compute_redirect_to_location
    public :_compute_redirect_to_location

The request.protocol will be http:// or https:// The request.host_with_port will call raw_host_with_port to check if there is the X_FORWARD_FOR else, it will take the input from HTTP_HOST then continue the _compute_redirect_to_location process. file action_dispatch\http\url.rb line 220

Ruby
      def raw_host_with_port
        if forwarded = x_forwarded_host.presence
          forwarded.split(/,\s?/).last
        else
          get_header("HTTP_HOST") || "#{server_name || server_addr}:#{get_header('SERVER_PORT')}"
        end
      end

So at the end, our input will be request.protocol + request.host_with_port + options and being set as Header["location"] value.

We cant change the HTTP_HOST to something else because the HTTP_HOST must be declare in the the config.host and raise 403 Status ( see the img4.JPG )

But if we use the IP, we can bypass this and here is the result, which 149.28.128.52 is the attacker IP, and there is no check for IP address but only the hostname.

The img2.JPG is the PoC request when use HTTP_HOST. The img3.JPG is the PoC request when use HTTP_X_FORWARD_HOST.

Fix: There should be a check for IP ADDRESS as well as the HOSTNAME in the enviroment config if user dont specific the host as parameter for redirect_to function.

Impact

Password Reset Poisoning - which user use the HTTP_HOST as the input for the reset token. Web-cache - which user use the HTTP_HOST as the input

Attachments

img2.JPG
img2.JPG From the original on HackerOne
img3.JPG
img3.JPG
img4.JPG
img4.JPG

The original report as published on HackerOne, by komang4130. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the application uses redirect_to with a relative path, causing Rails to build an absolute URL using the untrusted Host or X-Forwarded-Host header without validating that the host is an IP address or allowed hostname.

Impact

An attacker could perform password reset poisoning or web cache poisoning by manipulating the redirect location, but the attack is limited because the Host header must match config.host unless an IP address is used.

What the attacker needed

Application uses redirect_to with relative path; attacker can control Host or X-Forwarded-Host header

The fix

The application should validate both IP addresses and hostnames in the environment configuration when the host is not explicitly provided to redirect_to.

How it was found

  1. Send a request with a malicious Host header or X-Forwarded-Host header containing an attacker-controlled IP address.
  2. Observe that the server responds with a redirect to the attacker's IP address.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When a web application redirects you to another page, it often builds the full address using the address you typed in your browser. If the application trusts that address without checking, an attacker can make it point to their own site. This report shows that the redirect_to function in Ruby on Rails does exactly that, so an attacker could trick users into visiting a malicious site.

What is Open redirect?

A trusted site forwards users to any address an attacker picks; rarely paid alone, often the missing link in a chain.

More from Ruby on Rails

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Open redirect

The lesson behind this report.

A trusted site forwards users to any address an attacker picks; rarely paid alone, often the missing link in a chain.