I'm seeing two exceptions thrown every time a user is redirected to a provider for login. They are similar to the ones reported in #2990036 but not exactly the same. I have tested with an implementation we developed (Social Auth PBS) and the Github implementation.
Whenever a user hits the callback URL (e.g. /user/login/pbs or /user/login/github) the following two exceptions are logged --
LogicException: The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\Core\Routing\TrustedRedirectResponse. in Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (line 154 of [...]/docroot/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php).
RuntimeException: Failed to start the session because headers have already been sent by "[...]/vendor/symfony/http-foundation/Response.php" at line 1286. in Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->start() (line 141 of [...]/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php)
#0 [...]/docroot/core/lib/Drupal/Core/Session/SessionManager.php(164): Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->start()
#1 [...]/docroot/core/lib/Drupal/Core/Session/SessionManager.php(195): Drupal\Core\Session\SessionManager->startNow()
#2 [...]/vendor/symfony/http-foundation/Session/Session.php(196): Drupal\Core\Session\SessionManager->save()
#3 [...]/docroot/core/lib/Drupal/Core/StackMiddleware/Session.php(60): Symfony\Component\HttpFoundation\Session\Session->save()
#4 [...]/docroot/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#5 [...]/docroot/core/modules/page_cache/src/StackMiddleware/PageCache.php(184): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#6 [...]/docroot/core/modules/page_cache/src/StackMiddleware/PageCache.php(121): Drupal\page_cache\StackMiddleware\PageCache->fetch(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#7 [...]/docroot/core/modules/page_cache/src/StackMiddleware/PageCache.php(75): Drupal\page_cache\StackMiddleware\PageCache->lookup(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#8 [...]/docroot/modules/contrib/jsonapi/src/StackMiddleware/FormatSetter.php(41): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#9 [...]/docroot/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\jsonapi\StackMiddleware\FormatSetter->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#10 [...]/docroot/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#11 [...]/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#12 [...]/docroot/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#13 [...]/docroot/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#14 {main}.
The exceptions appear to be triggered somewhere in \Drupal\social_auth\Controller\OAuth2ControllerBase::redirectToProvider() but I haven't been able to troubleshoot very far. I don't think this was happening before 8.x-2.0-beta5, but I'm not 100% sure of that.
| Comment | File | Size | Author |
|---|---|---|---|
| #70 | interdiff-37-70.txt | 1.2 KB | wells |
| #70 | 3033444-70.patch | 7.69 KB | wells |
| #37 | interdiff-35-37.txt | 587 bytes | gvso |
| #37 | 3033444-37.patch | 7.35 KB | gvso |
Comments
Comment #2
wellsOh and to be clear -- this doesn't appear to prevent any functionality. I only noticed the log entries while looking in to something unrelated. Login for new and existing users works fine.
Comment #3
wellsComment #4
wellsI dug up a detailed write up from Lullabot for the
LogicExceptionand sorted out that this happens because the URL generation process adds cacheable metadata to the response. See: Early Rendering: A Lesson in Debugging Drupal 8.The attached patch uses the approach outlined in that article to capture and bubble up the metadata with
\Drupal\social_auth\Controller\OAuth2ControllerBase::redirectToProvider()'sTrustedRedirectResponsebut it seems that this would still be an API change requiring changes to implementors becauseOAuth2ControllerBaseneeds to inject the\Drupal\Core\Render\Rendererservice. E.g. to test this with Social Auth Github I had to modifyGitHubAuthControllerto injectRendererfor it'sparent::__constructcall.Comment #5
wellsOne small change to my patch in #4 -- I moved the
try ... catchblock forPluginExceptioninsideexecuteInRenderContextso that exception can actually be picked up.Comment #6
shaundychkoComment #7
shaundychkoWhen using
social_auth_googlethe errors reported here prevented logging in. I had to downgrade back to beta-4.Comment #8
shaundychkoA little patch review:
At
$response = \Drupal::service('renderer')->executeInRenderContext($context, function() {The class property
$renderershould be used.Comment #9
wellsD'oh! Whole reason for making API changes (:
Thanks for catching that. Updated patch attached.
Comment #10
gvsoThank you for this! I will be reviewing the provided documentation and patch this week. Even if this is not what I wanted, we might not have other options than to change the API to include the renderer.
@ShaunDychko, you got errors when using Social Auth Google? I've been testing Social Auth Google, and it works fine for me. What Drupal version are you using?
Comment #11
shaundychko@gvso I'm using the latest 8.6.9, and was also using 8.6.7. I included more DB log output in the duplicate issue I originally reported at #3033225: Logic Exception and RuntimeException after upgrade to beta-4. I'm also running PHP 7.2.
Comment #12
proxiss commented@gvso I got errors with Social Auth Google, too. These are hard to clarify. Completely new (for Google) users do work (on registration), some old Google users do not work (null Exception on registration and login), some old users do work (on login).
Version is 8.6.9 (upgraded from working 8.6.2), drupal/social_api 2.0.0-beta5, drupal/social_auth 2.0.0-beta5, drupal/social_auth_google 2.0.0-beta4, league/oauth2-google 3.0.1.
Comment #13
gvsoI updated the patch. No much changes, but I fix coding standard violations.
I also upload a patch for Social Auth Google for testing. Please, let me know if this works since I consider this a major issue if Social Auth Google stopped working.
Comment #15
shaundychkoThe two patches above fixed the 2 DB log errors that occurred upon redirect to Google.
However, returning back to the site at
/user/login/google/callbackresults in the following fatal error:Comment #16
shaundychko"fatal error" means white screen of death with the message
Comment #17
gvsoHmm. Would you mind sharing your set up? I am using Drupal 8.6 and Apache when testing this.
Comment #18
shaundychkoThe issue is a conflict with another module I have installed, but despite hours coming to that conclusion I haven't figured out which one. With the patches in #13, things work fine on a clean install.
As for your question about my setup, I created https://github.com/ShaunDychko/test-social-auth so that you could have the exact same setup on Platform.sh. I made that repo with the intention for you to see the error, but in fact it works fine.
I don't have an issue with module conflicts on the beta-3 version of social_auth_google (using my fork at https://github.com/ShaunDychko/social_auth_google to have league/oauth2-google:^3.0, and corresponding beta version of the other social_* modules), so I'll stick to that for now, but maybe find time later to investigate further which module is conflicting.
Comment #19
gvsoCool. It'd be nice to see if this works for others as well.
Comment #20
shaundychkoLatest update is that there's no module conflict. The issue is solved when I use Devel's "Reinstall Module" for
social_auth. It's not enough to reinstallsocial_auth_google. Since this requires deleting all social_auth entities, it's not a viable solution for the production site, so I suppose the mystery continues...Comment #21
wells@gvso, I finally got around to testing your patches and they work for a fresh install for me. I don't have any services using SA Google in production and I couldn't replicate @ShaunDychko's issue with just my test data.
@ShaunDychko, I wonder if there is any way you can replicate the problematic site setup without exposing sensitive data? Curious to see why the reinstall might have helped as the changes are really pretty minor.
Comment #22
shaundychko@wells, I've tested a fresh install with the same setup I'm currently using in production (matching beta versions), succeeded in creating an account via Google, then updated to the latest dev. along with patches from #13, and had bitter-sweet success. The upgrade path seems to be fine, but upgrading nevertheless fails on my production site.
Comment #23
spokjeFWIW:
- I've successfully applied both patches in #13.
- Both Exceptions didn't appear on both registering and log-in after patching, they where there before.
- Didn't experience the Fatal Error mentioned by @ShaunDychko
- Obviously the patch on
social_authbreaks other depending modules likesocial_auth_facebookandsocial_auth_twitterwith a nice WSOD onArgumentCountError: Too few arguments to function Drupal\social_auth\Controller\OAuth2ControllerBase::__construct(), 8 passed in /app/web/modules/contrib/social_auth_facebook/src/Controller/FacebookAuthController.php on line 42 and exactly 9 expected in Drupal\social_auth\Controller\OAuth2ControllerBase->__construct() (line 107 of /app/web/modules/contrib/social_auth/src/Controller/OAuth2ControllerBase.php)(The above is a
social_auth_facebookexample)Note:
I applied the patches on
"drupal/social_auth": "2.0-beta5"and"drupal/social_auth_google": "2.0-beta4"so _not_ on the dev versions.Comment #24
ndf commented#23 That WSOD
ArgumentCountError: Too few argumentsshould be fixed here I think too. With semantic versioning, this is probably a BC-break that needs a higher version.For backwards compatibility
RendererInterface $renderer = NULLis better here.The removed line could be the default value of $response if $renderer is NULL.
And I think a proper automated test would help a lot here to prevent issues like this in the future.
Comment #25
wellsDoes the approach suggested in #24 make sense if the exceptions are breaking logins in some cases anyway (see #7)? In my case this would help because the error doesn't actually prevent login or registration, but for others supporting a nullable renderer may just confuse things. But maybe that is still preferable to a WSOD... if we did do that, should we catch the exceptions and write something about updating the provider module to the site log? I'm not sure how/if it is possible to determine if the result is a completely failed login or just the exceptions being logged.
Comment #26
gvsoI was thinking that this might be an issue, but I was also thinking of adding backward compatibility by using \Drupal::service() if NULL. We can then remove this change later.
Comment #27
wellsAh, yeah I think that approach makes sense and is much simpler than trying to catch exceptions or sort out breakage level.
Comment #28
gvsoComment #29
m.lebedev commentedHi! I have a problem when a auth path has value "/user/login/google?destination=/some". The destination parameter does not allow to auth. I used patches #13 and #27.
Also I use require_login module. After apply the patch patch #13 or #27 I started getting problems. If I delete require_login module then auth works as usual.
I found that earlier...
Comment #30
gvso@m.lebedev, does this happens with Nginx? That was introduced in #2981737: Destination parameter forbids user to be redirected for authentication
Comment #31
m.lebedev commentedYes, it does. I use nginx + php-fpm. There is the same problem
Comment #32
wellsI applied and tested #28 successfully for both providers with the renderer service injected and without. Tested providers were PBS, GitHub and Slack. No dblog messages and login worked fine.
I’ll leave this to someone else to confirm with a Google oauth flow before we go RTBC.
Comment #33
ndf commentedI also applied #28 and tested with social_auth_google 8.x-2.0-beta4
Messages are gone, works perfect.
I also read through
social_auth/src/Controller/OAuth2ControllerBase.phpAlthough I don't understand everything the class seems very well documented and formatted.
So here we go: RTBC
Comment #34
gvsoThanks everyone! We still need to fix the issue with Nginx. I don't know if that would break the current solution. I'll have to try
Comment #35
gvsoI think this might solve all the warnings and errors.
Comment #36
wellsHm, I'm inclined to think that should be taken up in a separate issue. I also wonder if this is really the place to be fixing this? I can't find a clear reference for this at the moment, but my understanding is that modules should never be using
$response->send();directly, only returning a response to be sent by the kernel.Comment #37
gvsoIt turned out, I couldn't reproduce the bug as I used to do with the issue I referenced to. I removed the save method anyways since it's no longer used.
@m.lebedev, can you test this patch? I tested it with Nginx + PHP-FPM and by adding destination parameters.
Comment #38
eahonet commentedI can now login with an existing user, but cannot create users as it used to do before updating.
I've tried patches #13 #28 #35
I do use composer.
I have turned on that visitors can create accounts. I've uninstalled require_login and swiftmailer.
When I try to login with Google, I get to enter in my google user/pass, and when it goes back to my site, it gives me the below (only on non-existing users)
The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">LogicException</em>: The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller? in <em class="placeholder">Symfony\Component\HttpKernel\HttpKernel->handleRaw()</em> (line <em class="placeholder">169</em> of <em class="placeholder">/home/[user]/sites/main/vendor/symfony/http-kernel/HttpKernel.php</em>).Comment #39
wells@eahonet, did you try the patch in #37? And can you tell us more about your setup? I just tested successfully with a fresh, patched installed of Social Auth with the Github and Slack providers -- new users were created and no errors given.
Comment #40
gvsoIt seems like there's a conflict with require_login and Social Auth
Comment #41
m.lebedev commentedit still does not work with the destionation parameter. (nginx)
Comment #42
m.lebedev commentedThe request is sent when I go to the auth path. The request is correct.

after that, redirection occurs to the destination.
the require_login checks the path that is forbidden to go to anonymous users and the require_login redirects on the login page.
Comment #43
eahonet commentedUpdate from me. I am running up to date versions of
And the patch from #37
And it works!
I was having issues with creating accounts... but turns out my database was messed up from previous tests months ago and this solved the new user creation issue:
https://www.drupal.org/project/social_auth/issues/2914598#comment-13004018
My swiftmailer issue was unrelated and is now resolved.
I am still working to get require_login to work and that will be my focus this morning. I have been running require_auth and social_auth_google for many months with clients... so am looking forward to fixing this as it's the last issue before I can roll out all updates to production. And I don't know if the problem is related to social_auth at all... I'm guessing not.
Comment #44
wells@m.lebedev & @eahonet, could you clarify what versions of Social Auth and Social Auth Google currently *do* work with Require Login? Is it this patch that causes issues or something else on the dev branch? If I get some time today I'll also try to do some testing with this, I'm just not familiar with the Require Login module.
Comment #45
gvsoDoes this mean Social Auth works for you on PHP-FPM if require_login is not enabled?
Comment #46
eahonet commentedI am trying to get require_login:2.1 working with it.
Under /admin/config/people/require-login , I use these Path exclusions
I've also tried this patch: https://www.drupal.org/project/require_login/issues/3024495
I've tried looking more closely at /src/EventSubscriber/RequireLoginSubscriber.php (same area as targeted by patch above) and even just commenting out the return false that I thought was triggering it not to work... but still can't get past it.
-----
To better answer your question @wells, if I wanted to get a working version going of all these things, I run this command (I use the drupal composer project)
rm composer.lock; rm -Rf ./vendor/*; rm -Rf ./web/modules/contrib/*; composer require 'drupal/social_auth_google:2.x-dev#e3d47a3bab8eae2bc39b5167e9526135cd02f4e2' 'drupal/social_auth:2.x-dev#7ad52ff8eb621c5a7c4d4dbf0e8fb4c0070fbd60' 'drupal/social_api:2.x-dev#f92b77aa0cbffce13c16c7f7ffc56e4e47498b86'; drush crI then install require_login 1.12
With these patches:
Comment #47
gvso@eahonet, Did Social Auth (and require_login) work for you before this patch or before beta 5?
Comment #48
eahonet commented@gvso The only working require_login I've seen work was the older 1.12 with the older versions of social_auth 2.0-dev. require_login went through an overhaul recently and is now on 2.1.
I did make some progress and posted it here:
https://www.drupal.org/project/require_login/issues/3038652
It seems that when I go to /search... require_login says "no, you need to login first". Then when I try to login with social_auth_google, it starts the process, but ends up redirecting to /search before things finish... so no login finishes and then the user is taken back to the login page.
Comment #49
gvsoI don't think the error with require_login should be fixed here (not even that it should be fixed in Social Auth). Anyways, that issue should be handled separately, especially because this issue is about a major bug in Social Auth.
The only thing I'm concerned about is the compatibility with PHP FPM. I could run the authentication with destination successfully in my local server, but I would like to have input from @m.lebedev whether Social Auth works if require_login is disabled.
Comment #50
m.lebedev commentedYes. social_auth works If require_login is disabled.Comment #51
m.lebedev commentedSorry. I checked one time again. When require_login is disabled:
1) social_auth 8.x-2.0-beta5 without patches
Auth works
2) social_auth 8.x-2.0-beta5 with patches.
Auth doesn't work
Comment #52
m.lebedev commentedSource: /core/lib/Drupal/Core/EventSubscriber/RedirectResponseSubscriber.php
There is a change of target url.
Comment #53
m.lebedev commentedQuick fix.
Upd: perhaps needing to create a static variable and after need to check if it exists in response filtering
Availability of oauth2state validation does not provide sufficient accuracy
Comment #54
gvsoThat might work, but I wonder if we really need to go through all that "trouble" to solve it. Any thoughts?
Comment #55
eahonet commentedI do have an update. My path to social_auth_google + require_login + swiftmailer has made for a long week. I do have all of them fully working together now (we're testing this week with client). Some of the things I tried/thought were wrong. The final piece for me was removing the social_auth patch from this thread. My system had same/similar error... but it was something else... something older. I am running a more traditional LAMP stack with no nginx.
I wanted to at least update this thread and wish everyone luck. Also wanted to say thank you to those smarter than I that are working on these patches and these modules.
Comment #56
wells@eahonet, could you clarify one thing — does applying one of the last few patches in this thread break your working setup or just have no effect on it?
Comment #57
eahonet commented@wells - social_auth + require_login breaks with the #37 patch. It all started working when I removed the patch and rebuilt /vender and /modules/contrib. I'm not sure why or where... but it felt like something having to do with redirects. (https://www.drupal.org/project/require_login/issues/3038652)
Comment #58
wellsThanks, @eahonet. I have just tested with the following and had success --
Require login configured with the following path exclusions:
Social Auth with the GitHub provider works fine as long as no destination parameter is set, this is likely what you observed in your testing, @eahonet.
I also tested adding a
/user/login/*?destination=*path exclusion, but it seems that parameters will not accept wildcards so that doesn't work as a workaround.Regardless, I don't think this compatibility problem belongs in the scope of this particular issue. I think we should apply the patch in #37 and open a new issue to explore the Reqire login/destination problem, perhaps starting with @m.lebedev's research and a reroll of his patch. It looks more like this would be an issue for Require login, not Social Auth, but I'm not 100% sure about that.
Comment #59
m.lebedev commentedthis would be an issue for Social Auth because I disabled require login last time
Comment #60
wellsSorry, @m.lebedev, I missed that.
Since @gvso wasn’t able to reproduce with nginx+php-fpm, could you tell us more about your complete setup? Perhaps to include relevant nginx and php-fpm config?
You said you tested with a patched Social Auth 8.x-2.0-beta5. Could you also test with a patched 8.x-2.x-dev? And have you tested with a fresh D8 install with just Social Auth and it’s requiremnts?
I will try to review #53 later if I can get the time to build out the environment you having issues with.
Comment #61
eahonet commented@wells
require_login + the patch from this https://www.drupal.org/project/social_auth/issues/3033444 will ignore any ? get parameters on exclusion paths. That's what I use. It still works to have
/user/login?destination=/search
redirect to /search after login (drupal login). But social_auth_google has a parameter of where to go and is set to just go to the homepage.
It's when I tried using the above patch (which I guess I don't need on my setup now) that logging in with Google via Social Auth breaks.... or works as long as require_login is uninstalled.
Comment #62
wells@eahonet, I understand what you are saying and my test showed the same.
The important point that I got from my test, and why I don’t think this problem belongs with this issue, is that login with a patched Social Auth works and the reported errors are fixed regardless of whether or not Require login is installed (with proper exclusion configuration).
What doesn’t work is accessing login when a destination parameter is set. That, to me, signals an issue with Require login — it prevents us from accessing, e.g.,
/user/login/google?destination=/search, before even getting to a point where Social Auth would take action. But if you just go directly to/user/login/googleauthentication works fine.Comment #63
m.lebedev commentedMy configs:
Nginx
PHP-FPM
I checked the problem on the fresh drupal installation. I also checked last dev version of Social Auth.
The problem occurs if OAuth2ControllerBase doesn't run $response->send();
Comment #64
gvsoI think this issue has become very complex while the initial motivation was to avoid the warnings and errors. I propose the following
$response->send()in a different issue that might include something like the patch in #53Does this sound good? Any suggestions?
Comment #65
wells@gvso, my initial finding was that the sending instead of returning the response is preciscely what causes the
RuntimeExceptionwarning. Did you test #35 with an nginx config and not get any errors? If so I think your plan makes sense.Earlier I had enough time to get an nginx+php-fpm D8 going (thanks, @m.lebedev for providing your configs) but didn’t get around to an actual test. I should be able to do that within the next 8 or so hours if you didn’t get a chance to or want me to confirm.
Comment #66
wellsOk, here is the setup I used --
Host
Drupal
Test Results
No patches applied
Login successful -
LogicExceptionandRuntimeExceptionlogged.#35 patch applied
Login failed -
RuntimeExceptionlogged and error message, "Login failed. Invalid OAuth2 state. ".#37 patch applied
Login successful - no exceptions logged.
#53 patch applied
Login successful - no exceptions logged.
Conclusions
After these tests, I still think #37 is the correct patch to address this issue and #35, in my test, doesn't even get me logged in. #57 seems like a lot of extra work to me and I don't think Social Auth should be stopping propagation of the
KernelEvents::RESPONSEevent for fear of causing issues with other contrib modules that act on that event (but I'm also not well versed in the events system on the whole).I don't mean to ignore @m.lebedev's issue here, and since we got different results it may make sense for someone else to run similar tests, but I am fairly sure that
$response->send()will always result in theRuntimeExceptionerror due to the fact the Drupal kernel is always handling the response.Two additional notes re: my php-fpm setup, I used a socket as opposed to a local server and I only set the bare minimum FPM config (i.e. I excluded @m.lebedev's PHP config settings in the fpm pool config). The results could indicate some incompatibility issue with certain configurations of Nginx+PHP+PHP-FPM, but again I think that is not a topic for this issue (and I'd be happy to help investigate in another issue).
Comment #67
gvsoCan you confirm you actually mean 35 and 37 in the correct order? The detail about what works and what doesn't seem to contradict this conclusion
Comment #68
wellsShoot, sorry I mixed that up. I still recommend #37 over the rest. I’ll correct my comment above. Thanks.
Comment #69
m.lebedev commentedIt looks like my problem is not related to this module. I found an issue that describes what is happening.
I also created a module that reproduces the problem. After installing the module, go to "/drupal-test?destination=/" and there will be a redirect to the main page, but in fact there should be a redirect to google com.
I think we can close this issue and use patch #37.
Thanks.
UPD: I see that people solve this problem in their modules. For example.
We can do a similar solution.
It works for me. Auth works and redirect to destination works after auth.
Comment #70
wellsThanks, for digging that up, @m.lebedev! Patch attached with your recommended change. Testing and working for me with Apache and nginx.
Comment #72
gvsoThank you everyone! I'll create new release later tonight
Comment #73
gvsoI created a new release. Hope everything works fine now. Thank you once more. We have to update the implementers now.
Comment #74
ndf commentedThanks guys!
Comment #76
itaran commentedhad the similar problem with social_auth_twitter module, since TwitterAuthController doesn't extend OAuth2ControllerBase need separate patch to solve the problem https://www.drupal.org/files/issues/2020-08-12/3068326.patch, related issue https://www.drupal.org/project/social_auth_twitter/issues/3068326