The Host Authorization middleware in Action Pack is vulnerable to crafted X-Forwarded-Host values
The Host Authorization middleware in Rails compared the X-Forwarded-Host header without lowercasing it, so a mixed-case or uppercase host failed the allow-list check and was treated as absent. An attacker could then have the application redirect a victim to an arbitrary external site.
Originally disclosed on HackerOne
- Fixed in 2 hours
- Paid in 2 hours
- Disclosed after 30 days
Title: The Host Authorization middleware in Action Pack is vulnerable to crafted X-Forwarded-Host values Scope: https://github.com/rails/rails Weakness: Open Redirect Severity: Medium Link: https://hackerone.com/reports/1189310 Date: 2021-05-09 06:29:19 +0000 By: @mshtawy CVE IDs: CVE-2021-22942, CVE-2021-22881
Details:
Steps to reproduce
This is a follow up to CVE-2021-22881 and https://github.com/rails/rails/commit/83a6ac3fee8fd538ce7e0088913ff54f0f9bcb6f
with a controller like the following
class TestsController < ApplicationController
extend ActiveSupport::Concern
def index
redirect_to('/')
end
end
when sending a request like the following where the URL has a mixed case characters
curl 'http://localhost:3000/tests' -H 'X-Forwarded-Host: Evil.com'
Or all capital case
curl 'http://localhost:3000/tests' -H 'X-Forwarded-Host: EVIL.COM'
Expected behavior
<div id="container">
<h2>To allow requests to evil.com, add the following to your environment configuration:</h2>
<pre>config.hosts << "Evil.com"</pre>
</div>
Actual behavior
<html><body>You are being <a href="http://Evil.com/">redirected</a>.</body></html>%
System configuration
Rails version: Tested on Rails 6.1.3.1 and Rails 6.1.3.2 Ruby version: N/A
Notes
This was fixed in main in this PR https://github.com/rails/rails/pull/41435 but still affects <= 6.1.3.1
The problem is in this code https://github.com/rails/rails/blob/6-1-stable/actionpack/lib/action_dispatch/middleware/host_authorization.rb#L115
origin_host = valid_host.match(
request.get_header("HTTP_HOST").to_s.downcase)
forwarded_host = valid_host.match(
request.x_forwarded_host.to_s.split(/,\s?/).last)
forwarded_host is missing a downcase after the .to_s, which results in nil assigned to forwarded_host, which then results in true in the following code
origin_host && @permissions.allows?(origin_host[:host]) && (forwarded_host.nil? || @permissions.allows?(forwarded_host[:host]))
because of the nil? check on the forwarded_host
forwarded_host.nil? || @permissions.allows?(forwarded_host[:host])
The examples I gave are using localhost, but I also confirmed this using a production environment with a configuration like the following
Rails.application.config.hosts = %w(.EXAMPLE.com)
Impact
Hackers can redirect victims to a malicious website.
Timeline: 2021-08-03 20:27:55 +0000: @tenderlove (bug triaged)
2021-09-07 20:30:35 +0000: @tenderlove (cve id added)
2021-10-05 20:37:19 +0000: @tenderlove (bug resolved)
Impact
Hackers can redirect victims to a malicious website.
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
The forwarded host value was matched against the allow-list without being downcased, so a case-mismatched host produced nil and the nil check let the request through.
Impact
An attacker could redirect victims to a malicious website, though the text notes the issue was fixed in main and still affected versions up to 6.1.3.1.
What the attacker needed
App uses Host Authorization allow-list; attacker can set X-Forwarded-Host; victim follows a crafted link
The fix
The forwarded host should be downcased before matching against the allow-list, which the text says was done in main via a pull request.
How it was found
- Set up a controller action that redirects to the root path.
- Send a request to that action with an X-Forwarded-Host header containing a mixed-case or uppercase external host.
- Observe that instead of the host-blocked page, the app returns a redirect to that external host.
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.
Rails has a safety check that only lets requests through for approved hostnames. It read the hostname from a header the client controls but forgot to normalise its letter case, so a value like an uppercase version of a blocked host slipped past the check. The app then trusted that host and could send the user to an attacker's 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.