calvinmetcalf / crypto-pouch

plugin for encrypted pouchdb/couchdb databases
MIT License
243 stars 43 forks source link

TypeError: Key data must be a BufferSource for non-JWK formats #63

Closed ajmchambers closed 7 years ago

ajmchambers commented 7 years ago

Hey I read the post Security in Offline First Apps which got me interested in trying out crypto-pouch. I've been messing about trying to understand how this works but I've been getting the above error, just wondering what I'm doing wrong.

I'm using db.crypto with the callback function so I can get the key, I'm trying to create a copy of the key and use it with db.crypto({key: key}) but with the copy I'm getting the error: TypeError: Key data must be a BufferSource for non-JWK formats

I have this index.html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Crypto Pouch Test</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/pouchdb/latest/pouchdb.min.js"></script>
<script src="https://wzrd.in/standalone/crypto-pouch"></script>
<script src="app.js"></script>
</body>
</html>

this app.js:

var db = new PouchDB('testing');
var db2 = new PouchDB('testing');
var db3 = new PouchDB('testing');

db.crypto({
    password: 'secret',
    cb: (err, key) => {
        db2.crypto({key});
        db3.crypto({key: new Uint8Array(key)});
    }
});

db.post({_id: 'bleh', value: 'yeooo'})
    .then((res) => { 
        getBleh();
    })
    .catch((err) => {
        getBleh();
    });

function getBleh() {
    console.log('Getting bleh:');
    db.get('bleh')
        .then((res) => {
            console.log('db (original): bleh', JSON.stringify(res, null, 4))
            db2.get('bleh').then((res) => console.log('db2 (key stored): bleh', JSON.stringify(res, null, 4)));
            db3.get('bleh').then((res) => console.log('db3 (key copied): bleh', JSON.stringify(res, null, 4)));
        });
}

In the console I get the above mentioned error with db3:

Getting bleh:
db (original): bleh {
    "value": "yeooo",
    "_id": "bleh",
    "_rev": "1-b515fd9f7418729e927fab1d075b9faa"
}
db2 (key stored): bleh {
    "value": "yeooo",
    "_id": "bleh",
    "_rev": "1-b515fd9f7418729e927fab1d075b9faa"
}
Uncaught (in promise) TypeError: Key data must be a BufferSource for non-JWK formats

Would love a bit of help, I'm probably just doing something silly here.

Cheers!

calvinmetcalf commented 7 years ago

could you put some catches so we can figure out which db is actually giving the error?

ajmchambers commented 7 years ago

I've poked about a bit more simplified the example a bit.

I get the error at the db2.crypto({key: new Uint8Array(key)}); line not actually the .get or .post.

Heres the simplified javascript, and heres a jsbin:

var db = new PouchDB('testing');
var db2 = new PouchDB('testing');

db.crypto({
    password: 'secret',
    cb: (err, key) => {
        var keyCopy = new Uint8Array(key);
        console.log('key:', key);
        console.log('keyCopy:', keyCopy);

        // error occurs here
        db2.crypto({key: new Uint8Array(key)});
    }
});

Heres the error: error

calvinmetcalf commented 7 years ago

ok weird let me investigate

calvinmetcalf commented 7 years ago

the issue is just I assume something is a node buffer but don't take into account it just being a plain uint8 buffer, for now you can change

 db2.crypto({key: new Uint8Array(key)});

to

 db2.crypto({key: Buffer.from(key)});

while i prepare a patch