`bech32` CLI crashes (`fromJust: Nothing`) on valid-checksum strings whose data part cannot convert to bytes

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

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, commit 4624d3a (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.
  • noPadding requires bits < 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 n 5-bit words, leftover bits = 5n mod 8. For n = 1: leftover = 5, and 5 < 5 is false → Nothing.
  • a1q3g6mn3 is a genuine valid bech32 string: HRP a, data words [0], checksum 3g6mn3 computed with the BIP-173 reference algorithm (verified: bech32_polymod(hrp_expand("a") + [0, 17, 8, 26, 27, 19, 5]) == 1). decodeLenient therefore returns Right, and the CLI's fromJust throws.

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/noPadding evaluation for n = 1 was traced step by step: result' is empty ([-3, -11 .. 0] is empty), final noPadding guard 5 < 5 fails → 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

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 IntersectMBO/bech32

Tất cả issue của IntersectMBO/bech32

Issue tương tự

Thêm issue về Haskell

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.