Skip to content

Commit

Permalink
Check for Invalid Custom Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali Rıza Kat committed Oct 3, 2024
1 parent c475d77 commit 0e3ac4e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/countly-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
47 changes: 47 additions & 0 deletions test/tests_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},

Expand Down Expand Up @@ -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();
});
});

0 comments on commit 0e3ac4e

Please sign in to comment.