{-# LANGUAGE OverloadedStrings #-}
module Convex.ThreatModel.LargeValue (
largeValueAttack,
largeValueAttackWith,
) where
import Cardano.Api qualified as C
import Convex.ThreatModel
import Convex.ThreatModel.TxModifier (addPlutusScriptMint, alwaysSucceedsMintingPolicy)
import Data.ByteString.Char8 qualified as BS
import GHC.Exts (fromList)
largeValueAttack :: ThreatModel ()
largeValueAttack :: ThreatModel ()
largeValueAttack = Int -> ThreatModel ()
largeValueAttackWith Int
50
largeValueAttackWith :: Int -> ThreatModel ()
largeValueAttackWith :: Int -> ThreatModel ()
largeValueAttackWith Int
numTokens = [Char] -> ThreatModel () -> ThreatModel ()
forall a. [Char] -> ThreatModel a -> ThreatModel a
Named ([Char]
"Large Value Attack (" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
numTokens [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" tokens)") (ThreatModel () -> ThreatModel ())
-> ThreatModel () -> ThreatModel ()
forall a b. (a -> b) -> a -> b
$ do
[Output]
outputs <- ThreatModel [Output]
getTxOutputs
let scriptOutputs :: [Output]
scriptOutputs = (Output -> Bool) -> [Output] -> [Output]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Output -> Bool) -> Output -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AddressAny -> Bool
isKeyAddressAny (AddressAny -> Bool) -> (Output -> AddressAny) -> Output -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Output -> AddressAny
forall t. IsInputOrOutput t => t -> AddressAny
addressOf) [Output]
outputs
ThreatModel () -> ThreatModel ()
forall a. ThreatModel a -> ThreatModel a
threatPrecondition (ThreatModel () -> ThreatModel ())
-> ThreatModel () -> ThreatModel ()
forall a b. (a -> b) -> a -> b
$ Bool -> ThreatModel ()
ensure (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Output] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Output]
scriptOutputs)
Output
target <- [Output] -> ThreatModel Output
forall a. Show a => [a] -> ThreatModel a
pickAny [Output]
scriptOutputs
let policyId :: PolicyId
policyId = ScriptHash -> PolicyId
C.PolicyId (ScriptHash -> PolicyId) -> ScriptHash -> PolicyId
forall a b. (a -> b) -> a -> b
$ Script PlutusScriptV2 -> ScriptHash
forall lang. Script lang -> ScriptHash
hashScript (PlutusScriptVersion PlutusScriptV2
-> PlutusScript PlutusScriptV2 -> Script PlutusScriptV2
forall lang.
IsPlutusScriptLanguage lang =>
PlutusScriptVersion lang -> PlutusScript lang -> Script lang
C.PlutusScript PlutusScriptVersion PlutusScriptV2
C.PlutusScriptV2 PlutusScript PlutusScriptV2
alwaysSucceedsMintingPolicy)
junkTokens :: [(AssetName, Quantity)]
junkTokens =
[ (ByteString -> AssetName
C.UnsafeAssetName (ByteString -> AssetName) -> ByteString -> AssetName
forall a b. (a -> b) -> a -> b
$ [Char] -> ByteString
BS.pack ([Char] -> ByteString) -> [Char] -> ByteString
forall a b. (a -> b) -> a -> b
$ [Char]
"junk" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
i, Integer -> Quantity
C.Quantity Integer
1)
| Int
i <- [Int
1 .. Int
numTokens]
]
junkValue :: Value
junkValue =
[Item Value] -> Value
forall l. IsList l => [Item l] -> l
fromList
[ (PolicyId -> AssetName -> AssetId
C.AssetId PolicyId
policyId AssetName
name, Quantity
qty)
| (AssetName
name, Quantity
qty) <- [(AssetName, Quantity)]
junkTokens
]
bloatedValue :: Value
bloatedValue = Output -> Value
forall t. IsInputOrOutput t => t -> Value
valueOf Output
target Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Value
junkValue
[Char] -> ThreatModel ()
counterexampleTM ([Char] -> ThreatModel ()) -> [Char] -> ThreatModel ()
forall a b. (a -> b) -> a -> b
$
[[Char]] -> [Char]
paragraph
[ [Char]
"The transaction contains a script output at index"
, TxIx -> [Char]
forall a. Show a => a -> [Char]
show (Output -> TxIx
outputIx Output
target)
, [Char]
"."
]
[Char] -> ThreatModel ()
counterexampleTM ([Char] -> ThreatModel ()) -> [Char] -> ThreatModel ()
forall a b. (a -> b) -> a -> b
$
[[Char]] -> [Char]
paragraph
[ [Char]
"Testing if"
, Int -> [Char]
forall a. Show a => a -> [Char]
show Int
numTokens
, [Char]
"junk tokens can be minted and added to the output's value"
, [Char]
"while still passing validation."
]
[Char] -> ThreatModel ()
counterexampleTM ([Char] -> ThreatModel ()) -> [Char] -> ThreatModel ()
forall a b. (a -> b) -> a -> b
$
[[Char]] -> [Char]
paragraph
[ [Char]
"If this validates, the script's value validation is permissive."
, [Char]
"An attacker could exploit this to:"
, [Char]
"1) Increase min-UTxO requirements, locking victim's Ada"
, [Char]
"2) Inflate transaction sizes, increasing spending costs"
, [Char]
"3) Potentially lock funds permanently if size limits are exceeded"
]
let mintModifiers :: TxModifier
mintModifiers =
[TxModifier] -> TxModifier
forall a. Monoid a => [a] -> a
mconcat
[ PlutusScript PlutusScriptV2
-> AssetName -> Quantity -> ScriptData -> TxModifier
forall lang.
IsPlutusScriptInEra lang =>
PlutusScript lang
-> AssetName -> Quantity -> ScriptData -> TxModifier
addPlutusScriptMint PlutusScript PlutusScriptV2
alwaysSucceedsMintingPolicy AssetName
name Quantity
qty (() -> ScriptData
forall a. ToData a => a -> ScriptData
toScriptData ())
| (AssetName
name, Quantity
qty) <- [(AssetName, Quantity)]
junkTokens
]
TxModifier -> ThreatModel ()
shouldNotValidate (TxModifier -> ThreatModel ()) -> TxModifier -> ThreatModel ()
forall a b. (a -> b) -> a -> b
$
Output -> Value -> TxModifier
forall t. IsInputOrOutput t => t -> Value -> TxModifier
changeValueOf Output
target Value
bloatedValue
TxModifier -> TxModifier -> TxModifier
forall a. Semigroup a => a -> a -> a
<> TxModifier
mintModifiers