BooleanType.serialize accepts non-boolean values via truthiness

Đang mở Phù hợp với người mới
#577 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
backend

Hướng nghiên cứu

Lỗi nằm ở phương thức value_serializeToBytes của BooleanType, phương thức này dùng phép kiểm tra truthy (value ? 1 : 0). Hãy so sánh nó với các kiểm tra nghiêm ngặt đã có trong luồng fromJson và deserialize, rồi tái hiện các trường hợp ép kiểu trong issue bằng một script ngắn hoặc một bài test. Hoàn thành khi serialize ném ra lỗi kiểu rõ ràng với giá trị không phải boolean, trong khi true và false vẫn được mã hóa thành 0x01 và 0x00. Kiểm tra rằng các bài test của kiểu boolean vẫn pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
TypeScript
Star
62
Fork
30
Merge trung bình
4 giờ 37 phút
Pull request đã merge (30 ngày)
3

Chuẩn bị môi trường

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ChainSafe/ssz

Tất cả issue của ChainSafe/ssz

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.