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

fix: fixed cookie synchronization #2818

Merged
merged 6 commits into from Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/client/sandbox/cookie/index.ts
Expand Up @@ -135,14 +135,15 @@ class CookieSandboxProxyStrategy implements CookieSandboxStrategy {
else if (parsedCookie.isWindowSync)
this.setCookie(parsedCookie);
else if (parsedCookie.isClientSync) {
const currentDate = cookieUtils.getUTCDate();
const currentDate = new nativeMethods.date(); //eslint-disable-line new-cap
const maxAge = Number(parsedCookie.maxAge);
const expires = Number(parsedCookie.expires);

if (!isNaN(maxAge) && maxAge * 1000 < currentDate.getTime() - parsedCookie.lastAccessed.getTime() ||
if (!isNaN(maxAge) && maxAge * 1000 <= currentDate.getTime() - parsedCookie.lastAccessed.getTime() ||
!isNaN(expires) && expires < currentDate.getTime()) {
nativeMethods.documentCookieSetter.call(this.document, generateDeleteSyncCookieStr(parsedCookie));
CookieSandbox._updateClientCookieStr(parsedCookie.key, null);
serverSyncCookies.push(parsedCookie);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/cookie.ts
Expand Up @@ -5,6 +5,7 @@

import trim from './string-trim';
import { CookieRecord, ParsedClientSyncCookie } from '../typings/cookie';
import { isNil } from 'lodash';

const TIME_RADIX = 36;
const CLEAR_COOKIE_VALUE_STR = '=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT';
Expand Down Expand Up @@ -67,7 +68,7 @@ function formatSyncCookieKey (cookie: CookieRecord): string {
const path = encodeURIComponent(cookie.path);
const expires = cookie.expires !== 'Infinity' ? cookie.expires.getTime().toString(TIME_RADIX) : '';
const lastAccessed = cookie.lastAccessed.getTime().toString(TIME_RADIX);
const maxAge = cookie.maxAge && cookie.maxAge !== 'Infinity' ? cookie.maxAge.toString(TIME_RADIX) : '';
const maxAge = !isNil(cookie.maxAge) && !isNaN(Number(cookie.maxAge)) && String(cookie.maxAge) !== 'Infinity' ? cookie.maxAge.toString(TIME_RADIX) : '';

return `${syncType}|${cookie.sid}|${key}|${domain}|${path}|${expires}|${lastAccessed}|${maxAge}`;
}
Expand Down
60 changes: 49 additions & 11 deletions test/client/fixtures/sandbox/cookie-test.js
Expand Up @@ -17,17 +17,21 @@ var validDateStr = validDate.toUTCString();
// Need trying to turn on the disabled tests on the next Safari versions (15.3 and later)
var isGreaterThanSafari15_1 = browserUtils.isSafari && parseFloat(browserUtils.fullVersion) >= '15.1'; //eslint-disable-line camelcase

if (!isGreaterThanSafari15_1) { //eslint-disable-line camelcase
QUnit.testDone(function () {
nativeMethods.documentCookieGetter.call(document)
.split(';')
.forEach(function (cookie) {
var key = cookie.split('=')[0];
function clearCookie () {
nativeMethods.documentCookieGetter.call(document)
.split(';')
.forEach(function (cookie) {
var key = cookie.split('=')[0];

nativeMethods.documentCookieSetter.call(document, key + '=;Path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT');
});
nativeMethods.documentCookieSetter.call(document, key + '=;Path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT');
});

settings.get().cookie = '';
settings.get().cookie = '';
}

if (!isGreaterThanSafari15_1) { //eslint-disable-line camelcase
QUnit.testDone(function () {
clearCookie();
});

test('get/set', function () {
Expand Down Expand Up @@ -101,7 +105,7 @@ if (!isGreaterThanSafari15_1) { //eslint-disable-line camelcase
.then(function () {
return testCookies(storedForcedLocation, [
'Test1=Expired; expires=' + new Date((Math.floor(Date.now() / 1000) + 1) * 1000).toUTCString(),
'Test2=Expired; max-age=' + 1,
'Test2=Expired; max-age=' + 0,
], '', 2000);
})
.then(function () {
Expand Down Expand Up @@ -204,7 +208,6 @@ if (!isGreaterThanSafari15_1) { //eslint-disable-line camelcase

strictEqual(settings.get().cookie, 'temp=temp; test=123');

console.log(nativeMethods.documentCookieGetter.call(document));
strictEqual(nativeMethods.documentCookieGetter.call(document).replace(/\|[^|]+\|=/, '|lastAccessed|='),
'c|sessionId|temp|example.com|%2F||lastAccessed|=temp');
});
Expand Down Expand Up @@ -270,6 +273,41 @@ if (!isGreaterThanSafari15_1) { //eslint-disable-line camelcase
'c|sessionId|invalid|example.com|%2Fpath||lastAccessed|=path');
});

test('cookie with the max-age', function () {
strictEqual(document.cookie, '');

document.cookie = 'temp=temp; max-age=9';

strictEqual(settings.get().cookie, 'temp=temp');
strictEqual(nativeMethods.documentCookieGetter.call(document).replace(/(\|[^|]+\|)(\d*=)/, '|lastAccessed|$2'),
'c|sessionId|temp|example.com|%2F||lastAccessed|9=temp');

clearCookie();

document.cookie = 'temp=temp; max-age=0';

strictEqual(settings.get().cookie, '');

strictEqual(nativeMethods.documentCookieGetter.call(document).replace(/(\|[^|]+\|)(\d*=)/, '|lastAccessed|$2'),
'c|sessionId|temp|example.com|%2F||lastAccessed|0=temp');

clearCookie();

document.cookie = 'temp=temp; max-age=Infinity';

strictEqual(settings.get().cookie, 'temp=temp');
strictEqual(nativeMethods.documentCookieGetter.call(document).replace(/(\|[^|]+\|)(=)/, '|lastAccessed|$2'),
'c|sessionId|temp|example.com|%2F||lastAccessed|=temp');

clearCookie();

document.cookie = 'temp=temp; max-age=-Infinity';

strictEqual(settings.get().cookie, '');
strictEqual(nativeMethods.documentCookieGetter.call(document).replace(/(\|[^|]+\|)(-Infinity=)/, '|lastAccessed|$2'),
'c|sessionId|temp|example.com|%2F||lastAccessed|-Infinity=temp');
});

test('cookie with the invalid secure', function () {
var storedForcedLocation = destLocation.getLocation();

Expand Down