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
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::net::IpAddr;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Mutex, OnceLock};
use std::time::Duration;

mod event;
pub use event::{
    LIVE_SECURITY_EVENT_V1_JSON_SCHEMA, LiveSecurityEvent, SECURITY_EVENT_SCHEMA_VERSION,
};

static GLOBAL_SECURITY_STORE: OnceLock<SecurityStore> = OnceLock::new();

const MAX_LIVE_EVENTS: usize = 50;
const MAX_TELEMETRY_BANS: usize = 10_000;
const MAX_HONEYPOT_ROUTES: usize = 1_024;
const DEFAULT_HONEYPOT_BAN_TTL: Duration = Duration::from_secs(15 * 60);

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BannedIpRecord {
    pub ip: String,
    pub reason: String,
    pub timestamp_str: String,
    pub expires_at_unix_secs: u64,
}

pub struct SecurityStore {
    pub sanitizations_count: AtomicU64,
    pub honeypot_traps_count: AtomicU64,
    pub rbac_denials_count: AtomicU64,
    pub prompt_injections_blocked_count: AtomicU64,
    pub prompts_inspected_count: AtomicU64,
    pub pii_masked_count: AtomicU64,
    pub log_redactions_count: AtomicU64,
    pub zero_trust_mismatches_count: AtomicU64,
    pub schema_violations_count: AtomicU64,
    pub sri_signed_assets_count: AtomicU64,
    pub mfa_verifications_count: AtomicU64,
    pub deception_hits_count: AtomicU64,
    pub cswsh_blocks_count: AtomicU64,
    pub rate_limit_blocks_count: AtomicU64,
    /// Local alerts recorded through the legacy SIEM dispatch facade.
    /// This is not a successful external-delivery counter.
    pub siem_dispatches_count: AtomicU64,
    pub login_jail_bans_count: AtomicU64,
    pub dlp_secrets_masked_count: AtomicU64,
    pub secure_headers_applied_count: AtomicU64,
    pub idor_warnings_count: AtomicU64,
    pub timing_guard_protected_count: AtomicU64,
    pub banned_ips: DashMap<String, BannedIpRecord>,
    pub honeypot_route_hits: DashMap<String, AtomicU64>,
    pub live_events: Mutex<Vec<LiveSecurityEvent>>,
}

impl Default for SecurityStore {
    fn default() -> Self {
        Self::new()
    }
}

impl SecurityStore {
    pub fn new() -> Self {
        Self {
            sanitizations_count: AtomicU64::new(0),
            honeypot_traps_count: AtomicU64::new(0),
            rbac_denials_count: AtomicU64::new(0),
            prompt_injections_blocked_count: AtomicU64::new(0),
            prompts_inspected_count: AtomicU64::new(0),
            pii_masked_count: AtomicU64::new(0),
            log_redactions_count: AtomicU64::new(0),
            zero_trust_mismatches_count: AtomicU64::new(0),
            schema_violations_count: AtomicU64::new(0),
            sri_signed_assets_count: AtomicU64::new(0),
            mfa_verifications_count: AtomicU64::new(0),
            deception_hits_count: AtomicU64::new(0),
            cswsh_blocks_count: AtomicU64::new(0),
            rate_limit_blocks_count: AtomicU64::new(0),
            siem_dispatches_count: AtomicU64::new(0),
            login_jail_bans_count: AtomicU64::new(0),
            dlp_secrets_masked_count: AtomicU64::new(0),
            secure_headers_applied_count: AtomicU64::new(0),
            idor_warnings_count: AtomicU64::new(0),
            timing_guard_protected_count: AtomicU64::new(0),
            banned_ips: DashMap::new(),
            honeypot_route_hits: DashMap::new(),
            live_events: Mutex::new(Vec::new()),
        }
    }

    pub fn global() -> &'static SecurityStore {
        GLOBAL_SECURITY_STORE.get_or_init(SecurityStore::new)
    }

    pub fn inc_sanitizations(&self) {
        self.sanitizations_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_honeypot_traps(&self) {
        self.honeypot_traps_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_rbac_denials(&self) {
        self.rbac_denials_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn record_honeypot_trap(&self, ip: &str, path: &str) {
        self.record_honeypot_trap_with_ttl(ip, path, DEFAULT_HONEYPOT_BAN_TTL);
    }

    pub fn record_honeypot_trap_with_ttl(&self, ip: &str, path: &str, ban_ttl: Duration) {
        self.record_honeypot_event(ip, path, Some(ban_ttl));
    }

    /// Records a trap hit without claiming that the peer was added to an enforcement ban list.
    pub fn record_honeypot_observation(&self, ip: &str, path: &str) {
        self.record_honeypot_event(ip, path, None);
    }

    fn record_honeypot_event(&self, ip: &str, path: &str, ban_ttl: Option<Duration>) {
        self.inc_honeypot_traps();
        let now_str = current_timestamp_str();
        let now = unix_timestamp_secs();
        let normalized_ip = normalize_ip(ip);
        self.prune_expired_bans_at(now);

        if let Some(ban_ttl) = ban_ttl
            && normalized_ip != "unknown"
            && (self.banned_ips.len() < MAX_TELEMETRY_BANS
                || self.banned_ips.contains_key(&normalized_ip))
        {
            self.banned_ips.insert(
                normalized_ip.clone(),
                BannedIpRecord {
                    ip: normalized_ip.clone(),
                    reason: format!("Triggered honeypot route {path}"),
                    timestamp_str: now_str.clone(),
                    expires_at_unix_secs: now.saturating_add(ban_ttl.as_secs()),
                },
            );
        }

        if self.honeypot_route_hits.contains_key(path)
            || self.honeypot_route_hits.len() < MAX_HONEYPOT_ROUTES
        {
            self.honeypot_route_hits
                .entry(path.to_string())
                .or_insert_with(|| AtomicU64::new(0))
                .fetch_add(1, Ordering::Relaxed);
        }

        self.push_live_event(LiveSecurityEvent::local(
            "HONEYPOT_TRAP_TRIGGERED",
            format!("Peer {normalized_ip} accessed trap route {path}"),
            normalized_ip,
        ));
    }

    pub fn record_sanitization(&self, details: &str) {
        self.inc_sanitizations();
        self.push_live_event(LiveSecurityEvent::local(
            "XSS_PAYLOAD_NEUTRALIZED",
            details,
            "unknown",
        ));
    }

    /// Records a RASP rejection emitted by an actual inspector or middleware path.
    pub fn record_rasp_interception(&self, uri_bad: bool, headers_bad: bool, body_bad: bool) {
        self.push_live_event(LiveSecurityEvent::local(
            "RASP_PAYLOAD_INTERCEPTED",
            format!("Rejected request (uri={uri_bad}, headers={headers_bad}, body={body_bad})"),
            "unknown",
        ));
    }

    pub fn record_prompt_injection_blocked(&self, ip: &str, prompt_snippet: &str) {
        self.prompt_injections_blocked_count
            .fetch_add(1, Ordering::Relaxed);
        self.push_live_event(LiveSecurityEvent::local(
            "AI_PROMPT_INJECTION_SHIELDED",
            format!("Blocked malicious prompt snippet: {prompt_snippet}"),
            ip,
        ));
    }

    pub fn record_prompt_inspected(&self) {
        self.prompts_inspected_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn record_pii_masked(&self, count: usize) {
        self.pii_masked_count
            .fetch_add(count as u64, Ordering::Relaxed);
    }

    pub fn record_rbac_denial(&self, actor: &str, resource: &str) {
        self.inc_rbac_denials();
        self.push_live_event(LiveSecurityEvent::local(
            "RBAC_ACCESS_DENIED",
            format!("User {actor} denied access to {resource}"),
            "unknown",
        ));
    }

    /// Removes expired display records and returns the current active-ban count.
    pub fn active_banned_count(&self) -> usize {
        self.prune_expired_bans();
        self.banned_ips.len()
    }

    /// Removes expired honeypot ban records from telemetry dashboards.
    pub fn prune_expired_bans(&self) {
        self.prune_expired_bans_at(unix_timestamp_secs());
    }

    fn prune_expired_bans_at(&self, now: u64) {
        self.banned_ips
            .retain(|_, record| record.expires_at_unix_secs > now);
    }

    /// Stores an unsigned local event. `verified_hmac` is forced to false because callers cannot
    /// truthfully promote an in-memory event to cryptographically verified telemetry.
    pub fn push_local_event(&self, mut event: LiveSecurityEvent) {
        event.verified_hmac = false;
        self.push_live_event(event);
    }

    fn push_live_event(&self, event: LiveSecurityEvent) {
        if let Ok(mut events) = self.live_events.lock() {
            events.insert(0, event.normalized());
            if events.len() > MAX_LIVE_EVENTS {
                events.truncate(MAX_LIVE_EVENTS);
            }
        }
    }

    pub fn inc_log_redactions(&self) {
        self.log_redactions_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_zero_trust_mismatches(&self) {
        self.zero_trust_mismatches_count
            .fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_schema_violations(&self) {
        self.schema_violations_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_sri_signed_assets(&self) {
        self.sri_signed_assets_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_mfa_verifications(&self) {
        self.mfa_verifications_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_deception_hits(&self) {
        self.deception_hits_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_cswsh_blocks(&self) {
        self.cswsh_blocks_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_rate_limit_blocks(&self) {
        self.rate_limit_blocks_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_siem_dispatches(&self) {
        self.siem_dispatches_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_login_jail_bans(&self) {
        self.login_jail_bans_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_dlp_masked(&self) {
        self.dlp_secrets_masked_count
            .fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_secure_headers(&self) {
        self.secure_headers_applied_count
            .fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_idor_warnings(&self) {
        self.idor_warnings_count.fetch_add(1, Ordering::Relaxed);
    }

    pub fn inc_timing_guard_protected(&self) {
        self.timing_guard_protected_count
            .fetch_add(1, Ordering::Relaxed);
    }

    pub fn snapshot(&self) -> TelemetrySnapshot {
        self.prune_expired_bans();
        TelemetrySnapshot {
            sanitizations: self.sanitizations_count.load(Ordering::Relaxed),
            honeypot_traps: self.honeypot_traps_count.load(Ordering::Relaxed),
            rbac_denials: self.rbac_denials_count.load(Ordering::Relaxed),
            log_redactions: self.log_redactions_count.load(Ordering::Relaxed),
            zero_trust_mismatches: self.zero_trust_mismatches_count.load(Ordering::Relaxed),
            schema_violations: self.schema_violations_count.load(Ordering::Relaxed),
            sri_signed_assets: self.sri_signed_assets_count.load(Ordering::Relaxed),
            mfa_verifications: self.mfa_verifications_count.load(Ordering::Relaxed),
            deception_hits: self.deception_hits_count.load(Ordering::Relaxed),
            cswsh_blocks: self.cswsh_blocks_count.load(Ordering::Relaxed),
            rate_limit_blocks: self.rate_limit_blocks_count.load(Ordering::Relaxed),
            siem_dispatches: self.siem_dispatches_count.load(Ordering::Relaxed),
            login_jail_bans: self.login_jail_bans_count.load(Ordering::Relaxed),
            dlp_secrets_masked: self.dlp_secrets_masked_count.load(Ordering::Relaxed),
            secure_headers_applied: self.secure_headers_applied_count.load(Ordering::Relaxed),
            idor_warnings: self.idor_warnings_count.load(Ordering::Relaxed),
            timing_guard_protected: self.timing_guard_protected_count.load(Ordering::Relaxed),
            prompt_injections_blocked: self.prompt_injections_blocked_count.load(Ordering::Relaxed),
        }
    }
}

pub fn current_timestamp_str() -> String {
    chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true)
}

fn unix_timestamp_secs() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

/// Returns a canonical IP string only when the value is a syntactically valid address.
pub fn normalize_ip(value: &str) -> String {
    value
        .parse::<IpAddr>()
        .map(|ip| ip.to_string())
        .unwrap_or_else(|_| "unknown".to_string())
}

pub fn get_real_rss_memory_mb() -> Option<f64> {
    rullst_core::radar::get_process_memory_mb()
}

pub type SecurityTelemetry = SecurityStore;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TelemetrySnapshot {
    pub sanitizations: u64,
    pub honeypot_traps: u64,
    pub rbac_denials: u64,
    pub log_redactions: u64,
    pub zero_trust_mismatches: u64,
    pub schema_violations: u64,
    pub sri_signed_assets: u64,
    pub mfa_verifications: u64,
    pub deception_hits: u64,
    pub cswsh_blocks: u64,
    pub rate_limit_blocks: u64,
    pub siem_dispatches: u64,
    pub login_jail_bans: u64,
    pub dlp_secrets_masked: u64,
    pub secure_headers_applied: u64,
    pub idor_warnings: u64,
    pub timing_guard_protected: u64,
    pub prompt_injections_blocked: u64,
}

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

    #[test]
    fn timestamps_are_absolute_rfc3339_values() {
        let timestamp = current_timestamp_str();
        assert!(chrono::DateTime::parse_from_rfc3339(&timestamp).is_ok());
        assert!(!timestamp.ends_with("s ago"));
        assert_ne!(timestamp, "Just now");
    }

    #[test]
    fn local_events_cannot_claim_hmac_verification_or_fake_ips() {
        let store = SecurityStore::new();
        let mut event = LiveSecurityEvent::local("TEST", "local event", "attacker-controlled");
        event.verified_hmac = true;
        store.push_local_event(event);

        let events = store.live_events.lock().expect("telemetry lock");
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].client_ip, "unknown");
        assert!(!events[0].verified_hmac);
    }

    #[test]
    fn invalid_honeypot_identity_is_not_reported_as_an_active_ban() {
        let store = SecurityStore::new();
        store.record_honeypot_trap("not-a-peer-ip", "/.env");
        store.record_honeypot_observation("192.0.2.80", "/.git/config");

        assert_eq!(store.active_banned_count(), 0);
    }

    #[test]
    fn blocked_prompt_is_not_counted_as_two_inspections() {
        let store = SecurityStore::new();
        store.record_prompt_inspected();
        store.record_prompt_injection_blocked("192.0.2.42", "ignore previous instructions");

        assert_eq!(store.prompts_inspected_count.load(Ordering::Relaxed), 1);
        assert_eq!(
            store
                .prompt_injections_blocked_count
                .load(Ordering::Relaxed),
            1
        );
    }

    #[test]
    fn rasp_interceptions_are_local_unsigned_events() {
        let store = SecurityStore::new();
        store.record_rasp_interception(false, false, true);

        let events = store.live_events.lock().expect("telemetry lock");
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].event_type, "RASP_PAYLOAD_INTERCEPTED");
        assert!(events[0].details.contains("body=true"));
        assert!(!events[0].verified_hmac);
    }
}