-
Notifications
You must be signed in to change notification settings - Fork 0
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
Set csrf cookies on home page #263
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
"Set-Cookie", | ||
`nyplUserRegistered=false; Max-Age=-1; path=/; domain=${cookieDomain};` | ||
); | ||
let newTokenCookie; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only actual logical update, the rest of the PR is just a refactor with no real functional changes. I don't like how this looks, any suggestions on how to clean it up would be appreciated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Some minor comments but make sure to update the import.
pages/new/index.tsx
Outdated
generateNewCookieTokenAndHash, | ||
generateNewToken, | ||
parseTokenFromPostRequestCookies, | ||
} from "../../src/utils/utils"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be csrfUtils
. That's why the vercel preview fails.
pages/new/index.tsx
Outdated
|
||
import { serialize } from "cookie"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this is imported in CookieUtils
as well. Just to keep things tidy and in one place, export serialize from CookieUtils
so that you can import it from the file and then call cookie.serialize
.
pages/new/index.tsx
Outdated
if (!csrfToken) { | ||
csrfToken = generateNewToken(); | ||
const newTokenCookieString = generateNewCookieTokenAndHash(csrfToken); | ||
newTokenCookie = serialize( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do remove this export from cookie directly from this file and move it to CookieUtils
, you can create a new "serialize" function that only needs the newTokenCookieString
argument since metadata
is already in the same file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
brilliant! thanks. i knew there was something fishy going on there
return { | ||
createHash: jest.fn().mockReturnValue({ | ||
update: () => ({ | ||
digest: () => "666", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙈
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤘🏻
Description
Motivation and Context
The cookie setting in the getServerSideProps of the index page was overwriting the csrf token cookie headers. I refactored the csrf code to be more functional and to avoid unannounced side effects, in order for different flows to update the headers in different ways.
How Has This Been Tested?
All existing unit tests are passing, and I also updated the tests for the validateCsrf methods. I did not exhaustively unit test all of the more minor functions, mostly because they seem to be invoked in larger tests. I do think there is some gaps in the coverage in the validateAddress tests, which I will work on while this is being reviewed.
In order to properly run this code locally (and on live servers as well), you must start with deleting all cookies from the relevant domain. Part of why I missed this in the first place is I had been testing over and over again without deleting cookies from older sessions.
Checklist: