-
-
Notifications
You must be signed in to change notification settings - Fork 141
refactor(http): improve session management and CSRF protection #1829
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
Draft
innocenzi
wants to merge
28
commits into
3.x
Choose a base branch
from
refactor/session
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f1cf5a9 to
f6cce2d
Compare
8abdae0 to
0a64315
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
NOT READY FOR REVIEW. Don't mind the diff, this will be rebased on
3.xafter #1819 is merged.Closes #1734
Closes #1699
This pull request improves the implementation details of multiple parts of the
httppackage, separates the "previous URL", the validation errors and the form original values from the session, and implements CSRF protection throughSec-Fetch-*headers instead of a CSRF token.CSRF protection
I replaced the CSRF protection from a classic CSRF token approach to using
Sec-Fetch-SiteandSec-Fetch-Mode, which are baseline.This cleans up a bit of code, removes the hard-coded CSRF token from the session, removes the
<x-csrf />component and its need in<x-form>. And it's modern.Precedents: rails/rails#56350
Discord discussion: https://discord.com/channels/1236153076688359495/1236153079603269766/1449963794515361896
Previous URL
In an effort to decouple specific feature from the
Sessionobject, I ended up with a nice isolatedPreviousUrlclass and kept a middleware like before.The
PreviousUrlclass depends on the session and has utilities to track the previous and intended URLs:Validation errors and original values
In the same effort to decouple features from the
Sessionobject, I created aFormSessionclass, similar to thePreviousUrlclass: it isolates features related to forms, their validation errors and their original values.The
HtmlExceptionRendererintroduced in #1819 is still responsible for filling the proper values when validation errors occur. The components that were using the session directly now useFormSession.I'm not sure about this name though. I thought of
HtmlFormState, or justFormState, but none of those sound particularly good.These changes should be transparent to users unless they manually reached for
Session::VALIDATION_ERRORSor$session->getErrorsFor(), which was generally not required thanks to the view components.SessionandSessionManagerSo, I was never a fan of dependency location in
Session, especially given the fact that this class is meant to be serialized. My initial goal with this PR was to refactor it to make it a singleton value object and to improve performance of session managers.Sessionnow only holds its data and do nothing else. It doesn't have methods to save the session, destroy it or anything. Those are now the sole responsibility ofSessionManagerimplementations, which have also been simplified.I improved the performance of session manager by limiting their IO calls. Previously, they would load/save when retrieved and on any change. Now, they load once and save once, ad the start of the request and at the end. The
ManageSessionLifecycleMiddlewaremiddleware is responsible for the last part, but also implicitly the first part sinceSessionis lazy-loaded using an initializer.Sessiona standalone objectSessionManagerSessionManagerinterface more focusedSec-Fetch-*implementation