`Prelude.maximum: empty list` crash in fee estimation for empty `RequireAnyOf` (`{"type": "any", "scripts": []}`) scripts

Aperta Adatta ai principianti
#1,424 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cli, haskell, json
Ambito
cli, tooling

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, commit 52b4f94 (manifests via IntersectMBO/cardano-cli, commit dabfa5a, 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.

  1. Create the empty-any script file:
    cat > empty-any.json <<'EOF'
    {
      "type": "any",
      "scripts": []
    }
    EOF
    
  2. Attempt to build a transaction spending a UTxO locked by that script:
    cardano-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
    
    (Any --tx-in whose witness is the empty-any script 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):

  1. Parser accepts the empty list — cardano-api/cardano-api/src/Cardano/Api/Plutus/Internal/Script.hs, simpleScriptParsers:
    ("any", \obj -> RequireAnyOf <$> obj .: "scripts")
    
    No length check. Note the contrast with parseScriptAtLeast in the same file, which does validate (required > numScripts fails) — the any/all cases simply lack an equivalent check. RequireAllOf [] is harmless (sum [] == 0); only RequireAnyOf [] crashes.
  2. CLI reads the file through this parser — cardano-cli/.../Read.hs, deserialiseScriptInAnyLang falls back to Aeson.eitherDecodeStrict' → SimpleScript FromJSON → parseSimpleScript. The experimental path (Cardano.Api.Experimental.AnyScript.readSimpleScriptFromJson) uses the same parser.
  3. Fee estimation crashes — cardano-api/cardano-api/src/Cardano/Api/Tx/Internal/Fee.hs (estimateTxInWitnesses → maxWitnessesInSimpleScript):
    maxWitnessesInSimpleScript (RequireAnyOf simpleScripts) =
        maximum $ map maxWitnessesInSimpleScript simpleScripts
    
    With simpleScripts == [], maximum [] throws Prelude.maximum: empty list. The identical line exists in Cardano.Api.Experimental.Tx.Internal.Fee (line ~1892).
  4. 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 escapes makeTransactionBodyAutoBalance (an ExceptT that does not catch pure exceptions) to the CLI top handler, which prints it and exits 1.
  5. The same crash is reachable via the CBOR path (a text-envelope script file whose CBOR encodes an empty RequireAnyOf list — the ledger decoder accepts it) and via nesting ({"type": "all", "scripts": [{"type": "any", "scripts": []}]}), since maxWitnessesInSimpleScript recurses.
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:

  1. Parser (Cardano.Api.Plutus.Internal.Script, simpleScriptParsers): reject empty lists for any (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)
    
  2. Consumer (Cardano.Api.Tx.Internal.Fee and Cardano.Api.Experimental.Tx.Internal.Fee): make maxWitnessesInSimpleScript total — an empty any needs no witnesses (it can never be satisfied):
    maxWitnessesInSimpleScript (RequireAnyOf []) = 0
    maxWitnessesInSimpleScript (RequireAnyOf simpleScripts) =
        maximum $ map maxWitnessesInSimpleScript simpleScripts
    
    (The CBOR path can still produce an empty RequireAnyOf, so the consumer fix matters even with the parser fix.)
  3. Regression test: add {"type": "any", "scripts": []} to the simple-script JSON tests (expect parse failure) and a unit test that fee estimation on a RequireAnyOf [] 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 the atLeast case needs no fix for this bug, though rejecting required == 0 with 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

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di IntersectMBO/cardano-api

Tutte le issue di IntersectMBO/cardano-api

Issue simili

Altre issue su Haskell

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.