cryptobox.js
cryptobox.js copied to clipboard
Be more specific on "DecodeError.prototype.UNEXPECTED_TYPE"
The code has a plenty of throw new DecodeError(DecodeError.prototype.UNEXPECTED_TYPE); statements, which makes it hard to guess where this DecodeError comes from. We should be more specific on this one.
UNEXPECTED_TYPE is actually raised in very few places (as sort of a last-resort), but they're all deep inside cbor-codec, which is used all over the place in Proteus as part of the serialisation/deserialisation machinery.
I could, of course, extend cbor-codec's DecodeError to include a message about the expected type, but since UNEXPECTED_TYPE is used as a sort of catch-all whenever cbor-codec encounters data it wasn't expecting, there isn't always an expected type. I'm unconvinced that adding more messages to the places where it's raised will provide any benefit over just having the backtrace for the exception.
Since Proteus is so strict about the CBOR schema it accepts, if you're seeing a lot of these errors, the most likely cause is corrupted serialised data.
In cryptobox.js I see 7 places where DecodeError is thrown.
The error I faced has been caused by a malicious client (client which uses an old schema). It would be very handy if DecodeError says which property it doesn't like.
Those 7 places are actually from cbor-codec's Decoder.coffee, and at the site of the exception raising, cbor-codec doesn't know what property it's being assigned to (in some cases, it won't even be assigned to a property, just used as a counter in a loop).
Your best bet is to step up the backtrace to the relevant Proteus decode function and see which Decoder method is throwing.