garbados / comdb

A PouchDB plugin that transparently encrypts and decrypts its data.
61 stars 4 forks source link

Cannot decrypt using in-memory adapter #23

Closed garbados closed 2 years ago

garbados commented 2 years ago

Given code like the following, running in a web page:

const PouchDB = require('pouchdb')
PouchDB.plugin(require('pouchdb-adapter-memory'))
PouchDB.plugin(require('comdb'))

const PASSWORD = 'blahzeblaht'
const db = new PouchDB('local', { adapter: 'memory' })
db.setPassword(PASSWORD).then(async () => {
  await db.loadEncrypted()
  await db.post({ name: 'bob' })
})

This will work the first time, but subsequent runs will encounter a "Could not decrypt!" error. This is because ComDB stores an exportString in a local document in the decrypted database, which it uses along with the given password to decrypt the encrypted database. Because the in-memory database is wiped each time the program ends, this exportString value is also wiped, essentially orphaning all your encrypted data. This means ComDB does not currently work with an in-memory adapter, pending architectural changes.

garbados commented 2 years ago

One solution would be to store exportString in a local document in the encrypted database instead, so that it is only ever lost if the encrypted database is lost as well. This would be a breaking change, but that's why we're in beta.