BooleanType.serialize accepts non-boolean values via truthiness

オープン 初心者向け
#577 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript
領域
backend

調査の方向性

バグは BooleanType の value_serializeToBytes メソッドにあり、真偽値の判定(value ? 1 : 0)を使っています。すでに fromJson と deserialize のパスにある厳密なチェックと比較し、issue に記載された型の強制変換のケースを短いスクリプトまたはテストで再現してください。真偽値以外の値に対して serialize が分かりやすい型エラーを投げ、true と false が引き続き 0x01 と 0x00 にエンコードされれば完了です。boolean 型のテストがすべて通ることも確認してください。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
TypeScript
スター
62
フォーク
30
平均マージ
4時間 37分
マージ済み PR(30日)
3

環境構築

  • Dockerfile・Docker Compose ファイルなし
  • プルリクエストのテンプレートあり
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ChainSafe/ssz のほかの issue

ChainSafe/ssz の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。