Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Allows setting a domain-wide cookie in the oauth flow #905

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

cmelendez
Copy link

@cmelendez cmelendez commented Jun 6, 2023

WHY are these changes introduced?

Some OAuth flows require a domain-wide cookie instead of the usual behavior that only works within the starting domain. For example, if you start the OAuth flow in api.example.com and then redirect the user to app.example.com to finish it, you'll get a CookieNotFound error (check out issue #686).

WHAT is this pull request doing?

Allows to set an optional cookieDomain param when calling shopifyApi.

Type of change

  • Patch: Bug (non-breaking change which fixes an issue)

Checklist

  • I have used yarn changeset to create a draft changelog entry (do NOT update the CHANGELOG.md file manually)
  • I have added/updated tests for this change
  • I have documented new APIs/updated the documentation for modified APIs (for public APIs)

@cmelendez cmelendez requested a review from a team as a code owner June 6, 2023 13:50
@cmelendez
Copy link
Author

Forgot to add that this PR also changes the default path when setting the auth cookie.

Currently the auth cookie is valid only on exactly the same domain and the same path that started the OAuth flow, which works great when creating an app using Shopify's CLI but fails on other cases.

An example where the current config fails:

  1. OAuth flow starts in https://api.example.com/auth/start. The SDK sets the cookie on the api.example.com domain and /auth/start path.
  2. OAuth flow ends in https://dashboard.example.com/login/oauth with an error (CookieNotFound) because it won't be able to find the auth cookie since the domain and path doesn't match.

The PR changes the default behavior by making the auth cookie available on the root path but at the same time it allows setting a specific domain where to make the cookie readable, but if not domain is set then the current behavior of using the initial domain is kept.

@arabovs
Copy link

arabovs commented Jun 11, 2023

Nice work!

@rodrigogsqquid
Copy link

Awesome work. I hope an admin merges this soon.

@gusbueno
Copy link

gusbueno commented Jun 22, 2023

Yes please let's merge it 🙇🏽‍♂️

@jagthedrummer
Copy link

What will it take to get this merged?

@byrichardpowell
Copy link
Contributor

Thanks very much the PR @cmelendez ! Really appreciate you taking the time to open this.

I'm going to get this reviewed by app sec and back to you.

Copy link
Contributor

@paulomarg paulomarg left a comment

Choose a reason for hiding this comment

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

Hey folks, thanks for waiting while we got this reviewed.

I think we can take this PR, but there are a couple of things I think we need to tweak!

@@ -86,9 +86,10 @@ export function begin(config: ConfigInterface) {

await cookies.setAndSign(STATE_COOKIE_NAME, state, {
expires: new Date(Date.now() + 60000),
sameSite: 'lax',
sameSite: 'none',
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this required for the domain to work? OAuth should only rely on top-level redirects, and as per the MDN docs:

Lax
Means that the cookie is not sent on cross-site requests, such as on requests to load images or frames, but is sent when a user is navigating to the origin site from an external site (for example, when following a link).

I believe it should still work if we use lax. I ask because using none carries some risk for CSRF. If it is strictly necessary, I think we should only set it to none if cookieDomain is set to help mitigate that risk.

secure: true,
path: callbackPath,
path: '/',
domain: config.cookieDomain,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to fully skip domain if the setting isn't present, otherwise we'll end up with this Set-Cookie string:

Set-Cookie:
shopify_app_state=<state>;sameSite=lax; secure=true; path=/auth/callback; domain=undefined;

ryanray added a commit to JoinCollabMarket/shopify-api-js that referenced this pull request Jan 28, 2024
…ameSite=lax.

See this PR for other people who have ran into this issue
Shopify#905
Copy link

@Cody-traded-clothes Cody-traded-clothes left a comment

Choose a reason for hiding this comment

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

cool

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants