typeorm / ionic-example

Example project to demonstrate TypeORM in an Ionic app
67 stars 56 forks source link

Is it possible to utilize a sqlite encrypted database by TypeORM in Ionic? #8

Closed JeongJun-Lee closed 6 years ago

JeongJun-Lee commented 6 years ago

I am making a Ionic App with TypeORM, but we need to protect the sqlite database file by being encrypted with SQLCipher library(https://github.com/sqlcipher/sqlcipher). There is already for Cordova plug-in(https://github.com/litehelpers/Cordova-sqlcipher-adapter) for this. But TypeORM looks like not working with this plug-in yet.

Do you think that it's possible to be supported in TypeORM in near future if I request this new feature to TypeORM? Very thanks.

daniel-lang commented 6 years ago

I haven't tested it but you should be able to use the cordova plugin and specify your key as an extra option in your connection like this:

await createConnection({
   type: 'cordova',
   database: 'test',
   location: 'default',
   extra: {
      key: 'your-key'
   }
});

TypeORM doesn't care which plugin you use since all sqlite plugins by litehelpers have the same functions.

JeongJun-Lee commented 6 years ago

I uninstalled cordova-sqlite-storage plugin and installed cordova-sqlcipher-adapter plugin. I rewrote this ionic-exmple App's code as your recommendation and ran on the Android device. I think it works well because I can see this ionic-example App's main screen on the device again.

Though I wanted to re-verify that the database file was encrypted well by the specific key by investigation after taking it out from real device. But it's not successful until now because I cannot take out the database file from the device(Android 6.0.1) by all suggestions in stackoverflow(https://stackoverflow.com/questions/6928849/debugging-sqlite-database-on-the-device).

daniel-lang commented 6 years ago

I have never tries to get a database from an android app but what you could do is change the key in your options and see if you can still connect to your created database. Would that work?

JeongJun-Lee commented 6 years ago

When I tried the change of key, I can connect db normally without any connection refusal. And I can re-confirm it's not encrypted because I double checked it's not encrypted after taking out db file from emulator, not real device.

daniel-lang commented 6 years ago

This is quiet strange because I did the same:

  1. uninstall cordova-sqlite-storage with ionic cordova plugin remove cordova-sqlite-storage --save
  2. install cordova-sqlcipher-adapter with ionic cordova plugin add cordova-sqlcipher-adapter --save
  3. Add the extra option extra: { key: 'test-key' }
  4. Run the example on an android device ionic cordova run android
  5. Change the key to something else
  6. Run the example again with ionic cordova run android

But I get this error when trying to connect the database (unless I change the key back to 'test-key'): image

JeongJun-Lee commented 6 years ago

That's the what I have done exactly. I can't understand what's difference's there.

[cordova plugin list] cordova-plugin-device 1.1.7 "Device" cordova-plugin-splashscreen 4.1.0 "Splashscreen" cordova-plugin-statusbar 2.4.1 "StatusBar" cordova-plugin-whitelist 1.3.3 "Whitelist" cordova-sqlcipher-adapter 0.1.11 "Cordova sqlcipher adapter" ionic-plugin-keyboard 2.2.1 "Keyboard"

cordova platform ls Installed platforms: android 6.3.0

cordova -v 7.1.0

daniel-lang commented 6 years ago

my plugin versions (except for sqlcipher) are a bit older than yours because I haven't updated this example in a while. But that shouldn't make a difference. If you use Android and Chrome you can remote inspect your phone and take a look at the loaded plugins. Maybe something went wrong there and it is still loaded the "normal" version.

JeongJun-Lee commented 6 years ago

To verify the sqlcipher plugin without passing through TypeORM layer, I installed @ ionic-native/sqlite and added some codes on same code base like below:

image

I confirmed that the encrypted database was created with the key normally as my own intentions both on the device and emulator. And I got the same log like you when I changed the key.

image

But if I retest with TypeORM layer, it doesn't work again like before. There are not any error logs about loading fail of all plugins.

JeongJun-Lee commented 6 years ago

I upgraded TypeORM to 0.1.11 from 0.1.6 of code base. This issue was solved. I think there are any code changes in TypeORM. Anyway really thank you for your help.

daniel-lang commented 6 years ago

Oh sorry, yes I think in 0.1.7 we added support for additional options for the cordova driver. Before that you could only use location and name. Sorry about that. So this issue than be closed than?