From 3fb182717d8f439c6bab2e90b9e2e2c0abcf8600 Mon Sep 17 00:00:00 2001 From: Stanislav A Date: Mon, 31 Oct 2022 19:21:34 +0300 Subject: [PATCH] move path parsing to a helper --- src/helpers/cookie-utils.js | 30 +++++++++++++++++++++------- src/scriptlets/set-cookie-reload.js | 4 ++++ src/scriptlets/set-cookie.js | 16 +++++++++++++-- src/scriptlets/trusted-set-cookie.js | 14 ++++++------- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/helpers/cookie-utils.js b/src/helpers/cookie-utils.js index 03bc96149..df7cbbe43 100644 --- a/src/helpers/cookie-utils.js +++ b/src/helpers/cookie-utils.js @@ -1,5 +1,26 @@ import { nativeIsNaN } from './number-utils'; +/** + * Prepares path string if given parameters are ok + * @param {string} pathArg + * @returns {string|null} returns path string if ok OR null if not + */ +export const prepareCookiePath = (pathArg) => { + // eslint-disable-line no-console + const log = console.log.bind(console); + + let pathString; + if (pathArg === '/') { + pathString = 'path=/'; + } else if (pathArg === 'none') { + pathString = ''; + } else { + log(`Invalid cookie path: '${pathArg}'`); + return null; + } + return pathString; +}; + /** * Prepares cookie string if given parameters are ok * @param {string} name cookie name to set @@ -49,13 +70,8 @@ export const prepareCookie = (name, value, path) => { return null; } - let pathToSet; - if (path === '/') { - pathToSet = 'path=/'; - } else if (path === 'none') { - pathToSet = ''; - } else { - log(`Invalid cookie path: '${path}'`); + const pathToSet = prepareCookiePath(path); + if (!pathToSet) { return null; } diff --git a/src/scriptlets/set-cookie-reload.js b/src/scriptlets/set-cookie-reload.js index 24386a3d8..e164ffee1 100644 --- a/src/scriptlets/set-cookie-reload.js +++ b/src/scriptlets/set-cookie-reload.js @@ -3,6 +3,9 @@ import { nativeIsNaN, prepareCookie, isCookieSetWithValue, + // following helpers should be imported and injected + // because they are used by helpers above + prepareCookiePath, } from '../helpers/index'; /** @@ -68,4 +71,5 @@ setCookieReload.injections = [ nativeIsNaN, prepareCookie, isCookieSetWithValue, + prepareCookiePath, ]; diff --git a/src/scriptlets/set-cookie.js b/src/scriptlets/set-cookie.js index 441b368eb..c0b57d832 100644 --- a/src/scriptlets/set-cookie.js +++ b/src/scriptlets/set-cookie.js @@ -1,4 +1,11 @@ -import { hit, nativeIsNaN, prepareCookie } from '../helpers/index'; +import { + hit, + nativeIsNaN, + prepareCookie, + // following helpers should be imported and injected + // because they are used by helpers above + prepareCookiePath, +} from '../helpers/index'; /* eslint-disable max-len */ /** @@ -48,4 +55,9 @@ setCookie.names = [ 'set-cookie', ]; -setCookie.injections = [hit, nativeIsNaN, prepareCookie]; +setCookie.injections = [ + hit, + nativeIsNaN, + prepareCookie, + prepareCookiePath, +]; diff --git a/src/scriptlets/trusted-set-cookie.js b/src/scriptlets/trusted-set-cookie.js index cbe69b9ba..82614291e 100644 --- a/src/scriptlets/trusted-set-cookie.js +++ b/src/scriptlets/trusted-set-cookie.js @@ -2,6 +2,9 @@ import { hit, nativeIsNaN, isCookieSetWithValue, + // following helpers should be imported and injected + // because they are used by helpers above + prepareCookiePath, } from '../helpers/index'; /* eslint-disable max-len */ @@ -100,15 +103,11 @@ export function trustedSetCookie(source, name, value, offsetExpiresSec = '', rel cookieToSet += `${encodedName}=${encodedValue};`; - let pathToSet; - if (path === '/') { - pathToSet = 'path=/'; - } else if (path === 'none') { - pathToSet = ''; - } else { - log(`Invalid cookie path: '${path}'`); + const pathToSet = prepareCookiePath(path); + if (!pathToSet) { return; } + cookieToSet += pathToSet; // Set expiration date if offsetExpiresSec was passed @@ -159,4 +158,5 @@ trustedSetCookie.injections = [ hit, nativeIsNaN, isCookieSetWithValue, + prepareCookiePath, ];