Skip to content

Commit a33af5d

Browse files
committed
Added assertion to ensure the provided key is valid.
1 parent 4ab38da commit a33af5d

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

www/outsystems-secure-sqlite-init.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ function installNumericParametersWorkaround(db) {
147147
alreadyInstalledNumericParamsWorkaround = true;
148148
}
149149

150+
/**
151+
* Validates the options passed to a `openDatabase` call are correctly set.
152+
*
153+
* @param {Object} options Options object to be passed to a `openDatabase` call.
154+
*/
155+
function validateDbOptions(options) {
156+
if (typeof options.key !== 'string' || options.key.length === 0) {
157+
throw new Error("Attempting to open a database without a valid encryption key.");
158+
}
159+
}
160+
150161
// Set the `isSQLCipherPlugin` feature flag to help ensure the right plugin was loaded
151162
window.sqlitePlugin.sqliteFeatures["isSQLCipherPlugin"] = true;
152163

@@ -155,20 +166,24 @@ var originalOpenDatabase = window.sqlitePlugin.openDatabase;
155166
window.sqlitePlugin.openDatabase = function(options, successCallback, errorCallback) {
156167
return acquireLsk(
157168
function (key) {
158-
// Clone the options and set the `key`
169+
// Clone the options
159170
var newOptions = {};
160171
for (var prop in options) {
161172
if (options.hasOwnProperty(prop)) {
162173
newOptions[prop] = options[prop];
163174
}
164175
}
165-
newOptions["key"] = key;
166-
167-
// Ensure `location` is set (it is mandatory now)
176+
177+
// Ensure `location` is set (it is mandatory now)
168178
if (newOptions.location === undefined) {
169179
newOptions.location = "default";
170180
}
181+
182+
// Set the `key` to the one provided
183+
newOptions.key = key;
171184

185+
// Validate the options and call the original openDatabase
186+
validateDbOptions(newOptions);
172187
var db = originalOpenDatabase.call(window.sqlitePlugin, newOptions, successCallback, errorCallback);
173188
installNumericParametersWorkaround(db);
174189
return db;

0 commit comments

Comments
 (0)