From 3654b693e3c87585d421b0fce088f1c33b8306a3 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 15 Sep 2023 17:14:00 +0200 Subject: [PATCH] Fix referrer RegEx for permanent version redirect (#3864) --- src/util/permanentWebVersion.ts | 42 +++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/util/permanentWebVersion.ts b/src/util/permanentWebVersion.ts index 6dafe1a9a..dba30b135 100644 --- a/src/util/permanentWebVersion.ts +++ b/src/util/permanentWebVersion.ts @@ -3,7 +3,7 @@ import { getActions } from '../global'; import { PRODUCTION_HOSTNAME, WEB_VERSION_BASE } from '../config'; import { clearWebsync } from './websync'; -const SEARCH_ENGINE_REFERRERS = ['google', 'bing', 'duckduckgo', 'ya', 'yandex']; +const SEARCH_ENGINE_REGEX = /(^|\.)(google|bing|duckduckgo|ya|yandex)\./i; // Handled by the legacy version. Cannot be updated const PERMANENT_VERSION_KEY = 'kz_version'; const AVAILABLE_VERSIONS = ['Z', 'K'] as const; @@ -34,24 +34,30 @@ export function checkAndAssignPermanentWebVersion() { if (window.location.hostname !== PRODUCTION_HOSTNAME) return; const referrer = document.referrer.toLowerCase(); - if (!SEARCH_ENGINE_REFERRERS.some((engine) => referrer.match(`(\\/\\/:|\\.)${engine}\\.`))) return; + if (!referrer) return; + try { + const isSearchEngine = new URL(referrer).host.match(SEARCH_ENGINE_REGEX); + if (!isSearchEngine) return; - const currentVersion = getPermanentWebVersion(); - if (currentVersion) { - if (currentVersion !== CLIENT_VERSION) { - switchPermanentWebVersion(currentVersion); + const currentVersion = getPermanentWebVersion(); + if (currentVersion) { + if (currentVersion !== CLIENT_VERSION) { + switchPermanentWebVersion(currentVersion); + } + return; } - return; + + const urlParams = new URLSearchParams(window.location.search); + const hasTest = (urlParams.get('test') ?? undefined) !== undefined; + const shouldRedirect = Math.random() < 0.5; + + if (hasTest || !shouldRedirect) { + setPermanentWebVersion('Z'); + return; + } + + switchPermanentWebVersion('K'); + } catch (e) { + // Ignore } - - const urlParams = new URLSearchParams(window.location.search); - const hasTest = (urlParams.get('test') ?? undefined) !== undefined; - const shouldRedirect = Math.random() < 0.5; - - if (hasTest || !shouldRedirect) { - setPermanentWebVersion('Z'); - return; - } - - switchPermanentWebVersion('K'); }