{"id":"CVE-2026-40295","aliases":["GHSA-jp94-3292-c3xv"],"url":"https://o3.security/vulnerability/CVE-2026-40295","summary":"Devise: Open Redirect via Unvalidated `request.referrer` in Timeoutable Session Timeout Handler","details":"## Summary\n\nWhen the `Timeoutable` module is enabled in Devise, the `FailureApp#redirect_url` 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 `attempted_path`) and Devise's own `store_location_for` mechanism (which strips external hosts via `extract_path_from_location`), both of which are protected; only the non-GET timeout redirect path is unprotected.\n\n## Details\n\nThe vulnerable code is in `lib/devise/failure_app.rb`:\n\n```ruby\ndef redirect_url\n  if warden_message == :timeout\n    flash[:timedout] = true if is_flashing_format?\n\n    path = if request.get?\n      attempted_path          # safe: server-side value from warden options\n    else\n      request.referrer        # UNSAFE: HTTP Referer header, attacker-controlled\n    end\n\n    path || scope_url\n  else\n    scope_url\n  end\nend\n```\n\nThis is passed directly to `redirect_to`:\n\n```ruby\ndef redirect\n  store_location!\n  # ...\n  redirect_to redirect_url   # redirect_url may be an external attacker URL\nend\n```\n\nThe GET timeout path uses `attempted_path`, which is set server-side by Warden and cannot be influenced by the client. The `store_location!` method also only runs for GET requests, so no session-based protection is applied on POST timeouts.\n\nBy contrast, Devise's `store_location_for` method (used elsewhere) correctly sanitizes URLs via `extract_path_from_location`, which strips the scheme and host.\n\n## Impact\n\n- 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.\n- The redirect happens transparently via a trusted domain (the target app's domain), bypassing browser phishing warnings.\n- An attacker can redirect victims to a fake login page to harvest credentials (phishing), or to malicious download sites.\n\n_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.action_controller.action_on_open_redirect = :raise` (and the older `raise_on_open_redirects` setting) do not reach it.\n\n## Patches\n\nThis is patched in Devise v5.0.4. Users should upgrade as soon as possible.\n\n## Workaround\n\nNone 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#redirect_url` and `Devise::Controllers::StoreLocation#extract_path_from_location`). Remove the monkey-patch after upgrading.","published":"2026-05-22T19:10:57.039Z","modified":"2026-08-12T03:51:39.705619623Z","cvss":{"score":6.1,"severity":"MEDIUM","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N"},"epss":{"score":0.00241,"percentile":0.14963,"asOf":"2026-08-24"},"cisaKev":null,"exploitsKnown":0,"affectedPackages":[{"ecosystem":"RubyGems","name":"devise","fixedVersion":"5.0.4"}],"fix":{"url":"https://github.com/heartcombo/devise/commit/025fe2124f9928766fc46520e999633b598d0360","label":"heartcombo/devise@025fe21"},"references":[{"type":"ADVISORY","url":"https://github.com/CVEProject/cvelistV5/tree/main/cves/2026/40xxx/CVE-2026-40295.json"},{"type":"ADVISORY","url":"https://github.com/heartcombo/devise/security/advisories/GHSA-jp94-3292-c3xv"},{"type":"ADVISORY","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-40295"},{"type":"FIX","url":"https://github.com/heartcombo/devise/commit/025fe2124f9928766fc46520e999633b598d0360"},{"type":"PACKAGE","url":"https://github.com/heartcombo/devise"},{"type":"WEB","url":"https://github.com/rubysec/ruby-advisory-db/blob/master/gems/devise/CVE-2026-40295.yml"}],"provenance":{"sources":["OSV.dev","FIRST.org (EPSS)"],"lastVerified":"2026-08-12T03:51:39.705619623Z"}}