fromHexString decodes non hex characters as zero bytes instead of throwing

Aperta Adatta ai principianti
#584 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
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
node.js, typescript
Ambito
backend, data

Direzione di ricerca

Inizia con l’helper per l’analisi esadecimale in packages/ssz/src/util/byteArray.ts e verifica come viene usato il suo risultato da ByteVectorType, ByteListType e dalla decodifica JSON degli array di bit. Aggiungi test per i caratteri non validi, oltre al controllo esistente sulla lunghezza dispari. L’obiettivo è che fromHexString e i percorsi fromJson interessati rifiutino gli input non esadecimali.

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

Descrizione

What happens

fromHexString does not validate its input. Each pair of characters goes through parseInt(pair, 16), and a result of NaN is stored into a Uint8Array, where it silently becomes 0. A pair with a valid first digit and an invalid second digit keeps only the first digit (parseInt("1g", 16) is 1). Because ByteVectorType, ByteListType and the bit array types decode JSON through this helper, fromJson accepts strings that are not hex at all and returns plausible looking values.

Repro

Tested on @chainsafe/ssz 1.9.1 (master at 0fe6d52), Node 22.

import {ByteVectorType, ByteListType, ContainerType, fromHexString} from "@chainsafe/ssz";

const hex = (u: Uint8Array) => Buffer.from(u).toString("hex");

hex(fromHexString("0xzzzz"));                       // "0000"
hex(fromHexString("0x1g2-"));                       // "0102"
hex(new ByteVectorType(2).fromJson("0xzzzz"));      // "0000"
hex(new ByteVectorType(2).fromJson("0x1gzz"));      // "0100"
hex(new ByteListType(4).fromJson("0xnothex!!"));    // "00000e00"

const c = new ContainerType({root: new ByteVectorType(2)});
c.toJson(c.fromJson({root: "0xqqqq"}));             // {root: "0x0000"}
Expected

A string with non hex characters should be rejected, the same way a string with an odd length already is (hex string length 3 must be multiple of 2), instead of being decoded as zero bytes.

Actual

No error. The value round trips as 0x0000, so a corrupted or truncated JSON root can be mistaken for a valid all zero root.

Notes

The existing length check is the only validation in packages/ssz/src/util/byteArray.ts. This is different from the typed serialize issues #572, #575 and #577, which concern values that never go through hex parsing.

Lingua principale
TypeScript
Stelle
62
Fork
32
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.