1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
{-|
Copyright: (c) Tom Smeding 2026
License: AGPL-3.0-or-later
-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
module Spamcheck (newSpamcheck, wrapSpamcheck, handleSpamSubmit) where
import Control.Concurrent (forkIO, threadDelay)
import Control.Monad (forever)
import Crypto.Hash.SHA256 qualified as H
import Data.ByteString (ByteString)
import Data.ByteString qualified as BS
import Data.ByteString.Base16 qualified as Base16
import Data.ByteString.Builder qualified as BSB
import Data.ByteString.Char8 qualified as BS8
import Data.ByteString.Lazy qualified as BSL
import Data.Char (ord)
import Data.IORef
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Builder.Linear qualified as TBL
import Data.Text.Encoding qualified as TE
import System.Clock qualified as Clock
import System.Environment (lookupEnv)
import System.IO.Unsafe (unsafePerformIO)
import System.Random.Stateful (uniformByteStringM, globalStdGen, UniformRange (uniformRM))
import Network.HTTP.Server.Mini
import Pages
halfTime, maxScore, threshold :: Double
halfTime = 2.0 -- seconds
maxScore = 10000.0
threshold = 100.0
decay :: Double -> Double -> Double
decay prevScore timeDeltaSecs = prevScore * exp(-log 0.5 / halfTime * (-timeDeltaSecs))
-- curve: start * exp(-ln(0.5) / halfTime * (t0 - t))
-- curve(t0 + halfTime) = start * exp(-ln(0.5) / halfTime * -halfTime) = start * exp(ln(0.5)) = 0.5 * start
--
-- Fixed point at sustained rate of n ticks per second
-- decay val (1/n) + 1 == val
-- val * decay 1.0 (1/n) + 1 == val
-- val == 1 / (1 - decay 1.0 (1/n))
--
-- Steady state score at n requests per second: (assuming unbounded score;
-- this is the maximum score attained in the steady state loop, i.e. the thing
-- checked by 'tick')
--
-- >>> mapM_ print [(n, 1 / (1 - decay 1.0 (1/n))) | n <- [30, 34, 35, 40]]
-- (30.0,87.062665155614)
-- (34.0,98.60411222405249)
-- (35.0,101.48947803609427)
-- (40.0,115.91632529852637)
--
-- Thus with threshold 100, 35 req/s gets marked as spam but 34 doesn't.
--
-- >>> decay maxScore 13.32 + 1.0
-- 99.88723390392818
--
-- Thus after ~13 seconds of nothing, the system is considered quiet again, regardless of what happened before.
data Spamcheck = Spamcheck (IORef (Clock.TimeSpec, Double)) (IORef ByteString)
genKey :: IO ByteString
genKey = do
bytes <- uniformByteStringM 22 globalStdGen -- ~130 bits of entropy
return (BS.map (\b -> alphabet `BS.index` (fromIntegral b `rem` BS.length alphabet)) bytes)
where
alphabet = BS8.pack (['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9'])
{-# NOINLINE monitorKey #-}
monitorKey :: Maybe ByteString
monitorKey = unsafePerformIO $ fmap BS8.pack <$> lookupEnv "TIRCLOGV_MONITOR_SPAM_KEY"
newSpamcheck :: IO Spamcheck
newSpamcheck = do
now <- Clock.getTime Clock.Monotonic
scoreref <- newIORef (now, 0.0)
cookieref <- newIORef =<< genKey
-- Refresh the cookie key periodically
_ <- forkIO $ forever $ do
-- 1 to 1.5 day, randomly (if you change this interval, also change the Max-Age of the Set-Cookie)
extraminutes <- uniformRM (0, 12*60) globalStdGen
let minutes = 24*60 + extraminutes
threadDelay (minutes * 60 * 1000_000)
writeIORef cookieref =<< genKey
return (Spamcheck scoreref cookieref)
tick :: Spamcheck -> IO Bool
tick (Spamcheck ref _) = do
!now <- Clock.getTime Clock.Monotonic
atomicModifyIORef' ref $ \(ts, val) ->
let timeDelta = fromIntegral (Clock.toNanoSecs (Clock.diffTimeSpec ts now)) / 1e9
!val' = min maxScore (decay val timeDelta + 1.0)
in ((now, val'), val' >= threshold)
wrapSpamcheck :: Spamcheck -> Request -> IO Response -> IO Response
wrapSpamcheck spamcheck@(Spamcheck _ cookieref) req handler = do
protect <- tick spamcheck
let clientcookie = lookup (BS8.pack "spamkey") (reqCookies req)
if not protect then handler else do
correctcookie <- readIORef cookieref
case clientcookie of
Just ck | ck == correctcookie -> handler
| Just ck == monitorKey -> handler
_ -> challengePageResponse (reqURI req) correctcookie False
challengePageResponse :: ByteString -> ByteString -> Bool -> IO Response
challengePageResponse prevurl cookie incorrect = do
(question, answer) <- genQuestion
return $ responseBS (Status 403 "Forbidden") [("Content-Type", "text/html")] $
renderPageSpamcheck SpamcheckData
{ question = question
, hash = TE.decodeASCII (Base16.encode (H.hash (cookie <> TE.encodeUtf8 answer)))
, prevurl = TE.decodeUtf8Lenient prevurl
, incorrect = incorrect
}
data SubmitQueryParams = SubmitQueryParams
{ sqpAns :: !ByteString
, sqpHash :: !ByteString
, sqpURL :: !ByteString }
handleSpamSubmit :: Spamcheck -> [(ByteString, ByteString)] -> IO Response
handleSpamSubmit (Spamcheck _ cookieref) query = do
let params = fromQuery query
currentcookie <- readIORef cookieref
-- If they managed to produce an (answer, hash) pair that passes this check,
-- this means either:
-- 1. they computed the correct answer;
-- 2. they had access to the current cookie and brute-forced the answer.
-- If they already had the cookie I'm not sure why they'd do this spiel anyway, but whatever.
if Base16.encode (H.hash (currentcookie <> sqpAns params)) == sqpHash params
then return (responseBS (Status 303 "See Other")
[("Location", sqpURL params)
,("Set-Cookie", "spamkey=" <> currentcookie <> "; Secure; Max-Age=172800; Path=/")] mempty)
else challengePageResponse (sqpURL params) currentcookie True
where
fromQuery :: [(ByteString, ByteString)] -> SubmitQueryParams
fromQuery = foldl' add (SubmitQueryParams mempty mempty mempty)
where
add sqp ("ans" , x) = sqp { sqpAns = x }
add sqp ("hash", x) = sqp { sqpHash = x }
add sqp ("url" , x) = sqp { sqpURL = unescape x }
add sqp _ = sqp
unescape :: ByteString -> ByteString
unescape = BSL.toStrict . BSB.toLazyByteString . go
where go s = case BS.uncons s of
Nothing -> mempty
Just (c, cs) | c == fromIntegral (ord '%'), BS.length cs >= 2 ->
BSB.word8 (fromHexDig (cs `BS.index` 0) * 16 + fromHexDig (cs `BS.index` 1)) <> go (BS.drop 2 cs)
_ ->
let (pre, post) = BS.span (/= fromIntegral (ord '%')) s
in BSB.byteString pre <> go post
fromHexDig c
| 48 <= c, c < 58 = c - 48
| 97 <= c, c < 97+6 = c - 97 + 10
| 65 <= c, c < 65+6 = c - 65 + 10
| otherwise = 0
genQuestion :: IO (Text, Text)
genQuestion = do
-- 3: question type (2 add, 1 mul)
-- 2: operand flip
-- 10: one operand
-- 10: other operand
rval <- uniformRM (0, 3 * 2 * 10 * 10 - 1 :: Int) globalStdGen
let (rval1, qtype) = rval `quotRem` 3
(rval2, opflip) = rval1 `quotRem` 2
(rval3, op1) = rval2 `quotRem` 10
op2 = rval3
-- multiplications use only 1..5
let ismul = qtype == 2
op1' | ismul = op1 `rem` 5 + 1
| otherwise = op1 + 1
op2' | ismul = op2 `rem` 5 + 1
| otherwise = op2 + 1
let (lhs, rhs) | opflip == 0 = (op1', op2')
| otherwise = (op2', op1')
if qtype < 2
then return (TBL.runBuilder $ TBL.fromText "What is " <> TBL.fromDec lhs <>
TBL.fromText " + " <> TBL.fromDec rhs <> TBL.fromText "?"
,T.pack (show (lhs + rhs)))
else return (TBL.runBuilder $ TBL.fromText "What is " <> TBL.fromDec lhs <>
TBL.fromText " * " <> TBL.fromDec rhs <> TBL.fromText "?"
,T.pack (show (lhs * rhs)))
|