nexus-shield 0.4.1

Adaptive zero-trust security gateway with real-time endpoint protection — SQL firewall, SSRF guard, malware detection, process monitoring, network analysis, rootkit detection
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
// ============================================================================
// File: rate_governor.rs
// Description: Adaptive per-IP rate limiter with token bucket and escalation levels
// Author: Andrew Jewell Sr. - AutomataNexus
// Updated: March 24, 2026
//
// DISCLAIMER: This software is provided "as is", without warranty of any kind,
// express or implied. Use at your own risk. AutomataNexus and the author assume
// no liability for any damages arising from the use of this software.
// ============================================================================
//! Rate Governor — Adaptive rate limiting with behavioral escalation.
//!
//! Uses a token bucket algorithm per-IP with automatic escalation:
//! well-behaved clients get full capacity; repeat violators get progressively
//! restricted up to temporary bans.

use crate::config::RateConfig;
use parking_lot::RwLock;
use std::collections::HashMap;
use std::time::{Duration, Instant};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EscalationLevel {
    None,
    Warn,
    Throttle,
    Block,
    Ban,
}

#[derive(Debug)]
pub struct RateCheckResult {
    /// Whether the request is allowed to proceed.
    pub allowed: bool,
    /// Current escalation level for this client.
    pub escalation: EscalationLevel,
    /// Remaining tokens in the bucket.
    pub remaining: f64,
    /// Seconds until ban expires (only set for EscalationLevel::Ban).
    pub retry_after: Option<u64>,
    /// Total violation count for this IP.
    pub violations: u32,
}

struct TokenBucket {
    tokens: f64,
    max_tokens: f64,
    refill_rate: f64,
    last_refill: Instant,
    violations: u32,
    last_violation: Option<Instant>,
    ban_until: Option<Instant>,
}

impl TokenBucket {
    fn new(config: &RateConfig) -> Self {
        Self {
            tokens: config.burst_capacity,
            max_tokens: config.burst_capacity,
            refill_rate: config.requests_per_second,
            last_refill: Instant::now(),
            violations: 0,
            last_violation: None,
            ban_until: None,
        }
    }

    fn refill(&mut self) {
        let now = Instant::now();
        let elapsed = now.duration_since(self.last_refill).as_secs_f64();
        self.tokens = (self.tokens + elapsed * self.refill_rate).min(self.max_tokens);
        self.last_refill = now;
    }

    fn decay_violations(&mut self, decay_secs: u64) {
        if let Some(last) = self.last_violation {
            let elapsed = last.elapsed().as_secs();
            if elapsed > decay_secs && self.violations > 0 {
                // Decay one violation per decay period
                let decay_count = (elapsed / decay_secs) as u32;
                self.violations = self.violations.saturating_sub(decay_count);
                if self.violations == 0 {
                    self.last_violation = None;
                }
            }
        }
    }

    fn try_consume(&mut self) -> bool {
        self.refill();
        if self.tokens >= 1.0 {
            self.tokens -= 1.0;
            true
        } else {
            self.violations += 1;
            self.last_violation = Some(Instant::now());
            false
        }
    }

    fn escalation_level(&self, config: &RateConfig) -> EscalationLevel {
        if self.violations >= config.ban_after {
            EscalationLevel::Ban
        } else if self.violations >= config.block_after {
            EscalationLevel::Block
        } else if self.violations >= config.throttle_after {
            EscalationLevel::Throttle
        } else if self.violations >= config.warn_after {
            EscalationLevel::Warn
        } else {
            EscalationLevel::None
        }
    }
}

pub struct RateGovernor {
    config: RateConfig,
    buckets: RwLock<HashMap<String, TokenBucket>>,
}

impl RateGovernor {
    pub fn new(config: &crate::config::ShieldConfig) -> Self {
        Self {
            config: config.rate.clone(),
            buckets: RwLock::new(HashMap::new()),
        }
    }

    pub fn check(&self, client_ip: &str) -> RateCheckResult {
        let mut buckets = self.buckets.write();
        let bucket = buckets
            .entry(client_ip.to_string())
            .or_insert_with(|| TokenBucket::new(&self.config));

        // Decay old violations
        bucket.decay_violations(self.config.violation_decay_secs);

        // Check for active ban
        if let Some(ban_until) = bucket.ban_until {
            if Instant::now() < ban_until {
                let retry_after = ban_until.duration_since(Instant::now()).as_secs();
                return RateCheckResult {
                    allowed: false,
                    escalation: EscalationLevel::Ban,
                    remaining: 0.0,
                    retry_after: Some(retry_after),
                    violations: bucket.violations,
                };
            } else {
                // Ban expired — clear it but keep violation count for escalation
                bucket.ban_until = None;
            }
        }

        let consumed = bucket.try_consume();
        let escalation = bucket.escalation_level(&self.config);

        // Apply ban if escalation reaches Ban level
        if escalation == EscalationLevel::Ban && bucket.ban_until.is_none() {
            bucket.ban_until =
                Some(Instant::now() + Duration::from_secs(self.config.ban_duration_secs));
            return RateCheckResult {
                allowed: false,
                escalation: EscalationLevel::Ban,
                remaining: 0.0,
                retry_after: Some(self.config.ban_duration_secs),
                violations: bucket.violations,
            };
        }

        let allowed = match escalation {
            EscalationLevel::None | EscalationLevel::Warn => consumed,
            EscalationLevel::Throttle => {
                // Throttled: only allow if bucket is at least 50% full
                consumed && bucket.tokens > bucket.max_tokens * 0.5
            }
            EscalationLevel::Block | EscalationLevel::Ban => false,
        };

        RateCheckResult {
            allowed,
            escalation,
            remaining: bucket.tokens,
            retry_after: None,
            violations: bucket.violations,
        }
    }

    /// Get the current escalation level for an IP without consuming a token.
    pub fn peek_escalation(&self, client_ip: &str) -> EscalationLevel {
        let buckets = self.buckets.read();
        buckets
            .get(client_ip)
            .map(|b| b.escalation_level(&self.config))
            .unwrap_or(EscalationLevel::None)
    }

    /// Manually ban an IP address for the configured ban duration.
    pub fn ban_ip(&self, client_ip: &str) {
        let mut buckets = self.buckets.write();
        let bucket = buckets
            .entry(client_ip.to_string())
            .or_insert_with(|| TokenBucket::new(&self.config));
        bucket.ban_until =
            Some(Instant::now() + Duration::from_secs(self.config.ban_duration_secs));
        bucket.violations = self.config.ban_after;
        tracing::warn!(ip = client_ip, "IP manually banned");
    }

    /// Remove an IP ban.
    pub fn unban_ip(&self, client_ip: &str) {
        let mut buckets = self.buckets.write();
        if let Some(bucket) = buckets.get_mut(client_ip) {
            bucket.ban_until = None;
            bucket.violations = 0;
            tracing::info!(ip = client_ip, "IP unbanned");
        }
    }

    /// Prune stale entries (clients that haven't been seen in a while).
    pub fn prune_stale(&self, max_age: Duration) {
        let mut buckets = self.buckets.write();
        let now = Instant::now();
        buckets.retain(|_, bucket| {
            now.duration_since(bucket.last_refill) < max_age
        });
    }
}

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

    fn make_governor() -> RateGovernor {
        let mut config = ShieldConfig::default();
        config.rate.requests_per_second = 10.0;
        config.rate.burst_capacity = 10.0;
        config.rate.warn_after = 2;
        config.rate.block_after = 5;
        config.rate.ban_after = 8;
        RateGovernor::new(&config)
    }

    #[test]
    fn allows_under_limit() {
        let governor = make_governor();
        for _ in 0..10 {
            let result = governor.check("1.2.3.4");
            assert!(result.allowed);
        }
    }

    #[test]
    fn blocks_over_limit() {
        let governor = make_governor();
        // Drain the bucket
        for _ in 0..10 {
            governor.check("1.2.3.4");
        }
        // Next request should fail
        let result = governor.check("1.2.3.4");
        assert!(!result.allowed);
    }

    #[test]
    fn escalates_on_violations() {
        let governor = make_governor();
        // Drain the bucket to cause violations
        for _ in 0..20 {
            governor.check("5.6.7.8");
        }
        let level = governor.peek_escalation("5.6.7.8");
        assert!(
            matches!(level, EscalationLevel::Block | EscalationLevel::Ban),
            "Should escalate after many violations: {:?}",
            level
        );
    }

    #[test]
    fn manual_ban_works() {
        let governor = make_governor();
        governor.ban_ip("9.8.7.6");
        let result = governor.check("9.8.7.6");
        assert!(!result.allowed);
        assert_eq!(result.escalation, EscalationLevel::Ban);
        assert!(result.retry_after.is_some());
    }

    #[test]
    fn different_ips_independent() {
        let governor = make_governor();
        // Drain bucket for IP A
        for _ in 0..15 {
            governor.check("1.1.1.1");
        }
        // IP B should still be fine
        let result = governor.check("2.2.2.2");
        assert!(result.allowed);
    }

    // ── Escalation level progression ───────────────────────

    #[test]
    fn escalation_starts_at_none() {
        let governor = make_governor();
        let level = governor.peek_escalation("fresh_ip");
        assert_eq!(level, EscalationLevel::None);
    }

    #[test]
    fn peek_escalation_does_not_consume_token() {
        let governor = make_governor();
        // Peek many times, should still be allowed
        for _ in 0..100 {
            governor.peek_escalation("peek_ip");
        }
        let result = governor.check("peek_ip");
        assert!(result.allowed);
    }

    #[test]
    fn remaining_tokens_decrease_on_check() {
        let governor = make_governor();
        let r1 = governor.check("drain_ip");
        let r2 = governor.check("drain_ip");
        assert!(r2.remaining < r1.remaining, "Tokens should decrease");
    }

    #[test]
    fn violations_count_increases_when_denied() {
        let governor = make_governor();
        // Drain all tokens
        for _ in 0..10 {
            governor.check("violations_ip");
        }
        // Next checks should be denied and increase violations
        let result = governor.check("violations_ip");
        assert!(!result.allowed);
        assert!(result.violations > 0, "Violations should be > 0");
    }

    // ── Manual ban/unban ───────────────────────────────────

    #[test]
    fn unban_ip_allows_requests() {
        let governor = make_governor();
        governor.ban_ip("ban_test");
        let result = governor.check("ban_test");
        assert!(!result.allowed);

        governor.unban_ip("ban_test");
        let result = governor.check("ban_test");
        assert!(result.allowed, "Unbanned IP should be allowed");
    }

    #[test]
    fn unban_ip_resets_violations() {
        let governor = make_governor();
        governor.ban_ip("violation_reset");
        governor.unban_ip("violation_reset");
        let level = governor.peek_escalation("violation_reset");
        assert_eq!(level, EscalationLevel::None, "Violations should be reset after unban");
    }

    #[test]
    fn ban_ip_has_retry_after() {
        let governor = make_governor();
        governor.ban_ip("retry_after_ip");
        let result = governor.check("retry_after_ip");
        assert!(result.retry_after.is_some());
        assert!(result.retry_after.unwrap() > 0);
    }

    #[test]
    fn unban_unknown_ip_is_noop() {
        let governor = make_governor();
        governor.unban_ip("never_seen"); // should not panic
    }

    // ── Prune stale ────────────────────────────────────────

    #[test]
    fn prune_stale_removes_old_buckets() {
        let governor = make_governor();
        governor.check("stale_bucket");
        // Prune with zero max age — everything is stale
        governor.prune_stale(Duration::from_secs(0));
        // After pruning, the IP should be treated as new
        let level = governor.peek_escalation("stale_bucket");
        assert_eq!(level, EscalationLevel::None);
    }

    #[test]
    fn prune_stale_keeps_recent_buckets() {
        let governor = make_governor();
        governor.check("recent_bucket");
        // Prune with large max age
        governor.prune_stale(Duration::from_secs(3600));
        // Bucket should still exist — peek should not panic
        let _level = governor.peek_escalation("recent_bucket");
    }

    // ── Escalation level ordering ──────────────────────────

    #[test]
    fn escalation_level_equality() {
        assert_eq!(EscalationLevel::None, EscalationLevel::None);
        assert_eq!(EscalationLevel::Ban, EscalationLevel::Ban);
        assert_ne!(EscalationLevel::Warn, EscalationLevel::Block);
    }

    #[test]
    fn rate_check_result_initial_values() {
        let governor = make_governor();
        let result = governor.check("initial_check");
        assert!(result.allowed);
        assert_eq!(result.escalation, EscalationLevel::None);
        assert!(result.retry_after.is_none());
        assert_eq!(result.violations, 0);
    }

    // ── Burst behavior ─────────────────────────────────────

    #[test]
    fn burst_capacity_matches_config() {
        let mut config = ShieldConfig::default();
        config.rate.burst_capacity = 5.0;
        config.rate.requests_per_second = 0.0; // no refill
        let governor = RateGovernor::new(&config);

        let mut allowed_count = 0;
        for _ in 0..10 {
            if governor.check("burst_ip").allowed {
                allowed_count += 1;
            }
        }
        assert_eq!(allowed_count, 5, "Should allow exactly burst_capacity requests");
    }

    #[test]
    fn ban_escalation_blocks_all_subsequent() {
        let mut config = ShieldConfig::default();
        config.rate.burst_capacity = 2.0;
        config.rate.requests_per_second = 0.0;
        config.rate.warn_after = 1;
        config.rate.throttle_after = 2;
        config.rate.block_after = 3;
        config.rate.ban_after = 4;
        config.rate.ban_duration_secs = 600;
        let governor = RateGovernor::new(&config);

        // Drain bucket and trigger escalation to ban
        for _ in 0..20 {
            governor.check("escalate_ip");
        }

        let result = governor.check("escalate_ip");
        assert!(!result.allowed);
        assert!(
            matches!(result.escalation, EscalationLevel::Block | EscalationLevel::Ban),
            "Should be blocked or banned: {:?}", result.escalation
        );
    }
}