`Prelude.maximum: empty list` crash in fee estimation for empty `RequireAnyOf` (`{"type": "any", "scripts": []}`) scripts
I maintainer di solito rispondono entro 2 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 62/100
Direzione di ricerca
Inizia da maxWitnessesInSimpleScript in cardano-api/src/Cardano/Api/Tx/Internal/Fee.hs e dalla riga identica in cardano-api/src/Cardano/Api/Experimental/Tx/Internal/Fee.hs, poi controlla simpleScriptParsers in cardano-api/src/Cardano/Api/Plutus/Internal/Script.hs. È completato quando un RequireAnyOf vuoto produce 0 witnesses invece di chiamare maximum su una lista vuota, e il parser JSON rifiuta {"type": "any", "scripts": []} con un errore chiaro, con test di regressione aggiunti ai test JSON dei simple script.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Description
maxWitnessesInSimpleScript in cardano-api's fee-estimation code computes the witness count for RequireAnyOf with maximum $ map maxWitnessesInSimpleScript simpleScripts. The JSON parser for simple scripts (parseSimpleScript) accepts {"type": "any", "scripts": []} with no non-emptiness check, and the ledger Timelock decoder likewise accepts an empty RequireAnyOf list, so an empty any script is a structurally valid script that reaches fee estimation — where maximum [] throws an uncaught exception. cardano-cli transaction build with such a script as a --tx-in-script-file therefore aborts during fee calculation instead of reporting an invalid script. The same defective line exists in both the stable API (Cardano.Api.Tx.Internal.Fee) and the experimental API (Cardano.Api.Experimental.Tx.Internal.Fee).
Affected version
- Repo:
IntersectMBO/cardano-api, commit52b4f94(manifests viaIntersectMBO/cardano-cli, commitdabfa5a, with its pinned cardano-api) - Files:
cardano-api/src/Cardano/Api/Tx/Internal/Fee.hs(maxWitnessesInSimpleScript),cardano-api/src/Cardano/Api/Experimental/Tx/Internal/Fee.hs(identical line, ~line 1892),cardano-api/src/Cardano/Api/Plutus/Internal/Script.hs(simpleScriptParsers)
Reproduction
Prerequisite: cardano-cli built from IntersectMBO/cardano-cli at commit dabfa5a (with its pinned cardano-api), and access to a node (testnet or mainnet) for transaction build queries — or any Haskell program calling estimateMinFeeTx / makeTransactionBodyAutoBalance with the script.
- Create the empty-
anyscript file:cat > empty-any.json <<'EOF' { "type": "any", "scripts": [] } EOF - Attempt to build a transaction spending a UTxO locked by that script:
(Anycardano-cli transaction build \ --tx-in <TXID>#<IX> \ --tx-in-script-file empty-any.json \ --tx-out <ADDR>+1000000 \ --change-address <ADDR> \ --out-file tx.raw--tx-inwhose witness is the empty-anyscript triggers it; the UTxO does not need to actually be locked by the script for fee estimation to evaluate the witness count.)
Code-level proof (no build needed to see the failure mode):
- Parser accepts the empty list —
cardano-api/cardano-api/src/Cardano/Api/Plutus/Internal/Script.hs,simpleScriptParsers:
No length check. Note the contrast with("any", \obj -> RequireAnyOf <$> obj .: "scripts")parseScriptAtLeastin the same file, which does validate (required > numScriptsfails) — theany/allcases simply lack an equivalent check.RequireAllOf []is harmless (sum [] == 0); onlyRequireAnyOf []crashes. - CLI reads the file through this parser —
cardano-cli/.../Read.hs,deserialiseScriptInAnyLangfalls back toAeson.eitherDecodeStrict'→SimpleScriptFromJSON→parseSimpleScript. The experimental path (Cardano.Api.Experimental.AnyScript.readSimpleScriptFromJson) uses the same parser. - Fee estimation crashes —
cardano-api/cardano-api/src/Cardano/Api/Tx/Internal/Fee.hs(estimateTxInWitnesses→maxWitnessesInSimpleScript):
WithmaxWitnessesInSimpleScript (RequireAnyOf simpleScripts) = maximum $ map maxWitnessesInSimpleScript simpleScriptssimpleScripts == [],maximum []throwsPrelude.maximum: empty list. The identical line exists inCardano.Api.Experimental.Tx.Internal.Fee(line ~1892). - Nothing validates the script first: conversion to the ledger Timelock (
toAllegraTimelock/toDijkstraNativeScript,go (RequireAnyOf s) = ... Shelley.RequireAnyOf . fromList) performs no emptiness check, and ledger script validation only happens at submission — after fee estimation. The exception therefore escapesmakeTransactionBodyAutoBalance(anExceptTthat does not catch pure exceptions) to the CLI top handler, which prints it and exits 1. - The same crash is reachable via the CBOR path (a text-envelope script file whose CBOR encodes an empty
RequireAnyOflist — the ledger decoder accepts it) and via nesting ({"type": "all", "scripts": [{"type": "any", "scripts": []}]}), sincemaxWitnessesInSimpleScriptrecurses.
Expected vs actual behavior
Expected: a clean error (e.g. "empty script list" at parse time, or a ledger validation error at submission).
Actual: the command aborts during fee estimation with an uncaught exception:
cardano-cli: Prelude.maximum: empty list
Suggested fix
Fix at the parser (fail fast, one place, both APIs benefit) and defensively at the consumer:
- Parser (
Cardano.Api.Plutus.Internal.Script,simpleScriptParsers): reject empty lists forany(and, for symmetry,all):("any", \obj -> do scripts <- obj .: "scripts" when (null scripts) $ fail "Simple script of type \"any\" must contain at least one script." pure $ RequireAnyOf scripts) - Consumer (
Cardano.Api.Tx.Internal.FeeandCardano.Api.Experimental.Tx.Internal.Fee): makemaxWitnessesInSimpleScripttotal — an emptyanyneeds no witnesses (it can never be satisfied):
(The CBOR path can still produce an emptymaxWitnessesInSimpleScript (RequireAnyOf []) = 0 maxWitnessesInSimpleScript (RequireAnyOf simpleScripts) = maximum $ map maxWitnessesInSimpleScript simpleScriptsRequireAnyOf, so the consumer fix matters even with the parser fix.) - Regression test: add
{"type": "any", "scripts": []}to the simple-script JSON tests (expect parse failure) and a unit test that fee estimation on aRequireAnyOf []witness returns 0 instead of throwing.
Impact note
cardano-cli transaction build (and any cardano-api consumer that estimates fees for user-supplied scripts, e.g. a tx-building backend) aborts with an uncaught exception when given an empty any script, instead of a clean, actionable error. No funds at risk: the script can never validate on-chain (an empty any is unsatisfiable), so no transaction built with it could ever be submitted successfully; the bug only degrades the failure mode from "clean error" to "uncaught exception". Availability impact is limited to the affected CLI invocation / API call.
References
- CWE-248 (Uncaught Exception) / CWE-617 (Reachable Assertion).
- Note:
RequireMOf 0 [](from{"type": "atLeast", "required": 0, "scripts": []}, which the parser also accepts) does not crash —sum $ take 0 ... == 0— so theatLeastcase needs no fix for this bug, though rejectingrequired == 0with an empty list would be consistent. - Environment: code audit, 2026-11-23; no GHC build was available in the audit VM.
- Lingua principale
- Haskell
- Stelle
- 41
- Fork
- 33
- Merge medio
- 5g 17h
- PR unite (30g)
- 16
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di IntersectMBO/cardano-api
-
Difficoltà 3/5 Mezza giornata Idoneità per principianti 65/100
IntersectMBO/cardano-api#1425 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
IntersectMBO/cardano-api#1423 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 58/100
IntersectMBO/cardano-api#1419 ·
I maintainer di solito rispondono entro 2 giorni
-
Add a dedicated transaction viewing type to the experimental APIForse già presa @palas l’ha presa 11 giorni fa. Apertatype: enhancement
IntersectMBO/cardano-api#1416 · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
-
Dijkstra era type: enhancement
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
IntersectMBO/cardano-api#1386 ·
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di IntersectMBO/cardano-api
Issue simili
-
area:client bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Difficoltà 2/5 Meno di un'ora Idoneità per principianti 72/100
fmidue/term-tasks#34 · 1 reazione ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 75/100
fmidue/logic-tasks#394 ·
-
difficulty: easy open-public refactor scope
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
seal-harness/seal-harness#348 ·
I maintainer di solito rispondono entro 1 giorno