From 76b60759ec37afb371d18590348572ca8e822070 Mon Sep 17 00:00:00 2001 From: Sai Kumar Battinoju <88789928+saikumarrs@users.noreply.github.com> Date: Wed, 16 Aug 2023 17:23:17 +0530 Subject: [PATCH 1/2] fix: persisted data decryption for v3 (#1309) * fix: persisted data decryption for v3 * fix: polyfill detection conditions * chore: fix formatting --- .size-limit.js | 2 +- jest/jest.polyfills.js | 4 ++++ src/core/analytics.js | 26 ++++++++++++++------------ src/utils/constants.js | 2 +- src/utils/storage/storage.js | 7 +++++++ src/utils/storage/v3DecryptionUtils.js | 19 +++++++++++++++++++ 6 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 src/utils/storage/v3DecryptionUtils.js diff --git a/.size-limit.js b/.size-limit.js index a6380aea4..84cf40a14 100644 --- a/.size-limit.js +++ b/.size-limit.js @@ -13,7 +13,7 @@ module.exports = [ name: 'All Integrations - CDN', path: 'dist/legacy/js-integrations/*.min.js', gzip: true, - limit: '435.5 kB', + limit: '436 kB', }, { name: 'Core - NPM', diff --git a/jest/jest.polyfills.js b/jest/jest.polyfills.js index 9b170f22b..f35ad923e 100644 --- a/jest/jest.polyfills.js +++ b/jest/jest.polyfills.js @@ -4,3 +4,7 @@ require('isomorphic-fetch'); // Mocking Math random global.Math.random = () => 0.5; + +import { TextDecoder } from 'util'; + +Object.assign(global, { TextDecoder }); diff --git a/src/core/analytics.js b/src/core/analytics.js index f28d6b17e..e36fc990d 100644 --- a/src/core/analytics.js +++ b/src/core/analytics.js @@ -1345,18 +1345,20 @@ class Analytics { ? options.polyfillIfRequired : true; return ( - polyfillIfRequired && - (!String.prototype.endsWith || - !String.prototype.startsWith || - !String.prototype.includes || - !Array.prototype.find || - !Array.prototype.includes || - typeof window.URL !== 'function' || - typeof Promise === 'undefined' || - !Object.entries || - !Object.values || - !String.prototype.replaceAll || - !this.isDatasetAvailable()) + (polyfillIfRequired && + (!String.prototype.endsWith || + !String.prototype.startsWith || + !String.prototype.includes || + !Array.prototype.find || + !Array.prototype.includes || + typeof window.URL !== 'function' || + typeof Promise === 'undefined' || + !Object.entries || + !Object.values || + !String.prototype.replaceAll || + !this.isDatasetAvailable() || + typeof TextDecoder !== 'function' || + typeof Uint8Array !== 'function')) ); } diff --git a/src/utils/constants.js b/src/utils/constants.js index 63ea95e32..306fcd1df 100644 --- a/src/utils/constants.js +++ b/src/utils/constants.js @@ -22,7 +22,7 @@ const INTEGRATION_LOAD_CHECK_INTERVAL = 1000; const DEFAULT_DATA_PLANE_EVENTS_BUFFER_TIMEOUT_MS = 10000; const INTG_SUFFIX = '_RS'; const POLYFILL_URL = - 'https://polyfill.io/v3/polyfill.min.js?features=Number.isNaN%2CURL%2CArray.prototype.find%2CArray.prototype.includes%2CPromise%2CString.prototype.endsWith%2CString.prototype.includes%2CString.prototype.startsWith%2CObject.entries%2CObject.values%2CElement.prototype.dataset%2CString.prototype.replaceAll'; + 'https://polyfill.io/v3/polyfill.min.js?features=Number.isNaN%2CURL%2CArray.prototype.find%2CArray.prototype.includes%2CPromise%2CString.prototype.endsWith%2CString.prototype.includes%2CString.prototype.startsWith%2CObject.entries%2CObject.values%2CElement.prototype.dataset%2CString.prototype.replaceAll%2CTextDecoder%2Uint8Array'; const GENERIC_TRUE_VALUES = ['true', 'True', 'TRUE', 't', 'T', '1']; const GENERIC_FALSE_VALUES = ['false', 'False', 'FALSE', 'f', 'F', '0']; diff --git a/src/utils/storage/storage.js b/src/utils/storage/storage.js index 39d598f2b..c01acd5ee 100644 --- a/src/utils/storage/storage.js +++ b/src/utils/storage/storage.js @@ -5,6 +5,7 @@ import get from 'get-value'; import logger from '../logUtil'; import { Cookie } from './cookie'; import { Store } from './store'; +import { fromBase64 } from './v3DecryptionUtils'; const defaults = { user_storage_key: 'rl_user_id', @@ -17,6 +18,7 @@ const defaults = { session_info: 'rl_session', auth_token: 'rl_auth_token', prefix: 'RudderEncrypt:', + prefixV3: 'RS_ENC_v3_', // prefix for v3 encryption key: 'Rudder', }; @@ -65,6 +67,11 @@ function decryptValue(value) { if (value.substring(0, defaults.prefix.length) === defaults.prefix) { return AES.decrypt(value.substring(defaults.prefix.length), defaults.key).toString(Utf8); } + + // Try if it is v3 encrypted value + if (value.substring(0, defaults.prefixV3.length) === defaults.prefixV3) { + return fromBase64(value.substring(defaults.prefixV3.length)); + } return value; } diff --git a/src/utils/storage/v3DecryptionUtils.js b/src/utils/storage/v3DecryptionUtils.js new file mode 100644 index 000000000..8deb556c7 --- /dev/null +++ b/src/utils/storage/v3DecryptionUtils.js @@ -0,0 +1,19 @@ +/** + * Converts a base64 encoded string to bytes array + * @param base64Str base64 encoded string + * @returns bytes array + */ +const base64ToBytes = (base64Str) => { + const binString = globalThis.atob(base64Str); + const bytes = binString.split('').map((char) => char.charCodeAt(0)); + return new Uint8Array(bytes); +}; + +/** + * Decodes a base64 encoded string + * @param value base64 encoded string + * @returns decoded string + */ +const fromBase64 = (value) => new TextDecoder().decode(base64ToBytes(value)); + +export { fromBase64 }; From b09463f1a3c69a16e36d5280d63e470ec62e41eb Mon Sep 17 00:00:00 2001 From: GitHub actions Date: Wed, 16 Aug 2023 12:11:50 +0000 Subject: [PATCH 2/2] chore(release): 2.40.2 --- CHANGELOG.md | 7 +++++++ package.json | 2 +- packages/npm/package.json | 2 +- sonar-project.properties | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b89525507..c69510d3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [2.40.2](https://github.com/rudderlabs/rudder-sdk-js/compare/v2.40.1...v2.40.2) (2023-08-16) + + +### Bug Fixes + +* persisted data decryption for v3 ([#1309](https://github.com/rudderlabs/rudder-sdk-js/issues/1309)) ([76b6075](https://github.com/rudderlabs/rudder-sdk-js/commit/76b60759ec37afb371d18590348572ca8e822070)) + ### [2.40.1](https://github.com/rudderlabs/rudder-sdk-js/compare/v2.40.0...v2.40.1) (2023-08-16) diff --git a/package.json b/package.json index ac4a6db00..2cf9c0648 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rudder-analytics", - "version": "2.40.1", + "version": "2.40.2", "description": "", "main": "./dist/browser.min.js", "scripts": { diff --git a/packages/npm/package.json b/packages/npm/package.json index dbb4137a7..0680bdf96 100644 --- a/packages/npm/package.json +++ b/packages/npm/package.json @@ -1,6 +1,6 @@ { "name": "rudder-sdk-js", - "version": "2.40.1", + "version": "2.40.2", "description": "RudderStack Javascript SDK", "main": "index.js", "module": "index.es.js", diff --git a/sonar-project.properties b/sonar-project.properties index d4d832766..57fa5150b 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -6,7 +6,7 @@ sonar.qualitygate.wait=false sonar.projectKey=rudderlabs_rudder-sdk-js sonar.organization=rudderlabs sonar.projectName=rudder-sdk-js -sonar.projectVersion=2.40.1 +sonar.projectVersion=2.40.2 # Meta-data for the project sonar.links.scm=https://github.com/rudderlabs/rudder-sdk-js