Skip to content

Commit 800af7f

Browse files
committed
add whitelist parsing tests
1 parent 90e5cbe commit 800af7f

File tree

3 files changed

+63
-19
lines changed
  • cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit
  • cardano-db-sync/src/Cardano/DbSync/Config

3 files changed

+63
-19
lines changed

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ unitTests iom knownMigrations =
3434
, testCase "mismatched conway genesis hash" Config.wrongConwayGenesisHash
3535
, testCase "default insert config" Config.defaultInsertConfig
3636
, testCase "insert config" Config.insertConfig
37+
, testGroup
38+
"invalid whitelist hashes"
39+
[ testCase "Fail if Shelley stake address hash is invalid" Config.invalidShelleyStkAddrHash
40+
, testCase "Fail if multi-asset policies hash is invalid" Config.invalidMultiAssetPoliciesHash
41+
, testCase "Fail if Plutus script hash invalid" Config.invalidPlutusScriptHash
42+
]
3743
, testGroup
3844
"tx-out"
3945
[ test "consumed_by_tx_id column check" MigrateConsumedPruneTxOut.txConsumedColumnCheck

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/Parse.hs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@ module Test.Cardano.Db.Mock.Unit.Conway.Config.Parse (
88
wrongConwayGenesisHash,
99
insertConfig,
1010
defaultInsertConfig,
11-
) where
11+
invalidShelleyStkAddrHash,
12+
invalidMultiAssetPoliciesHash,
13+
invalidPlutusScriptHash,
14+
)
15+
where
1216

1317
import Cardano.DbSync.Config
1418
import Cardano.DbSync.Config.Types
1519
import Cardano.DbSync.Error
1620
import Cardano.Prelude hiding (from, isNothing)
1721
import qualified Data.Aeson as Aeson
1822
import Data.Default.Class (Default (..))
23+
import Data.String (String)
24+
import Data.Text (pack)
1925
import Test.Cardano.Db.Mock.Config
2026
import Test.Tasty.HUnit (Assertion (), assertBool, (@?=))
2127
import Prelude ()
2228

2329
conwayGenesis :: Assertion
2430
conwayGenesis =
2531
mkSyncNodeConfig configDir initCommandLineArgs
26-
>>= void . mkConfig configDir mutableDir cmdLineArgs
32+
>>= void
33+
. mkConfig configDir mutableDir cmdLineArgs
2734
where
2835
configDir = "config-conway"
2936
mutableDir = mkMutableDir "conwayConfigSimple"
@@ -106,3 +113,27 @@ insertConfig = do
106113
dncInsertOptions cfg @?= expected
107114
where
108115
configDir = "config-conway-insert-options"
116+
117+
invalidShelleyStkAddrHash :: Assertion
118+
invalidShelleyStkAddrHash =
119+
let invalidJson = "{ \"enable\": true, \"stake_addresses\": " <> invalidHash <> " }"
120+
decodedResult :: Either String ShelleyInsertConfig
121+
decodedResult = Aeson.eitherDecodeStrict $ encodeUtf8 $ pack invalidJson
122+
in assertBool "Decoding should fail for invalid Shelley stake address hash" (isLeft decodedResult)
123+
124+
invalidMultiAssetPoliciesHash :: Assertion
125+
invalidMultiAssetPoliciesHash =
126+
let invalidJson = "{ \"enable\": true, \"policies\": " <> invalidHash <> " }"
127+
decodedResult :: Either String MultiAssetConfig
128+
decodedResult = Aeson.eitherDecodeStrict $ encodeUtf8 $ pack invalidJson
129+
in assertBool "Decoding should fail for invalid MultiAsset policies hash" (isLeft decodedResult)
130+
131+
invalidPlutusScriptHash :: Assertion
132+
invalidPlutusScriptHash =
133+
let invalidJson = "{ \"enable\": true, \"script_hashes\": " <> invalidHash <> " }"
134+
decodedResult :: Either String PlutusConfig
135+
decodedResult = Aeson.eitherDecodeStrict $ encodeUtf8 $ pack invalidJson
136+
in assertBool "Decoding should fail for invalid Plutus script hash" (isLeft decodedResult)
137+
138+
invalidHash :: String
139+
invalidHash = "[\"\\xe0758b08dea05dabd1cd3510689ebd9efb6a49316acb30eead750e2e9e\"]"

cardano-db-sync/src/Cardano/DbSync/Config/Types.hs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import qualified Data.Aeson as Aeson
6868
import Data.Aeson.Types (Parser, typeMismatch)
6969
import Data.ByteString.Short (ShortByteString (), fromShort, toShort)
7070
import Data.Default.Class (Default (..))
71+
import qualified Data.Text as T
7172
import Ouroboros.Consensus.Cardano.CanHardFork (TriggerHardFork (..))
7273

7374
newtype LogFileDir = LogFileDir
@@ -474,11 +475,12 @@ instance FromJSON ShelleyInsertConfig where
474475
enable <- obj .: "enable"
475476
stakeAddrs <- obj .:? "stake_addresses"
476477

477-
pure $
478-
case (enable, stakeAddrs) of
479-
(False, _) -> ShelleyDisable
480-
(True, Nothing) -> ShelleyEnable
481-
(True, Just addrs) -> ShelleyStakeAddrs (map parseShortByteString addrs)
478+
case (enable, stakeAddrs) of
479+
(False, _) -> pure ShelleyDisable
480+
(True, Nothing) -> pure ShelleyEnable
481+
(True, Just addrs) -> do
482+
addrsParsed <- traverse parseValidateHash addrs
483+
pure $ ShelleyStakeAddrs addrsParsed
482484

483485
instance ToJSON MultiAssetConfig where
484486
toJSON cfg =
@@ -495,11 +497,12 @@ instance FromJSON MultiAssetConfig where
495497
enable <- obj .: "enable"
496498
policies <- obj .:? "policies"
497499

498-
pure $
499-
case (enable, policies) of
500-
(False, _) -> MultiAssetDisable
501-
(True, Nothing) -> MultiAssetEnable
502-
(True, Just ps) -> MultiAssetPolicies (map parseShortByteString ps)
500+
case (enable, policies) of
501+
(False, _) -> pure MultiAssetDisable
502+
(True, Nothing) -> pure MultiAssetEnable
503+
(True, Just ps) -> do
504+
policiesParsed <- traverse parseValidateHash ps
505+
pure $ MultiAssetPolicies policiesParsed
503506

504507
instance ToJSON MetadataConfig where
505508
toJSON cfg =
@@ -537,11 +540,12 @@ instance FromJSON PlutusConfig where
537540
enable <- obj .: "enable"
538541
scriptHashes <- obj .:? "script_hashes"
539542

540-
pure $
541-
case (enable, scriptHashes) of
542-
(False, _) -> PlutusDisable
543-
(True, Nothing) -> PlutusEnable
544-
(True, Just hs) -> PlutusScripts (map parseShortByteString hs)
543+
case (enable, scriptHashes) of
544+
(False, _) -> pure PlutusDisable
545+
(True, Nothing) -> pure PlutusEnable
546+
(True, Just hs) -> do
547+
hsParsed <- traverse parseValidateHash hs
548+
pure $ PlutusScripts hsParsed
545549

546550
instance ToJSON GovernanceConfig where
547551
toJSON = boolToEnableDisable . isGovernanceEnabled
@@ -601,8 +605,11 @@ enableDisableToBool = \case
601605
"disable" -> Just False
602606
_ -> Nothing
603607

604-
parseShortByteString :: Text -> ShortByteString
605-
parseShortByteString = toShort . encodeUtf8
608+
parseValidateHash :: Text -> Parser ShortByteString
609+
parseValidateHash txt =
610+
if "\\x" `T.isPrefixOf` txt
611+
then fail $ "Invalid Hash: starts with \\x please adjust it: " <> show txt
612+
else pure $ toShort $ encodeUtf8 txt
606613

607614
shortByteStringToJSON :: ShortByteString -> Aeson.Value
608615
shortByteStringToJSON = toJSON . decodeUtf8 . fromShort

0 commit comments

Comments
 (0)