You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The MessagePack specification directs an encoder to use the shortest representation that holds the value, so a decoder that accepts a wider encoding admits a second encoding of a value its own encoder would never emit.
msgpack-javascript accepts an input that its own encoder would
never produce. Decoding the witness below succeeds, and re-encoding the
decoded value with the same library yields different bytes. The library is
therefore not the inverse of itself on this input, so two peers that disagree
about whether to accept it will disagree about the value's encoding, and any
system that treats the encoding as identifying (content addressing, signature
preimages, deduplication) can be made to see two encodings of one value.
The re-encoding differs from the input, which is the defect.
Expected behavior
The decoder should reject this input rather than accept it, because the
encoding is not the canonical one for the decoded value. A canonical encoding of the same value is 9334406d, which round-trips byte-identically.
Suggested fix
While decoding an integer argument, check that the value could not have been
written in a shorter form, and reject it if it could. Normalising at encode
time does not help: by then the non-canonical bytes have already been accepted.
If an object can be represented in multiple possible output formats, serializers SHOULD use the format which represents the data in the smallest number of bytes.
"SHOULD," in all caps in an Internet specification, is most obviously read as meaning what it does in RFCs:
This word, or the adjective "RECOMMENDED", mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course.
Additionally, the spec makes no attempt to define a canonical form.
Even if the serialization section said that serializers MUST represent using the smallest number of bytes, its section on deserialization says nothing about requiring deserializers to reject other forms. (Postel's law is relevant here.)
Activity
joshkel commentedon Sep 26, 2026
I do not believe this behavior is a violation of the spec.
From http://github.com/msgpack/msgpack/blob/master/spec.md#serialization-type-to-format-conversion:
"SHOULD," in all caps in an Internet specification, is most obviously read as meaning what it does in RFCs:
Additionally, the spec makes no attempt to define a canonical form.
Even if the serialization section said that serializers MUST represent using the smallest number of bytes, its section on deserialization says nothing about requiring deserializers to reject other forms. (Postel's law is relevant here.)