FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Decoder accepts non-minimal integer encodings · Issue #296 · msgpack/msgpack-javascript · GitHub

Repository navigation

Decoder accepts non-minimal integer encodings #296

Description

Summary

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.

Specification: https://github.com/msgpack/msgpack/blob/master/spec.md

Problem

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.

Version tested: @msgpack/msgpack 3.1.3

Reproduction

import { decode, encode } from '@msgpack/msgpack';
const buf = Buffer.from('cc6c', 'hex');
const v = decode(buf);
const re = Buffer.from(encode(v));
console.log('input   :', buf.toString('hex'));
console.log('decoded :', JSON.stringify(v, (_k, x) => typeof x === 'bigint' ? x.toString() : x));
console.log('re-encode:', re.toString('hex'));
console.log(re.equals(buf) ? 'ROUNDTRIP-STABLE' : 'REENCODE-DIFFERS');

Observed output:

input   : cc6c
decoded : 108
re-encode: 6c
REENCODE-DIFFERS

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.

Activity

  1. joshkel commented on Sep 26, 2026

    Contributor

    I do not believe this behavior is a violation of the spec.

    From https://github.com/msgpack/msgpack/blob/master/spec.md#serialization-type-to-format-conversion:

    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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL