Skip to content

Commit 464a9d7

Browse files
chessaiedmundnoble
authored andcommitted
port pact4 miner key injection test to pact5
Change-Id: I073a658c91f39cb34a6ff3369c5ed701655e916d
1 parent 973bf1f commit 464a9d7

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

test/unit/Chainweb/Test/Pact5/TransactionTests.hs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE FlexibleContexts #-}
3+
{-# LANGUAGE ImportQualifiedPost #-}
34
{-# LANGUAGE LambdaCase #-}
45
{-# LANGUAGE OverloadedStrings #-}
56
{-# LANGUAGE QuasiQuotes #-}
67
{-# LANGUAGE ScopedTypeVariables #-}
78

89
module Chainweb.Test.Pact5.TransactionTests (tests) where
910

10-
import Test.Tasty
11-
import Test.Tasty.HUnit
12-
11+
import Chainweb.Pact5.Templates
12+
import Chainweb.Miner.Pact
1313
import Control.Lens hiding ((.=))
14-
1514
import Data.Foldable
1615
import Data.Text (unpack)
17-
import qualified Data.Map.Strict as Map
18-
19-
-- internal pact modules
20-
21-
import Pact.Core.Repl
22-
import Pact.Core.Pretty
2316
import Pact.Core.Environment
2417
import Pact.Core.Errors
2518
import Pact.Core.Info
19+
import Pact.Core.Pretty
20+
import Pact.Core.Repl
2621
import Pact.Core.Repl.Utils
22+
import Control.Monad (when)
23+
import Data.Text qualified as Text
24+
import Pact.Types.KeySet qualified as Pact4
25+
import Test.Tasty
26+
import Test.Tasty.HUnit
27+
import Data.Map.Strict qualified as Map
2728

2829
-- ---------------------------------------------------------------------- --
2930
-- Global settings
3031

32+
tests :: TestTree
33+
tests = testGroup "Chainweb.Test.Pact5.TransactionTests"
34+
[ testCase "coin contract v6" $ runReplTest coinReplV6
35+
, testCase "namespace v1" $ runReplTest nsReplV1
36+
, testCase "namespace v2" $ runReplTest nsReplV2
37+
, testCase "miner key injection" injectionTest
38+
]
39+
3140
coinReplV6 :: FilePath
3241
coinReplV6 = "pact/pact5/coin-contract/coin.repl"
3342

@@ -57,9 +66,26 @@ runReplTest file = do
5766
ReplTestFailed msg ->
5867
failWithErr rstate (PEExecutionError (EvalError msg) [] _loc)
5968

60-
tests :: TestTree
61-
tests = testGroup "Chainweb.Test.Pact5.TransactionTests"
62-
[ testCase "coin contract v6" $ runReplTest coinReplV6
63-
, testCase "namespace v1" $ runReplTest nsReplV1
64-
, testCase "namespace v2" $ runReplTest nsReplV2
65-
]
69+
injectionTest :: Assertion
70+
injectionTest = do
71+
let (expr, pv) = mkCoinbaseTerm badMinerId minerKeys0 1.0
72+
73+
assertEqual "Precompiled exploit yields correct code" (renderText expr) $
74+
"(coin.coinbase \"alpha\" (read-keyset \"miner-keyset\") 9999999.99)"
75+
<> "(coin.coinbase \"alpha\" (read-keyset \"miner-keyset\") (read-decimal \"reward\"))"
76+
77+
let stmt = renderText pv
78+
79+
when ("coinbase" `Text.isInfixOf` stmt) $
80+
assertFailure "Precompiled statement contains exploitable code"
81+
82+
when ("read-keyset" `Text.isInfixOf` stmt) $
83+
assertFailure "Precompiled statement contains exploitable code"
84+
85+
badMinerId :: MinerId
86+
badMinerId = MinerId "alpha\" (read-keyset \"miner-keyset\") 9999999.99)(coin.coinbase \"alpha"
87+
88+
minerKeys0 :: MinerKeys
89+
minerKeys0 = MinerKeys $ Pact4.mkKeySet
90+
["f880a433d6e2a13a32b6169030f56245efdd8c1b8a5027e9ce98a88e886bef27"]
91+
"default"

0 commit comments

Comments
 (0)