Attesto.StrictCBOR (Attesto v2.28.0)

Copy Markdown View Source

Strict CBOR decoder with duplicate-map-key rejection and a nesting bound.

RFC 8949 §5.6 leaves it to each protocol to say what a receiver does with a map that has the same key twice. COSE answers: "Labels in each of the maps MUST be unique. When processing messages, if a label appears multiple times, the message MUST be rejected as malformed" (RFC 9052 §3), and "Applications MUST NOT parse and process messages with the same label used twice as a key in a single map" (RFC 9052 §9).

The general-purpose decoder builds an Elixir map as it goes, so a repeated key silently keeps one of its values and the duplicate is gone by the time a caller looks. This module walks the encoded bytes first and refuses the input when any map, at any depth, repeats a key. Two keys are the same when they decode to the same term, so a key written with a non-minimal or indefinite-length encoding is caught as well as a byte-for-byte repeat. The walk also refuses input nested deeper than :max_depth, which the general-purpose decoder, recursive per nesting level, does not bound.

A byte string is opaque to the walk. CBOR carried inside one (a COSE protected header, a tag-24 embedded item) is checked when its bytes are decoded in turn, which every caller in this library does through this module.

It is the CBOR counterpart of Attesto.StrictJSON, and the one decode boundary for COSE and mdoc input.

Summary

Functions

Decode exactly one CBOR data item from bytes.

Functions

decode(bytes, opts \\ [])

@spec decode(
  term(),
  keyword()
) :: {:ok, term()} | {:error, :invalid_cbor}

Decode exactly one CBOR data item from bytes.

Returns {:ok, value} in the shape CBOR.decode/1 gives, or {:error, :invalid_cbor} when the input is not a binary, is longer than :max_bytes (when given), is not well-formed, has bytes after the item, nests deeper than :max_depth (default 32), or contains a map with a repeated key.