summaryrefslogtreecommitdiffhomepage
path: root/Types.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2017-04-18 14:43:16 -0400
committerJoey Hess <joeyh@joeyh.name>2017-04-18 14:43:16 -0400
commit713521318289919cc481bf15f28a4a06554485dc (patch)
tree8f4359a808165487ebf92b8e53080c406bced93a /Types.hs
parent9102a47c6c68039a288a6ee8f43fe14b034ce356 (diff)
downloaddebug-me-713521318289919cc481bf15f28a4a06554485dc.tar.gz
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.
Diffstat (limited to 'Types.hs')
-rw-r--r--Types.hs23
1 files changed, 23 insertions, 0 deletions
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