acton-htmx 1.0.0-beta.7

Opinionated Rust web framework for HTMX applications
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
//! Security headers middleware
//!
//! Automatically adds security-related HTTP headers to all responses:
//! - X-Frame-Options: Prevent clickjacking
//! - X-Content-Type-Options: Prevent MIME sniffing
//! - X-XSS-Protection: Enable browser XSS protection
//! - Strict-Transport-Security: Enforce HTTPS
//! - Content-Security-Policy: Control resource loading
//! - Referrer-Policy: Control referrer information
//!
//! # Example
//!
//! ```rust,no_run
//! # use acton_htmx::middleware::{SecurityHeadersConfig, SecurityHeadersLayer};
//! # use axum::Router;
//! # #[tokio::main]
//! # async fn main() {
//! let config = SecurityHeadersConfig::strict();
//! let app: Router<()> = Router::new()
//!     .layer(SecurityHeadersLayer::new(config));
//! # }
//! ```

use axum::{
    body::Body,
    http::{header, Request, Response},
    middleware::Next,
    response::IntoResponse,
};
use std::fmt;

/// Configuration for security headers middleware
///
/// Provides preset configurations for different security levels:
/// - `strict()`: Maximum security for production
/// - `development()`: Relaxed security for local development
/// - `custom()`: Full control over each header
#[derive(Debug, Clone)]
pub struct SecurityHeadersConfig {
    /// X-Frame-Options header
    /// - DENY: Prevent all framing
    /// - SAMEORIGIN: Allow framing from same origin
    /// - None: Disable header
    pub frame_options: Option<FrameOptions>,

    /// X-Content-Type-Options header
    /// - true: Set to "nosniff"
    /// - false: Disable header
    pub content_type_options: bool,

    /// X-XSS-Protection header
    /// - Some(true): Enable with mode=block
    /// - Some(false): Enable without mode=block
    /// - None: Disable header (modern browsers use CSP instead)
    pub xss_protection: Option<bool>,

    /// Strict-Transport-Security header
    /// - Some(duration): Enable HSTS with max-age in seconds
    /// - None: Disable header (use for development/HTTP)
    pub hsts: Option<HstsConfig>,

    /// Content-Security-Policy header
    /// - Some(policy): Set CSP policy
    /// - None: Disable header
    pub csp: Option<String>,

    /// Referrer-Policy header
    /// - Some(policy): Set referrer policy
    /// - None: Disable header
    pub referrer_policy: Option<ReferrerPolicy>,
}

/// Frame options for X-Frame-Options header
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameOptions {
    /// Prevent all framing (DENY)
    Deny,
    /// Allow framing from same origin (SAMEORIGIN)
    SameOrigin,
}

impl fmt::Display for FrameOptions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Deny => write!(f, "DENY"),
            Self::SameOrigin => write!(f, "SAMEORIGIN"),
        }
    }
}

/// HSTS configuration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HstsConfig {
    /// Max age in seconds (typically 31536000 = 1 year)
    pub max_age: u32,
    /// Include subdomains
    pub include_subdomains: bool,
    /// Include in browser preload list
    pub preload: bool,
}

impl HstsConfig {
    /// Strict HSTS for production (1 year, subdomains, preload)
    #[must_use]
    pub const fn strict() -> Self {
        Self {
            max_age: 31_536_000, // 1 year
            include_subdomains: true,
            preload: true,
        }
    }

    /// Moderate HSTS (1 year, no subdomains, no preload)
    #[must_use]
    pub const fn moderate() -> Self {
        Self {
            max_age: 31_536_000, // 1 year
            include_subdomains: false,
            preload: false,
        }
    }
}

impl fmt::Display for HstsConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "max-age={}", self.max_age)?;
        if self.include_subdomains {
            write!(f, "; includeSubDomains")?;
        }
        if self.preload {
            write!(f, "; preload")?;
        }
        Ok(())
    }
}

/// Referrer policy options
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReferrerPolicy {
    /// No referrer information
    NoReferrer,
    /// No referrer on downgrade (HTTPS -> HTTP)
    NoReferrerWhenDowngrade,
    /// Only origin (no path)
    Origin,
    /// Origin on cross-origin, full URL on same-origin
    OriginWhenCrossOrigin,
    /// Same origin only
    SameOrigin,
    /// Full URL on same origin, origin on cross-origin
    StrictOrigin,
    /// Strict origin on downgrade
    StrictOriginWhenCrossOrigin,
    /// Always send full URL
    UnsafeUrl,
}

impl fmt::Display for ReferrerPolicy {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NoReferrer => write!(f, "no-referrer"),
            Self::NoReferrerWhenDowngrade => write!(f, "no-referrer-when-downgrade"),
            Self::Origin => write!(f, "origin"),
            Self::OriginWhenCrossOrigin => write!(f, "origin-when-cross-origin"),
            Self::SameOrigin => write!(f, "same-origin"),
            Self::StrictOrigin => write!(f, "strict-origin"),
            Self::StrictOriginWhenCrossOrigin => write!(f, "strict-origin-when-cross-origin"),
            Self::UnsafeUrl => write!(f, "unsafe-url"),
        }
    }
}

impl SecurityHeadersConfig {
    /// Strict security configuration for production
    ///
    /// - X-Frame-Options: DENY
    /// - X-Content-Type-Options: nosniff
    /// - X-XSS-Protection: 1; mode=block
    /// - Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
    /// - Content-Security-Policy: default-src 'self'
    /// - Referrer-Policy: strict-origin-when-cross-origin
    #[must_use]
    pub fn strict() -> Self {
        Self {
            frame_options: Some(FrameOptions::Deny),
            content_type_options: true,
            xss_protection: Some(true),
            hsts: Some(HstsConfig::strict()),
            csp: Some("default-src 'self'".to_string()),
            referrer_policy: Some(ReferrerPolicy::StrictOriginWhenCrossOrigin),
        }
    }

    /// Relaxed security configuration for development
    ///
    /// - X-Frame-Options: SAMEORIGIN
    /// - X-Content-Type-Options: nosniff
    /// - X-XSS-Protection: Disabled (rely on CSP)
    /// - Strict-Transport-Security: Disabled (HTTP in dev)
    /// - Content-Security-Policy: Permissive for development
    /// - Referrer-Policy: strict-origin-when-cross-origin
    #[must_use]
    pub fn development() -> Self {
        Self {
            frame_options: Some(FrameOptions::SameOrigin),
            content_type_options: true,
            xss_protection: None, // Modern browsers use CSP
            hsts: None,           // No HTTPS in development
            csp: Some(
                "default-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' data:"
                    .to_string(),
            ),
            referrer_policy: Some(ReferrerPolicy::StrictOriginWhenCrossOrigin),
        }
    }

    /// Custom security configuration
    ///
    /// Start with all headers disabled, then enable as needed
    #[must_use]
    pub const fn custom() -> Self {
        Self {
            frame_options: None,
            content_type_options: false,
            xss_protection: None,
            hsts: None,
            csp: None,
            referrer_policy: None,
        }
    }

    /// Enable X-Frame-Options header
    #[must_use]
    pub const fn with_frame_options(mut self, options: FrameOptions) -> Self {
        self.frame_options = Some(options);
        self
    }

    /// Enable X-Content-Type-Options: nosniff
    #[must_use]
    pub const fn with_content_type_options(mut self) -> Self {
        self.content_type_options = true;
        self
    }

    /// Enable X-XSS-Protection header
    #[must_use]
    pub const fn with_xss_protection(mut self, block_mode: bool) -> Self {
        self.xss_protection = Some(block_mode);
        self
    }

    /// Enable Strict-Transport-Security header
    #[must_use]
    pub const fn with_hsts(mut self, config: HstsConfig) -> Self {
        self.hsts = Some(config);
        self
    }

    /// Enable Content-Security-Policy header
    #[must_use]
    pub fn with_csp(mut self, policy: String) -> Self {
        self.csp = Some(policy);
        self
    }

    /// Enable Referrer-Policy header
    #[must_use]
    pub const fn with_referrer_policy(mut self, policy: ReferrerPolicy) -> Self {
        self.referrer_policy = Some(policy);
        self
    }
}

/// Security headers middleware layer
///
/// Creates a tower layer that adds security headers to all responses.
///
/// # Example
///
/// ```rust,no_run
/// # use acton_htmx::middleware::{SecurityHeadersConfig, SecurityHeadersLayer};
/// # use axum::Router;
/// # #[tokio::main]
/// # async fn main() {
/// let config = SecurityHeadersConfig::strict();
/// let app: Router<()> = Router::new()
///     .layer(SecurityHeadersLayer::new(config));
/// # }
/// ```
#[derive(Clone)]
pub struct SecurityHeadersLayer {
    config: SecurityHeadersConfig,
}

impl SecurityHeadersLayer {
    /// Create a new security headers layer with the given configuration
    #[must_use]
    pub const fn new(config: SecurityHeadersConfig) -> Self {
        Self { config }
    }
}

impl<S> tower::Layer<S> for SecurityHeadersLayer {
    type Service = SecurityHeadersMiddleware<S>;

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

/// Security headers middleware service
#[derive(Clone)]
pub struct SecurityHeadersMiddleware<S> {
    inner: S,
    config: SecurityHeadersConfig,
}

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

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

    fn call(&mut self, request: Request<Body>) -> Self::Future {
        let config = self.config.clone();
        let future = self.inner.call(request);

        Box::pin(async move {
            let mut response = future.await?;
            add_security_headers(&mut response, &config);
            Ok(response)
        })
    }
}

/// Add security headers to a response
fn add_security_headers(response: &mut Response<Body>, config: &SecurityHeadersConfig) {
    let headers = response.headers_mut();

    // X-Frame-Options
    if let Some(frame_options) = &config.frame_options {
        headers.insert("x-frame-options", frame_options.to_string().parse().unwrap());
    }

    // X-Content-Type-Options
    if config.content_type_options {
        headers.insert(
            "x-content-type-options",
            "nosniff".parse().unwrap(),
        );
    }

    // X-XSS-Protection
    if let Some(block_mode) = config.xss_protection {
        let value = if block_mode {
            "1; mode=block"
        } else {
            "1"
        };
        headers.insert("x-xss-protection", value.parse().unwrap());
    }

    // Strict-Transport-Security
    if let Some(hsts) = &config.hsts {
        headers.insert(
            header::STRICT_TRANSPORT_SECURITY,
            hsts.to_string().parse().unwrap(),
        );
    }

    // Content-Security-Policy
    if let Some(csp) = &config.csp {
        headers.insert(
            header::CONTENT_SECURITY_POLICY,
            csp.parse().unwrap(),
        );
    }

    // Referrer-Policy
    if let Some(referrer_policy) = &config.referrer_policy {
        headers.insert(
            header::REFERER,
            referrer_policy.to_string().parse().unwrap(),
        );
    }
}

/// Axum middleware function for security headers
///
/// Alternative to using the layer directly.
///
/// # Example
///
/// ```rust,no_run
/// # use acton_htmx::middleware::security_headers::{SecurityHeadersConfig, security_headers};
/// # use axum::{Router, middleware};
/// # #[tokio::main]
/// # async fn main() {
/// let config = SecurityHeadersConfig::strict();
/// let app: Router<()> = Router::new()
///     .layer(middleware::from_fn(move |req, next| {
///         security_headers(req, next, config.clone())
///     }));
/// # }
/// ```
pub async fn security_headers(
    request: Request<Body>,
    next: Next,
    config: SecurityHeadersConfig,
) -> impl IntoResponse {
    let mut response = next.run(request).await;
    add_security_headers(&mut response, &config);
    response
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::{
        body::Body,
        http::{Request, StatusCode},
        response::IntoResponse,
        routing::get,
        Router,
    };
    use tower::ServiceExt;

    async fn test_handler() -> impl IntoResponse {
        (StatusCode::OK, "Hello, World!")
    }

    #[tokio::test]
    async fn test_strict_config_headers() {
        let config = SecurityHeadersConfig::strict();
        let app = Router::new()
            .route("/", get(test_handler))
            .layer(SecurityHeadersLayer::new(config));

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

        let response = app.oneshot(request).await.unwrap();

        let headers = response.headers();
        assert_eq!(headers.get("x-frame-options").unwrap(), "DENY");
        assert_eq!(headers.get("x-content-type-options").unwrap(), "nosniff");
        assert_eq!(headers.get("x-xss-protection").unwrap(), "1; mode=block");
        assert!(headers.contains_key("strict-transport-security"));
        assert!(headers.contains_key("content-security-policy"));
    }

    #[tokio::test]
    async fn test_development_config_headers() {
        let config = SecurityHeadersConfig::development();
        let app = Router::new()
            .route("/", get(test_handler))
            .layer(SecurityHeadersLayer::new(config));

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

        let response = app.oneshot(request).await.unwrap();

        let headers = response.headers();
        assert_eq!(headers.get("x-frame-options").unwrap(), "SAMEORIGIN");
        assert_eq!(headers.get("x-content-type-options").unwrap(), "nosniff");
        assert!(!headers.contains_key("x-xss-protection"));
        assert!(!headers.contains_key("strict-transport-security"));
        assert!(headers.contains_key("content-security-policy"));
    }

    #[tokio::test]
    async fn test_custom_config() {
        let config = SecurityHeadersConfig::custom()
            .with_frame_options(FrameOptions::SameOrigin)
            .with_content_type_options()
            .with_referrer_policy(ReferrerPolicy::NoReferrer);

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

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

        let response = app.oneshot(request).await.unwrap();

        let headers = response.headers();
        assert_eq!(headers.get("x-frame-options").unwrap(), "SAMEORIGIN");
        assert_eq!(headers.get("x-content-type-options").unwrap(), "nosniff");
        assert!(!headers.contains_key("x-xss-protection"));
        assert!(!headers.contains_key("strict-transport-security"));
        assert!(!headers.contains_key("content-security-policy"));
    }

    #[test]
    fn test_hsts_config_display() {
        let hsts = HstsConfig::strict();
        assert_eq!(
            hsts.to_string(),
            "max-age=31536000; includeSubDomains; preload"
        );

        let hsts = HstsConfig::moderate();
        assert_eq!(hsts.to_string(), "max-age=31536000");
    }

    #[test]
    fn test_frame_options_display() {
        assert_eq!(FrameOptions::Deny.to_string(), "DENY");
        assert_eq!(FrameOptions::SameOrigin.to_string(), "SAMEORIGIN");
    }

    #[test]
    fn test_referrer_policy_display() {
        assert_eq!(ReferrerPolicy::NoReferrer.to_string(), "no-referrer");
        assert_eq!(
            ReferrerPolicy::StrictOriginWhenCrossOrigin.to_string(),
            "strict-origin-when-cross-origin"
        );
    }

    #[test]
    fn test_config_builder() {
        let config = SecurityHeadersConfig::custom()
            .with_frame_options(FrameOptions::Deny)
            .with_content_type_options()
            .with_xss_protection(true)
            .with_hsts(HstsConfig::strict())
            .with_csp("default-src 'self'".to_string())
            .with_referrer_policy(ReferrerPolicy::StrictOriginWhenCrossOrigin);

        assert_eq!(config.frame_options, Some(FrameOptions::Deny));
        assert!(config.content_type_options);
        assert_eq!(config.xss_protection, Some(true));
        assert!(config.hsts.is_some());
        assert!(config.csp.is_some());
        assert_eq!(
            config.referrer_policy,
            Some(ReferrerPolicy::StrictOriginWhenCrossOrigin)
        );
    }
}