@@ -147,6 +147,17 @@ function installNumericParametersWorkaround(db) {
147
147
alreadyInstalledNumericParamsWorkaround = true ;
148
148
}
149
149
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
+
150
161
// Set the `isSQLCipherPlugin` feature flag to help ensure the right plugin was loaded
151
162
window . sqlitePlugin . sqliteFeatures [ "isSQLCipherPlugin" ] = true ;
152
163
@@ -155,20 +166,24 @@ var originalOpenDatabase = window.sqlitePlugin.openDatabase;
155
166
window . sqlitePlugin . openDatabase = function ( options , successCallback , errorCallback ) {
156
167
return acquireLsk (
157
168
function ( key ) {
158
- // Clone the options and set the `key`
169
+ // Clone the options
159
170
var newOptions = { } ;
160
171
for ( var prop in options ) {
161
172
if ( options . hasOwnProperty ( prop ) ) {
162
173
newOptions [ prop ] = options [ prop ] ;
163
174
}
164
175
}
165
- newOptions [ "key" ] = key ;
166
-
167
- // Ensure `location` is set (it is mandatory now)
176
+
177
+ // Ensure `location` is set (it is mandatory now)
168
178
if ( newOptions . location === undefined ) {
169
179
newOptions . location = "default" ;
170
180
}
181
+
182
+ // Set the `key` to the one provided
183
+ newOptions . key = key ;
171
184
185
+ // Validate the options and call the original openDatabase
186
+ validateDbOptions ( newOptions ) ;
172
187
var db = originalOpenDatabase . call ( window . sqlitePlugin , newOptions , successCallback , errorCallback ) ;
173
188
installNumericParametersWorkaround ( db ) ;
174
189
return db ;
0 commit comments