autumn-web 0.5.0

An opinionated, convention-over-configuration web framework for Rust
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
//! Security response headers middleware.
//!
//! Applies OWASP-recommended security headers to every HTTP response.
//! The headers are configured via [`HeadersConfig`]
//! in `autumn.toml` under `[security.headers]`.
//!
//! # Headers applied
//!
//! | Header | When | Default |
//! |--------|------|---------|
//! | `X-Frame-Options` | `x_frame_options` is non-empty | `DENY` |
//! | `X-Content-Type-Options` | `x_content_type_options` is `true` | `nosniff` |
//! | `X-XSS-Protection` | `xss_protection` is `true` | `1; mode=block` |
//! | `Strict-Transport-Security` | `strict_transport_security` is `true` | `max-age=31536000; includeSubDomains` |
//! | `Content-Security-Policy` | `content_security_policy` is non-empty | htmx-compatible same-origin policy |
//! | `Referrer-Policy` | `referrer_policy` is non-empty | `strict-origin-when-cross-origin` |
//! | `Permissions-Policy` | `permissions_policy` is non-empty | not sent |
//!
//! # Examples
//!
//! The layer is applied automatically by [`AppBuilder::run`](crate::app::AppBuilder::run).
//! For custom Axum routers:
//!
//! ```rust,no_run
//! use autumn_web::security::SecurityHeadersLayer;
//! use autumn_web::security::HeadersConfig;
//!
//! let config = HeadersConfig::default();
//! let app = axum::Router::<()>::new()
//!     .route("/", axum::routing::get(|| async { "ok" }))
//!     .layer(SecurityHeadersLayer::from_config(&config));
//! ```

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use axum::extract::{FromRequestParts, OptionalFromRequestParts};
use axum::http::{HeaderValue, Request, Response, StatusCode};
use http::header::HeaderName;
use pin_project_lite::pin_project;
use tower::{Layer, Service};

use super::config::{HeadersConfig, default_content_security_policy};

/// Placeholder token embedded in the nonce-aware CSP template.
///
/// At request time every occurrence is replaced with the generated nonce value.
const NONCE_PLACEHOLDER: &str = "AUTUMN_CSP_NONCE";

/// Per-request Content Security Policy nonce.
///
/// When `security.headers.csp_nonce.enabled = true` in `autumn.toml`, the
/// [`SecurityHeadersLayer`] generates a fresh 128-bit URL-safe-base64 nonce
/// for every request, stores it in request extensions, and injects it into
/// the `Content-Security-Policy` response header.
///
/// Extract the nonce in a handler to embed it in inline `<script>` and
/// `<style>` tags:
///
/// ```rust,ignore
/// use autumn_web::prelude::*;
/// use autumn_web::security::CspNonce;
///
/// #[get("/page")]
/// async fn page(nonce: CspNonce) -> Markup {
///     html! {
///         script nonce=(nonce.value()) { "console.log('hello')" }
///         style  nonce=(nonce.value()) { "body { margin: 0 }" }
///     }
/// }
/// ```
///
/// Use `Option<CspNonce>` when the layer may or may not be active:
///
/// ```rust,ignore
/// async fn page(nonce: Option<CspNonce>) -> Markup {
///     let nonce_attr = nonce.as_ref().map(|n| n.value()).unwrap_or("");
///     html! { script nonce=(nonce_attr) { "// ..." } }
/// }
/// ```
#[derive(Clone, Debug)]
pub struct CspNonce(String);

impl CspNonce {
    /// Returns the raw nonce string for embedding in HTML attributes.
    #[must_use]
    pub fn value(&self) -> &str {
        &self.0
    }
}

impl<S> FromRequestParts<S> for CspNonce
where
    S: Send + Sync,
{
    type Rejection = (StatusCode, &'static str);

    async fn from_request_parts(
        parts: &mut axum::http::request::Parts,
        _state: &S,
    ) -> Result<Self, Self::Rejection> {
        parts.extensions.get::<Self>().cloned().ok_or((
            StatusCode::INTERNAL_SERVER_ERROR,
            "CSP nonce not found in request extensions. Is CspNonce enabled in security.headers.csp_nonce?",
        ))
    }
}

impl<S> OptionalFromRequestParts<S> for CspNonce
where
    S: Send + Sync,
{
    type Rejection = std::convert::Infallible;

    async fn from_request_parts(
        parts: &mut axum::http::request::Parts,
        _state: &S,
    ) -> Result<Option<Self>, Self::Rejection> {
        Ok(parts.extensions.get::<Self>().cloned())
    }
}

/// Generate a cryptographically-random 128-bit URL-safe base64 nonce.
fn generate_nonce() -> String {
    use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
    let mut bytes = [0u8; 16];
    getrandom::getrandom(&mut bytes).expect("getrandom must not fail on supported platforms");
    URL_SAFE_NO_PAD.encode(bytes)
}

/// Build the nonce-aware default CSP template.
///
/// Replaces `'unsafe-inline'` in `style-src` with `'nonce-AUTUMN_CSP_NONCE'`
/// and appends `'nonce-AUTUMN_CSP_NONCE'` to `script-src`.
fn nonce_aware_default_csp() -> String {
    "default-src 'self'; \
     img-src 'self' data:; \
     style-src 'self' 'nonce-AUTUMN_CSP_NONCE'; \
     script-src 'self' 'nonce-AUTUMN_CSP_NONCE'; \
     connect-src 'self'; \
     form-action 'self'; \
     frame-ancestors 'none'; \
     base-uri 'self'"
        .to_owned()
}

/// Pre-computed header pairs to inject into every response.
///
/// Created once from [`HeadersConfig`] and shared via `Arc` across
/// all clones of [`SecurityHeadersService`].
///
/// When `csp_nonce` is enabled and the default CSP is in use, `nonce_csp_template`
/// holds the CSP string with [`NONCE_PLACEHOLDER`] tokens. The CSP header is
/// NOT in `static_pairs`; it is built per-request by replacing the placeholder
/// with the generated nonce value.
#[derive(Debug, Clone)]
struct ComputedHeaders {
    /// Headers applied to every response (everything except the dynamic CSP
    /// when nonce injection is active).
    static_pairs: Vec<(HeaderName, HeaderValue)>,
    /// CSP template containing `AUTUMN_CSP_NONCE` placeholder tokens.
    /// `None` when nonce injection is disabled or the user has set a custom CSP.
    nonce_csp_template: Option<Arc<str>>,
    csp_nonce_enabled: bool,
}

impl ComputedHeaders {
    fn from_config(config: &HeadersConfig) -> Self {
        let mut static_pairs = Vec::with_capacity(8);

        if !config.x_frame_options.is_empty()
            && let Ok(val) = HeaderValue::from_str(&config.x_frame_options)
        {
            static_pairs.push((HeaderName::from_static("x-frame-options"), val));
        }

        if config.x_content_type_options {
            static_pairs.push((
                HeaderName::from_static("x-content-type-options"),
                HeaderValue::from_static("nosniff"),
            ));
        }

        if config.xss_protection {
            static_pairs.push((
                HeaderName::from_static("x-xss-protection"),
                HeaderValue::from_static("1; mode=block"),
            ));
        }

        if config.strict_transport_security {
            let mut hsts = format!("max-age={}", config.hsts_max_age_secs);
            if config.hsts_include_subdomains {
                hsts.push_str("; includeSubDomains");
            }
            if let Ok(val) = HeaderValue::from_str(&hsts) {
                static_pairs.push((HeaderName::from_static("strict-transport-security"), val));
            }
        }

        // Determine whether to use per-request nonce injection for CSP.
        //
        // Nonce injection is active when:
        //   1. `csp_nonce.enabled = true`, AND
        //   2. The CSP string is the framework default (not user-overridden).
        //
        // Apps that set an explicit `content_security_policy` opt out automatically:
        // their custom value is used verbatim and the nonce is still generated
        // (for the extractor) but not written into the header.
        let using_default_csp = config.content_security_policy == default_content_security_policy();
        let nonce_csp_template = if config.csp_nonce.enabled && using_default_csp {
            Some(Arc::from(nonce_aware_default_csp().as_str()))
        } else {
            // Static CSP (either custom, or nonce disabled).
            if !config.content_security_policy.is_empty()
                && let Ok(val) = HeaderValue::from_str(&config.content_security_policy)
            {
                static_pairs.push((HeaderName::from_static("content-security-policy"), val));
            }
            None
        };

        if !config.referrer_policy.is_empty()
            && let Ok(val) = HeaderValue::from_str(&config.referrer_policy)
        {
            static_pairs.push((HeaderName::from_static("referrer-policy"), val));
        }

        if !config.permissions_policy.is_empty()
            && let Ok(val) = HeaderValue::from_str(&config.permissions_policy)
        {
            static_pairs.push((HeaderName::from_static("permissions-policy"), val));
        }

        Self {
            static_pairs,
            nonce_csp_template,
            csp_nonce_enabled: config.csp_nonce.enabled,
        }
    }
}

/// Tower [`Layer`] that adds security headers to every response.
///
/// Created from a [`HeadersConfig`] and applied to the Axum router.
/// The headers are pre-computed once and shared across all clones.
#[derive(Clone, Debug)]
pub struct SecurityHeadersLayer {
    headers: Arc<ComputedHeaders>,
}

impl SecurityHeadersLayer {
    /// Create a new layer from the given headers configuration.
    #[must_use]
    pub fn from_config(config: &HeadersConfig) -> Self {
        Self {
            headers: Arc::new(ComputedHeaders::from_config(config)),
        }
    }
}

impl<S> Layer<S> for SecurityHeadersLayer {
    type Service = SecurityHeadersService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        SecurityHeadersService {
            inner,
            headers: Arc::clone(&self.headers),
        }
    }
}

/// Tower [`Service`] produced by [`SecurityHeadersLayer`].
///
/// Adds pre-computed security headers to every HTTP response.
#[derive(Clone, Debug)]
pub struct SecurityHeadersService<S> {
    inner: S,
    headers: Arc<ComputedHeaders>,
}

impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for SecurityHeadersService<S>
where
    S: Service<Request<ReqBody>, Response = Response<ResBody>>,
{
    type Response = S::Response;
    type Error = S::Error;
    type Future = SecurityHeadersFuture<S::Future>;

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

    fn call(&mut self, mut req: Request<ReqBody>) -> Self::Future {
        // When nonce injection is active: generate a nonce, insert it into
        // request extensions (so handlers can extract it), and pass it along
        // to the response future where it will be substituted into the CSP.
        let nonce = if self.headers.csp_nonce_enabled {
            let n = generate_nonce();
            req.extensions_mut().insert(CspNonce(n.clone()));
            Some(n)
        } else {
            None
        };
        SecurityHeadersFuture {
            inner: self.inner.call(req),
            headers: Some(Arc::clone(&self.headers)),
            nonce,
        }
    }
}

pin_project! {
    /// Future that injects security headers into the response.
    pub struct SecurityHeadersFuture<F> {
        #[pin]
        inner: F,
        headers: Option<Arc<ComputedHeaders>>,
        nonce: Option<String>,
    }
}

impl<F, ResBody, E> Future for SecurityHeadersFuture<F>
where
    F: Future<Output = Result<Response<ResBody>, E>>,
{
    type Output = Result<Response<ResBody>, E>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();
        match this.inner.poll(cx) {
            Poll::Ready(Ok(mut response)) => {
                if let Some(computed) = this.headers.take() {
                    let resp_headers = response.headers_mut();
                    for (name, value) in &computed.static_pairs {
                        resp_headers.insert(name.clone(), value.clone());
                    }
                    // Inject the per-request nonce into the CSP template.
                    if let (Some(nonce), Some(template)) =
                        (this.nonce.take(), &computed.nonce_csp_template)
                    {
                        let csp_value = template.replace(NONCE_PLACEHOLDER, &nonce);
                        if let Ok(val) = HeaderValue::from_str(&csp_value) {
                            resp_headers
                                .insert(HeaderName::from_static("content-security-policy"), val);
                        }
                    }
                }
                Poll::Ready(Ok(response))
            }
            Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
            Poll::Pending => Poll::Pending,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::Router;
    use axum::body::Body;
    use axum::routing::get;
    use std::task::{Context, Poll};
    use tower::{Layer, Service, ServiceExt};

    #[tokio::test]
    async fn default_headers_applied() {
        let config = HeadersConfig::default();
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        assert_eq!(response.headers().get("x-frame-options").unwrap(), "DENY");
        assert_eq!(
            response.headers().get("x-content-type-options").unwrap(),
            "nosniff"
        );
        assert_eq!(
            response.headers().get("x-xss-protection").unwrap(),
            "1; mode=block"
        );
        assert_eq!(
            response.headers().get("referrer-policy").unwrap(),
            "strict-origin-when-cross-origin"
        );
        // HSTS not present by default
        assert!(
            response
                .headers()
                .get("strict-transport-security")
                .is_none()
        );
        // CSP present by default with htmx-compatible policy
        let csp = response
            .headers()
            .get("content-security-policy")
            .expect("default CSP should be emitted");
        let csp = csp.to_str().unwrap();
        assert!(csp.contains("default-src 'self'"), "csp = {csp}");
        assert!(csp.contains("script-src 'self'"), "csp = {csp}");
        assert!(csp.contains("img-src 'self' data:"), "csp = {csp}");
        assert!(!csp.contains("'unsafe-eval'"), "csp = {csp}");
    }

    #[tokio::test]
    async fn default_csp_allows_htmx_to_function() {
        // htmx and Autumn's htmx CSRF helper are served from /static/js/
        // (same origin), operate via addEventListener, and issue hx-* requests
        // to the same origin. The default CSP must allow all of that without
        // requiring inline scripts.
        let config = HeadersConfig::default();
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        let csp = response
            .headers()
            .get("content-security-policy")
            .expect("default CSP missing")
            .to_str()
            .unwrap()
            .to_owned();

        // htmx script loads from same origin
        assert!(
            csp.contains("script-src 'self'"),
            "csp must allow same-origin scripts for htmx: {csp}"
        );
        // htmx XHR/fetch requests go to same origin
        assert!(
            csp.contains("connect-src 'self'"),
            "csp must allow same-origin connects for htmx swaps: {csp}"
        );
        // No eval required for htmx standard operation
        assert!(
            !csp.contains("'unsafe-eval'"),
            "default csp should not weaken script-src with unsafe-eval: {csp}"
        );
    }

    #[tokio::test]
    async fn hsts_header_when_enabled() {
        let config = HeadersConfig {
            strict_transport_security: true,
            hsts_max_age_secs: 86400,
            hsts_include_subdomains: true,
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        assert_eq!(
            response.headers().get("strict-transport-security").unwrap(),
            "max-age=86400; includeSubDomains"
        );
    }

    #[tokio::test]
    async fn csp_header_when_configured() {
        let config = HeadersConfig {
            content_security_policy: "default-src 'self'".to_owned(),
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        assert_eq!(
            response.headers().get("content-security-policy").unwrap(),
            "default-src 'self'"
        );
    }

    #[tokio::test]
    async fn empty_x_frame_options_not_sent() {
        let config = HeadersConfig {
            x_frame_options: String::new(),
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        assert!(response.headers().get("x-frame-options").is_none());
    }

    #[tokio::test]
    async fn empty_csp_not_sent() {
        let config = HeadersConfig {
            content_security_policy: String::new(),
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        assert!(response.headers().get("content-security-policy").is_none());
    }

    #[tokio::test]
    async fn empty_referrer_policy_not_sent() {
        let config = HeadersConfig {
            referrer_policy: String::new(),
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        assert!(response.headers().get("referrer-policy").is_none());
    }

    #[tokio::test]
    async fn empty_permissions_policy_not_sent() {
        let config = HeadersConfig {
            permissions_policy: String::new(),
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        assert!(response.headers().get("permissions-policy").is_none());
    }

    #[tokio::test]
    async fn referrer_policy_when_configured() {
        let config = HeadersConfig {
            referrer_policy: "strict-origin-when-cross-origin".to_owned(),
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        assert_eq!(
            response.headers().get("referrer-policy").unwrap(),
            "strict-origin-when-cross-origin"
        );
    }

    #[tokio::test]
    async fn permissions_policy_when_configured() {
        let config = HeadersConfig {
            permissions_policy: "camera=(), microphone=()".to_owned(),
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        assert_eq!(
            response.headers().get("permissions-policy").unwrap(),
            "camera=(), microphone=()"
        );
    }

    #[tokio::test]
    async fn hsts_without_subdomains() {
        let config = HeadersConfig {
            strict_transport_security: true,
            hsts_max_age_secs: 3600,
            hsts_include_subdomains: false,
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        assert_eq!(
            response.headers().get("strict-transport-security").unwrap(),
            "max-age=3600"
        );
    }

    #[derive(Clone)]
    struct PendingService;

    impl<ReqBody> Service<Request<ReqBody>> for PendingService {
        type Response = axum::response::Response;
        type Error = std::convert::Infallible;
        type Future = std::future::Ready<Result<Self::Response, Self::Error>>;

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

        fn call(&mut self, _req: Request<ReqBody>) -> Self::Future {
            unreachable!("poll_ready should block this")
        }
    }

    #[test]
    fn layer_poll_ready_passes_through() {
        let layer = SecurityHeadersLayer::from_config(&HeadersConfig::default());
        let mut service = layer.layer(PendingService);

        let mut cx = Context::from_waker(futures::task::noop_waker_ref());
        let poll = Service::<Request<axum::body::Body>>::poll_ready(&mut service, &mut cx);

        assert!(
            poll.is_pending(),
            "poll_ready should pass through Pending from inner service"
        );
    }

    // ── CSP nonce tests (RED phase) ───────────────────────────────────────────

    use super::super::config::CspNonceConfig;

    #[tokio::test]
    async fn nonce_injected_into_csp_script_src_when_enabled() {
        let config = HeadersConfig {
            csp_nonce: CspNonceConfig { enabled: true },
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        let csp = response
            .headers()
            .get("content-security-policy")
            .expect("CSP header must be present")
            .to_str()
            .unwrap();

        assert!(
            csp.contains("script-src 'self' 'nonce-"),
            "script-src must contain nonce directive: {csp}"
        );
    }

    #[tokio::test]
    async fn nonce_injected_into_csp_style_src_when_enabled() {
        let config = HeadersConfig {
            csp_nonce: CspNonceConfig { enabled: true },
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        let csp = response
            .headers()
            .get("content-security-policy")
            .expect("CSP header must be present")
            .to_str()
            .unwrap();

        assert!(
            csp.contains("style-src 'self' 'nonce-"),
            "style-src must contain nonce directive: {csp}"
        );
    }

    #[tokio::test]
    async fn nonce_csp_removes_unsafe_inline_from_style_src() {
        let config = HeadersConfig {
            csp_nonce: CspNonceConfig { enabled: true },
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        let csp = response
            .headers()
            .get("content-security-policy")
            .unwrap()
            .to_str()
            .unwrap();

        assert!(
            !csp.contains("'unsafe-inline'"),
            "CSP with nonce must not contain 'unsafe-inline': {csp}"
        );
    }

    #[tokio::test]
    async fn nonce_value_differs_between_requests() {
        let config = HeadersConfig {
            csp_nonce: CspNonceConfig { enabled: true },
            ..Default::default()
        };
        let layer = SecurityHeadersLayer::from_config(&config);

        let make_app = || {
            Router::new()
                .route("/", get(|| async { "ok" }))
                .layer(layer.clone())
        };

        let r1 = make_app()
            .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
            .await
            .unwrap();
        let r2 = make_app()
            .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
            .await
            .unwrap();

        let csp1 = r1
            .headers()
            .get("content-security-policy")
            .unwrap()
            .to_str()
            .unwrap()
            .to_owned();
        let csp2 = r2
            .headers()
            .get("content-security-policy")
            .unwrap()
            .to_str()
            .unwrap()
            .to_owned();

        assert_ne!(csp1, csp2, "Each request must receive a unique nonce");
    }

    #[tokio::test]
    async fn explicit_csp_not_modified_when_nonce_enabled() {
        let config = HeadersConfig {
            content_security_policy: "default-src 'none'".to_owned(),
            csp_nonce: CspNonceConfig { enabled: true },
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        assert_eq!(
            response
                .headers()
                .get("content-security-policy")
                .unwrap()
                .to_str()
                .unwrap(),
            "default-src 'none'",
            "Explicit CSP string must be used verbatim (no nonce injection)"
        );
    }

    #[tokio::test]
    async fn csp_nonce_extractor_returns_nonempty_value() {
        async fn handler(nonce: CspNonce) -> String {
            nonce.value().to_owned()
        }

        let config = HeadersConfig {
            csp_nonce: CspNonceConfig { enabled: true },
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(handler))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        assert_eq!(response.status(), StatusCode::OK);
        let (_, body) = response.into_parts();
        let body_bytes = axum::body::to_bytes(body, usize::MAX).await.unwrap();
        let nonce_value = std::str::from_utf8(&body_bytes).unwrap();

        assert!(!nonce_value.is_empty(), "Extracted nonce must be non-empty");
    }

    #[tokio::test]
    async fn csp_nonce_extractor_value_matches_csp_header_nonce() {
        async fn handler(nonce: CspNonce) -> String {
            nonce.value().to_owned()
        }

        let config = HeadersConfig {
            csp_nonce: CspNonceConfig { enabled: true },
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(handler))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        let (parts, body) = response.into_parts();
        let csp = parts
            .headers
            .get("content-security-policy")
            .expect("CSP header missing")
            .to_str()
            .unwrap()
            .to_owned();

        let body_bytes = axum::body::to_bytes(body, usize::MAX).await.unwrap();
        let nonce_value = std::str::from_utf8(&body_bytes).unwrap();

        assert!(
            csp.contains(nonce_value),
            "CSP header must contain the same nonce as the extractor: nonce={nonce_value}, csp={csp}"
        );
    }

    #[tokio::test]
    async fn csp_nonce_is_128_bit_url_safe_base64() {
        use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};

        async fn handler(nonce: CspNonce) -> String {
            nonce.value().to_owned()
        }

        let config = HeadersConfig {
            csp_nonce: CspNonceConfig { enabled: true },
            ..Default::default()
        };
        let app = Router::new()
            .route("/", get(handler))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        let (_, body) = response.into_parts();
        let body_bytes = axum::body::to_bytes(body, usize::MAX).await.unwrap();
        let nonce_value = std::str::from_utf8(&body_bytes).unwrap();
        let decoded = URL_SAFE_NO_PAD
            .decode(nonce_value)
            .unwrap_or_else(|_| panic!("nonce must be URL-safe base64, got: {nonce_value}"));

        assert!(
            decoded.len() >= 16,
            "nonce must be ≥128 bits (16 bytes), got {} bytes",
            decoded.len()
        );
    }

    #[tokio::test]
    async fn optional_nonce_extractor_none_when_disabled() {
        async fn handler(nonce: Option<CspNonce>) -> String {
            nonce.map(|n| n.value().to_owned()).unwrap_or_default()
        }

        let config = HeadersConfig::default(); // csp_nonce.enabled = false
        let app = Router::new()
            .route("/", get(handler))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        let (_, body) = response.into_parts();
        let body_bytes = axum::body::to_bytes(body, usize::MAX).await.unwrap();
        let value = std::str::from_utf8(&body_bytes).unwrap();

        assert!(
            value.is_empty(),
            "Optional nonce extractor must return None when nonce is disabled"
        );
    }

    #[tokio::test]
    async fn nonce_disabled_uses_static_csp_with_unsafe_inline() {
        let config = HeadersConfig::default(); // csp_nonce disabled

        let app = Router::new()
            .route("/", get(|| async { "ok" }))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        let csp = response
            .headers()
            .get("content-security-policy")
            .expect("CSP header must be present when disabled")
            .to_str()
            .unwrap();

        assert!(
            csp.contains("'unsafe-inline'"),
            "Default CSP without nonce should still have unsafe-inline in style-src: {csp}"
        );
        assert!(
            !csp.contains("'nonce-"),
            "Default CSP without nonce must not contain nonce directive: {csp}"
        );
    }

    #[tokio::test]
    async fn custom_csp_with_nonce_enabled_generates_nonce_for_extractor() {
        use super::super::config::CspNonceConfig;

        async fn handler(nonce: CspNonce) -> String {
            nonce.value().to_owned()
        }

        let config = HeadersConfig {
            content_security_policy: "default-src 'self'; script-src 'self'".to_string(),
            csp_nonce: CspNonceConfig { enabled: true },
            ..Default::default()
        };

        let app = Router::new()
            .route("/", get(handler))
            .layer(SecurityHeadersLayer::from_config(&config));

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

        // 1. Verify custom CSP header is sent unmodified
        let csp = response
            .headers()
            .get("content-security-policy")
            .expect("CSP header must be present")
            .to_str()
            .unwrap();
        assert_eq!(csp, "default-src 'self'; script-src 'self'");

        // 2. Verify CspNonce extractor gets a valid nonce
        let (_, body) = response.into_parts();
        let body_bytes = axum::body::to_bytes(body, usize::MAX).await.unwrap();
        let nonce_val = std::str::from_utf8(&body_bytes).unwrap();

        assert!(!nonce_val.is_empty(), "Nonce must not be empty");
        assert_eq!(
            nonce_val.len(),
            22,
            "Nonce must be a 22-character base64 string"
        );
    }
}