BooleanType.serialize accepts non-boolean values via truthiness

Aperta Adatta ai principianti
#577 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
typescript
Ambito
backend

Direzione di ricerca

Il bug si trova nel metodo value_serializeToBytes di BooleanType, che usa un controllo di veridicità (value ? 1 : 0). Confrontalo con i controlli rigorosi già presenti nei percorsi fromJson e deserialize, e riproduci i casi di coercizione della issue con uno script breve o un test. È completato quando serialize genera un errore di tipo chiaro per i valori non booleani, mentre true e false continuano a essere codificati come 0x01 e 0x00. Verifica che i test del tipo booleano continuino a passare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
TypeScript
Stelle
62
Fork
30
Merge medio
4h 37m
PR unite (30g)
3

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ChainSafe/ssz

Tutte le issue di ChainSafe/ssz

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.