summaryrefslogtreecommitdiffhomepage
path: root/Val.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 /Val.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 'Val.hs')
-rw-r--r--Val.hs13
1 files changed, 9 insertions, 4 deletions
diff --git a/Val.hs b/Val.hs
index 86a35c9..40e718b 100644
--- a/Val.hs
+++ b/Val.hs
@@ -2,18 +2,23 @@
module Val where
-import Data.ByteString
+import Memory
+
import GHC.Generics (Generic)
import Data.Aeson
import Data.Aeson.Types
import qualified Codec.Binary.Base64 as B64
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
+import qualified Data.ByteString as B
-- | Newtype of ByteString so we can have JSON instances without orphans.
-newtype Val = Val { val :: ByteString }
+newtype Val = Val { val :: B.ByteString }
deriving (Show, Generic, Eq, Monoid)
+instance DataSize Val where
+ dataSize (Val b) = fromIntegral (B.length b)
+
-- | JSON instances for Val, using base64 encoding when the value
-- is not utf-8 encoded, and otherwise using a more efficient encoding.
instance ToJSON Val where
@@ -28,10 +33,10 @@ instance FromJSON Val where
Nothing -> Val <$> (unb64 =<< o .: "b64")
parseJSON invalid = typeMismatch "ByteString" invalid
-b64 :: ByteString -> T.Text
+b64 :: B.ByteString -> T.Text
b64 = T.decodeUtf8 . B64.encode
-unb64 :: Monad m => T.Text -> m ByteString
+unb64 :: Monad m => T.Text -> m B.ByteString
unb64 t = either
(\_ -> fail "bad base64 data")
return