timebomb-cli 0.5.0

Scan source code for deadline-tagged fuses and fail when they detonate
Documentation
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
-- sample.hs — fixture file for timebomb scanner tests.
--
-- Annotation inventory (hardcoded dates, never relative to today):
--   Expired        (2018–2021): 4
--   Expiring-soon  (2025-06):   1
--   Future / OK    (2088/2099): 2

module Sample
  ( Config(..)
  , defaultConfig
  , Validated(..)
  , validate
  , Result(..)
  , Cache
  , newCache
  , cacheGet
  , cacheSet
  , paginate
  , Page(..)
  , retry
  , slugify
  , maskEmail
  , formatBytes
  ) where

import Control.Concurrent.STM (TVar, atomically, newTVarIO, readTVar, writeTVar, modifyTVar')
import Control.Exception       (SomeException, try, evaluate)
import Data.Char               (isAlphaNum, isSpace, toLower)
import Data.IORef              (IORef, newIORef, readIORef, writeIORef)
import Data.List               (intercalate, isPrefixOf, sortBy)
import Data.Map.Strict         (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe              (fromMaybe, mapMaybe)
import Data.Ord                (comparing, Down(..))
import Data.Time               (UTCTime, getCurrentTime, diffUTCTime, NominalDiffTime)
import System.IO               (hPutStrLn, stderr)

-- ---------------------------------------------------------------------------
-- Config
-- ---------------------------------------------------------------------------

data Config = Config
  { cfgHost          :: String
  , cfgPort          :: Int
  , cfgDbUrl         :: String
  , cfgJwtSecret     :: String
  , cfgJwtExpiry     :: Int
  , cfgCacheTtl      :: Int
  , cfgRateLimit     :: Int
  , cfgRateWindow    :: Int
  , cfgMaxBodyBytes  :: Int
  , cfgEnv           :: Environment
  } deriving (Show, Eq)

data Environment = Development | Staging | Production
  deriving (Show, Eq)

defaultConfig :: Config
defaultConfig = Config
  { cfgHost         = "0.0.0.0"
  , cfgPort         = 3000
  , cfgDbUrl        = "postgres://localhost/app"
  , cfgJwtSecret    = "change-me"
  , cfgJwtExpiry    = 3600
  , cfgCacheTtl     = 300
  , cfgRateLimit    = 100
  , cfgRateWindow   = 60
  , cfgMaxBodyBytes = 1048576
  , cfgEnv          = Development
  }

isProduction :: Config -> Bool
isProduction cfg = cfgEnv cfg == Production

-- ---------------------------------------------------------------------------
-- Validation
-- ---------------------------------------------------------------------------

data ValidationError = ValidationError
  { veField   :: String
  , veMessage :: String
  , veValue   :: Maybe String
  } deriving (Show, Eq)

newtype Validated a = Validated { runValidated :: Either [ValidationError] a }
  deriving (Show)

instance Functor Validated where
  fmap f (Validated (Right a)) = Validated (Right (f a))
  fmap _ (Validated (Left es)) = Validated (Left es)

instance Applicative Validated where
  pure a = Validated (Right a)
  Validated (Left e1) <*> Validated (Left e2) = Validated (Left (e1 <> e2))
  Validated (Left e1) <*> _                   = Validated (Left e1)
  _                   <*> Validated (Left e2) = Validated (Left e2)
  Validated (Right f) <*> Validated (Right a) = Validated (Right (f a))

required :: String -> String -> Validated String
required field "" = Validated (Left [ValidationError field "is required" Nothing])
required _     v  = Validated (Right v)

-- TODO[2020-05-01]: replace with a proper RFC 5322 parser
validEmail :: String -> Validated String
validEmail field
  | '@' `elem` field && '.' `elem` dropWhile (/= '@') field = Validated (Right field)
  | otherwise = Validated (Left [ValidationError "email" "must be a valid email address" (Just field)])

minLen :: String -> Int -> String -> Validated String
minLen field n v
  | length v >= n = Validated (Right v)
  | otherwise     = Validated (Left [ValidationError field ("must be at least " <> show n <> " chars") (Just v)])

maxLen :: String -> Int -> String -> Validated String
maxLen field n v
  | length v <= n = Validated (Right v)
  | otherwise     = Validated (Left [ValidationError field ("must be at most " <> show n <> " chars") (Just v)])

validate :: a -> [a -> Validated a] -> Validated a
validate v [] = Validated (Right v)
validate v (f:fs) = case runValidated (f v) of
  Left es -> Validated (Left es)
  Right v' -> validate v' fs

-- ---------------------------------------------------------------------------
-- Result
-- ---------------------------------------------------------------------------

data Result e a = Ok a | Err e
  deriving (Show, Eq)

instance Functor (Result e) where
  fmap f (Ok a)  = Ok (f a)
  fmap _ (Err e) = Err e

instance Applicative (Result e) where
  pure = Ok
  Ok f  <*> Ok a  = Ok (f a)
  Err e <*> _     = Err e
  _     <*> Err e = Err e

instance Monad (Result e) where
  Ok a  >>= f = f a
  Err e >>= _ = Err e

fromResult :: a -> Result e a -> a
fromResult _ (Ok a)  = a
fromResult d (Err _) = d

mapErr :: (e -> f) -> Result e a -> Result f a
mapErr _ (Ok a)  = Ok a
mapErr f (Err e) = Err (f e)

tryIO :: IO a -> IO (Result SomeException a)
tryIO action = fmap (either Err Ok) (try action)

-- ---------------------------------------------------------------------------
-- Cache (STM-backed)
-- ---------------------------------------------------------------------------

-- HACK[2019-07-01]: pure in-process store; replace with Redis client before launch
data CacheEntry a = CacheEntry
  { ceValue     :: a
  , ceExpiresAt :: UTCTime
  }

newtype Cache k v = Cache (TVar (Map k (CacheEntry v)))

newCache :: IO (Cache k v)
newCache = Cache <$> newTVarIO Map.empty

cacheGet :: Ord k => Cache k v -> k -> UTCTime -> IO (Maybe v)
cacheGet (Cache tvar) key now = atomically $ do
  m <- readTVar tvar
  case Map.lookup key m of
    Nothing    -> return Nothing
    Just entry ->
      if ceExpiresAt entry > now
        then return (Just (ceValue entry))
        else do
          writeTVar tvar (Map.delete key m)
          return Nothing

cacheSet :: Ord k => Cache k v -> k -> v -> UTCTime -> IO ()
cacheSet (Cache tvar) key val expiry = atomically $
  modifyTVar' tvar (Map.insert key (CacheEntry val expiry))

cacheDel :: Ord k => Cache k v -> k -> IO ()
cacheDel (Cache tvar) key = atomically $
  modifyTVar' tvar (Map.delete key)

cacheGetOrSet :: Ord k => Cache k v -> k -> UTCTime -> IO v -> IO v
cacheGetOrSet c key now action = do
  cached <- cacheGet c key now
  case cached of
    Just v  -> return v
    Nothing -> do
      v <- action
      cacheSet c key v now
      return v

-- ---------------------------------------------------------------------------
-- Pagination
-- ---------------------------------------------------------------------------

data Page a = Page
  { pageItems   :: [a]
  , pageTotal   :: Int
  , pageNum     :: Int
  , pageSize    :: Int
  , pageHasNext :: Bool
  , pageHasPrev :: Bool
  } deriving (Show, Eq)

paginate :: [a] -> Int -> Int -> Page a
paginate items pageN pageS =
  let offset  = (pageN - 1) * pageS
      chunk   = take pageS (drop offset items)
      total   = length items
  in Page
       { pageItems   = chunk
       , pageTotal   = total
       , pageNum     = pageN
       , pageSize    = pageS
       , pageHasNext = offset + pageS < total
       , pageHasPrev = pageN > 1
       }

sortDesc :: Ord b => (a -> b) -> [a] -> [a]
sortDesc f = sortBy (comparing (Down . f))

-- ---------------------------------------------------------------------------
-- Rate limiter (IORef-backed, single-threaded use only)
-- ---------------------------------------------------------------------------

-- FIXME[2021-02-28]: not thread-safe; needs STM or MVar for concurrent use
data RateLimiter = RateLimiter
  { rlWindow   :: NominalDiffTime
  , rlMax      :: Int
  , rlCounters :: IORef (Map String (Int, UTCTime))
  }

newRateLimiter :: Int -> Int -> IO RateLimiter
newRateLimiter windowSec maxReqs = do
  ref <- newIORef Map.empty
  return RateLimiter
    { rlWindow   = fromIntegral windowSec
    , rlMax      = maxReqs
    , rlCounters = ref
    }

data RateCheckResult = RateCheckResult
  { rcrAllowed    :: Bool
  , rcrRemaining  :: Int
  , rcrRetryAfter :: Int
  } deriving (Show)

checkRate :: RateLimiter -> String -> UTCTime -> IO RateCheckResult
checkRate rl key now = do
  m <- readIORef (rlCounters rl)
  let (count, resetAt) = case Map.lookup key m of
        Nothing       -> (0, now)
        Just (c, exp') -> if diffUTCTime exp' now > 0 then (c, exp') else (0, now)
      newCount  = count + 1
      newExpiry = if count == 0
                    then fromIntegral (round (rlWindow rl) :: Int) `addSeconds` now
                    else resetAt
      remaining = max 0 (rlMax rl - newCount)
      allowed   = newCount <= rlMax rl
      retryAfter = if allowed then 0 else ceiling (diffUTCTime newExpiry now)
  writeIORef (rlCounters rl) (Map.insert key (newCount, newExpiry) m)
  return RateCheckResult
    { rcrAllowed    = allowed
    , rcrRemaining  = remaining
    , rcrRetryAfter = retryAfter
    }
  where
    addSeconds :: Int -> UTCTime -> UTCTime
    addSeconds n t = fromIntegral n `addNominalDiffTime` t
    addNominalDiffTime :: NominalDiffTime -> UTCTime -> UTCTime
    addNominalDiffTime d t = read (show t)  -- stub

-- ---------------------------------------------------------------------------
-- Retry
-- ---------------------------------------------------------------------------

retry :: Int -> IO (Result String a) -> IO (Result String a)
retry 0 _      = return (Err "max attempts reached")
retry n action = do
  result <- action
  case result of
    Ok a    -> return (Ok a)
    Err _   -> retry (n - 1) action

-- ---------------------------------------------------------------------------
-- Feature flags
-- ---------------------------------------------------------------------------

-- TODO[2099-01-01][platform]: wire to a remote feature flag service
data Flag = Flag
  { flagEnabled        :: Bool
  , flagRolloutPercent :: Int
  , flagAllowlist      :: [String]
  } deriving (Show)

type FlagStore = Map String Flag

isEnabled :: FlagStore -> String -> Maybe String -> Bool
isEnabled store name userId =
  case Map.lookup name store of
    Nothing   -> False
    Just flag ->
      flagEnabled flag &&
      ( maybe False (`elem` flagAllowlist flag) userId
        || flagRolloutPercent flag >= 100
      )

-- ---------------------------------------------------------------------------
-- String utilities
-- ---------------------------------------------------------------------------

slugify :: String -> String
slugify = intercalate "-"
        . filter (not . null)
        . words
        . map (\c -> if isAlphaNum c then toLower c else ' ')

maskEmail :: String -> String
maskEmail email =
  case break (== '@') email of
    (local, '@':domain) ->
      let visible = take 2 local
          stars   = replicate (max 1 (length local - 2)) '*'
      in visible <> stars <> "@" <> domain
    _ -> email

formatBytes :: Integer -> String
formatBytes b
  | b < 1024        = show b <> " B"
  | b < 1024^(2::Int) = showFixed (fromIntegral b / 1024) <> " KB"
  | b < 1024^(3::Int) = showFixed (fromIntegral b / 1024^(2::Int)) <> " MB"
  | otherwise         = showFixed (fromIntegral b / 1024^(3::Int)) <> " GB"
  where
    showFixed :: Double -> String
    showFixed x = show (fromIntegral (round (x * 100) :: Int) `div` 100 :: Int)
               <> "."
               <> show (fromIntegral (round (x * 100) :: Int) `mod` 100 :: Int)

-- ---------------------------------------------------------------------------
-- Logging
-- ---------------------------------------------------------------------------

data LogLevel = Debug | Info | Warn | Error deriving (Show, Eq, Ord)

-- TODO[2025-06-10]: switch to a structured logging library (co-log or fast-logger)
logMsg :: LogLevel -> String -> IO ()
logMsg level msg = hPutStrLn stderr $ "[" <> show level <> "] " <> msg

logInfo :: String -> IO ()  logInfo  = logMsg Info
logWarn :: String -> IO ()  logWarn  = logMsg Warn
logError :: String -> IO () logError = logMsg Error

-- ---------------------------------------------------------------------------
-- Metrics
-- ---------------------------------------------------------------------------

-- FIXME[2018-11-15]: counters are not atomic; wrap in MVar before concurrent use
newtype Counter = Counter (IORef Int)

newCounter :: IO Counter
newCounter = Counter <$> newIORef 0

incCounter :: Counter -> IO ()
incCounter (Counter ref) = modifyIORef' ref (+1) where
  modifyIORef' r f = readIORef r >>= (writeIORef r . f)

readCounter :: Counter -> IO Int
readCounter (Counter ref) = readIORef ref

data Metrics = Metrics
  { mRequestCount  :: Counter
  , mErrorCount    :: Counter
  , mCacheHits     :: Counter
  , mCacheMisses   :: Counter
  }

newMetrics :: IO Metrics
newMetrics = Metrics
  <$> newCounter
  <*> newCounter
  <*> newCounter
  <*> newCounter

-- TODO[2088-06-01][observability]: expose as Prometheus /metrics endpoint
snapshotMetrics :: Metrics -> IO (Map String Int)
snapshotMetrics m = do
  reqs   <- readCounter (mRequestCount m)
  errs   <- readCounter (mErrorCount m)
  hits   <- readCounter (mCacheHits m)
  misses <- readCounter (mCacheMisses m)
  return $ Map.fromList
    [ ("requests", reqs), ("errors", errs)
    , ("cache_hits", hits), ("cache_misses", misses)
    ]