sova-rate-limit 0.1.2

Rate limit plugin for Sova (local sliding + shared fixed window)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! Rate limiting for Sova (Express [`express-rate-limit`](https://www.npmjs.com/package/express-rate-limit)-style).

mod events;

pub use events::RateLimitExceeded;

use sova_core::extend::{named, MwEntry};
use sova_core::{
    with_state, App, ClientAddr, EventBus, Plugin, RateLimitIdentity, Request, Response,
};
use sova_store::KvStore;
use serde_json::Value as JsonValue;
use std::collections::HashMap;
use std::net::IpAddr;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

/// How the rate-limit key is derived (overridden by [`RateLimit::key_fn`]).
#[derive(Clone, Copy, Debug, Default)]
pub enum RateLimitKey {
    /// Client IP ([`ClientAddr`]).
    #[default]
    Ip,
    /// [`RateLimitIdentity`] when set, otherwise IP.
    Identity,
    /// `ip` + lowercased form/json field (empty field → IP only).
    IpAndInput {
        field: &'static str,
    },
}

/// Result of a rate-limit check.
struct Outcome {
    allowed: bool,
    limit: u64,
    remaining: u64,
    /// Unix timestamp (seconds) when the window resets.
    reset: u64,
}

type KeyFn = Arc<dyn Fn(&Request) -> String + Send + Sync>;
type SkipFn = Arc<dyn Fn(&Request) -> bool + Send + Sync>;

/// Rate limiter plugin / route middleware.
pub struct RateLimit {
    max: usize,
    window: Duration,
    max_entries: usize,
    message: String,
    key: RateLimitKey,
    /// Optional prefix so presets (login/forgot) do not share buckets.
    prefix: Option<&'static str>,
    key_fn: Option<KeyFn>,
    skip: Option<SkipFn>,
    backend: Backend,
}

enum Backend {
    LocalSliding,
    SharedFixed(Arc<dyn KvStore>),
}

impl RateLimit {
    /// Local process sliding window (default).
    pub fn new(max: usize, window: Duration) -> Self {
        Self {
            max,
            window,
            max_entries: 10_000,
            message: "Too Many Requests".into(),
            key: RateLimitKey::Ip,
            prefix: None,
            key_fn: None,
            skip: None,
            backend: Backend::LocalSliding,
        }
    }

    pub fn per_minute(max: usize) -> Self {
        Self::new(max, Duration::from_secs(60))
    }

    /// Cap distinct client keys retained for local sliding (default 10_000).
    pub fn max_entries(mut self, n: usize) -> Self {
        self.max_entries = n.max(1);
        self
    }

    /// 429 response body (default `"Too Many Requests"`).
    pub fn message(mut self, msg: impl Into<String>) -> Self {
        self.message = msg.into();
        self
    }

    /// Built-in key strategy (default [`RateLimitKey::Ip`]).
    pub fn key(mut self, key: RateLimitKey) -> Self {
        self.key = key;
        self
    }

    /// Prefix prepended to the resolved key (`login:…`, `forgot:…`).
    pub fn prefix(mut self, prefix: &'static str) -> Self {
        self.prefix = Some(prefix);
        self
    }

    /// Custom rate-limit key (overrides [`Self::key`]).
    pub fn key_fn<F>(mut self, f: F) -> Self
    where
        F: Fn(&Request) -> String + Send + Sync + 'static,
    {
        self.key_fn = Some(Arc::new(f));
        self
    }

    /// Skip limiting when this returns true.
    pub fn skip<F>(mut self, f: F) -> Self
    where
        F: Fn(&Request) -> bool + Send + Sync + 'static,
    {
        self.skip = Some(Arc::new(f));
        self
    }

    /// Multi-process / shared store: fixed window via atomic `incr`.
    pub fn fixed_window(store: Arc<dyn KvStore>, max: usize, window: Duration) -> Self {
        Self {
            max,
            window,
            max_entries: 10_000,
            message: "Too Many Requests".into(),
            key: RateLimitKey::Ip,
            prefix: None,
            key_fn: None,
            skip: None,
            backend: Backend::SharedFixed(store),
        }
    }

    /// Login POST throttle: 5 / 60s, key = IP + email.
    pub fn login() -> Self {
        Self::per_minute(5)
            .prefix("login")
            .key(RateLimitKey::IpAndInput { field: "email" })
    }

    /// Forgot-password throttle: 5 / 60s, IP + email.
    pub fn forgot() -> Self {
        Self::per_minute(5)
            .prefix("forgot")
            .key(RateLimitKey::IpAndInput { field: "email" })
    }

    /// 2FA challenge throttle: 5 / 60s, IP.
    pub fn challenge() -> Self {
        Self::per_minute(5).prefix("2fa").key(RateLimitKey::Ip)
    }

    /// Email verification resend: 6 / 60s, IP + email.
    pub fn resend() -> Self {
        Self::new(6, Duration::from_secs(60))
            .prefix("verify-resend")
            .key(RateLimitKey::IpAndInput { field: "email" })
    }

    /// Route / mount middleware (same check as the plugin).
    pub fn middleware(self) -> MwEntry {
        let runtime = self.into_runtime();
        named(
            runtime.mw_name(),
            with_state(runtime, |rt, mut req, next| async move {
                if rt.skip.as_ref().is_some_and(|f| f(&req)) {
                    return next(req).await;
                }
                let key =
                    resolve_key(&mut req, rt.key_fn.as_ref(), rt.key, rt.prefix).await;
                let out = match &rt.backend {
                    RuntimeBackend::Sliding(w) => w.check(&key),
                    RuntimeBackend::Fixed(w) => w.check(&key).await,
                };
                if !out.allowed {
                    emit_exceeded(&req, &key, &out);
                    return limited(rt.message.as_str(), &out);
                }
                let mut res = next(req).await;
                attach_headers(&mut res, &out);
                res
            }),
        )
    }

    fn into_runtime(self) -> RateLimitRuntime {
        let backend = match self.backend {
            Backend::LocalSliding => RuntimeBackend::Sliding(Arc::new(SlidingWindow::new(
                self.max,
                self.window,
                self.max_entries,
            ))),
            Backend::SharedFixed(store) => RuntimeBackend::Fixed(Arc::new(FixedWindow {
                store,
                max: self.max as u64,
                window: self.window,
            })),
        };
        RateLimitRuntime {
            message: self.message,
            key: self.key,
            prefix: self.prefix,
            key_fn: self.key_fn,
            skip: self.skip,
            backend,
            name: self
                .prefix
                .map(|p| format!("rate-limit:{p}"))
                .unwrap_or_else(|| "rate-limit".into()),
        }
    }
}

struct RateLimitRuntime {
    message: String,
    key: RateLimitKey,
    prefix: Option<&'static str>,
    key_fn: Option<KeyFn>,
    skip: Option<SkipFn>,
    backend: RuntimeBackend,
    name: String,
}

impl RateLimitRuntime {
    fn mw_name(&self) -> String {
        self.name.clone()
    }
}

enum RuntimeBackend {
    Sliding(Arc<SlidingWindow>),
    Fixed(Arc<FixedWindow>),
}

impl Plugin for RateLimit {
    fn id(&self) -> &'static str {
        "rate-limit"
    }

    fn meta(&self) -> sova_core::PluginMeta {
        sova_core::PluginMeta::new("Rate limit")
            .description("Per-key request rate limiting")
            .version(env!("CARGO_PKG_VERSION"))
    }

    fn install(self, app: &mut App) {
        let runtime = self.into_runtime();
        app.use_middleware(named(
            runtime.mw_name(),
            with_state(runtime, |rt, mut req, next| async move {
                if rt.skip.as_ref().is_some_and(|f| f(&req)) {
                    return next(req).await;
                }
                let key =
                    resolve_key(&mut req, rt.key_fn.as_ref(), rt.key, rt.prefix).await;
                let out = match &rt.backend {
                    RuntimeBackend::Sliding(w) => w.check(&key),
                    RuntimeBackend::Fixed(w) => w.check(&key).await,
                };
                if !out.allowed {
                    emit_exceeded(&req, &key, &out);
                    return limited(rt.message.as_str(), &out);
                }
                let mut res = next(req).await;
                attach_headers(&mut res, &out);
                res
            }),
        ));
    }
}

fn emit_exceeded(req: &Request, key: &str, out: &Outcome) {
    if let Some(bus) = req.try_state::<EventBus>() {
        let now = unix_now();
        bus.dispatch(RateLimitExceeded {
            key: key.to_string(),
            limit: out.limit,
            retry_after: Some(out.reset.saturating_sub(now)),
        });
    }
}

async fn resolve_key(
    req: &mut Request,
    key_fn: Option<&KeyFn>,
    strategy: RateLimitKey,
    prefix: Option<&'static str>,
) -> String {
    let raw = if let Some(f) = key_fn {
        f(req)
    } else {
        match strategy {
            RateLimitKey::Ip => client_ip(req).to_string(),
            RateLimitKey::Identity => {
                if let Some(id) = req.get::<RateLimitIdentity>() {
                    format!("id:{}", id.0)
                } else {
                    client_ip(req).to_string()
                }
            }
            RateLimitKey::IpAndInput { field } => {
                let ip = client_ip(req);
                match peek_input_field(req, field).await {
                    Some(v) if !v.is_empty() => format!("{ip}:{}", v.to_ascii_lowercase()),
                    _ => ip.to_string(),
                }
            }
        }
    };
    match prefix {
        Some(p) => format!("{p}:{raw}"),
        None => raw,
    }
}

async fn peek_input_field(req: &mut Request, field: &str) -> Option<String> {
    let ct = req
        .header("content-type")
        .unwrap_or("")
        .to_ascii_lowercase();
    if ct.contains("application/json") {
        let Ok(v) = req.json::<JsonValue>().await else {
            return None;
        };
        return v
            .get(field)
            .and_then(|x| x.as_str())
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty());
    }
    // form / multipart / urlencoded — cached on request via input()
    if let Ok(data) = req.input().await {
        return data
            .get(field)
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty());
    }
    None
}

fn client_ip(req: &Request) -> IpAddr {
    req.get::<ClientAddr>()
        .map(|a| a.0.ip())
        .unwrap_or_else(|| IpAddr::from([127, 0, 0, 1]))
}

fn limited(message: &str, out: &Outcome) -> Response {
    let mut res = Response::text(message).status(429);
    attach_headers(&mut res, out);
    res
}

fn attach_headers(res: &mut Response, out: &Outcome) {
    if let Ok(v) = out.limit.to_string().parse() {
        res.headers_mut().insert("ratelimit-limit", v);
    }
    if let Ok(v) = out.remaining.to_string().parse() {
        res.headers_mut().insert("ratelimit-remaining", v);
    }
    if let Ok(v) = out.reset.to_string().parse() {
        res.headers_mut().insert("ratelimit-reset", v);
    }
}

fn unix_now() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

struct SlidingWindow {
    inner: Arc<Mutex<HashMap<String, Vec<Instant>>>>,
    max: usize,
    window: Duration,
    max_entries: usize,
}

impl SlidingWindow {
    fn new(max: usize, window: Duration, max_entries: usize) -> Self {
        Self {
            inner: Arc::new(Mutex::new(HashMap::new())),
            max,
            window,
            max_entries,
        }
    }

    fn check(&self, key: &str) -> Outcome {
        let mut map = self.inner.lock().unwrap();
        let now = Instant::now();
        let reset = unix_now() + self.window.as_secs().max(1);

        if let Some(entries) = map.get_mut(key) {
            entries.retain(|t| now.duration_since(*t) < self.window);
            if entries.is_empty() {
                map.remove(key);
            }
        }

        if map.len() >= self.max_entries && !map.contains_key(key) {
            map.retain(|_, v| {
                v.retain(|t| now.duration_since(*t) < self.window);
                !v.is_empty()
            });
            while map.len() >= self.max_entries {
                if let Some(k) = map.keys().next().cloned() {
                    map.remove(&k);
                } else {
                    break;
                }
            }
        }

        let entries = map.entry(key.to_string()).or_default();
        entries.retain(|t| now.duration_since(*t) < self.window);
        let limit = self.max as u64;
        if entries.len() >= self.max {
            return Outcome {
                allowed: false,
                limit,
                remaining: 0,
                reset,
            };
        }
        entries.push(now);
        let remaining = limit.saturating_sub(entries.len() as u64);
        Outcome {
            allowed: true,
            limit,
            remaining,
            reset,
        }
    }

    #[cfg(test)]
    fn len(&self) -> usize {
        self.inner.lock().unwrap().len()
    }

    #[cfg(test)]
    fn allow(&self, key: &str) -> bool {
        self.check(key).allowed
    }
}

struct FixedWindow {
    store: Arc<dyn KvStore>,
    max: u64,
    window: Duration,
}

impl FixedWindow {
    async fn check(&self, key: &str) -> Outcome {
        let secs = self.window.as_secs().max(1);
        let now = unix_now();
        let bucket = now / secs;
        let reset = (bucket + 1) * secs;
        let store_key = format!("rl:{key}:{bucket}");
        let n = self.store.incr(&store_key, 1, Some(self.window)).await;
        let limit = self.max;
        if n > self.max {
            Outcome {
                allowed: false,
                limit,
                remaining: 0,
                reset,
            }
        } else {
            Outcome {
                allowed: true,
                limit,
                remaining: limit.saturating_sub(n),
                reset,
            }
        }
    }

    #[cfg(test)]
    async fn allow(&self, key: &str) -> bool {
        self.check(key).await.allowed
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use sova_store::MemoryStore;

    #[test]
    fn max_entries_caps_distinct_keys() {
        let w = SlidingWindow::new(100, Duration::from_secs(60), 3);
        assert!(w.allow("1"));
        assert!(w.allow("2"));
        assert!(w.allow("3"));
        assert_eq!(w.len(), 3);
        assert!(w.allow("4"));
        assert!(w.len() <= 3);
    }

    #[test]
    fn rate_blocks_after_max() {
        let w = SlidingWindow::new(2, Duration::from_secs(60), 10);
        assert!(w.allow("ip"));
        assert!(w.allow("ip"));
        assert!(!w.allow("ip"));
    }

    #[tokio::test]
    async fn fixed_window_incr() {
        let store = Arc::new(MemoryStore::new());
        let fw = FixedWindow {
            store,
            max: 2,
            window: Duration::from_secs(60),
        };
        assert!(fw.allow("ip").await);
        assert!(fw.allow("ip").await);
        assert!(!fw.allow("ip").await);
    }
}