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

Allow ssr cookies #1318

Merged
merged 18 commits into from
Jun 30, 2023
Merged

Allow ssr cookies #1318

merged 18 commits into from
Jun 30, 2023

Conversation

bredmond-sf
Copy link
Collaborator

@bredmond-sf bredmond-sf commented Jun 26, 2023

Description

SSR currently blocks setting cookies and strips them from requests because our CloudFront doesn't pass cookies. We are adding an environment setting to allow it in CloudFront and this allows the setting to disable the cookie blocking code from here.

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update
  • Breaking change (could cause existing functionality to not work as expected)
  • Other changes (non-breaking changes that does not fit any of the above)

Breaking changes include:

  • Removing a public function or component or prop
  • Adding a required argument to a function
  • Changing the data type of a function parameter or return value
  • Adding a new peer dependency to package.json

Changes

  • Put setting in SSR code to disable cookie blocking
  • Add test endpoint to MRT app for cookies

How to Test-Drive This PR

  • Locally set MRT_ALLOW_COOKIES to true in env or configure localAllowCookies to true and then see that cookies work
  • Use /cookie?name=test&value=test in mrt app to check

Checklists

General

  • Changes are covered by test cases
  • CHANGELOG.md updated with a short description of changes (not required for documentation updates)

Accessibility Compliance

You must check off all items in one of the follow two lists:

  • There are no changes to UI

or...

Localization

  • Changes include a UI text update in the Retail React App (which requires translation)

hajinsuha1
hajinsuha1 previously approved these changes Jun 27, 2023
Copy link
Collaborator

@hajinsuha1 hajinsuha1 left a comment

Choose a reason for hiding this comment

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

LGTM!

@bredmond-sf bredmond-sf marked this pull request as ready for review June 27, 2023 18:30
@bredmond-sf bredmond-sf requested a review from a team as a code owner June 27, 2023 18:30
Copy link
Collaborator

@bfeister bfeister left a comment

Choose a reason for hiding this comment

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

@bredmond-sf don't we want to test this in local dev / build-dev-server.js?

I was thinking about our recent conversation and realized that although we don't have .env.development you could still test this locally via SSR_ALLOW_COOKIES=true && npm start if you add the change there

Copy link
Collaborator

@bfeister bfeister left a comment

Choose a reason for hiding this comment

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

Also... @bredmond-sf you need to run npm run lint:fix from the monorepo root to get passing tests and fix your linting errors

@@ -138,6 +141,12 @@ export const RemoteServerFactory = {
// This is the ORIGIN under which we are serving the page.
// because it's an origin, it does not end with a slash.
options.appOrigin = process.env.APP_ORIGIN = `${options.protocol}://${options.appHostname}`

if ('SSR_ALLOW_COOKIES' in process.env) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

superr nit: based on this discussion we could strip the ssr_ prefix everywhere and just use allow_cookies throughout the codebase

Copy link
Collaborator

Choose a reason for hiding this comment

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

As per our Slack discussion, we'll want to use a reserved prefix here to avoid clashes with user provided environment variables.

A good name would MRT_ALLOW_COOKIES as the MRT prefix is reserved and there is emerging precedent for using it in the code.

Comment on lines 117 to 118
// Toggle cookies being passed and set
ssrAllowCookies: false
Copy link
Collaborator

Choose a reason for hiding this comment

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

The impact of this setting, is that when set during local development, the development server will not strip cookies sent in HTTP requests and will not strip cookies set in HTTP responses.

I don't believe we need the ssr prefix here.

Is there a name we could use that suggests that this setting is only for local development and is not respected in MRT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

localAllowCookies would work for me. devAllowCookies could work but seems less clear to me.


if ('SSR_ALLOW_COOKIES' in process.env) {
// Toggle cookies being passed and set
options.ssrAllowCookies = process.env.SSR_ALLOW_COOKIES?.toLowerCase() === 'true'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice guard. This is also the right typing here, because Lambda will pass us in a string.

johnboxall
johnboxall previously approved these changes Jun 27, 2023
Copy link
Collaborator

@johnboxall johnboxall left a comment

Choose a reason for hiding this comment

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

Please address the naming of the env var and server setting, then we're in good shape to ...

🚢 🚢 🚢

Copy link
Collaborator

@bfeister bfeister left a comment

Choose a reason for hiding this comment

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

Looks good 🚢

@bredmond-sf
Copy link
Collaborator Author

@bredmond-sf don't we want to test this in local dev / build-dev-server.js?

I was thinking about our recent conversation and realized that although we don't have .env.development you could still test this locally via SSR_ALLOW_COOKIES=true && npm start if you add the change there

@bfeister I dug a bit and understand this better. I'm using the mixin now in dev server

@bredmond-sf
Copy link
Collaborator Author

Please address the naming of the env var and server setting, then we're in good shape to ...

🚢 🚢 🚢

@johnboxall I think I've got this right now

@bredmond-sf
Copy link
Collaborator Author

Also... @bredmond-sf you need to run npm run lint:fix from the monorepo root to get passing tests and fix your linting errors

@bfeister thank you, done!

bfeister
bfeister previously approved these changes Jun 29, 2023
@@ -899,6 +915,9 @@ export const RemoteServerFactory = {
* contain both the certificate and the private key.
* @param {function} customizeApp - a callback that takes an express app
* as an argument. Use this to customize the server.
* @param {Boolean} [options.ssrAllowCookies] - This boolean value indicates
Copy link
Collaborator

Choose a reason for hiding this comment

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

should this now be:

Suggested change
* @param {Boolean} [options.ssrAllowCookies] - This boolean value indicates
* @param {Boolean} [options.allowCookies] - This boolean value indicates

@@ -920,8 +939,9 @@ export const RemoteServerFactory = {
* ExpressJS middleware that processes any non-proxy request passing
* through the Express app.
*
* Strips Cookie headers from incoming requests, and configures the
* Response so that it cannot have cookies set on it.
* If ssrAllowCookies is false, strips Cookie headers from incoming requests, and
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* If ssrAllowCookies is false, strips Cookie headers from incoming requests, and
* If allowCookies is false, strips Cookie headers from incoming requests, and

hajinsuha1
hajinsuha1 previously approved these changes Jun 29, 2023
Copy link
Collaborator

@hajinsuha1 hajinsuha1 left a comment

Choose a reason for hiding this comment

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

🙌 🙌 🙌
Screenshot 2023-06-29 at 10 45 06 AM

Comment on lines 179 to 183
/**
* Express handler that sets a simple cookie and returns a JSON response with
* diagnostic values.
*/
const cookieTest = async (req, res) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it worth including a comment stating that you need to use /cookie?name=xxx&value=xxx to set the cookie?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

absolutely, done

Copy link
Collaborator

Choose a reason for hiding this comment

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

@bredmond-sf – thinking ahead, do you also need an endpoint that returns a "private" cache-control directive so that can we can confirm that not caching works correctly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good plan, done!

@bredmond-sf bredmond-sf dismissed stale reviews from hajinsuha1 and bfeister via 53f71dd June 29, 2023 15:49
@hajinsuha1 hajinsuha1 self-requested a review June 29, 2023 16:28
hajinsuha1
hajinsuha1 previously approved these changes Jun 29, 2023
@bredmond-sf bredmond-sf requested a review from wjhsf June 29, 2023 16:30
@bredmond-sf bredmond-sf requested a review from kevinxh June 29, 2023 18:58
* ?name=test-name&value=test-value to set a cookie.
*/
const cookieTest = async (req, res) => {
if (Object.hasOwn(req.query, 'name')) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

curious why did you choose to do Object.hasOwn instead of just

    if (req.query.name) {

@johnboxall johnboxall enabled auto-merge (squash) June 29, 2023 21:23
@johnboxall johnboxall merged commit e0a936f into develop Jun 30, 2023
@wjhsf wjhsf deleted the ssr-allow-cookies branch October 13, 2023 17:17
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.

5 participants