From 99f5c80ee99a0bf37fdf4892893dda2594bd6b44 Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Mon, 17 Jul 2023 10:10:24 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20Avoid=20setting=20non-object?= =?UTF-8?q?=20values=20for=20contexts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/tools/serialisation/contextManager.spec.ts | 10 ++++++++++ .../core/src/tools/serialisation/contextManager.ts | 10 ++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/core/src/tools/serialisation/contextManager.spec.ts b/packages/core/src/tools/serialisation/contextManager.spec.ts index f57f0cd7df..2b104faa52 100644 --- a/packages/core/src/tools/serialisation/contextManager.spec.ts +++ b/packages/core/src/tools/serialisation/contextManager.spec.ts @@ -92,6 +92,16 @@ describe('createContextManager', () => { expect(manager.getContext()).toEqual({}) }) + it('should prevent setting non object values', () => { + const manager = createContextManager(CustomerDataType.GlobalContext) + manager.setContext(null as any) + expect(manager.getContext()).toEqual({}) + manager.setContext(undefined as any) + expect(manager.getContext()).toEqual({}) + manager.setContext(2 as any) + expect(manager.getContext()).toEqual({}) + }) + describe('bytes count computation', () => { it('should be done every time the context is updated', () => { const computeBytesCountStub = jasmine.createSpy('computeBytesCountStub').and.returnValue(1) diff --git a/packages/core/src/tools/serialisation/contextManager.ts b/packages/core/src/tools/serialisation/contextManager.ts index 983cae9812..7fb5bf8d50 100644 --- a/packages/core/src/tools/serialisation/contextManager.ts +++ b/packages/core/src/tools/serialisation/contextManager.ts @@ -1,6 +1,7 @@ import { computeBytesCount } from '../utils/byteUtils' import { throttle } from '../utils/functionUtils' import { deepClone } from '../mergeInto' +import { getType } from '../utils/typeUtils' import { jsonStringify } from './jsonStringify' import { sanitize } from './sanitize' import { warnIfCustomerDataLimitReached } from './heavyCustomerDataWarning' @@ -51,8 +52,13 @@ export function createContextManager(customerDataType: CustomerDataType, compute getContext: () => deepClone(context), setContext: (newContext: Context) => { - context = sanitize(newContext) - computeBytesCountThrottled(context) + if (getType(newContext) === 'object') { + context = sanitize(newContext) + computeBytesCountThrottled(context) + } else { + context = {} + bytesCountCache = 0 + } }, setContextProperty: (key: string, property: any) => { From 3f75cc51b2b845b43e3e95e996d07bfae688b06d Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Mon, 17 Jul 2023 10:44:04 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=91=8C=20re-use=20clear=20context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/tools/serialisation/contextManager.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/tools/serialisation/contextManager.ts b/packages/core/src/tools/serialisation/contextManager.ts index 7fb5bf8d50..d9355db041 100644 --- a/packages/core/src/tools/serialisation/contextManager.ts +++ b/packages/core/src/tools/serialisation/contextManager.ts @@ -26,7 +26,7 @@ export function createContextManager(customerDataType: CustomerDataType, compute } }, BYTES_COMPUTATION_THROTTLING_DELAY) - return { + const contextManager = { getBytesCount: () => bytesCountCache, /** @deprecated use getContext instead */ get: () => context, @@ -56,8 +56,7 @@ export function createContextManager(customerDataType: CustomerDataType, compute context = sanitize(newContext) computeBytesCountThrottled(context) } else { - context = {} - bytesCountCache = 0 + contextManager.clearContext() } }, @@ -76,4 +75,5 @@ export function createContextManager(customerDataType: CustomerDataType, compute bytesCountCache = 0 }, } + return contextManager }