Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard against overflows in Shelley TxIns #4956

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions cardano-api/src/Cardano/Api/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ module Cardano.Api.TxBody (
) where

import Control.Applicative (some)
import Control.Monad (guard)
import Control.Monad (guard, unless)
import Data.Aeson (object, withObject, (.:), (.:?), (.=))
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Key as Aeson
Expand Down Expand Up @@ -2463,26 +2463,31 @@ validateTxBodyContent era txBodContent@TxBodyContent {
in case era of
ShelleyBasedEraShelley -> do
validateTxIns txIns
guardShelleyTxInsOverflow (map fst txIns)
validateTxOuts era txOuts
validateMetadata txMetadata
ShelleyBasedEraAllegra -> do
validateTxIns txIns
guardShelleyTxInsOverflow (map fst txIns)
validateTxOuts era txOuts
validateMetadata txMetadata
ShelleyBasedEraMary -> do
validateTxIns txIns
guardShelleyTxInsOverflow (map fst txIns)
validateTxOuts era txOuts
validateMetadata txMetadata
validateMintValue txMintValue
ShelleyBasedEraAlonzo -> do
validateTxIns txIns
guardShelleyTxInsOverflow (map fst txIns)
validateTxOuts era txOuts
validateMetadata txMetadata
validateMintValue txMintValue
validateTxInsCollateral txInsCollateral languages
validateProtocolParameters txProtocolParams languages
ShelleyBasedEraBabbage -> do
validateTxIns txIns
guardShelleyTxInsOverflow (map fst txIns)
validateTxOuts era txOuts
validateMetadata txMetadata
validateMintValue txMintValue
Expand Down Expand Up @@ -2524,9 +2529,10 @@ validateTxInsCollateral
:: TxInsCollateral era -> Set Alonzo.Language -> Either TxBodyError ()
validateTxInsCollateral txInsCollateral languages =
case txInsCollateral of
TxInsCollateralNone | not (Set.null languages)
-> Left TxBodyEmptyTxInsCollateral
_ -> return ()
TxInsCollateralNone ->
unless (Set.null languages) (Left TxBodyEmptyTxInsCollateral)
TxInsCollateral _ collateralTxIns ->
guardShelleyTxInsOverflow collateralTxIns

validateTxOuts :: ShelleyBasedEra era -> [TxOut CtxTx era] -> Either TxBodyError ()
validateTxOuts era txOuts =
Expand Down Expand Up @@ -3551,6 +3557,11 @@ getLedgerEraConstraint ShelleyBasedEraAlonzo f = f
getLedgerEraConstraint ShelleyBasedEraBabbage f = f
getLedgerEraConstraint ShelleyBasedEraConway f = f

guardShelleyTxInsOverflow :: [TxIn] -> Either TxBodyError ()
guardShelleyTxInsOverflow txIns = do
for_ txIns $ \txin@(TxIn _ (TxIx txix)) ->
guard (txix <= maxShelleyTxInIx) ?! TxBodyInIxOverflow txin

makeShelleyTransactionBody
:: ShelleyBasedEra era
-> TxBodyContent BuildTx era
Expand Down