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

Commit

Permalink
fix(createBrowserLikeFetch): configure tough-cookie for localhost (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
smackfu authored Jan 16, 2024
1 parent 30e5b74 commit 2b484e5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
24 changes: 24 additions & 0 deletions __tests__/createBrowserLikeFetch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,30 @@ describe('createCookiePassingFetch', () => {
});
});

it('correctly calls setCookie when hostname is localhost', async () => {
const mockFetch = jest.fn(() => Promise.resolve({
headers: new Headers({
'set-cookie': [
'sessionid=123456; Secure; HttpOnly; domain=localhost; Max-Age=3600',
],
}),
}));
const hostname = 'localhost';
const setCookie = jest.fn();
const fetchWithRequestHeaders = createBrowserLikeFetch({
hostname,
setCookie,
})(mockFetch);

await fetchWithRequestHeaders('https://localhost', {
credentials: 'include',
});

expect(setCookie.mock.calls[0][0]).toEqual('sessionid');
expect(setCookie.mock.calls[0][1]).toEqual('123456');
expect(setCookie.mock.calls[0][2].domain).toEqual('localhost');
});

it('uses res.cookie to set cookie', async () => {
const mockFetch = jest.fn(() => Promise.resolve({
headers: new Headers({
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
},
"dependencies": {
"abort-controller": "^3.0.0",
"tough-cookie": "4.1.3"
"tough-cookie": "^4.1.3"
},
"release": {
"branches": [
Expand Down
6 changes: 4 additions & 2 deletions src/createBrowserLikeFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function createBrowserLikeFetch({
// jar acts as browser's cookie jar for the life of the SSR
const jar = new CookieJar();

const dottedHostnamePublicSuffix = hostname && `.${getPublicSuffix(hostname)}`;
const dottedHostnamePublicSuffix = hostname && `.${getPublicSuffix(hostname, { allowSpecialUseDomain: true })}`;
// build a list of cookies on creation to ease deduplication on each request
const headerCookies = parseCookieHeader(headers.cookie);

Expand Down Expand Up @@ -119,7 +119,9 @@ function createBrowserLikeFetch({
// subdomains."
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes
// host includes the hostname and port but getPublicSuffix expects only the hostname
cookieOptions.domain = getPublicSuffix(new URL(url).hostname);
cookieOptions.domain = getPublicSuffix(new URL(url).hostname, {
allowSpecialUseDomain: true,
});
}

// then check if this cookie relates to this hostname
Expand Down

0 comments on commit 2b484e5

Please sign in to comment.