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.
Impact
A race condition in Devise's Confirmable module allows an attacker to confirm an email address they do not own. This affects any Devise application using the reconfirmable option (the default when using Confirmable with email changes).
By sending two concurrent email change requests, an attacker can desynchronize the confirmationtoken and unconfirmedemail fields. The confirmation token is sent to an email the attacker controls, but the unconfirmedemail in the database points to a victim's email address. When the attacker uses the token, the victim's email is confirmed on the attacker's account.
Patches
This is patched in Devise v5.0.3. Users should upgrade as soon as possible.
Workarounds
Applications can override this specific method from Devise models to force unconfirmedemail to be persisted when unchanged: (assuming your model is User)
ruby class User < ApplicationRecord protected
def postponeemailchangeuntilconfirmationandregenerateconfirmationtoken unconfirmedemailwillchange! super end end
Note: Mongoid does not seem to respect that willchange! should force the attribute to be persisted, even if it did not really change, so you might have to implement a workaround similar to Devise by setting changedattributes["unconfirmedemail"] = nil as well.