modo-rs 0.8.0

Rust web framework for small monolithic apps
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
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use axum::body::Body;
use cookie::{Cookie, CookieJar, SameSite};
use http::{HeaderValue, Request, Response};
use tower::{Layer, Service};

use crate::cookie::{CookieConfig, Key};

use super::state::{FlashEntry, FlashState};

const COOKIE_NAME: &str = "flash";
const MAX_AGE_SECS: i64 = 300;

// --- Layer ---

/// Tower [`Layer`] that enables cookie-based flash messages for a router.
///
/// On each request the layer reads the signed `flash` cookie and populates
/// the [`Flash`](crate::flash::Flash) extractor. On response it either writes a new
/// signed cookie (when messages were queued) or removes the existing one (when
/// messages were consumed via [`Flash::messages`](crate::flash::Flash::messages)).
///
/// # Cookie details
///
/// - Name: `flash`
/// - Signed with HMAC using the application [`Key`]
/// - `Max-Age`: 300 seconds (5 minutes)
/// - Path, `Secure`, `HttpOnly`, and `SameSite` attributes come from [`CookieConfig`]
pub struct FlashLayer {
    key: Key,
    config: CookieConfig,
}

impl Clone for FlashLayer {
    fn clone(&self) -> Self {
        Self {
            key: self.key.clone(),
            config: self.config.clone(),
        }
    }
}

impl FlashLayer {
    /// Create a new `FlashLayer` from a cookie configuration and signing key.
    pub fn new(config: &CookieConfig, key: &Key) -> Self {
        Self {
            key: key.clone(),
            config: config.clone(),
        }
    }
}

impl<S> Layer<S> for FlashLayer {
    type Service = FlashMiddleware<S>;

    fn layer(&self, inner: S) -> Self::Service {
        FlashMiddleware {
            inner,
            key: self.key.clone(),
            config: self.config.clone(),
        }
    }
}

// --- Service ---

/// Tower [`Service`] produced by [`FlashLayer`].
pub struct FlashMiddleware<S> {
    inner: S,
    key: Key,
    config: CookieConfig,
}

impl<S: Clone> Clone for FlashMiddleware<S> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            key: self.key.clone(),
            config: self.config.clone(),
        }
    }
}

impl<S, ReqBody> Service<Request<ReqBody>> for FlashMiddleware<S>
where
    S: Service<Request<ReqBody>, Response = Response<Body>> + Clone + Send + 'static,
    S::Future: Send + 'static,
    S::Error: Into<Box<dyn std::error::Error + Send + Sync>> + Send + 'static,
    ReqBody: 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, mut request: Request<ReqBody>) -> Self::Future {
        let key = self.key.clone();
        let config = self.config.clone();
        let mut inner = self.inner.clone();
        std::mem::swap(&mut self.inner, &mut inner);

        Box::pin(async move {
            // --- Request path ---
            let incoming = read_flash_cookie(request.headers(), &key);
            let flash_state = Arc::new(FlashState::new(incoming));
            request.extensions_mut().insert(flash_state.clone());

            // --- Run inner service ---
            let mut response = inner.call(request).await?;

            // --- Response path ---
            let outgoing = flash_state.drain_outgoing();
            let was_read = flash_state.was_read();

            if !outgoing.is_empty() {
                write_flash_cookie(&mut response, &outgoing, &config, &key);
            } else if was_read {
                remove_flash_cookie(&mut response, &config, &key);
            }

            Ok(response)
        })
    }
}

fn read_flash_cookie(headers: &http::HeaderMap, key: &Key) -> Vec<FlashEntry> {
    let Some(cookie_header) = headers.get(http::header::COOKIE) else {
        return vec![];
    };
    let Ok(cookie_str) = cookie_header.to_str() else {
        return vec![];
    };

    for pair in cookie_str.split(';') {
        let pair = pair.trim();
        if let Some((name, value)) = pair.split_once('=')
            && name.trim() == COOKIE_NAME
        {
            let mut jar = CookieJar::new();
            jar.add_original(Cookie::new(
                COOKIE_NAME.to_string(),
                value.trim().to_string(),
            ));
            if let Some(verified) = jar.signed(key).get(COOKIE_NAME)
                && let Ok(entries) = serde_json::from_str::<Vec<FlashEntry>>(verified.value())
            {
                return entries;
            }
            return vec![];
        }
    }
    vec![]
}

fn write_flash_cookie(
    response: &mut Response<Body>,
    entries: &[FlashEntry],
    config: &CookieConfig,
    key: &Key,
) {
    let Ok(json) = serde_json::to_string(entries) else {
        tracing::error!("failed to serialize flash messages");
        return;
    };

    set_cookie(response, &json, MAX_AGE_SECS, config, key);
}

fn remove_flash_cookie(response: &mut Response<Body>, config: &CookieConfig, key: &Key) {
    set_cookie(response, "", 0, config, key);
}

fn set_cookie(
    response: &mut Response<Body>,
    value: &str,
    max_age_secs: i64,
    config: &CookieConfig,
    key: &Key,
) {
    let mut jar = CookieJar::new();
    jar.signed_mut(key)
        .add(Cookie::new(COOKIE_NAME.to_string(), value.to_string()));
    let signed_value = jar
        .get(COOKIE_NAME)
        .expect("cookie was just added")
        .value()
        .to_string();

    let same_site = match config.same_site.as_str() {
        "strict" => SameSite::Strict,
        "none" => SameSite::None,
        _ => SameSite::Lax,
    };
    let set_cookie_str = Cookie::build((COOKIE_NAME.to_string(), signed_value))
        .path("/")
        .secure(config.secure)
        .http_only(config.http_only)
        .same_site(same_site)
        .max_age(cookie::time::Duration::seconds(max_age_secs))
        .build()
        .to_string();

    match HeaderValue::from_str(&set_cookie_str) {
        Ok(v) => {
            response.headers_mut().append(http::header::SET_COOKIE, v);
        }
        Err(e) => {
            tracing::error!("failed to set flash cookie header: {e}");
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::Router;
    use axum::routing::get;
    use http::StatusCode;
    use tower::ServiceExt;

    fn test_config() -> CookieConfig {
        CookieConfig {
            secret: "a".repeat(64),
            secure: false,
            http_only: true,
            same_site: "lax".into(),
        }
    }

    fn test_key(config: &CookieConfig) -> Key {
        crate::cookie::key_from_config(config).unwrap()
    }

    fn make_signed_cookie(entries: &[FlashEntry], key: &Key) -> String {
        let json = serde_json::to_string(entries).unwrap();
        let mut jar = CookieJar::new();
        jar.signed_mut(key)
            .add(Cookie::new(COOKIE_NAME.to_string(), json));
        let signed_value = jar.get(COOKIE_NAME).unwrap().value().to_string();
        format!("{COOKIE_NAME}={signed_value}")
    }

    /// Extract the flash Set-Cookie header from response (handles multiple Set-Cookie headers)
    fn extract_flash_set_cookie(resp: &Response<Body>) -> Option<String> {
        resp.headers()
            .get_all(http::header::SET_COOKIE)
            .iter()
            .find_map(|v| {
                let s = v.to_str().ok()?;
                if s.starts_with("flash=") {
                    Some(s.to_string())
                } else {
                    None
                }
            })
    }

    // --- Module-level handlers (axum Handler bounds require module-level async fn) ---

    async fn noop_handler() -> StatusCode {
        StatusCode::OK
    }

    async fn set_flash_handler(flash: crate::flash::Flash) -> StatusCode {
        flash.success("it worked");
        StatusCode::OK
    }

    async fn set_multiple_handler(flash: crate::flash::Flash) -> StatusCode {
        flash.error("bad");
        flash.warning("careful");
        StatusCode::OK
    }

    async fn mark_read_handler(req: Request<Body>) -> StatusCode {
        let state = req.extensions().get::<Arc<FlashState>>().unwrap();
        state.mark_read();
        StatusCode::OK
    }

    async fn read_and_write_handler(req: Request<Body>) -> StatusCode {
        let state = req.extensions().get::<Arc<FlashState>>().unwrap();
        state.mark_read();
        state.push("success", "new");
        StatusCode::OK
    }

    // --- Tests ---

    #[tokio::test]
    async fn no_cookie_empty_state_no_set_cookie() {
        let config = test_config();
        let key = test_key(&config);
        let app = Router::new()
            .route("/", get(noop_handler))
            .layer(FlashLayer::new(&config, &key));

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let resp = app.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert!(extract_flash_set_cookie(&resp).is_none());
    }

    #[tokio::test]
    async fn outgoing_writes_cookie() {
        let config = test_config();
        let key = test_key(&config);
        let app = Router::new()
            .route("/", get(set_flash_handler))
            .layer(FlashLayer::new(&config, &key));

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let resp = app.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let cookie_str = extract_flash_set_cookie(&resp).expect("should have Set-Cookie");
        assert!(cookie_str.contains("flash="));
        assert!(cookie_str.contains("HttpOnly"));
    }

    #[tokio::test]
    async fn valid_signed_cookie_populates_incoming() {
        let config = test_config();
        let key = test_key(&config);

        let entries = vec![FlashEntry {
            level: "success".into(),
            message: "saved".into(),
        }];
        let cookie_val = make_signed_cookie(&entries, &key);

        let app = Router::new()
            .route("/", get(noop_handler))
            .layer(FlashLayer::new(&config, &key));

        let req = Request::builder()
            .uri("/")
            .header(http::header::COOKIE, cookie_val)
            .body(Body::empty())
            .unwrap();
        let resp = app.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        // No read, no write — cookie untouched
        assert!(extract_flash_set_cookie(&resp).is_none());
    }

    #[tokio::test]
    async fn invalid_cookie_gives_empty_incoming() {
        let config = test_config();
        let key = test_key(&config);

        let app = Router::new()
            .route("/", get(noop_handler))
            .layer(FlashLayer::new(&config, &key));

        let req = Request::builder()
            .uri("/")
            .header(http::header::COOKIE, "flash=tampered_value")
            .body(Body::empty())
            .unwrap();
        let resp = app.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert!(extract_flash_set_cookie(&resp).is_none());
    }

    #[tokio::test]
    async fn read_flag_clears_cookie() {
        let config = test_config();
        let key = test_key(&config);

        let entries = vec![FlashEntry {
            level: "info".into(),
            message: "hello".into(),
        }];
        let cookie_val = make_signed_cookie(&entries, &key);

        let app = Router::new()
            .route("/", get(mark_read_handler))
            .layer(FlashLayer::new(&config, &key));

        let req = Request::builder()
            .uri("/")
            .header(http::header::COOKIE, cookie_val)
            .body(Body::empty())
            .unwrap();
        let resp = app.oneshot(req).await.unwrap();

        let cookie_str = extract_flash_set_cookie(&resp).expect("should clear cookie");
        assert!(cookie_str.contains("Max-Age=0"));
    }

    #[tokio::test]
    async fn outgoing_plus_read_writes_only_outgoing() {
        let config = test_config();
        let key = test_key(&config);

        let entries = vec![FlashEntry {
            level: "info".into(),
            message: "old".into(),
        }];
        let cookie_val = make_signed_cookie(&entries, &key);

        let app = Router::new()
            .route("/", get(read_and_write_handler))
            .layer(FlashLayer::new(&config, &key));

        let req = Request::builder()
            .uri("/")
            .header(http::header::COOKIE, cookie_val)
            .body(Body::empty())
            .unwrap();
        let resp = app.oneshot(req).await.unwrap();

        let cookie_str = extract_flash_set_cookie(&resp).expect("should have cookie");
        assert!(!cookie_str.contains("Max-Age=0"));
        assert!(cookie_str.contains("flash="));
    }

    #[tokio::test]
    async fn multiple_outgoing_messages() {
        let config = test_config();
        let key = test_key(&config);
        let app = Router::new()
            .route("/", get(set_multiple_handler))
            .layer(FlashLayer::new(&config, &key));

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let resp = app.oneshot(req).await.unwrap();

        let cookie_str = extract_flash_set_cookie(&resp).expect("should have Set-Cookie");
        assert!(cookie_str.contains("flash="));
    }

    #[tokio::test]
    async fn cookie_attributes_applied() {
        let config = CookieConfig {
            secret: "a".repeat(64),
            secure: true,
            http_only: true,
            same_site: "strict".into(),
        };
        let key = test_key(&config);
        let app = Router::new()
            .route("/", get(set_flash_handler))
            .layer(FlashLayer::new(&config, &key));

        let req = Request::builder().uri("/").body(Body::empty()).unwrap();
        let resp = app.oneshot(req).await.unwrap();

        let cookie_str = extract_flash_set_cookie(&resp).expect("should have Set-Cookie");
        assert!(cookie_str.contains("Secure"));
        assert!(cookie_str.contains("HttpOnly"));
        assert!(cookie_str.contains("SameSite=Strict"));
        assert!(cookie_str.contains("Path=/"));
    }
}