Invalid property name type object when key is Buffer
Closed this issue · 2 comments
dmitrystas commented
If we try to decode cbor data like this one D87982A1446E616D654361626301
we got an error 'Invalid property name type object' coz key is bytes, not the string 121([{h'6E616D65': h'616263'}, 1])
In function safeKey we must take into account that key can be Buffer. Something like that
function safeKey(key) {
// protect against prototype pollution
if (typeof key === 'string') return key === '__proto__' ? '__proto_' : key
if (typeof key !== 'object' || Buffer.isBuffer(key)) return key.toString()
// protect against expensive (DoS) string conversions
throw new Error('Invalid property name type ' + typeof key);
}
kriszyp commented
Wouldn't you want to decode this as a Map
instead of an object, if you want a key that is a buffer (as objects only have string keys):
let encoder = new Encoder({ mapsAsObjects: false })
let decoded = encoder.decode(Buffer.from('D87982A1446E616D654361626301', 'hex'));
dmitrystas commented
wow, I probably need to dig deeper into the cbor-x code/docs :) I didn't know about this possibility, thank you Kris!