Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the order of the CSRF and the private response listener #2074

Merged
merged 7 commits into from
Aug 5, 2020

Conversation

ausi
Copy link
Member

@ausi ausi commented Aug 4, 2020

Q A
Fixed issues Fixes #2067

The problem in #2067 was the order of the CSRF and make-response-private listeners: First, the make-response-private listener kept the response public, then the CSRF listener added a cookie-header to remove the CSRF cookie. This “cookie deletion” was then stored in the HTTP cache and every subsequent access to this resource always resulted in the CSRF cookie to get removed.

I’m not sure what the “correct” priorities for them are, but I think both listeners should come very late. /cc @Toflar

ToDo

  • Unit test to make sure the relative order is kept in the future.

@ausi ausi added the bug label Aug 4, 2020
@ausi ausi added this to the 4.9 milestone Aug 4, 2020
@ausi ausi requested a review from a team August 4, 2020 13:06
@ausi ausi self-assigned this Aug 4, 2020
@Toflar
Copy link
Member

Toflar commented Aug 4, 2020

Nice find. Looks good to me. However, I think we need a comment and an extension test here to ensure the order of the listeners like we did for others that depend on the priority to prevent regressions.

/edit: didn't see your todo or maybe you added it later on :D

@ausi
Copy link
Member Author

ausi commented Aug 4, 2020

/edit: didn't see your todo or maybe you added it later on :D

Added it later ☺️

@ausi
Copy link
Member Author

ausi commented Aug 4, 2020

The order of all listeners is now:

Priority Listener
1000 Contao\CoreBundle\EventListener\InitializeControllerListener
256 Contao\CoreBundle\EventListener\MergeHttpHeadersListener
10 FOS\HttpCacheBundle\EventListener\CacheControlListener
0 TrustedCookieResponseListener_c7f9b85
0 Lexik\Bundle\MaintenanceBundle\Listener\MaintenanceListener
0 Nelmio\SecurityBundle\EventListener\ContentTypeListener
0 Nelmio\SecurityBundle\EventListener\ReferrerPolicyListener
0 Contao\CoreBundle\EventListener\PreviewToolbarListener
0 Contao\CoreBundle\EventListener\StoreRefererListener
0 Symfony\Component\HttpKernel\EventListener\ResponseListener
0 Symfony\Component\HttpKernel\EventListener\SurrogateListener
0 Symfony\Component\HttpKernel\DataCollector\RequestDataCollector
0 Symfony\Component\Security\Http\RememberMe\ResponseListener
0 Nelmio\SecurityBundle\EventListener\ClickjackingListener
0 Nelmio\SecurityBundle\EventListener\ContentSecurityPolicyListener
0 Nelmio\SecurityBundle\EventListener\XssProtectionListener
0 FOS\HttpCacheBundle\EventListener\TagListener
0 Symfony\Component\Security\Http\Firewall\ContextListener
-98 Contao\CoreBundle\EventListener\CsrfTokenCookieSubscriber
-99 Contao\CoreBundle\EventListener\MakeResponsePrivateListener
-100 Symfony\Component\HttpKernel\EventListener\ProfilerListener
-128 Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener
-255 Symfony\Component\HttpKernel\EventListener\DisallowRobotsIndexingListener
-255 Contao\CoreBundle\EventListener\SubrequestCacheSubscriber
-768 Contao\CoreBundle\EventListener\ClearSessionDataListener
-1000 Symfony\Component\HttpKernel\EventListener\SessionListener
-1024 Symfony\Component\HttpKernel\EventListener\StreamedResponseListener

As I don’t know what most of them do, I’m not sure if -99 and -98 is correct?

@ausi
Copy link
Member Author

ausi commented Aug 4, 2020

I think we need a comment and an extension test here to ensure the order of the listeners like we did for others that depend on the priority to prevent regressions.

Done.

@Toflar
Copy link
Member

Toflar commented Aug 4, 2020

Imho the listeners should also come after ClearSessionDataListener as that one clears expired session data. The SubrequestCacheSubscriber can be ignored because it only handles subrequests. I think. /cc @aschempp

@ausi
Copy link
Member Author

ausi commented Aug 4, 2020

The SubrequestCacheSubscriber can be ignored because it only handles subrequests.

Sure? Because it checks the Cache-Control header which is a header we set in the make-private listener.

@m-vo
Copy link
Member

m-vo commented Aug 4, 2020

Would it make sense to always leave gaps between the priorities?
So that I could in theory register my own listener to run in between? 🙂

@aschempp
Copy link
Member

aschempp commented Aug 5, 2020

Without checking the code, I would assume the SubrequestCacheSubscriber merges cache headers from subrequests into the main response, so it affects all requests.

@ausi
Copy link
Member Author

ausi commented Aug 5, 2020

Imho the listeners should also come after ClearSessionDataListener as that one clears expired session data.

So, how about -896 (exactly between 768 and 1024) for MakeResponsePrivateListener and -832 (exactly between 768 and 896) for CsrfTokenCookieSubscriber?

@leofeyer leofeyer merged commit d0ec9de into contao:4.9 Aug 5, 2020
@leofeyer
Copy link
Member

leofeyer commented Aug 5, 2020

Thank you @ausi.

@leofeyer leofeyer changed the title Fix order of CSRF and private response listeners Fix the order of the CSRF and the private response listener Aug 10, 2020
@ausi ausi deleted the fix/csrf-cookie-cached-response branch December 3, 2021 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants