`bech32` CLI crashes (`fromJust: Nothing`) on valid-checksum strings whose data part cannot convert to bytes
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
- 78/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- cli
Hướng nghiên cứu
Lỗi crash nằm ở runDecode trong bech32/bech32/app/Main.hs, nơi fromJust bọc dataPartToBytes sau Bech32.decodeLenient. Thay phép unwrap đó bằng một case gọi fail khi là Nothing, và xóa import fromJust không dùng đến. Các test hồi quy đặt trong bech32/test/AppSpec.hs: a1q3g6mn3 phải thoát với mã thoát khác không và một thông báo lỗi, thay vì một exception không được xử lý.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
The bech32 executable's decode path (bech32/bech32/app/Main.hs, runDecode) calls fromJust $ dataPartToBytes dataPart on the result of Bech32.decodeLenient. decodeLenient verifies the checksum but does not check that the data part converts to bytes; dataPartToBytes returns Nothing whenever the 5-bit-word count leaves a leftover bit-group of 5–7 bits (word counts ≡ 1, 3, 6 mod 8) or non-zero padding bits. Any valid-checksum string in that class — e.g. a1q3g6mn3 (human-readable part a, one data word) — therefore crashes the CLI with an uncaught Maybe.fromJust: Nothing exception instead of producing output or a clean error. The library function itself is total (Maybe); only the CLI's use of fromJust is at fault.
Note the library-level callers handle this correctly — cardano-api/cardano-keys (deserialiseFromBech32) and cardano-addresses (fromBech32) both map Nothing to a proper error (Bech32DataPartToBytesError / "invalid data-part..."). Only this CLI app unwraps with fromJust.
Affected version
- Repo:
IntersectMBO/bech32, commit4624d3a(release 1.1.11) - File:
bech32/bech32/app/Main.hs(runDecode)
Reproduction
Prerequisite: a checkout of IntersectMBO/bech32 at commit 4624d3a with GHC/cabal.
cabal build exe:bech32
printf 'a1q3g6mn3' | cabal run exe:bech32
Other triggering inputs (all valid-checksum): a1qqqd87cpp (3 data words), a1qqqqqq3q4q7k (6 data words), addr1qqqwygn75 (3 data words). Any valid-checksum string whose data-word count n satisfies 5n mod 8 ∈ {5, 6, 7} triggers it; strings with n ≡ 2, 5, 7 (mod 8) also trigger it when the leftover padding bits are non-zero.
Bit-math proof (no build needed to see the failure mode):
dataPartToBytes→toBase256→convertBits words 5 8 noPadding.noPaddingrequiresbits < frombits && padValue == 0, i.e. the leftover bit-group after packing into bytes must be fewer than 5 bits and all zero.- For a data part of
n5-bit words, leftover bits =5n mod 8. Forn = 1: leftover = 5, and5 < 5is false →Nothing. a1q3g6mn3is a genuine valid bech32 string: HRPa, data words[0], checksum3g6mn3computed with the BIP-173 reference algorithm (verified:bech32_polymod(hrp_expand("a") + [0, 17, 8, 26, 27, 19, 5]) == 1).decodeLenienttherefore returnsRight, and the CLI'sfromJustthrows.
Relevant code (bech32/bech32/app/Main.hs):
runDecode source =
case Bech32.decodeLenient source of
Left err ->
fail (show err)
Right (_, dataPart) -> do
let base16 = convertToBase BA.Base16
B8.putStrLn $ base16 $ fromJust $ dataPartToBytes dataPart
Expected vs actual behavior
Expected: either the decoded bytes as hex, or a clean error such as "data part cannot be converted to bytes".
Actual: uncaught exception, process aborts:
bech32: Maybe.fromJust: Nothing
Suggested fix
In bech32/bech32/app/Main.hs, replace the fromJust with an explicit error (and drop the now-unused fromJust import):
runDecode source =
case Bech32.decodeLenient source of
Left err ->
fail (show err)
Right (_, dataPart) ->
case dataPartToBytes dataPart of
Nothing ->
fail "Unable to convert the data part to bytes: \
\the number of data words is not compatible with \
\byte conversion (invalid padding)."
Just bytes ->
B8.putStrLn $ convertToBase BA.Base16 bytes
Regression test: add a1q3g6mn3 (and one string from each residue class 1, 3, 6 mod 8) to bech32/test/AppSpec.hs, asserting a clean non-zero exit with an error message rather than an uncaught exception.
Impact note
The bech32 CLI — a user-facing developer tool shipped with the package — aborts with an uncaught exception on valid input. A user decoding a legitimate bech32 string (e.g. a short datum, a key fragment, or any payload whose word count lands in the bad residue classes) gets a crash instead of output or an intelligible error. Impact is confined to the local CLI process: no data loss, no disclosure, no network effect. Scripts that pipe bech32 strings through the CLI and do not expect a crash exit code may mis-handle the failure. The underlying library is not at fault and needs no change.
References
- CWE-248 (Uncaught Exception).
- The
convertBits/noPaddingevaluation forn = 1was traced step by step:result'is empty ([-3, -11 .. 0]is empty), finalnoPaddingguard5 < 5fails →Nothing. The triggering strings were generated with the verbatim BIP-173 reference implementation and cross-checked against a simulation whose polymod reproduces 22/22 of the repo's own valid test vectors. - Related but distinct from the long-string crash in
locateErrors(a separate issue): this finding needs no long input and a valid checksum, and lives in the CLI app rather than the library. - Environment: code audit, 2026-11-23; empirical Haskell check not run.
- Ngôn ngữ chính
- Haskell
- Star
- 50
- Fork
- 12
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của IntersectMBO/bech32
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
IntersectMBO/bech32#90 ·
-
CODEOWNERS.md FixĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 45/100
IntersectMBO/bech32#78 ·
-
bech32mĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
IntersectMBO/bech32#77 ·
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 25/100
IntersectMBO/bech32#76 ·
-
documentation
Độ khó 2/5 1-2 ngày Mức phù hợp với người mới 45/100
IntersectMBO/bech32#75 · 1 bình luận ·
Tất cả issue của IntersectMBO/bech32
Issue tương tự
-
area:client bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
`.ecrc` not taken into accountĐang mở
Độ khó 2/5 Dưới một giờ Mức phù hợp với người mới 72/100
fmidue/term-tasks#34 · 1 reaction ·
-
`.ecrc` not taken into accountĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 75/100
fmidue/logic-tasks#394 ·
-
difficulty: easy open-public refactor scope
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
seal-harness/seal-harness#348 ·
Maintainer thường phản hồi trong vòng 1 ngày