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

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

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

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
領域
cli

調査の方向性

クラッシュは bech32/bech32/app/Main.hs の runDecode で発生しています。Bech32.decodeLenient の後で dataPartToBytes を fromJust でラップしている箇所です。このアンラップを、Nothing の場合に fail を呼ぶ case に置き換え、未使用になった fromJust のインポートを削除してください。回帰テストは bech32/test/AppSpec.hs に追加します。a1q3g6mn3 は、未処理の例外ではなく、ゼロ以外の終了コードとエラーメッセージで終了するべきです。

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

説明

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.
主要言語
Haskell
スター
50
フォーク
12
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

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

IntersectMBO/bech32 のほかの issue

IntersectMBO/bech32 の issue をすべて見る

似ている issue

Haskell の issue をもっと見る

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

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