rullst-security 12.1.2

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
use crate::error::SecurityError;
use axum::{
    body::Body,
    extract::ConnectInfo,
    http::{Request, Response, StatusCode},
    response::IntoResponse,
};
use std::collections::{HashMap, HashSet};
use std::future::Future;
use std::net::{IpAddr, SocketAddr};
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use std::time::{Duration, Instant};
use tower::{Layer, Service};

/// Default lifetime of a honeypot peer ban.
pub const DEFAULT_HONEYPOT_BAN_TTL: Duration = Duration::from_secs(15 * 60);
/// Default upper bound for concurrently tracked banned peers.
pub const DEFAULT_MAX_HONEYPOT_BANS: usize = 100_000;
/// Upper bound for configured exact trap paths.
pub const MAX_HONEYPOT_TRAP_PATHS: usize = 1_024;

#[derive(Clone, Debug)]
pub struct HoneypotState {
    banned_ips: Arc<Mutex<HashMap<IpAddr, Instant>>>,
    trap_paths: Arc<Vec<String>>,
    ban_ttl: Duration,
    max_bans: usize,
}

impl Default for HoneypotState {
    fn default() -> Self {
        Self::new(vec![
            "/.env".to_string(),
            "/.env.local".to_string(),
            "/.env.production".to_string(),
            "/.git/config".to_string(),
            "/.aws/credentials".to_string(),
            "/.vscode/sftp.json".to_string(),
            "/.ds_store".to_string(),
            "/admin.php".to_string(),
            "/wp-login.php".to_string(),
            "/wp-admin/".to_string(),
            "/phpmyadmin/".to_string(),
            "/config.json".to_string(),
            "/setup.php".to_string(),
            "/xmlrpc.php".to_string(),
            "/actuator/health".to_string(),
            "/console".to_string(),
            "/api/v1/debug".to_string(),
            "/swagger-ui.html".to_string(),
            "/database.sqlite".to_string(),
            "/backup.sql".to_string(),
            "/server-status".to_string(),
            "/docker-compose.yml".to_string(),
        ])
    }
}

impl HoneypotState {
    /// Compatibility constructor using bounded, expiring defaults.
    ///
    /// Invalid paths are ignored and excess paths are truncated. Use [`Self::try_with_limits`]
    /// when configuration errors must abort application startup.
    pub fn new(trap_paths: Vec<String>) -> Self {
        let trap_paths = canonical_trap_paths(trap_paths)
            .into_iter()
            .take(MAX_HONEYPOT_TRAP_PATHS)
            .collect();
        Self::new_inner(
            trap_paths,
            DEFAULT_HONEYPOT_BAN_TTL,
            DEFAULT_MAX_HONEYPOT_BANS,
        )
    }

    /// Creates a honeypot state with explicit finite ban lifetime and cardinality limits.
    pub fn try_with_limits(
        trap_paths: Vec<String>,
        ban_ttl: Duration,
        max_bans: usize,
    ) -> Result<Self, SecurityError> {
        if ban_ttl.is_zero() {
            return Err(SecurityError::General(
                "honeypot ban TTL must be greater than zero".to_string(),
            ));
        }
        if max_bans == 0 {
            return Err(SecurityError::General(
                "honeypot maximum ban cardinality must be greater than zero".to_string(),
            ));
        }
        if trap_paths.iter().any(|path| !is_valid_trap_path(path)) {
            return Err(SecurityError::General(
                "honeypot trap paths must be absolute paths without queries, fragments, or control characters"
                    .to_string(),
            ));
        }

        let paths = canonical_trap_paths(trap_paths);
        if paths.len() > MAX_HONEYPOT_TRAP_PATHS {
            return Err(SecurityError::General(format!(
                "honeypot accepts at most {MAX_HONEYPOT_TRAP_PATHS} trap paths"
            )));
        }
        if paths.is_empty() {
            return Err(SecurityError::General(
                "honeypot requires at least one valid absolute trap path".to_string(),
            ));
        }

        Ok(Self::new_inner(paths, ban_ttl, max_bans))
    }

    fn new_inner(trap_paths: Vec<String>, ban_ttl: Duration, max_bans: usize) -> Self {
        Self {
            banned_ips: Arc::new(Mutex::new(HashMap::new())),
            trap_paths: Arc::new(trap_paths),
            ban_ttl,
            max_bans,
        }
    }

    pub fn is_banned(&self, ip: &str) -> bool {
        let Ok(ip) = ip.parse::<IpAddr>() else {
            return false;
        };
        self.is_peer_banned(ip)
    }

    fn is_peer_banned(&self, ip: IpAddr) -> bool {
        let now = Instant::now();
        let Ok(mut bans) = self.banned_ips.lock() else {
            // A poisoned security state must not silently allow requests.
            return true;
        };
        bans.retain(|_, expires_at| *expires_at > now);
        bans.contains_key(&ip)
    }

    pub fn ban_ip(&self, ip: String) {
        if let Ok(ip) = ip.parse::<IpAddr>() {
            self.ban_peer(ip);
        }
    }

    fn ban_peer(&self, ip: IpAddr) {
        let now = Instant::now();
        let Ok(mut bans) = self.banned_ips.lock() else {
            return;
        };
        bans.retain(|_, expires_at| *expires_at > now);

        if bans.len() >= self.max_bans
            && !bans.contains_key(&ip)
            && let Some(oldest_ip) = bans
                .iter()
                .min_by_key(|(_, expires_at)| **expires_at)
                .map(|(ip, _)| *ip)
        {
            bans.remove(&oldest_ip);
        }
        let Some(expires_at) = now.checked_add(self.ban_ttl) else {
            return;
        };
        bans.insert(ip, expires_at);
    }

    /// Matches only a complete configured URI path; substrings and prefixes are not traps.
    pub fn is_trap(&self, path: &str) -> bool {
        self.trap_paths
            .iter()
            .any(|trap| trap.eq_ignore_ascii_case(path))
    }

    pub fn banned_count(&self) -> usize {
        let now = Instant::now();
        let Ok(mut bans) = self.banned_ips.lock() else {
            return self.max_bans;
        };
        bans.retain(|_, expires_at| *expires_at > now);
        bans.len()
    }
}

fn canonical_trap_paths(paths: Vec<String>) -> Vec<String> {
    let mut seen = HashSet::new();
    paths
        .into_iter()
        .filter(|path| is_valid_trap_path(path))
        .filter(|path| seen.insert(path.to_ascii_lowercase()))
        .collect()
}

fn is_valid_trap_path(path: &str) -> bool {
    path.starts_with('/')
        && path.len() <= 2_048
        && !path.contains('?')
        && !path.contains('#')
        && !path.chars().any(char::is_control)
}

#[derive(Clone)]
pub struct HoneypotLayer {
    state: HoneypotState,
}

impl HoneypotLayer {
    pub fn new(state: HoneypotState) -> Self {
        Self { state }
    }
}

impl<S> Layer<S> for HoneypotLayer {
    type Service = HoneypotService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        HoneypotService {
            inner,
            state: self.state.clone(),
        }
    }
}

#[derive(Clone)]
pub struct HoneypotService<S> {
    inner: S,
    state: HoneypotState,
}

impl<S> Service<Request<Body>> for HoneypotService<S>
where
    S: Service<Request<Body>, Response = Response<Body>> + Send + 'static,
    S::Future: Send + 'static,
{
    type Response = Response<Body>;
    type Error = S::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: Request<Body>) -> Self::Future {
        // ConnectInfo is created from the accepted socket. Untrusted forwarding headers are never
        // used as an enforcement identity.
        let peer_ip = req
            .extensions()
            .get::<ConnectInfo<SocketAddr>>()
            .map(|connection| connection.0.ip());

        if peer_ip.is_some_and(|ip| self.state.is_peer_banned(ip)) {
            let response = (
                StatusCode::FORBIDDEN,
                "Access Denied: IP Banned by Rullst Honey",
            )
                .into_response();
            return Box::pin(async move { Ok(response) });
        }

        let path = req.uri().path().to_string();
        if self.state.is_trap(&path) {
            let client_ip = peer_ip
                .map(|ip| ip.to_string())
                .unwrap_or_else(|| "unknown".to_string());
            if let Some(ip) = peer_ip {
                self.state.ban_peer(ip);
            }
            tracing::warn!(target: "rullst_security::honey", ip = %client_ip, path = %path, "Honeypot trap triggered");
            crate::telemetry::SecurityStore::global().record_honeypot_trap_with_ttl(
                &client_ip,
                &path,
                self.state.ban_ttl,
            );
            let response = (
                StatusCode::FORBIDDEN,
                "Access Denied: Honeypot Trap Triggered",
            )
                .into_response();
            return Box::pin(async move { Ok(response) });
        }

        let future = self.inner.call(req);
        Box::pin(future)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::{Router, routing::get};
    use std::thread;
    use tower::ServiceExt;

    #[test]
    fn traps_use_exact_paths_and_bans_expire() {
        let state =
            HoneypotState::try_with_limits(vec!["/.env".to_string()], Duration::from_millis(10), 2)
                .expect("valid honeypot state");

        assert!(state.is_trap("/.env"));
        assert!(!state.is_trap("/download/.env"));
        assert!(!state.is_trap("/.environment"));

        state.ban_ip("192.0.2.1".to_string());
        assert!(state.is_banned("192.0.2.1"));
        thread::sleep(Duration::from_millis(20));
        assert!(!state.is_banned("192.0.2.1"));
    }

    #[test]
    fn ban_cardinality_is_bounded_and_invalid_ips_are_ignored() {
        let state =
            HoneypotState::try_with_limits(vec!["/.env".to_string()], Duration::from_secs(60), 2)
                .expect("valid honeypot state");

        state.ban_ip("not-an-ip".to_string());
        state.ban_ip("192.0.2.1".to_string());
        state.ban_ip("192.0.2.2".to_string());
        state.ban_ip("192.0.2.3".to_string());
        assert_eq!(state.banned_count(), 2);
    }

    #[test]
    fn strict_configuration_rejects_unsafe_or_unbounded_values() {
        let valid_path = vec!["/.env".to_string()];
        assert!(HoneypotState::try_with_limits(valid_path.clone(), Duration::ZERO, 1).is_err());
        assert!(HoneypotState::try_with_limits(valid_path, Duration::from_secs(1), 0).is_err());
        assert!(
            HoneypotState::try_with_limits(
                vec!["relative/path".to_string()],
                Duration::from_secs(1),
                1,
            )
            .is_err()
        );
        assert!(HoneypotState::try_with_limits(Vec::new(), Duration::from_secs(1), 1).is_err());

        let too_many_paths = (0..=MAX_HONEYPOT_TRAP_PATHS)
            .map(|index| format!("/trap-{index}"))
            .collect();
        assert!(HoneypotState::try_with_limits(too_many_paths, Duration::from_secs(1), 1).is_err());
    }

    #[test]
    fn compatibility_constructor_filters_invalid_and_duplicate_paths() {
        let state = HoneypotState::new(vec![
            "/Trap".to_string(),
            "/trap".to_string(),
            "relative".to_string(),
            "/bad?query=true".to_string(),
        ]);
        assert!(state.is_trap("/trap"));
        assert_eq!(state.trap_paths.len(), 1);
    }

    #[tokio::test]
    async fn middleware_ignores_forwarded_identity_and_uses_socket_peer() {
        let state = HoneypotState::new(vec!["/.env".to_string()]);
        let app = Router::new()
            .route("/{*path}", get(|| async { StatusCode::OK }))
            .layer(HoneypotLayer::new(state.clone()));

        let forged = Request::builder()
            .uri("/.env")
            .header("x-forwarded-for", "198.51.100.99")
            .body(Body::empty())
            .expect("valid request");
        let response = app.clone().oneshot(forged).await.expect("response");
        assert_eq!(response.status(), StatusCode::FORBIDDEN);
        assert!(!state.is_banned("198.51.100.99"));

        let mut verified = Request::builder()
            .uri("/.env")
            .header("x-forwarded-for", "198.51.100.99")
            .body(Body::empty())
            .expect("valid request");
        verified.extensions_mut().insert(ConnectInfo(
            "192.0.2.25:443"
                .parse::<SocketAddr>()
                .expect("valid socket address"),
        ));
        let response = app.clone().oneshot(verified).await.expect("response");
        assert_eq!(response.status(), StatusCode::FORBIDDEN);
        assert!(state.is_banned("192.0.2.25"));
        assert!(!state.is_banned("198.51.100.99"));

        let mut banned_peer = Request::builder()
            .uri("/safe")
            .body(Body::empty())
            .expect("valid request");
        banned_peer.extensions_mut().insert(ConnectInfo(
            "192.0.2.25:8443"
                .parse::<SocketAddr>()
                .expect("valid socket address"),
        ));
        let response = app.clone().oneshot(banned_peer).await.expect("response");
        assert_eq!(response.status(), StatusCode::FORBIDDEN);

        let lookalike = Request::builder()
            .uri("/download/.env")
            .body(Body::empty())
            .expect("valid request");
        let response = app.oneshot(lookalike).await.expect("response");
        assert_eq!(response.status(), StatusCode::OK);
    }
}