Use secure entropy source to generate CSRF tokens

This commit is contained in:
Hypercube 2021-05-11 11:32:07 +08:00
parent 5d8566ad5c
commit b6215582d8
3 changed files with 23 additions and 4 deletions

View File

@ -46,6 +46,7 @@ import qualified Network.Wai as W
import Data.ByteString.Lazy.Char8 ()
import Data.Bits ((.|.), finiteBitSize, shiftL)
import Data.Text (Text)
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as BL
@ -59,7 +60,7 @@ import Yesod.Core.Class.Dispatch
import Yesod.Core.Internal.Run
import Text.Read (readMaybe)
import System.Environment (getEnvironment)
import qualified System.Random as Random
import System.Entropy (getEntropy)
import Control.AutoUpdate (mkAutoUpdate, defaultUpdateSettings, updateAction, updateFreq)
import Yesod.Core.Internal.Util (getCurrentMaxExpiresRFC1123)
@ -92,8 +93,19 @@ toWaiAppPlain site = do
, yreGetMaxExpires = getMaxExpires
}
-- | Generate a random number uniformly distributed in the full range
-- of 'Int'.
--
-- Note: Before 1.7.0, this generates pseudo-random number in an
-- unspecified range. The range size may not be a power of 2. Since
-- 1.7.0, this uses a secure entropy source and generates in the full
-- range of 'Int'.
defaultGen :: IO Int
defaultGen = Random.getStdRandom Random.next
defaultGen = bsToInt <$> getEntropy bytes
where
bits = finiteBitSize (undefined :: Int)
bytes = div (bits + 7) 8
bsToInt = S.foldl' (\v i -> shiftL v 8 .|. fromIntegral i) 0
-- | Pure low level function to construct WAI application. Usefull
-- when you need not standard way to run your app, or want to embed it

View File

@ -196,7 +196,13 @@ data YesodRunnerEnv site = YesodRunnerEnv
, yreSite :: !site
, yreSessionBackend :: !(Maybe SessionBackend)
, yreGen :: !(IO Int)
-- ^ Generate a random number
-- ^ Generate a random number uniformly distributed in the full
-- range of 'Int'.
--
-- Note: Before 1.7.0, the default value generates pseudo-random
-- number in an unspecified range. The range size may not be a power
-- of 2. Since 1.7.0, the default value uses a secure entropy source
-- and generates in the full range of 'Int'.
, yreGetMaxExpires :: !(IO Text)
}

View File

@ -1,5 +1,5 @@
name: yesod-core
version: 1.6.19.0
version: 1.7.0
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>
@ -39,6 +39,7 @@ library
, containers >= 0.2
, cookie >= 0.4.3 && < 0.5
, deepseq >= 1.3
, entropy
, fast-logger >= 2.2
, http-types >= 0.7
, memory