BooleanType.serialize accepts non-boolean values via truthiness

未关闭 适合新手
#577 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
typescript
领域
backend

调研方向

问题出在 BooleanType 的 value_serializeToBytes 方法中,它使用了真值判断(value ? 1 : 0)。请将其与 fromJson 和 deserialize 路径中已有的严格检查进行对比,并用一个简短的脚本或测试复现 issue 中的强制转换情况。完成的标准是:serialize 对非布尔值抛出清晰的类型错误,同时 true 和 false 仍分别编码为 0x01 和 0x00。另外请确认布尔类型的测试仍然全部通过。

由索引模型根据 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 分钟
30 天内合并 PR
3

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ChainSafe/ssz 的其他 Issue

查看 ChainSafe/ssz 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。