CVE-2026-40295: Devise: Open Redirect via Unvalidated `request.referrer` in Timeoutable Session Timeout Handler

Published May 8, 2026
·
Updated

Summary

When the Timeoutable module is enabled in Devise, the FailureApp#redirecturl method returns request.referrer — the HTTP Referer header, which is attacker-controllable — without validation for any non-GET request that results in a session timeout. An attacker who hosts a page with an auto-submitting cross-origin form can cause a victim with an expired Devise session to be redirected to an arbitrary external URL. This contrasts with the GET timeout path (which uses server-side attemptedpath) and Devise's own storelocationfor mechanism (which strips external hosts via extractpathfromlocation), both of which are protected; only the non-GET timeout redirect path is unprotected.

Details

The vulnerable code is in lib/devise/failureapp.rb:

ruby def redirecturl if wardenmessage == :timeout flash[:timedout] = true if isflashingformat?

path = if request.get? attemptedpath # safe: server-side value from warden options else request.referrer # UNSAFE: HTTP Referer header, attacker-controlled end

path || scopeurl else scopeurl end end

This is passed directly to redirectto:

ruby def redirect storelocation! # ... redirectto redirecturl # redirecturl may be an external attacker URL end

The GET timeout path uses attemptedpath, which is set server-side by Warden and cannot be influenced by the client. The storelocation! method also only runs for GET requests, so no session-based protection is applied on POST timeouts.

By contrast, Devise's storelocationfor method (used elsewhere) correctly sanitizes URLs via extractpathfromlocation, which strips the scheme and host.

Impact

- Victims with expired sessions who click any attacker-crafted link or visit an attacker page with an auto-submitting form are redirected to an arbitrary external URL. - The redirect happens transparently via a trusted domain (the target app's domain), bypassing browser phishing warnings. - An attacker can redirect victims to a fake login page to harvest credentials (phishing), or to malicious download sites.

Note: Rails' built-in open-redirect protection does not mitigate this issue. Devise::FailureApp is an ActionController::Metal app with its own isolated copy of the relevant redirect configuration, so config.actioncontroller.actiononopenredirect = :raise (and the older raiseonopenredirects setting) do not reach it.

Patches

This is patched in Devise v5.0.4. Users should upgrade as soon as possible.

Workaround

None beyond upgrading. If an upgrade is not immediately possible, the same changes from the patch commit can be applied as a monkey-patch in a Rails initializer (Devise::FailureApp#redirecturl and Devise::Controllers::StoreLocation#extractpathfromlocation). Remove the monkey-patch after upgrading.

Other sources

Devise is an authentication solution for Rails based on Warden. In versions 5.0.3 and below, when the Timeoutable module is enabled in Devise, the FailureApp#redirecturl method returns request.referrer — the HTTP Referer header, which is attacker-controllable — without validation for any non-GET request that results in a session timeout. An attacker who hosts a page with an auto-submitting cross-origin form can cause a victim with an expired Devise session to be redirected to an arbitrary external URL. This contrasts with the GET timeout path (which uses server-side attemptedpath) and Devise's own storelocationfor mechanism (which strips external hosts via extractpathfromlocation), both of which are protected; only the non-GET timeout redirect path is unprotected. Expired-session users can be silently redirected from the trusted app domain to attacker-controlled URLs, enabling phishing and malware delivery while bypassing browser warnings. Note: Rails' built-in open-redirect protection does not mitigate this issue. Devise::FailureApp is an ActionController::Metal app with its own isolated copy of the relevant redirect configuration, so config.actioncontroller.actiononopenredirect = :raise (and the older raiseonopenredirects setting) do not reach it. This issue has been fixed in version 5.0.4.

MITRE

Affected Software

2 affected componentsFixes available
rubygems/devise<=5.0.3
5.0.4
Heartcombo Devise Ruby<5.0.4

Remediation

Recommended actions to resolve this vulnerability, in priority order.

  1. Upgrade

    Upgrade rubygems/devise to a version that resolves this vulnerability.

    Fixed in 5.0.4
  2. Upgrade

    Upgrade Devise to a version that resolves this vulnerability.

    Fixed in 5.0.4
  3. Configuration

    If upgrade is not immediately possible, apply the patch-commit changes as a monkey-patch in a Rails initializer: override `Devise::FailureApp#redirect_url` so that it does not return `request.referrer` for the non-GET Timeoutable session timeout redirect path (where the current vulnerable code returns the HTTP Referer header, attacker-controlled, without validation). After upgrading, remove this monkey-patch.

    Devise (Rails initializer monkey-patch) Devise::FailureApp#redirect_url = Use sanitized path instead of request.referrer (apply changes from the patch commit via a Rails initializer monkey-patch)
  4. Configuration

    If upgrade is not immediately possible, include the corresponding change from the patch commit for URL sanitization by ensuring `Devise::Controllers::StoreLocation#extract_path_from_location` strips the scheme and host (so redirects use only the path, not an external attacker URL). After upgrading, remove this monkey-patch.

    Devise::Controllers::StoreLocation extract_path_from_location = Ensure it strips scheme and host from locations (use `extract_path_from_location` behavior)
  5. Operational

    After upgrading to Devise 5.0.4, remove the temporary Rails initializer monkey-patch for `Devise::FailureApp#redirect_url` and `Devise::Controllers::StoreLocation#extract_path_from_location`.

Event History

May 8, 2026
Advisory Published
via GitHub·03:41 PM
Data Sourced
via GitHub·03:41 PM
DescriptionSeverityWeaknessAffected Software
May 22, 2026
CVE Published
via MITRE·07:10 PM
Data Sourced
via MITRE·07:10 PM
DescriptionSeverityWeakness
Data Sourced
via NVD·08:16 PM
RemedyDescriptionSeverityWeaknessAffected Software

Frequently Asked Questions

1

What is the severity of CVE-2026-40295?

CVE-2026-40295 is considered a high severity vulnerability due to the risk of open redirect attacks.

2

How do I fix CVE-2026-40295?

To fix CVE-2026-40295, update the Devise gem to version 5.0.4 or later.

3

What type of applications are affected by CVE-2026-40295?

CVE-2026-40295 affects Ruby on Rails applications using the Devise gem with the Timeoutable module enabled.

4

Does CVE-2026-40295 affect GET requests?

No, CVE-2026-40295 primarily affects non-GET requests that trigger a session timeout.

5

How can an attacker exploit CVE-2026-40295?

An attacker can exploit CVE-2026-40295 by manipulating the HTTP Referer header to redirect users to a malicious site after a session timeout.

Contact

SecAlerts Pty Ltd.
132 Wickham Terrace
Fortitude Valley,
QLD 4006, Australia
info@secalerts.co
By using SecAlerts services, you agree to our services end-user license agreement. This website is safeguarded by reCAPTCHA and governed by the Google Privacy Policy and Terms of Service. All names, logos, and brands of products are owned by their respective owners, and any usage of these names, logos, and brands for identification purposes only does not imply endorsement. If you possess any content that requires removal, please get in touch with us.
© 2026 SecAlerts Pty Ltd.
ABN: 70 645 966 203, ACN: 645 966 203