summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2026-11-06 00:50:10 +0100
committerTom Smeding <tom@tomsmeding.com>2026-11-06 00:50:10 +0100
commite3ed5f39e8a83d070466964c8ac37246a260c778 (patch)
tree3dbe0acd224f0214bcda1e1ab5af2d928c340efd /src
parent45f94d99bd855511bca9fd942a641942298642a5 (diff)
Spamcheck: an attempt at a simple no-JS captcha
Diffstat (limited to 'src')
-rw-r--r--src/Main.hs53
-rw-r--r--src/Pages.hs9
-rw-r--r--src/Spamcheck.hs189
3 files changed, 229 insertions, 22 deletions
diff --git a/src/Main.hs b/src/Main.hs
index e80391a..c5417aa 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -3,7 +3,7 @@
{-# LANGUAGE ViewPatterns #-}
module Main (main) where
-import Control.Applicative ((<|>))
+import Control.Applicative ((<|>), asum)
import Control.Monad (guard, forM_, forM)
import Data.ByteString (ByteString)
import Data.ByteString qualified as BS
@@ -13,6 +13,7 @@ import Data.Char (isDigit, ord)
import Data.Function (on)
import Data.List.NonEmpty (NonEmpty((:|)), groupBy)
import Data.Map.Strict qualified as Map
+import Data.Maybe (fromJust)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Encoding qualified as TE
@@ -30,6 +31,7 @@ import Calendar
import Config
import Index
import Pages
+import Spamcheck
import Util
import ZNC.Parser
@@ -325,6 +327,7 @@ mainServe confpath = do
config <- enrichConfig <$> readConfig confpath
index <- initIndex (confLogsDir config) (econfChannels config)
+ spamcheck <- newSpamcheck
let staticFiles =
[("style.css", "text/css")
@@ -335,26 +338,34 @@ mainServe confpath = do
let settings = defaultSettings { setPort = confPort config }
atomicPrintS $ "Listening on port " ++ show (confPort config)
- run settings $ \req ->
- case prefilter req of
- Just res -> return res
- Nothing -> case reqPath req of
- [] ->
- pageIndex config
- ["log", TE.decodeUtf8' -> Right alias] ->
- pageLog config index req alias
- ["plainlog", TE.decodeUtf8' -> Right alias] ->
- pagePlainLog config index alias
- ["plainlog", TE.decodeUtf8' -> Right alias, TE.decodeUtf8' -> Right date] ->
- pagePlainLogDay config index alias date
- ["cal", TE.decodeUtf8' -> Right alias] ->
- pageCalendar config index alias
- ["cal", TE.decodeUtf8' -> Right alias, TE.decodeUtf8' -> Right date] ->
- pageCalendarDay config index req alias date
- [fname] | Just mime <- lookup fname staticFiles ->
- return $ responseFile status200 [("Content-Type", mime)] ("pages/" ++ BS8.unpack fname)
- _ ->
- sendText404 "URL not found"
+ run settings $ \req -> do
+ let handleFree = \case
+ [] ->
+ Just $ pageIndex config
+ ["plainlog", TE.decodeUtf8' -> Right alias] ->
+ Just $ pagePlainLog config index alias
+ ["plainlog", TE.decodeUtf8' -> Right alias, TE.decodeUtf8' -> Right date] ->
+ Just $ pagePlainLogDay config index alias date
+ [fname] | Just mime <- lookup fname staticFiles ->
+ Just $ return $ responseFile status200 [("Content-Type", mime)] ("pages/" ++ BS8.unpack fname)
+ ["spamcheck", "submit"] ->
+ Just $ handleSpamSubmit spamcheck (reqQuery req)
+ _ -> Nothing
+
+ let handleProtected = \case
+ ["log", TE.decodeUtf8' -> Right alias] ->
+ Just $ pageLog config index req alias
+ ["cal", TE.decodeUtf8' -> Right alias] ->
+ Just $ pageCalendar config index alias
+ ["cal", TE.decodeUtf8' -> Right alias, TE.decodeUtf8' -> Right date] ->
+ Just $ pageCalendarDay config index req alias date
+ _ -> Nothing
+
+ fromJust $ asum
+ [return <$> prefilter req
+ ,handleFree (reqPath req)
+ ,wrapSpamcheck spamcheck req <$> handleProtected (reqPath req)
+ ,Just (sendText404 "URL not found")]
testParseLog :: FilePath -> IO ()
testParseLog fname = print . parseLog =<< BS.readFile fname
diff --git a/src/Pages.hs b/src/Pages.hs
index dbbf440..965cf2e 100644
--- a/src/Pages.hs
+++ b/src/Pages.hs
@@ -119,6 +119,12 @@ data EventData tm dttm = EventData
, message :: Text
}
+data SpamcheckData = SpamcheckData
+ { question :: Text
+ , hash :: Text
+ , prevurl :: Text
+ , incorrect :: Bool }
+
$(do let dropSuffix suf str =
let (actualSuf, rstr) = splitAt (length suf) (reverse str)
in if actualSuf == reverse suf then reverse rstr else str
@@ -158,4 +164,5 @@ $(do let dropSuffix suf str =
,("plainlog", ''PlainLogData)
,("calendar-day", ''CalendarDayData)
,("index", ''IndexData)
- ,("calendar", ''CalendarData)])
+ ,("calendar", ''CalendarData)
+ ,("spamcheck", ''SpamcheckData)])
diff --git a/src/Spamcheck.hs b/src/Spamcheck.hs
new file mode 100644
index 0000000..3191794
--- /dev/null
+++ b/src/Spamcheck.hs
@@ -0,0 +1,189 @@
+{-# 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.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 1..8 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 threshold 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'])
+
+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
+ if clientcookie == Just correctcookie then handler else do
+ 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)))