ssz icon indicating copy to clipboard operation
ssz copied to clipboard

UintNumberType.serialize silently wraps out-of-range values (negative, NaN, overflow)

Open kutluhaneth46 opened this issue 6 days ago • 0 comments

Description

UintNumberType.serialize / value_serializeToBytes writes through DataView setters with no range check. Out-of-range JS numbers are truncated or wrapped into valid-looking uint bytes instead of throwing.

#564 tightened the deserialize path for values above Number.MAX_SAFE_INTEGER. The serialize path still accepts negatives, non-integers, NaN, and values >= 2^(8*byteLength).

SSZ uintN is [0, 2^N - 1]. Silent wrap means hashTreeRoot / encoding can succeed with the wrong root when a caller passes a bad number.

Version

@chainsafe/[email protected]

Steps to reproduce

import { UintNumberType } from "@chainsafe/ssz";

const u8 = new UintNumberType(1);
const u64 = new UintNumberType(8);

console.log(Array.from(u8.serialize(-1)));   // [255]
console.log(Array.from(u8.serialize(256)));  // [0]
console.log(Array.from(u8.serialize(1.9)));  // [1]
console.log(Array.from(u8.serialize(NaN)));  // [0]
console.log(Array.from(u64.serialize(-1)));  // [255,255,255,255,0,0,0,0]

UintBigintType.serialize has the same wrap behavior for negatives and values past the bit width (for example new UintBigintType(1).serialize(256n) → [0]).

Expected behaviour

Serialize should reject values outside the uint domain (negative, non-integer, NaN, +/-Infinity unless clipInfinity, and >= 2^(8*byteLength)), similar in spirit to the deserialize guard from #564.

Actual behaviour

Serialize succeeds and emits wrapped / truncated bytes. Callers get a plausible encoding with no error.

kutluhaneth46 avatar Nov 26 '26 13:11 kutluhaneth46