Skip to content
This repository was archived by the owner on Jan 9, 2026. It is now read-only.

Commit 2f6bb9f

Browse files
committed
escape unicode null character in sql text values
1 parent 324f52b commit 2f6bb9f

File tree

7 files changed

+42
-44
lines changed

7 files changed

+42
-44
lines changed

haskell-src/chainweb-data.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ library
7272
ChainwebDb.Types.Event
7373
ChainwebDb.Types.MinerKey
7474
ChainwebDb.Types.Signer
75+
ChainwebDb.Types.PgText
7576
ChainwebDb.Types.Transaction
7677
ChainwebDb.Types.Transfer
7778
build-depends:

haskell-src/exec/Chainweb/BackfillTransfers.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
{-# LANGUAGE LambdaCase #-}
44
{-# LANGUAGE OverloadedStrings #-}
55
{-# LANGUAGE NoImplicitPrelude #-}
6-
{-# LANGUAGE NumericUnderscores #-}
76
{-# LANGUAGE ScopedTypeVariables #-}
87
{-# LANGUAGE TupleSections #-}
98
{-# LANGUAGE TypeApplications #-}
@@ -19,6 +18,7 @@ import ChainwebData.Env
1918
import ChainwebData.Types
2019
import ChainwebDb.Types.Event
2120
import ChainwebDb.Types.Transfer
21+
import ChainwebDb.Types.PgText
2222

2323
import Control.Concurrent.Async (race_)
2424
import Control.Lens hiding ((<.), reuse)
@@ -109,8 +109,8 @@ createTransfer ev = do
109109
<*> pure (_ev_idx ev)
110110
<*> pure (_ev_module ev)
111111
<*> pure (_ev_moduleHash ev)
112-
<*> from_acct
113-
<*> to_acct
112+
<*> (PgText <$> from_acct)
113+
<*> (PgText <$> to_acct)
114114
<*> getAmount (unwrap $ _ev_params ev)
115115
where
116116
from_acct = _ev_params ev ^? to unwrap . ix 0 . _String

haskell-src/exec/Chainweb/Lookups.hs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2-
{-# LANGUAGE LambdaCase #-}
31
{-# LANGUAGE OverloadedStrings #-}
4-
{-# LANGUAGE NumericUnderscores #-}
52
{-# LANGUAGE ScopedTypeVariables #-}
63
{-# LANGUAGE TypeFamilies #-}
7-
{-# LANGUAGE TypeApplications #-}
84

95
module Chainweb.Lookups
106
( -- * Endpoints
@@ -46,6 +42,7 @@ import ChainwebDb.Types.Block
4642
import ChainwebDb.Types.Common
4743
import ChainwebDb.Types.DbHash
4844
import ChainwebDb.Types.Event
45+
import ChainwebDb.Types.PgText
4946
import ChainwebDb.Types.Signer
5047
import ChainwebDb.Types.Transaction
5148
import ChainwebDb.Types.Transfer
@@ -238,8 +235,8 @@ mkTransferRows height cid@(ChainId cid') blockhash _creationTime pl eventMinHeig
238235
, _tr_idx = _ev_idx ev
239236
, _tr_modulename = _ev_module ev
240237
, _tr_moduleHash = _ev_moduleHash ev
241-
, _tr_from_acct = fromAccount
242-
, _tr_to_acct = toAccount
238+
, _tr_from_acct = PgText fromAccount
239+
, _tr_to_acct = PgText toAccount
243240
, _tr_amount = amount
244241
}
245242
getAmount :: [Value] -> Maybe KDAScientific
@@ -259,7 +256,7 @@ mkTransferRows height cid@(ChainId cid') blockhash _creationTime pl eventMinHeig
259256
createNonCoinBaseTransfers xs = [ transfer
260257
| (txhash,_,evs) <- xs
261258
, ev <- evs
262-
, T.takeEnd 8 (_ev_qualName ev) == "TRANSFER"
259+
, T.takeEnd 8 (unPgText $ _ev_qualName ev) == "TRANSFER"
263260
, length (unwrap (_ev_params ev)) == 3
264261
, transfer <- maybeToList $ mkTransfer (Just txhash) ev
265262
]
@@ -346,11 +343,11 @@ mkEvent (ChainId chainid) height block requestkey ev idx = Event
346343
, _ev_chainid = fromIntegral chainid
347344
, _ev_height = height
348345
, _ev_idx = idx
349-
, _ev_name = ename ev
350-
, _ev_qualName = qname ev
351-
, _ev_module = emodule ev
352-
, _ev_moduleHash = emoduleHash ev
353-
, _ev_paramText = T.decodeUtf8 $ toStrict $ encode $ params ev
346+
, _ev_name = PgText $ ename ev
347+
, _ev_qualName = PgText $ qname ev
348+
, _ev_module = PgText $ emodule ev
349+
, _ev_moduleHash = PgText $ emoduleHash ev
350+
, _ev_paramText = PgText $ T.decodeUtf8 $ toStrict $ encode $ params ev
354351
, _ev_params = PgJSONB $ toList $ params ev
355352
}
356353
where

haskell-src/exec/Chainweb/Server.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import ChainwebData.TxSummary
8181
import ChainwebDb.Types.Block
8282
import ChainwebDb.Types.Common
8383
import ChainwebDb.Types.DbHash
84+
import ChainwebDb.Types.PgText
8485
import ChainwebDb.Types.Signer
8586
import ChainwebDb.Types.Transfer
8687
import ChainwebDb.Types.Transaction
@@ -391,7 +392,7 @@ toApiTxDetail tx contHist blk evs signers sigs = TxDetail
391392
where
392393
unMaybeValue = maybe Null unPgJsonb
393394
toTxEvent ev =
394-
TxEvent (_ev_qualName ev) (unPgJsonb $ _ev_params ev)
395+
TxEvent (unPgText $ _ev_qualName ev) (unPgJsonb $ _ev_params ev)
395396

396397
getMaxBlockHeight :: LogFunctionIO Text -> Connection -> IO (Maybe BlockHeight)
397398
getMaxBlockHeight logger c =
@@ -511,10 +512,10 @@ accountHandler logger pool req account token chain minheight maxheight limit mbO
511512
continuation <- mkContinuation readEventToken mbOffset mbNext
512513
isBounded <- isBoundedStrategyM req
513514
let searchParams = TransferSearchParams
514-
{ tspToken = usedCoinType
515+
{ tspToken = PgText usedCoinType
515516
, tspChainId = chain
516517
, tspHeightRange = HeightRangeParams minheight maxheight
517-
, tspAccount = account
518+
, tspAccount = PgText account
518519
}
519520
liftIO $ M.with pool $ \(c, throttling) -> do
520521
let
@@ -531,15 +532,15 @@ accountHandler logger pool req account token chain minheight maxheight limit mbO
531532
continuation
532533
resultLimit
533534
return $ maybe noHeader (addHeader . mkEventToken) mbCont $ results <&> \(tr, extras) -> TransferDetail
534-
{ _trDetail_token = _tr_modulename tr
535+
{ _trDetail_token = unPgText $ _tr_modulename tr
535536
, _trDetail_chain = fromIntegral $ _tr_chainid tr
536537
, _trDetail_height = fromIntegral $ _tr_height tr
537538
, _trDetail_blockHash = unDbHash $ unBlockId $ _tr_block tr
538539
, _trDetail_requestKey = getTxHash $ _tr_requestkey tr
539540
, _trDetail_idx = fromIntegral $ _tr_idx tr
540541
, _trDetail_amount = StringEncoded $ getKDAScientific $ _tr_amount tr
541-
, _trDetail_fromAccount = _tr_from_acct tr
542-
, _trDetail_toAccount = _tr_to_acct tr
542+
, _trDetail_fromAccount = unPgText $ _tr_from_acct tr
543+
, _trDetail_toAccount = unPgText $ _tr_to_acct tr
543544
, _trDetail_crossChainAccount = tseXChainAccount extras
544545
, _trDetail_crossChainId = fromIntegral <$> tseXChainId extras
545546
, _trDetail_blockTime = tseBlockTime extras
@@ -607,9 +608,9 @@ evHandler logger pool req limit mbOffset qSearch qParam qName qModuleName minhei
607608
resultLimit
608609
return $ maybe noHeader (addHeader . mkEventToken) mbCont $
609610
results <&> \(ev,extras) -> EventDetail
610-
{ _evDetail_name = _ev_qualName ev
611+
{ _evDetail_name = unPgText $ _ev_qualName ev
611612
, _evDetail_params = unPgJsonb $ _ev_params ev
612-
, _evDetail_moduleHash = _ev_moduleHash ev
613+
, _evDetail_moduleHash = unPgText $ _ev_moduleHash ev
613614
, _evDetail_chain = fromIntegral $ _ev_chainid ev
614615
, _evDetail_height = fromIntegral $ _ev_height ev
615616
, _evDetail_blockTime = eseBlockTime extras

haskell-src/lib/ChainwebDb/Queries.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import ChainwebDb.Database
4040
import ChainwebDb.Types.Block
4141
import ChainwebDb.Types.DbHash
4242
import ChainwebDb.Types.Event
43+
import ChainwebDb.Types.PgText
4344
import ChainwebDb.Types.Transaction
4445
import ChainwebDb.Types.Transfer
4546
import ChainwebDb.Types.Common (ReqKeyOrCoinbase)
@@ -177,14 +178,14 @@ eventSearchCond EventSearchParams{..} ev = and_ $
177178
where
178179
searchString search = "%" <> search <> "%"
179180
searchCond = fromMaybeArg espSearch $ \s ->
180-
(_ev_qualName ev `like_` val_ (searchString s)) ||.
181-
(_ev_paramText ev `like_` val_ (searchString s))
181+
(_ev_qualName ev `like_` val_ (searchString $ PgText s)) ||.
182+
(_ev_paramText ev `like_` val_ (searchString $ PgText s))
182183
qualNameCond = fromMaybeArg espName $ \(EventName n) ->
183-
_ev_qualName ev `like_` val_ (searchString n)
184+
_ev_qualName ev `like_` val_ (searchString $ PgText n)
184185
paramCond = fromMaybeArg espParam $ \(EventParam p) ->
185-
_ev_paramText ev `like_` val_ (searchString p)
186+
_ev_paramText ev `like_` val_ (searchString $ PgText p)
186187
moduleCond = fromMaybeArg espModuleName $ \(EventModuleName m) ->
187-
_ev_module ev ==. val_ m
188+
_ev_module ev ==. val_ (PgText m)
188189
fromMaybeArg mbA f = f <$> maybeToList mbA
189190

190191
data EventCursorT f = EventCursor
@@ -242,10 +243,10 @@ toAccountsSearchCursor Transfer{..} = EventCursor
242243
(asc _tr_idx)
243244

244245
data TransferSearchParams = TransferSearchParams
245-
{ tspToken :: Text
246+
{ tspToken :: PgText
246247
, tspChainId :: Maybe ChainId
247248
, tspHeightRange :: HeightRangeParams
248-
, tspAccount :: Text
249+
, tspAccount :: PgText
249250
}
250251

251252
transfersSearchSource ::

haskell-src/lib/ChainwebDb/Types/Event.hs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
{-# LANGUAGE DerivingStrategies #-}
44
{-# LANGUAGE FlexibleContexts #-}
55
{-# LANGUAGE FlexibleInstances #-}
6-
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
76
{-# LANGUAGE ImpredicativeTypes #-}
8-
{-# LANGUAGE LambdaCase #-}
97
{-# LANGUAGE MultiParamTypeClasses #-}
108
{-# LANGUAGE TypeFamilies #-}
119
{-# LANGUAGE UndecidableInstances #-}
@@ -18,12 +16,12 @@ module ChainwebDb.Types.Event where
1816
------------------------------------------------------------------------------
1917
import Data.Aeson
2018
import Data.Int
21-
import Data.Text (Text)
2219
import Database.Beam
2320
import Database.Beam.Postgres
2421
------------------------------------------------------------------------------
2522
import ChainwebDb.Types.Block
2623
import ChainwebDb.Types.Common
24+
import ChainwebDb.Types.PgText
2725
------------------------------------------------------------------------------
2826

2927
data EventT f = Event
@@ -32,11 +30,11 @@ data EventT f = Event
3230
, _ev_chainid :: C f Int64
3331
, _ev_height :: C f Int64
3432
, _ev_idx :: C f Int64
35-
, _ev_qualName :: C f Text
36-
, _ev_name :: C f Text
37-
, _ev_module :: C f Text
38-
, _ev_moduleHash :: C f Text
39-
, _ev_paramText :: C f Text
33+
, _ev_qualName :: C f PgText
34+
, _ev_name :: C f PgText
35+
, _ev_module :: C f PgText
36+
, _ev_moduleHash :: C f PgText
37+
, _ev_paramText :: C f PgText
4038
, _ev_params :: C f (PgJSONB [Value])
4139
}
4240
deriving stock (Generic)

haskell-src/lib/ChainwebDb/Types/Transfer.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ module ChainwebDb.Types.Transfer where
1818
----------------------------------------------------------------------------
1919
import BasePrelude
2020
import Data.Scientific
21-
import Data.Text (Text)
2221
import Database.Beam
2322
import Database.Beam.Backend.SQL.SQL92
2423
import Database.Beam.Postgres
@@ -28,17 +27,18 @@ import Database.PostgreSQL.Simple.FromField
2827
------------------------------------------------------------------------------
2928
import ChainwebDb.Types.Block
3029
import ChainwebDb.Types.Common
30+
import ChainwebDb.Types.PgText
3131
------------------------------------------------------------------------------
3232
data TransferT f = Transfer
3333
{ _tr_block :: PrimaryKey BlockT f
3434
, _tr_requestkey :: C f ReqKeyOrCoinbase
3535
, _tr_chainid :: C f Int64
3636
, _tr_height :: C f Int64
3737
, _tr_idx :: C f Int64
38-
, _tr_modulename :: C f Text
39-
, _tr_moduleHash :: C f Text
40-
, _tr_from_acct :: C f Text
41-
, _tr_to_acct :: C f Text
38+
, _tr_modulename :: C f PgText
39+
, _tr_moduleHash :: C f PgText
40+
, _tr_from_acct :: C f PgText
41+
, _tr_to_acct :: C f PgText
4242
, _tr_amount :: C f KDAScientific
4343
}
4444
deriving stock (Generic)
@@ -48,7 +48,7 @@ type Transfer = TransferT Identity
4848
type TransferId = PrimaryKey TransferT Identity
4949

5050
instance Table TransferT where
51-
data PrimaryKey TransferT f = TransferId (PrimaryKey BlockT f) (C f ReqKeyOrCoinbase) (C f Int64) (C f Int64) (C f Text)
51+
data PrimaryKey TransferT f = TransferId (PrimaryKey BlockT f) (C f ReqKeyOrCoinbase) (C f Int64) (C f Int64) (C f PgText)
5252
deriving stock (Generic)
5353
deriving anyclass (Beamable)
5454
primaryKey = TransferId <$> _tr_block <*> _tr_requestkey <*> _tr_chainid <*> _tr_idx <*> _tr_moduleHash

0 commit comments

Comments
 (0)