From 0e3ac4e0dc872409acf59e38dff0a58a54afbd97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=20R=C4=B1za=20Kat?= Date: Thu, 3 Oct 2024 14:48:37 +0300 Subject: [PATCH] Check for Invalid Custom Methods --- lib/countly-storage.js | 18 +++++++++++++++- test/tests_storage.js | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/lib/countly-storage.js b/lib/countly-storage.js index 024f442..98ac6e3 100644 --- a/lib/countly-storage.js +++ b/lib/countly-storage.js @@ -132,7 +132,7 @@ var initStorage = function(userPath, storageType, isBulk = false, persistQueue = storageMethod = memoryStorage; } else if (storageType === StorageTypes.CUSTOM) { - if (customStorageMethod) { + if (hasValidMethods(customStorageMethod)) { storageMethod = customStorageMethod; } else { @@ -147,6 +147,22 @@ var initStorage = function(userPath, storageType, isBulk = false, persistQueue = } }; +var hasValidMethods = function(storage) { + if (!storage) { + return false; + } + if (typeof storage.storeSet !== 'function') { + return false; + } + if (typeof storage.storeGet !== 'function') { + return false; + } + if (typeof storage.storeRemove !== 'function') { + return false; + } + return true; +}; + /** * Sets the storage path, defaulting to a specified path if none is provided. * @param {String} userPath - User provided storage path diff --git a/test/tests_storage.js b/test/tests_storage.js index 99b28a7..8d5762e 100644 --- a/test/tests_storage.js +++ b/test/tests_storage.js @@ -99,6 +99,35 @@ function recordValuesToStorageAndValidate(userPath, memoryOnly = false, isBulk = assert.equal(storage.storeGet("cly_object"), undefined); assert.equal(storage.storeGet("cly_null"), undefined); } +const nonValidStorageMethods = { + _storage: {}, + + setInvalid: function(key, value, callback) { + if (key) { + const existingValue = this._storage[key]; + if (typeof value === 'string' && typeof existingValue === 'string') { + this._storage[key] = existingValue + value; + } + else { + this._storage[key] = value; + } + if (typeof callback === "function") { + callback(null); + } + } + }, + getInvalid: function(key, def) { + const value = this._storage[key]; + if (typeof value === 'string') { + return value.split('').reverse().join(''); + } + + return value !== undefined ? value : def; + }, + removeInvalid: function(key) { + delete this._storage[key]; + }, +}; const funkyMemoryStorage = { _storage: {}, @@ -585,4 +614,22 @@ describe("Storage Tests", () => { assert.equal("2eulaVegarotSmotsuCeulaVegarotSmotsuC", storage.storeGet("CustomStorageKey", null)); done(); }); + + // tests init time storage config options + // choosing Custom storage type and passing invalid custom storage methods + // SDK should not use custom methods as storage method, and switch to File Storage + it("26- Providing Invalid Custom Storage Method", (done) => { + hp.clearStorage(); + Countly.init({ + app_key: "YOUR_APP_KEY", + url: "https://test.url.ly", + device_id: "Test-Device-Id", + clear_stored_device_id: true, + storage_type: StorageTypes.CUSTOM, + custom_storage_method: nonValidStorageMethods, + }); + assert.equal(storage.getStoragePath(), "../data/"); + assert.equal(storage.getStorageType(), StorageTypes.FILE); + done(); + }); }); \ No newline at end of file