From 713521318289919cc481bf15f28a4a06554485dc Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 18 Apr 2017 14:43:16 -0400 Subject: memory DOS prevention Prevent DOS of user side by limiting the size of the BackLog that is maintained. This should not cause problems in even high latency environments, and should prevent memory use > 16 mb. The developer side does not keep much data, other than a list of the Hashes of things it has recently sent, so is not susceptable to memory DOS. This commit was sponsored by Brock Spratlen on Patreon. --- Types.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'Types.hs') diff --git a/Types.hs b/Types.hs index c3b5340..ec21254 100644 --- a/Types.hs +++ b/Types.hs @@ -12,6 +12,7 @@ module Types ( ) where import Val +import Memory import GHC.Generics (Generic) import Data.Aeson @@ -23,6 +24,9 @@ data Seen = Seen } deriving (Show, Generic) +instance DataSize Seen where + dataSize = dataSize . seenData + -- | Things that the developer enters. data Entered = Entered { enteredData :: Val @@ -32,6 +36,9 @@ data Entered = Entered } deriving (Show, Generic) +instance DataSize Entered where + dataSize e = dataSize (enteredData e) + dataSize (echoData e) + -- | High level protocol. data Proto a = Proto a @@ -40,6 +47,10 @@ data Proto a -- ^ sent by user to indicate when an Entered value was rejected. deriving (Show, Generic) +instance DataSize a => DataSize (Proto a) where + dataSize (Proto a) = dataSize a + dataSize (Rejected a) = dataSize a + -- | A Proto activity (either Entered or Seen) with a pointer -- to the Activity before this one. -- @@ -51,15 +62,27 @@ data Activity a = Activity } deriving (Show, Generic) +instance DataSize a => DataSize (Activity a) where + dataSize a = dataSize (activity a) + + maybe 0 dataSize (prevActivity a) + + dataSize (signature a) + newtype Signature = Signature Val deriving (Show, Generic) +instance DataSize Signature where + dataSize _ = 42 -- FIXME real size here + data Hash = Hash { hashMethod :: HashMethod , hashValue :: Val } deriving (Show, Generic, Eq) +instance DataSize Hash where + dataSize (Hash { hashMethod = SHA256 }) = 64 + dataSize (Hash { hashMethod = SHA3 }) = 56 + -- | We use SHA256. (SHA3 is included to future proof, and because it -- improves the generated JSON.) data HashMethod = SHA256 | SHA3 -- cgit v1.2.3