Skip to content

Commit

Permalink
fix: fiexed setting max-age to the cookies (#2830)
Browse files Browse the repository at this point in the history
* fix: fixed clearing cookies

* test: added test max age should be null if not specified

* fix: fixed setting max-age

* test: fixed test

* fix: removed isNil

* fix: used loose comparing for null

* fix: refactored method isNull

* refactor: removed unnecessary comment
  • Loading branch information
Aleksey28 authored Dec 30, 2022
1 parent da3c96f commit e4ae013
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/client/sandbox/cookie/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { CookieRecord } from '../../../typings/cookie';
import ChildWindowSandbox from '../child-window';
import getTopOpenerWindow from '../../utils/get-top-opener-window';
import { isNull } from '../../utils/types';

const MIN_DATE_VALUE = new nativeMethods.date(0).toUTCString(); // eslint-disable-line new-cap

Expand Down Expand Up @@ -67,7 +68,7 @@ class CookieSandboxProxyStrategy implements CookieSandboxStrategy {
}

getCookie (): string {
this.syncCookie();
this.syncCookie(true);

// eslint-disable-next-line no-restricted-properties
return settings.get().cookie || '';
Expand Down Expand Up @@ -103,7 +104,7 @@ class CookieSandboxProxyStrategy implements CookieSandboxStrategy {
let clientCookieStr = null;

if ((!parsedCookie.expires || parsedCookie.expires === 'Infinity' || parsedCookie.expires > currentDate) &&
(isNaN(parsedCookie.maxAge) || parsedCookie.maxAge > 0))
(isNull(parsedCookie.maxAge) || isNaN(parsedCookie.maxAge) || parsedCookie.maxAge > 0))
clientCookieStr = cookieUtils.formatClientString(parsedCookie);

CookieSandbox._updateClientCookieStr(parsedCookie.key, clientCookieStr);
Expand All @@ -117,7 +118,7 @@ class CookieSandboxProxyStrategy implements CookieSandboxStrategy {
}
}

syncCookie (): void {
syncCookie (gettingCookies = false): void {
const cookies = nativeMethods.documentCookieGetter.call(this.document);
const parsedCookies = parseClientSyncCookieStr(cookies);
const sessionId = settings.get().sessionId;
Expand All @@ -134,16 +135,15 @@ class CookieSandboxProxyStrategy implements CookieSandboxStrategy {
serverSyncCookies.push(parsedCookie);
else if (parsedCookie.isWindowSync)
this.setCookie(parsedCookie);
else if (parsedCookie.isClientSync) {
const currentDate = new nativeMethods.date(); //eslint-disable-line new-cap
const maxAge = Number(parsedCookie.maxAge);
else if (gettingCookies && parsedCookie.isClientSync) {
const currentDate = cookieUtils.getUTCDate();
const maxAge = !isNull(parsedCookie.maxAge) && Number(parsedCookie.maxAge);
const expires = Number(parsedCookie.expires);

if (!isNaN(maxAge) && maxAge * 1000 <= currentDate.getTime() - parsedCookie.lastAccessed.getTime() ||
!isNaN(expires) && expires < currentDate.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 Expand Up @@ -214,7 +214,7 @@ class CookieSandboxProxyStrategy implements CookieSandboxStrategy {
parsedCookie.isClientSync = true;
parsedCookie.isWindowSync = true;
parsedCookie.sid = settings.get().sessionId;
parsedCookie.lastAccessed = new nativeMethods.date(); //eslint-disable-line new-cap
parsedCookie.lastAccessed = cookieUtils.getUTCDate();

prepareSyncCookieProperties(parsedCookie);

Expand Down
4 changes: 3 additions & 1 deletion src/client/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export function parse (str) {
break;

case 'max-age':
parsedCookie.maxAge = Number(value);
if (value)
parsedCookie.maxAge = Number(value);

break;

case 'path':
Expand Down
13 changes: 9 additions & 4 deletions src/client/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// -------------------------------------------------------------
// WARNING: this file is used by both the client and the server.
// Do not use any browser or node-specific API!
// -------------------------------------------------------------
import { isIE } from './browser';

export function inaccessibleTypeToStr (obj) {
return obj === null ? 'null' : 'undefined';
Expand All @@ -16,3 +13,11 @@ export function isPrimitiveType (obj) {

return objType !== 'object' && objType !== 'function';
}

export function isNull (obj) {
//Some times IE cannot compare null correctly
return isIE
// eslint-disable-next-line eqeqeq
? obj == null
: obj === null;
}
2 changes: 1 addition & 1 deletion src/typings/cookie.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface CookieRecord {
domain: string;
path: string;
expires: Date | 'Infinity';
maxAge: number | 'Infinity' | '-Infinity';
maxAge: number | 'Infinity' | '-Infinity' | null;
lastAccessed: Date;
syncKey?: string;
cookieStr?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function parseSyncCookie (cookieStr: string): CookieRecord | null {
path: decodeURIComponent(parsedKey[4]),
expires: parsedKey[5] ? new Date(parseInt(parsedKey[5], TIME_RADIX)) : 'Infinity',
lastAccessed: new Date(parseInt(parsedKey[6], TIME_RADIX)),
maxAge: parseInt(parsedKey[7], TIME_RADIX),
maxAge: parsedKey[7] ? parseInt(parsedKey[7], TIME_RADIX) : null,
syncKey: key,

value,
Expand Down
13 changes: 12 additions & 1 deletion test/client/fixtures/sandbox/cookie-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,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=' + 0,
'Test2=Expired; max-age=' + 1,
], '', 2000);
})
.then(function () {
Expand Down Expand Up @@ -186,6 +186,17 @@ if (!isGreaterThanSafari15_1) { //eslint-disable-line camelcase
strictEqual(parsedCookie.actual[0].value, '123=456=789');
});

test('max age should be null if not specified', function () {
nativeMethods.documentCookieSetter.call(document, 's|sessionId|test|example.com|%2F||1fckm5ln2|=123;path=/');

var parsedCookie = sharedCookieUtils.parseClientSyncCookieStr(nativeMethods.documentCookieGetter.call(document));

strictEqual(parsedCookie.actual.length, 1);
strictEqual(parsedCookie.outdated.length, 0);
strictEqual(parsedCookie.actual[0].syncKey, 's|sessionId|test|example.com|%2F||1fckm5ln2|');
strictEqual(parsedCookie.actual[0].maxAge, null);
});

module('server synchronization with client');

test('process synchronization cookies on document.cookie getter', function () {
Expand Down

0 comments on commit e4ae013

Please sign in to comment.