earendil-works/pi · error · CborError

CBOR payload contains trailing data

Error message

CBOR payload contains trailing data

What it means

CborError thrown by CborReader.decode when the top-level item finishes decoding before all input bytes are consumed, i.e. the payload contains extra data after a complete CBOR value — a strictness guard against concatenated or corrupted payloads.

Solutions

  1. Verify the byte buffer contains exactly one CBOR item with no trailing bytes
  2. Check for framing bugs that concatenated two payloads
  3. Re-encode the value with the matching encoder and compare lengths
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/protocol/src/cbor/decoder.ts:22 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of earendil-works/pi@4af9d21d3b (2026-08-24). Data as JSON: /api/errors/ad0f9a7bb0cb41d3. Report an issue: GitHub.

Appendix: source

Thrown at packages/protocol/src/cbor/decoder.ts:22

	type ResolvedCborOptions,
	resolveOptions,
	textDecoder,
	UINT32_BASE,
} from "./options.ts";

class CborReader {
	private readonly bytes: Uint8Array;
	private offset = 0;
	private readonly options: ResolvedCborOptions;

	constructor(bytes: Uint8Array, options: ResolvedCborOptions) {
		this.bytes = bytes;
		this.options = options;
	}

	decode(): unknown {
		const value = this.readItem(0);
		if (this.offset !== this.bytes.byteLength) throw new CborError("CBOR payload contains trailing data");
		return value;
	}

	private readItem(depth: number): unknown {
		if (depth > this.options.maxDepth) {
			throw new CborError(`CBOR nesting depth exceeds configured limit of ${this.options.maxDepth}`);
		}
		const initial = this.readByte();
		const majorType = initial >>> 5;
		const additionalInformation = initial & 0x1f;

		switch (majorType) {
			case 0:
				return this.readArgument(additionalInformation);
			case 1: {
				const value = -1 - this.readArgument(additionalInformation);
				if (!Number.isSafeInteger(value)) throw new CborError("Decoded CBOR integer is outside the safe range");
				return value;

View on GitHub (pinned to 4af9d21d3b)