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

Set csrf cookies on home page #263

Merged
merged 17 commits into from
Feb 13, 2025
Merged

Conversation

charmingduchess
Copy link
Collaborator

@charmingduchess charmingduchess commented Feb 12, 2025

Description

  • Refactor getCsrfToken into lots of little functinos, most notably methods to validate and parse the tokens from the request body and cookies, as well as to set the new csrf token on the response headers.

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:

  • I have updated the documentation accordingly.
  • All new and existing tests passed.

Copy link

vercel bot commented Feb 12, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nypl-library-card-app ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 12, 2025 7:00pm

@charmingduchess charmingduchess changed the title Scc 4533/fix csrf cookies Set csrf cookies on home page Feb 12, 2025
"Set-Cookie",
`nyplUserRegistered=false; Max-Age=-1; path=/; domain=${cookieDomain};`
);
let newTokenCookie;
Copy link
Collaborator Author

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.

Copy link
Member

@EdwinGuzman EdwinGuzman left a 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.

generateNewCookieTokenAndHash,
generateNewToken,
parseTokenFromPostRequestCookies,
} from "../../src/utils/utils";
Copy link
Member

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.


import { serialize } from "cookie";
Copy link
Member

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.

if (!csrfToken) {
csrfToken = generateNewToken();
const newTokenCookieString = generateNewCookieTokenAndHash(csrfToken);
newTokenCookie = serialize(
Copy link
Member

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.

Copy link
Collaborator Author

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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤘🏻

@charmingduchess charmingduchess merged commit 5b55161 into main Feb 13, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants