`bech32` CLI crashes (`fromJust: Nothing`) on valid-checksum strings whose data part cannot convert to bytes
まだ誰も着手していません。
評価
- 難易度
- 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, 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.
- 主要言語
- Haskell
- スター
- 50
- フォーク
- 12
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
IntersectMBO/bech32 のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
IntersectMBO/bech32#90 ·
-
CODEOWNERS.md Fixオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 45/100
IntersectMBO/bech32#78 ·
-
bech32mオープン
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
IntersectMBO/bech32#77 ·
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 25/100
IntersectMBO/bech32#76 ·
-
documentation
難易度 2/5 1〜2日 初心者へのやさしさ 45/100
IntersectMBO/bech32#75 · コメント 1 件 ·
IntersectMBO/bech32 の issue をすべて見る
似ている issue
-
area:client bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
難易度 2/5 1時間未満 初心者へのやさしさ 72/100
fmidue/term-tasks#34 · リアクション 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 75/100
fmidue/logic-tasks#394 ·
-
difficulty: easy open-public refactor scope
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
seal-harness/seal-harness#348 ·
メンテナーはふだん 1 日以内に返信