Repository navigation
Description
Currently params provided to the request phase via the query string in the GET request are stored as session["omniauth.params"]:
omniauth/lib/omniauth/strategy.rb
Line 280 in cc0f552
| session['omniauth.params'] = request.GET |
and then retrieved and made available in the callback phase as request.env["omniauth.params"]:
omniauth/lib/omniauth/strategy.rb
Line 236 in cc0f552
| @env['omniauth.params'] = session.delete('omniauth.params') || {} |
This useful feature, whilst mentioned in third-party blogs like here and here, is still lacking official documentation as mentioned in issue #909. That's unfortunate, but there is a further issue caused by a combination of two facts:
- This only works when the request phase is triggered with
GET. - When mitigating CVE-2015-9284 via the advice given in https://github.com/omniauth/omniauth/wiki/Resolving-CVE-2015-9284, it is necessary to trigger the request phase via
POST, since allowingGET /auth/:provideris unsafe.
So when attempting to write code which uses omniauth safely, there is no obvious way to preserve custom application-specific parameters which are passed at login time.
By looking at lib/omniauth/strategy.rb I managed to figure out that I could do something like:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET']
provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
before_request_phase do |env|
# Prior to login, save user parameters to session so that we can
# retrieve them after authentication in order to update the user's
# preferences.
#
# This API is basically undocumented and was figured from reading
# omniauth's strategy.rb.
env["rack.session"]["user_params"] = env["rack.request.form_hash"]["user"]
end
end
Then later on after authentication has succeeded and the callback redirects to my users_controller.rb, I can access these custom parameters via session[:user_params]. However it does not seem good that developers should have to assume such knowledge about the internals of the middleware in order to write code which safely preserves parameters across the various HTTP requests in the authentication flow. Therefore I suggest that something similar to omniauth.params is implemented for POST requests - it could be called omniauth.form_params, for instance.
And of course, all of this should be clearly documented, and explained in https://github.com/omniauth/omniauth/wiki/Resolving-CVE-2015-9284 too :-)
Activity
gingerlime commentedon May 10, 2021
Also wondered about the same thing, and the search led me here. Is there any willingness to support this from the maintainers? are PRs for this going to be considered? or is it a dead-end?
miguelperez commentedon May 25, 2021
I am using this strategy https://github.com/stevenkaras/omniauth-mailchimp and it does not work like that.
Basically, anything I add to the session in the request phase is lost when I am in the callback controller.
This is using rails 6.0.3
Then in the callback controller
Nothing is printed. Any Idea what could be happening here?
BobbyMcWho commentedon May 25, 2021
I would need to look into any potential ramifications of the changes being discussed, and overall community need
BobbyMcWho commentedon May 25, 2021
I'm not guaranteeing this works, but have you tried accessing with string keys?
miguelperez commentedon May 26, 2021
Yes, I tried that, but still does not work.
sshock commentedon Jun 29, 2021
I also ran into this problem after switching from
GET /auth/:providertoPOST /auth/:provider.I was able to workaround it by looking at
::Rack::Utils.parse_query(URI(request.env["omniauth.origin"]).query)["myparam"]instead ofrequest.env["omniauth.params"]["myparam"].I also tried the strategy above of setting
env["rack.session"]["myparam"] = env.dig("rack.request.form_hash", "myparam")in the before_request_phase, and that also worked.If there is a better way, please let me know.
ibrahima commentedon Jun 17, 2025
FWIW I've found that for me, in current Omniauth, query parameters (even for a POST) do seem to show up in the callback phase in
request.env["omniauth.params"]. I don't know if this is intended, but it is helpful.omniauth/lib/omniauth/strategy.rb
Line 238 in 0bcfd5b
/auth/:providerfor me.Not preserving POST parameters seems to be an intentional change: #867
Keep form params from the request phase in omniauth.params
sferik commentedon Oct 20, 2026
Fixed in ebdec17: the request phase now stores both the query string and the form params in
omniauth.params, so params posted with the (now required) POST request phase come back in the callback phase. The authenticity token is left out, which was the reason POST params were dropped in #867. This will ship in 2.2.0.