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
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
//! Data Loss Prevention (DLP) Response Interceptor.
//! Intercepts HTTP response streams to prevent accidental leaks of private keys, AWS credentials, and database secrets.

use crate::telemetry::{LiveSecurityEvent, SecurityStore};
use axum::{
    body::{Body, HttpBody},
    http::{
        HeaderMap, HeaderValue, Method, Request, Response, StatusCode,
        header::{self, HeaderName},
    },
};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tower::{Layer, Service};

const MAX_BUFFERED_RESPONSE_BYTES: u64 = 2 * 1024 * 1024;

fn textual_media_type(headers: &HeaderMap) -> Option<&str> {
    let value = headers.get(header::CONTENT_TYPE)?.to_str().ok()?;
    let media_type = value.split(';').next()?.trim();

    if media_type.eq_ignore_ascii_case("text/event-stream") {
        return None;
    }

    let is_text = media_type
        .get(..5)
        .is_some_and(|prefix| prefix.eq_ignore_ascii_case("text/"));
    let is_json = media_type.eq_ignore_ascii_case("application/json")
        || media_type
            .strip_suffix("+json")
            .is_some_and(|prefix| prefix.starts_with("application/"));

    (is_text || is_json).then_some(media_type)
}

fn has_identity_encoding(headers: &HeaderMap) -> bool {
    headers
        .get(header::CONTENT_ENCODING)
        .is_none_or(|value| value.as_bytes().eq_ignore_ascii_case(b"identity"))
}

/// Only fixed, in-memory bodies are inspected. Unknown-size bodies are likely streams and must
/// pass through untouched because consuming a partial stream cannot be rolled back safely.
fn is_safely_bufferable(headers: &HeaderMap, body: &Body) -> bool {
    let hint = body.size_hint();
    let Some(upper) = hint.upper() else {
        return false;
    };

    if !is_fixed_body_size_within_limit(hint.lower(), upper) {
        return false;
    }

    match headers.get(header::CONTENT_LENGTH) {
        Some(value) => value
            .to_str()
            .ok()
            .and_then(|value| value.parse::<u64>().ok())
            .is_some_and(|declared| declared == upper),
        None => true,
    }
}

const fn is_fixed_body_size_within_limit(lower: u64, upper: u64) -> bool {
    lower == upper && upper <= MAX_BUFFERED_RESPONSE_BYTES
}

fn remove_stale_representation_headers(headers: &mut HeaderMap, body_len: usize) {
    headers.remove(header::ETAG);
    headers.remove(header::CONTENT_RANGE);
    headers.remove(header::ACCEPT_RANGES);
    headers.remove(HeaderName::from_static("content-md5"));
    headers.remove(HeaderName::from_static("digest"));
    headers.remove(HeaderName::from_static("content-digest"));

    headers.remove(header::CONTENT_LENGTH);
    if let Ok(value) = HeaderValue::from_str(&body_len.to_string()) {
        headers.insert(header::CONTENT_LENGTH, value);
    }
}

fn body_collection_failure() -> Response<Body> {
    let mut response = Response::new(Body::from("response inspection failed"));
    *response.status_mut() = StatusCode::BAD_GATEWAY;
    response.headers_mut().insert(
        header::CONTENT_TYPE,
        HeaderValue::from_static("text/plain; charset=utf-8"),
    );
    response
        .headers_mut()
        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
    response
}

/// Masks sensitive patterns from response payloads. Returns (sanitized_bytes, was_masked).
pub fn mask_response_payload(input: &[u8]) -> (Vec<u8>, bool) {
    if input.is_empty() {
        return (Vec::new(), false);
    }

    let Ok(text) = std::str::from_utf8(input) else {
        return (input.to_vec(), false);
    };
    let mut sanitized = text.to_owned();
    let mut modified = false;

    // 1. Mask Private Keys (RSA / OpenSSH / Generic)
    for (begin, end_marker) in [
        ("-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----"),
        (
            "-----BEGIN RSA PRIVATE KEY-----",
            "-----END RSA PRIVATE KEY-----",
        ),
        (
            "-----BEGIN OPENSSH PRIVATE KEY-----",
            "-----END OPENSSH PRIVATE KEY-----",
        ),
    ] {
        let mut cursor = 0;
        while let Some(offset) = sanitized[cursor..].find(begin) {
            let start = cursor + offset;
            let search_from = start + begin.len();
            let Some(end_offset) = sanitized[search_from..].find(end_marker) else {
                break;
            };
            let end = search_from + end_offset + end_marker.len();
            sanitized.replace_range(start..end, "[DLP_BLOCKED_PRIVATE_KEY]");
            modified = true;
            cursor = start + "[DLP_BLOCKED_PRIVATE_KEY]".len();
        }
    }

    // 2. Mask all AWS Access Keys (AKIA...)
    let mut cursor = 0;
    while let Some(offset) = sanitized[cursor..].find("AKIA") {
        let start = cursor + offset;
        let remainder = &sanitized.as_bytes()[start..];
        let is_access_key = remainder.len() >= 20
            && remainder[..20]
                .iter()
                .all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit());

        if is_access_key {
            sanitized.replace_range(start..start + 20, "AKIA****************");
            modified = true;
            cursor = start + 20;
        } else {
            cursor = start + 4;
        }
        if cursor >= sanitized.len() {
            break;
        }
    }

    // 3. Mask all database connection string passwords (postgres://user:pass@host:5432/db)
    for scheme in &["postgres://", "postgresql://", "mysql://", "redis://"] {
        let mut cursor = 0;
        while let Some(offset) = sanitized[cursor..].find(scheme) {
            let start = cursor + offset;
            let rest = &sanitized[start + scheme.len()..];
            if let Some(at_idx) = rest.find('@') {
                let auth_part = &rest[..at_idx];
                if let Some(colon_idx) = auth_part.find(':') {
                    let pass_start = start + scheme.len() + colon_idx + 1;
                    let pass_end = start + scheme.len() + at_idx;
                    if &sanitized[pass_start..pass_end] != "*****" {
                        sanitized.replace_range(pass_start..pass_end, "*****");
                        modified = true;
                        // The replacement can shorten the URL. Continue immediately after
                        // the `@` in the updated string instead of reusing its stale offset.
                        cursor = pass_start + "*****".len() + 1;
                    } else {
                        cursor = pass_end + 1;
                    }
                    if cursor >= sanitized.len() {
                        break;
                    }
                    continue;
                }
            }
            cursor = start + scheme.len();
            if cursor >= sanitized.len() {
                break;
            }
        }
    }

    if modified {
        let store = SecurityStore::global();
        store.inc_dlp_masked();

        store.push_local_event(LiveSecurityEvent::local(
            "DLP_SECRET_LEAK_PREVENTED",
            "Neutralized secret credentials/key from outgoing HTTP response",
            "unknown",
        ));
        (sanitized.into_bytes(), true)
    } else {
        (input.to_vec(), false)
    }
}

/// Tower Layer for Data Loss Prevention (DLP) response interception.
#[derive(Clone, Default)]
pub struct DlpResponseLayer;

impl<S> Layer<S> for DlpResponseLayer {
    type Service = DlpResponseService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        DlpResponseService { inner }
    }
}

/// Tower Service for DLP response interception.
#[derive(Clone)]
pub struct DlpResponseService<S> {
    inner: S,
}

impl<S> Service<Request<Body>> for DlpResponseService<S>
where
    S: Service<Request<Body>, Response = Response<Body>> + Clone + 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 {
        let clone = self.inner.clone();
        let mut inner = std::mem::replace(&mut self.inner, clone);
        let request_method = req.method().clone();

        Box::pin(async move {
            let res = inner.call(req).await?;

            if request_method == Method::HEAD
                || res.status().is_informational()
                || matches!(
                    res.status(),
                    StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED
                )
                || textual_media_type(res.headers()).is_none()
                || !has_identity_encoding(res.headers())
                || !is_safely_bufferable(res.headers(), res.body())
            {
                return Ok(res);
            }

            let (mut parts, body) = res.into_parts();
            let bytes = match axum::body::to_bytes(body, MAX_BUFFERED_RESPONSE_BYTES as usize).await
            {
                Ok(bytes) => bytes,
                Err(_) => return Ok(body_collection_failure()),
            };

            let (sanitized_bytes, was_modified) = mask_response_payload(&bytes);
            if was_modified {
                remove_stale_representation_headers(&mut parts.headers, sanitized_bytes.len());
            }

            Ok(Response::from_parts(parts, Body::from(sanitized_bytes)))
        })
    }
}

/// Alias for DlpResponseLayer
pub type DlpLayer = DlpResponseLayer;

/// Alias for DlpResponseService
pub type DlpService<S> = DlpResponseService<S>;

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

    #[test]
    fn test_mask_private_key() {
        let payload = b"Error: key was -----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA...\n-----END RSA PRIVATE KEY----- in config";
        let (masked, was_modified) = mask_response_payload(payload);
        assert!(was_modified);
        let masked_str = String::from_utf8(masked).unwrap();
        assert!(masked_str.contains("[DLP_BLOCKED_PRIVATE_KEY]"));
        assert!(!masked_str.contains("BEGIN RSA PRIVATE KEY"));
    }

    #[test]
    fn test_mask_database_url() {
        let payload =
            b"{\"db\": \"postgres://admin:super_secret_password_123@localhost:5432/app\"}";
        let (masked, was_modified) = mask_response_payload(payload);
        assert!(was_modified);
        let masked_str = String::from_utf8(masked).unwrap();
        assert!(masked_str.contains("postgres://admin:*****@localhost:5432/app"));
        assert!(!masked_str.contains("super_secret_password_123"));
    }

    #[test]
    fn test_clean_payload_untouched() {
        let payload = b"{\"message\": \"Hello Rullst!\", \"status\": 200}";
        let (masked, was_modified) = mask_response_payload(payload);
        assert!(!was_modified);
        assert_eq!(masked, payload);
    }

    #[test]
    fn invalid_utf8_and_incomplete_pem_are_not_corrupted() {
        let binary = b"\xff\xfeAKIAIOSFODNN7EXAMPLE";
        let (masked, was_modified) = mask_response_payload(binary);
        assert!(!was_modified);
        assert_eq!(masked, binary);

        let incomplete = b"prefix -----BEGIN PRIVATE KEY----- unfinished payload";
        let (masked, was_modified) = mask_response_payload(incomplete);
        assert!(!was_modified);
        assert_eq!(masked, incomplete);
    }

    #[test]
    fn test_mask_aws_access_key() {
        let payload = b"{\"aws_key\": \"AKIAIOSFODNN7EXAMPLE\"}";
        let (masked, was_modified) = mask_response_payload(payload);
        assert!(was_modified);
        let masked_str = String::from_utf8(masked).unwrap();
        assert!(masked_str.contains("AKIA****************"));
        assert!(!masked_str.contains("AKIAIOSFODNN7EXAMPLE"));
    }

    #[test]
    fn test_mask_mysql_and_redis_urls() {
        let payload = b"mysql://root:my_secret_sql_pass@127.0.0.1:3306/db and redis://default:redis_auth_token@cache:6379";
        let (masked, was_modified) = mask_response_payload(payload);
        assert!(was_modified);
        let masked_str = String::from_utf8(masked).unwrap();
        assert!(masked_str.contains("mysql://root:*****@127.0.0.1:3306/db"));
        assert!(masked_str.contains("redis://default:*****@cache:6379"));
    }

    #[test]
    fn test_mask_multiple_keys_and_dsns() {
        let payload = br#"{"keys": ["AKIA1111111111111111", "AKIA2222222222222222"], "dbs": ["postgres://u1:p1@h1:5432/d1", "postgres://u2:p2@h2:5432/d2"]}"#;
        let (masked, was_modified) = mask_response_payload(payload);
        assert!(was_modified);
        let masked_str = String::from_utf8(masked).unwrap();
        assert!(!masked_str.contains("AKIA1111111111111111"));
        assert!(!masked_str.contains("AKIA2222222222222222"));
        assert!(!masked_str.contains(":p1@"));
        assert!(!masked_str.contains(":p2@"));
        assert!(masked_str.contains("postgres://u1:*****@h1:5432/d1"));
        assert!(masked_str.contains("postgres://u2:*****@h2:5432/d2"));
    }

    #[test]
    fn database_url_masking_preserves_unicode_boundaries_after_shortening() {
        // A fuzzing reproducer placed the stale, pre-redaction cursor inside this NBSP.
        let payload = "redis://user:abcdefghijklmnopqrst@abcdefghijklmn\u{a0}z";
        let (masked, was_modified) = mask_response_payload(payload.as_bytes());

        assert!(was_modified);
        assert_eq!(
            String::from_utf8(masked).unwrap(),
            "redis://user:*****@abcdefghijklmn\u{a0}z"
        );
    }

    #[test]
    fn already_masked_database_urls_remain_stable_during_other_redactions() {
        let payload = b"AKIAIOSFODNN7EXAMPLE redis://user:*****@cache.internal:6379/session";
        let (masked, was_modified) = mask_response_payload(payload);

        assert!(was_modified);
        assert_eq!(
            String::from_utf8(masked).unwrap(),
            "AKIA**************** redis://user:*****@cache.internal:6379/session"
        );
    }

    #[tokio::test]
    async fn test_dlp_layer_middleware() {
        use axum::http::{Request, StatusCode, header};
        use axum::response::IntoResponse;
        use axum::routing::get;
        use tower::ServiceExt;

        async fn secret_handler() -> impl IntoResponse {
            let mut response = (
                StatusCode::OK,
                "Config leaked: postgres://user:secret123@db:5432/main",
            )
                .into_response();
            response
                .headers_mut()
                .insert(header::ETAG, HeaderValue::from_static("\"old-validator\""));
            response
        }

        async fn binary_handler() -> Response<Body> {
            let payload = b"\xffpostgres://user:secret123@db:5432/main".to_vec();
            let mut response = Response::new(Body::from(payload));
            response.headers_mut().insert(
                header::CONTENT_TYPE,
                HeaderValue::from_static("application/octet-stream"),
            );
            response
        }

        async fn event_stream_handler() -> Response<Body> {
            let mut response = Response::new(Body::from(
                "data: postgres://user:secret123@db:5432/main\n\n",
            ));
            response.headers_mut().insert(
                header::CONTENT_TYPE,
                HeaderValue::from_static("text/event-stream"),
            );
            response
        }

        async fn oversized_handler() -> Response<Body> {
            let mut payload = vec![b'a'; MAX_BUFFERED_RESPONSE_BYTES as usize + 1];
            payload.extend_from_slice(b"postgres://user:secret123@db:5432/main");
            let mut response = Response::new(Body::from(payload));
            response.headers_mut().insert(
                header::CONTENT_TYPE,
                HeaderValue::from_static("text/plain; charset=utf-8"),
            );
            response
        }

        let app = axum::Router::new()
            .route("/secret", get(secret_handler))
            .route("/binary", get(binary_handler))
            .route("/events", get(event_stream_handler))
            .route("/oversized", get(oversized_handler))
            .layer(DlpResponseLayer);

        let req = Request::builder()
            .uri("/secret")
            .body(Body::empty())
            .unwrap();

        let resp = app.clone().oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert!(resp.headers().get(header::ETAG).is_none());
        let declared_length = resp
            .headers()
            .get(header::CONTENT_LENGTH)
            .unwrap()
            .to_str()
            .unwrap()
            .parse::<usize>()
            .unwrap();
        let body = axum::body::to_bytes(resp.into_body(), 10000).await.unwrap();
        assert_eq!(declared_length, body.len());
        let body_str = String::from_utf8(body.to_vec()).unwrap();
        assert!(body_str.contains("postgres://user:*****@db:5432/main"));
        assert!(!body_str.contains("secret123"));

        for (path, limit) in [
            ("/binary", 10_000usize),
            ("/events", 10_000usize),
            ("/oversized", MAX_BUFFERED_RESPONSE_BYTES as usize + 10_000),
        ] {
            let response = app
                .clone()
                .oneshot(Request::builder().uri(path).body(Body::empty()).unwrap())
                .await
                .unwrap();
            assert_eq!(response.status(), StatusCode::OK);
            let bytes = axum::body::to_bytes(response.into_body(), limit)
                .await
                .unwrap();
            assert!(
                bytes
                    .windows(b"secret123".len())
                    .any(|window| window == b"secret123"),
                "{path} must bypass DLP without truncation"
            );
        }
    }
}

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

    #[kani::proof]
    fn proof_buffer_size_gate_boundaries() {
        let lower: u64 = kani::any();
        let upper: u64 = kani::any();
        let accepted = is_fixed_body_size_within_limit(lower, upper);

        assert_eq!(
            accepted,
            lower == upper && upper <= MAX_BUFFERED_RESPONSE_BYTES
        );
        if accepted {
            assert_eq!(lower, upper);
            assert!(upper <= MAX_BUFFERED_RESPONSE_BYTES);
        }
    }
}