rullst-security 12.3.0

Runtime application security, RASP, OWASP headers, Login Jail, and WAF middleware for Rullst.
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
//! Anti-Bruteforce Tarpit & Login Jail Security Engine.
//! Provides progressive async delay (tarpit) and temporary in-memory jail bans for repeated auth failures.
//!
//! Both the failure counters and the jails are bounded by
//! [`LoginGuard::max_identities`]. When the failure index is full, a new
//! identity evicts the counter with the oldest last attempt instead of being
//! ignored; when the jail index is full, a new offender evicts the jail that
//! expires soonest. A flood of unrelated identities can therefore shorten the
//! memory of old failures, but it cannot stop a newly observed identity from
//! being counted and jailed. Expired counters and jails are pruned on every
//! operation in time order.

mod state;

#[cfg(test)]
mod capacity_tests;

use crate::telemetry::{LiveSecurityEvent, SecurityStore};
use sha2::{Digest, Sha256};
use state::{IdentityKey, TimeOrderedIndex};
use std::sync::{Mutex, OnceLock};
use std::time::{Duration, Instant};

static GLOBAL_LOGIN_GUARD: OnceLock<LoginGuard> = OnceLock::new();

/// Delay returned for jailed identities and for fail-closed states.
const JAILED_DELAY: Duration = Duration::from_secs(5);

/// Anti-Bruteforce Tarpit and Login Jail Engine.
pub struct LoginGuard {
    /// Failure counters ordered by last attempt and active jails ordered by expiry.
    state: Mutex<LoginState>,
    /// Max failures allowed before triggering temporary jail (default: 5).
    pub max_failures: u32,
    /// Duration of the temporary jail ban (default: 15 minutes).
    pub jail_duration: Duration,
    /// Reset window for consecutive failures (default: 10 minutes).
    pub window_duration: Duration,
    /// Maximum identities retained in either in-memory map.
    ///
    /// At capacity the least recently failed counter, or the jail that expires
    /// soonest, is evicted so that a new identity is still tracked. A value of
    /// zero stores nothing and returns the jailed delay for every failure.
    pub max_identities: usize,
}

struct LoginState {
    /// Consecutive failure counts, ordered by last attempt.
    failures: TimeOrderedIndex<u32>,
    /// Active temporary bans, ordered by expiration.
    jails: TimeOrderedIndex<()>,
}

impl LoginState {
    fn prune_expired(&mut self, now: Instant, window: Duration) {
        self.failures
            .prune_while(|last_attempt| now.saturating_duration_since(last_attempt) >= window);
        self.jails.prune_while(|expires_at| expires_at <= now);
    }
}

impl Default for LoginGuard {
    fn default() -> Self {
        Self {
            state: Mutex::new(LoginState {
                failures: TimeOrderedIndex::new(),
                jails: TimeOrderedIndex::new(),
            }),
            max_failures: 5,
            jail_duration: Duration::from_secs(900), // 15 minutes
            window_duration: Duration::from_secs(600), // 10 minutes
            max_identities: 100_000,
        }
    }
}

impl LoginGuard {
    /// Creates a new LoginGuard instance.
    pub fn new() -> Self {
        Self::default()
    }

    /// Accesses the global static LoginGuard instance.
    pub fn global() -> &'static LoginGuard {
        GLOBAL_LOGIN_GUARD.get_or_init(LoginGuard::new)
    }

    /// Checks if a client IP or user identity is currently jailed.
    pub fn is_jailed(&self, identity: &str) -> bool {
        let Ok(mut state) = self.state.lock() else {
            return true;
        };
        state.prune_expired(Instant::now(), self.window_duration);
        state.jails.contains(&identity_key(identity))
    }

    /// Returns the remaining jail duration for an identity, if jailed.
    pub fn remaining_jail_time(&self, identity: &str) -> Option<Duration> {
        let Ok(mut state) = self.state.lock() else {
            return Some(JAILED_DELAY);
        };
        let now = Instant::now();
        state.prune_expired(now, self.window_duration);
        state
            .jails
            .get(&identity_key(identity))
            .map(|(expires_at, _)| expires_at.saturating_duration_since(now))
    }

    /// Records a failed authentication attempt. Returns the progressive tarpit delay duration.
    ///
    /// A new identity is always counted: at capacity the counter with the
    /// oldest last attempt is evicted. Reaching [`Self::max_failures`] always
    /// creates a jail, evicting the jail that expires soonest when the jail
    /// index is full, and only then clears the identity's failure counter.
    pub fn record_login_failure(&self, identity: &str) -> Duration {
        let Ok(mut state) = self.state.lock() else {
            return JAILED_DELAY;
        };
        if self.max_identities == 0 {
            return JAILED_DELAY;
        }
        let now = Instant::now();
        state.prune_expired(now, self.window_duration);
        let identity_key = identity_key(identity);

        // Check if already jailed
        if state.jails.contains(&identity_key) {
            return JAILED_DELAY;
        }

        let current_count = match state.failures.get(&identity_key) {
            Some((last_attempt, count))
                if now.saturating_duration_since(last_attempt) <= self.window_duration =>
            {
                count.saturating_add(1)
            }
            // Missing or beyond the window: restart the sequence.
            _ => 1,
        };

        if current_count >= self.max_failures {
            let expires_at = saturating_deadline(now, self.jail_duration);
            if expires_at > now {
                state.jails.make_room(self.max_identities);
                state.jails.upsert(identity_key, expires_at, ());
                state.failures.remove(&identity_key);
                drop(state);
                record_jail_telemetry(identity, self.jail_duration, current_count);
                return JAILED_DELAY;
            }
            // A zero-length jail cannot be created; keep counting instead.
            track_failure(
                &mut state,
                identity_key,
                now,
                current_count,
                self.max_identities,
            );
            return JAILED_DELAY;
        }

        track_failure(
            &mut state,
            identity_key,
            now,
            current_count,
            self.max_identities,
        );
        // Progressive tarpit delay: 1st=0s, 2nd=1s, 3rd=2s, 4th=4s
        Duration::from_secs(progressive_delay_seconds(current_count))
    }

    /// Records a failed login and applies the returned progressive delay.
    ///
    /// Prefer this method in authentication handlers so the tarpit cannot be
    /// accidentally reduced to a duration that the caller forgets to await.
    pub async fn record_login_failure_and_wait(&self, identity: &str) -> Duration {
        let delay = self.record_login_failure(identity);
        if !delay.is_zero() {
            tokio::time::sleep(delay).await;
        }
        delay
    }

    /// Records a successful authentication, resetting the failure history.
    pub fn record_login_success(&self, identity: &str) {
        let Ok(mut state) = self.state.lock() else {
            return;
        };
        let identity_key = identity_key(identity);
        state.failures.remove(&identity_key);
        state.jails.remove(&identity_key);
    }
}

/// Stores a failure counter, evicting the least recently failed identity when full.
fn track_failure(
    state: &mut LoginState,
    identity_key: IdentityKey,
    now: Instant,
    count: u32,
    capacity: usize,
) {
    if !state.failures.contains(&identity_key) {
        state.failures.make_room(capacity);
    }
    state.failures.upsert(identity_key, now, count);
}

fn record_jail_telemetry(identity: &str, jail_duration: Duration, current_count: u32) {
    let store = SecurityStore::global();
    store.inc_login_jail_bans();
    store.push_local_event(LiveSecurityEvent::local(
        "LOGIN_JAIL_TRIGGERED",
        format!(
            "Identity/IP '{}' placed in a {}s jail after {} failed login attempts",
            bounded_identity_for_log(identity),
            jail_duration.as_secs(),
            current_count
        ),
        bounded_identity_for_log(identity),
    ));
}

/// Returns `now + duration`, shortening a duration the clock cannot represent.
fn saturating_deadline(now: Instant, duration: Duration) -> Instant {
    let mut duration = duration;
    loop {
        if let Some(deadline) = now.checked_add(duration) {
            return deadline;
        }
        duration /= 2;
    }
}

const fn progressive_delay_seconds(current_count: u32) -> u64 {
    match current_count {
        1 => 0,
        2 => 1,
        3 => 2,
        _ => 4,
    }
}

fn identity_key(identity: &str) -> IdentityKey {
    Sha256::digest(identity.trim().as_bytes()).into()
}

fn bounded_identity_for_log(identity: &str) -> String {
    const MAX_LOGGED_IDENTITY_BYTES: usize = 128;
    let identity = identity.trim();
    if identity.len() <= MAX_LOGGED_IDENTITY_BYTES {
        return identity.to_string();
    }
    let mut boundary = MAX_LOGGED_IDENTITY_BYTES;
    while !identity.is_char_boundary(boundary) {
        boundary -= 1;
    }
    format!("{}…", &identity[..boundary])
}

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

    impl LoginGuard {
        pub(super) fn failure_count(&self) -> usize {
            self.state.lock().unwrap().failures.len()
        }

        pub(super) fn jail_count(&self) -> usize {
            self.state.lock().unwrap().jails.len()
        }

        pub(super) fn insert_jail(&self, identity: &str, expires_at: Instant) {
            self.state
                .lock()
                .unwrap()
                .jails
                .upsert(identity_key(identity), expires_at, ());
        }

        pub(super) fn insert_failure(&self, identity: &str, count: u32, last_attempt: Instant) {
            self.state
                .lock()
                .unwrap()
                .failures
                .upsert(identity_key(identity), last_attempt, count);
        }
    }

    pub(super) fn past(duration: Duration) -> Instant {
        Instant::now().checked_sub(duration).unwrap()
    }

    #[test]
    fn concurrent_identity_admission_preserves_the_failure_and_jail_limits() {
        for max_failures in [1, 5] {
            for _ in 0..16 {
                let guard = LoginGuard {
                    max_identities: 1,
                    max_failures,
                    ..LoginGuard::default()
                };
                let barrier = std::sync::Barrier::new(16);
                std::thread::scope(|scope| {
                    for index in 0..16 {
                        let guard = &guard;
                        let barrier = &barrier;
                        scope.spawn(move || {
                            barrier.wait();
                            guard.record_login_failure(&format!("concurrent-{index}"));
                        });
                    }
                });
                assert!(guard.failure_count() <= 1);
                assert!(guard.jail_count() <= 1);
            }
        }
    }

    #[test]
    // TM-AUTH-07: repeated failures receive bounded delay and temporary jailing.
    fn test_login_guard_tarpit_and_jail() {
        let guard = LoginGuard::new();
        let ip = "192.168.10.45";

        assert_eq!(guard.record_login_failure(ip), Duration::ZERO);
        assert_eq!(guard.record_login_failure(ip), Duration::from_secs(1));
        assert_eq!(guard.record_login_failure(ip), Duration::from_secs(2));
        assert_eq!(guard.record_login_failure(ip), Duration::from_secs(4));

        // 5th failure triggers jail
        assert_eq!(guard.record_login_failure(ip), Duration::from_secs(5));
        assert!(guard.is_jailed(ip));
        assert!(guard.remaining_jail_time(ip).is_some());

        // Reset on success
        guard.record_login_success(ip);
        assert!(!guard.is_jailed(ip));
    }

    #[test]
    fn test_login_guard_global_and_expired_jail() {
        let global = LoginGuard::global();
        assert_eq!(global.max_failures, 5);

        let guard = LoginGuard::new();
        // Insert expired jail
        guard.insert_jail("expired_user", past(Duration::from_secs(10)));
        assert!(!guard.is_jailed("expired_user"));
        assert!(guard.remaining_jail_time("expired_user").is_none());

        // Insert active jail
        guard.insert_jail("active_user", Instant::now() + Duration::from_secs(100));
        assert!(guard.is_jailed("active_user"));
        assert!(guard.remaining_jail_time("active_user").is_some());
    }

    #[test]
    fn already_jailed_and_capacity_exhaustion_fail_closed() {
        let guard = LoginGuard::new();
        guard.insert_jail("jailed-user", Instant::now() + Duration::from_secs(60));
        assert_eq!(
            guard.record_login_failure("jailed-user"),
            Duration::from_secs(5)
        );

        let mut full_guard = LoginGuard::new();
        full_guard.max_identities = 0;
        assert_eq!(
            full_guard.record_login_failure("new-user"),
            Duration::from_secs(5)
        );
        assert_eq!(full_guard.failure_count(), 0);
        assert_eq!(full_guard.jail_count(), 0);
    }

    #[test]
    fn expired_failure_window_restarts_the_tarpit_sequence() {
        let mut guard = LoginGuard::new();
        guard.window_duration = Duration::from_millis(1);
        guard.insert_failure("window-user", 4, past(Duration::from_secs(1)));
        assert_eq!(guard.record_login_failure("window-user"), Duration::ZERO);
    }

    #[test]
    fn logged_identity_is_trimmed_and_truncated_on_utf8_boundary() {
        assert_eq!(
            bounded_identity_for_log("  short identity  "),
            "short identity"
        );
        let long = format!("{}é-tail", "a".repeat(127));
        let bounded = bounded_identity_for_log(&long);
        assert!(bounded.ends_with('…'));
        assert!(bounded.len() <= 131);
        assert!(!bounded.contains("tail"));
    }

    #[tokio::test(start_paused = true)]
    async fn async_failure_api_applies_the_progressive_delay() {
        let guard = LoginGuard::new();
        assert_eq!(
            guard.record_login_failure_and_wait("async-user").await,
            Duration::ZERO
        );
        assert_eq!(
            guard.record_login_failure_and_wait("async-user").await,
            Duration::from_secs(1)
        );
    }
}

#[cfg(kani)]
#[cfg_attr(mutants, mutants::skip)]
mod kani_proofs {
    use super::*;

    #[kani::proof]
    fn proof_progressive_delay_is_bounded() {
        let current_count: u32 = kani::any();
        let delay = progressive_delay_seconds(current_count);

        assert!(delay <= 4);
        match current_count {
            1 => assert_eq!(delay, 0),
            2 => assert_eq!(delay, 1),
            3 => assert_eq!(delay, 2),
            _ => assert_eq!(delay, 4),
        }
    }
}