ssz icon indicating copy to clipboard operation
ssz copied to clipboard

BooleanType.serialize accepts non-boolean values via truthiness

Open kutluhaneth46 opened this issue 4 days ago • 0 comments

Description

BooleanType.fromJson and BooleanType.deserialize both require a real boolean (true/false, or byte 0x00/0x01). BooleanType.serialize does not: it uses a truthy check (value ? 1 : 0), so numbers, strings, null, and undefined are silently encoded as 0x00 or 0x01.

That makes the encode path accept values the JSON and decode paths reject, and it can hide type mistakes in client code.

Related but separate from #574 / PR #574 (packed tree deserialize accepting byte values other than 0/1). This report is about the value serialize API coercing non-booleans.

Version

@chainsafe/ssz 1.9.1 (also reproduced on current packages/ssz master)

Repro

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

const b = new BooleanType();

b.serialize(true);   // Uint8Array [1]
b.serialize(false);  // Uint8Array [0]

// Non-booleans are accepted and coerced:
b.serialize(2 as any);          // Uint8Array [1]
b.serialize("x" as any);        // Uint8Array [1]
b.serialize(0 as any);          // Uint8Array [0]
b.serialize("" as any);         // Uint8Array [0]
b.serialize(null as any);       // Uint8Array [0]
b.serialize(undefined as any);  // Uint8Array [0]

// Contrast: fromJson / deserialize are strict
b.fromJson(1);                       // throws: JSON invalid type number expected boolean
b.deserialize(Uint8Array.of(2));     // throws: Boolean: invalid value: 2

Cause is here:

value_serializeToBytes(output, offset, value) {
  output.uint8Array[offset] = value ? 1 : 0;
  return offset + 1;
}

Expected

serialize should reject non-boolean values the same way fromJson does (throw a clear type error), so only true/false encode to 0x01/0x00.

Actual

Any truthy value encodes as 0x01 and any falsy value as 0x00, with no error.

kutluhaneth46 avatar Nov 28 '26 13:11 kutluhaneth46