ferro-rs 0.2.3

A Laravel-inspired 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
//! Security headers middleware
//!
//! Adds OWASP-recommended security headers to all responses.
//! Provides sensible defaults that work for both Inertia.js and JSON-UI apps
//! without breaking development workflows.
//!
//! Reference: <https://owasp.org/www-project-secure-headers/>

use crate::http::{HttpResponse, Request, Response};
use crate::middleware::{Middleware, Next};
use async_trait::async_trait;

/// Middleware that adds security headers to every response.
///
/// Ships OWASP-recommended defaults out of the box. Each header can be
/// overridden or disabled via the builder API.
///
/// HSTS is **off by default** because it breaks `localhost` over HTTP.
/// Call [`with_hsts`](Self::with_hsts) to enable it in production.
///
/// # Example
///
/// ```rust,ignore
/// use ferro::SecurityHeaders;
///
/// // Use defaults (safe for development)
/// global_middleware!(SecurityHeaders::new());
///
/// // Production: enable HSTS
/// global_middleware!(SecurityHeaders::new().with_hsts());
///
/// // Custom overrides
/// global_middleware!(
///     SecurityHeaders::new()
///         .x_frame_options("SAMEORIGIN")
///         .without("Permissions-Policy")
/// );
/// ```
pub struct SecurityHeaders {
    x_content_type_options: Option<String>,
    x_frame_options: Option<String>,
    content_security_policy: Option<String>,
    referrer_policy: Option<String>,
    permissions_policy: Option<String>,
    cross_origin_opener_policy: Option<String>,
    x_xss_protection: Option<String>,
    strict_transport_security: Option<String>,
}

impl SecurityHeaders {
    /// Create with OWASP-recommended defaults.
    ///
    /// All headers except HSTS are enabled. HSTS is off by default
    /// to avoid breaking development over HTTP.
    pub fn new() -> Self {
        Self {
            x_content_type_options: Some("nosniff".to_string()),
            x_frame_options: Some("DENY".to_string()),
            content_security_policy: Some(
                "default-src 'self'; \
                 script-src 'self' 'unsafe-inline' 'unsafe-eval'; \
                 style-src 'self' 'unsafe-inline'; \
                 img-src 'self' data: blob:; \
                 font-src 'self' data:; \
                 connect-src 'self' ws: wss:; \
                 frame-ancestors 'none'"
                    .to_string(),
            ),
            referrer_policy: Some("strict-origin-when-cross-origin".to_string()),
            permissions_policy: Some("geolocation=(), camera=(), microphone=()".to_string()),
            cross_origin_opener_policy: Some("same-origin".to_string()),
            x_xss_protection: Some("0".to_string()),
            strict_transport_security: None,
        }
    }

    /// Enable HSTS with `max-age=31536000; includeSubDomains` (no preload).
    ///
    /// Safe for production use. Does not include `preload` because preload
    /// submission is permanent and affects all subdomains.
    pub fn with_hsts(mut self) -> Self {
        self.strict_transport_security = Some("max-age=31536000; includeSubDomains".to_string());
        self
    }

    /// Enable HSTS with `preload` directive.
    ///
    /// Only use this if you intend to submit your domain to the HSTS preload
    /// list. Preload is permanent — removing a domain takes months.
    pub fn with_hsts_preload(mut self) -> Self {
        self.strict_transport_security =
            Some("max-age=31536000; includeSubDomains; preload".to_string());
        self
    }

    /// Disable HSTS (same as default, for explicitness).
    pub fn without_hsts(mut self) -> Self {
        self.strict_transport_security = None;
        self
    }

    /// Override the X-Frame-Options header value.
    ///
    /// Default is `DENY`. Use `SAMEORIGIN` to allow framing from the same origin.
    pub fn x_frame_options(mut self, value: impl Into<String>) -> Self {
        self.x_frame_options = Some(value.into());
        self
    }

    /// Override the Content-Security-Policy header value.
    pub fn content_security_policy(mut self, value: impl Into<String>) -> Self {
        self.content_security_policy = Some(value.into());
        self
    }

    /// Override the Referrer-Policy header value.
    pub fn referrer_policy(mut self, value: impl Into<String>) -> Self {
        self.referrer_policy = Some(value.into());
        self
    }

    /// Override the Permissions-Policy header value.
    pub fn permissions_policy(mut self, value: impl Into<String>) -> Self {
        self.permissions_policy = Some(value.into());
        self
    }

    /// Override the Cross-Origin-Opener-Policy header value.
    pub fn cross_origin_opener_policy(mut self, value: impl Into<String>) -> Self {
        self.cross_origin_opener_policy = Some(value.into());
        self
    }

    /// Disable a specific header by name.
    ///
    /// The name is matched case-insensitively.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// SecurityHeaders::new()
    ///     .without("X-Frame-Options")
    ///     .without("Permissions-Policy");
    /// ```
    pub fn without(mut self, header_name: &str) -> Self {
        match header_name.to_ascii_lowercase().as_str() {
            "x-content-type-options" => self.x_content_type_options = None,
            "x-frame-options" => self.x_frame_options = None,
            "content-security-policy" => self.content_security_policy = None,
            "referrer-policy" => self.referrer_policy = None,
            "permissions-policy" => self.permissions_policy = None,
            "cross-origin-opener-policy" => self.cross_origin_opener_policy = None,
            "x-xss-protection" => self.x_xss_protection = None,
            "strict-transport-security" => self.strict_transport_security = None,
            _ => {}
        }
        self
    }

    /// Apply all enabled headers to a response.
    pub(crate) fn apply_headers(&self, resp: HttpResponse) -> HttpResponse {
        let mut resp = resp;
        if let Some(ref v) = self.x_content_type_options {
            resp = resp.header("X-Content-Type-Options", v.as_str());
        }
        if let Some(ref v) = self.x_frame_options {
            resp = resp.header("X-Frame-Options", v.as_str());
        }
        if let Some(ref v) = self.content_security_policy {
            resp = resp.header("Content-Security-Policy", v.as_str());
        }
        if let Some(ref v) = self.referrer_policy {
            resp = resp.header("Referrer-Policy", v.as_str());
        }
        if let Some(ref v) = self.permissions_policy {
            resp = resp.header("Permissions-Policy", v.as_str());
        }
        if let Some(ref v) = self.cross_origin_opener_policy {
            resp = resp.header("Cross-Origin-Opener-Policy", v.as_str());
        }
        if let Some(ref v) = self.x_xss_protection {
            resp = resp.header("X-XSS-Protection", v.as_str());
        }
        if let Some(ref v) = self.strict_transport_security {
            resp = resp.header("Strict-Transport-Security", v.as_str());
        }
        resp
    }
}

impl Default for SecurityHeaders {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Middleware for SecurityHeaders {
    async fn handle(&self, request: Request, next: Next) -> Response {
        let response = next(request).await;
        match response {
            Ok(resp) => Ok(self.apply_headers(resp)),
            Err(resp) => Err(self.apply_headers(resp)),
        }
    }
}

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

    #[test]
    fn test_default_headers() {
        let sh = SecurityHeaders::new();

        assert_eq!(sh.x_content_type_options.as_deref(), Some("nosniff"));
        assert_eq!(sh.x_frame_options.as_deref(), Some("DENY"));
        assert!(sh
            .content_security_policy
            .as_ref()
            .unwrap()
            .contains("default-src 'self'"));
        assert!(sh
            .content_security_policy
            .as_ref()
            .unwrap()
            .contains("frame-ancestors 'none'"));
        assert_eq!(
            sh.referrer_policy.as_deref(),
            Some("strict-origin-when-cross-origin")
        );
        assert_eq!(
            sh.permissions_policy.as_deref(),
            Some("geolocation=(), camera=(), microphone=()")
        );
        assert_eq!(
            sh.cross_origin_opener_policy.as_deref(),
            Some("same-origin")
        );
        assert_eq!(sh.x_xss_protection.as_deref(), Some("0"));
        assert!(sh.strict_transport_security.is_none());
    }

    #[test]
    fn test_with_hsts() {
        let sh = SecurityHeaders::new().with_hsts();

        let hsts = sh.strict_transport_security.as_ref().unwrap();
        assert!(hsts.contains("max-age=31536000"));
        assert!(hsts.contains("includeSubDomains"));
        assert!(!hsts.contains("preload"));
    }

    #[test]
    fn test_with_hsts_preload() {
        let sh = SecurityHeaders::new().with_hsts_preload();

        let hsts = sh.strict_transport_security.as_ref().unwrap();
        assert!(hsts.contains("max-age=31536000"));
        assert!(hsts.contains("includeSubDomains"));
        assert!(hsts.contains("preload"));
    }

    #[test]
    fn test_builder_overrides() {
        let sh = SecurityHeaders::new().x_frame_options("SAMEORIGIN");
        assert_eq!(sh.x_frame_options.as_deref(), Some("SAMEORIGIN"));

        let sh = SecurityHeaders::new().content_security_policy("default-src 'none'");
        assert_eq!(
            sh.content_security_policy.as_deref(),
            Some("default-src 'none'")
        );

        let sh = SecurityHeaders::new().referrer_policy("no-referrer");
        assert_eq!(sh.referrer_policy.as_deref(), Some("no-referrer"));

        let sh = SecurityHeaders::new().permissions_policy("camera=(self)");
        assert_eq!(sh.permissions_policy.as_deref(), Some("camera=(self)"));

        let sh = SecurityHeaders::new().cross_origin_opener_policy("unsafe-none");
        assert_eq!(
            sh.cross_origin_opener_policy.as_deref(),
            Some("unsafe-none")
        );
    }

    #[test]
    fn test_without_disables_header() {
        let sh = SecurityHeaders::new().without("X-Frame-Options");
        assert!(sh.x_frame_options.is_none());

        // Other headers remain set
        assert!(sh.x_content_type_options.is_some());
        assert!(sh.content_security_policy.is_some());
    }

    #[test]
    fn test_without_case_insensitive() {
        let sh = SecurityHeaders::new().without("x-frame-options");
        assert!(sh.x_frame_options.is_none());

        let sh = SecurityHeaders::new().without("PERMISSIONS-POLICY");
        assert!(sh.permissions_policy.is_none());
    }

    #[test]
    fn test_without_unknown_header_is_noop() {
        let sh = SecurityHeaders::new().without("X-Unknown-Header");
        // All defaults still set
        assert!(sh.x_content_type_options.is_some());
        assert!(sh.x_frame_options.is_some());
        assert!(sh.content_security_policy.is_some());
    }

    #[test]
    fn test_apply_headers() {
        let sh = SecurityHeaders::new();
        let resp = HttpResponse::text("ok");
        let resp = sh.apply_headers(resp);
        let hyper_resp = resp.into_hyper();

        assert_eq!(
            hyper_resp.headers().get("X-Content-Type-Options").unwrap(),
            "nosniff"
        );
        assert_eq!(hyper_resp.headers().get("X-Frame-Options").unwrap(), "DENY");
        assert!(hyper_resp
            .headers()
            .get("Content-Security-Policy")
            .unwrap()
            .to_str()
            .unwrap()
            .contains("default-src 'self'"));
        assert_eq!(
            hyper_resp.headers().get("Referrer-Policy").unwrap(),
            "strict-origin-when-cross-origin"
        );
        assert_eq!(
            hyper_resp.headers().get("Permissions-Policy").unwrap(),
            "geolocation=(), camera=(), microphone=()"
        );
        assert_eq!(
            hyper_resp
                .headers()
                .get("Cross-Origin-Opener-Policy")
                .unwrap(),
            "same-origin"
        );
        assert_eq!(hyper_resp.headers().get("X-XSS-Protection").unwrap(), "0");
        // HSTS should not be present by default
        assert!(hyper_resp
            .headers()
            .get("Strict-Transport-Security")
            .is_none());
    }

    #[test]
    fn test_apply_headers_with_hsts() {
        let sh = SecurityHeaders::new().with_hsts();
        let resp = HttpResponse::text("ok");
        let resp = sh.apply_headers(resp);
        let hyper_resp = resp.into_hyper();

        assert!(hyper_resp
            .headers()
            .get("Strict-Transport-Security")
            .is_some());
    }

    #[test]
    fn test_apply_headers_without_disabled() {
        let sh = SecurityHeaders::new()
            .without("X-Frame-Options")
            .without("Permissions-Policy");
        let resp = HttpResponse::text("ok");
        let resp = sh.apply_headers(resp);
        let hyper_resp = resp.into_hyper();

        assert!(hyper_resp.headers().get("X-Frame-Options").is_none());
        assert!(hyper_resp.headers().get("Permissions-Policy").is_none());
        // Others still present
        assert!(hyper_resp.headers().get("X-Content-Type-Options").is_some());
    }

    #[test]
    fn test_default_impl() {
        let from_new = SecurityHeaders::new();
        let from_default = SecurityHeaders::default();

        assert_eq!(
            from_new.x_content_type_options,
            from_default.x_content_type_options
        );
        assert_eq!(from_new.x_frame_options, from_default.x_frame_options);
        assert_eq!(
            from_new.content_security_policy,
            from_default.content_security_policy
        );
        assert_eq!(from_new.referrer_policy, from_default.referrer_policy);
        assert_eq!(from_new.permissions_policy, from_default.permissions_policy);
        assert_eq!(
            from_new.cross_origin_opener_policy,
            from_default.cross_origin_opener_policy
        );
        assert_eq!(from_new.x_xss_protection, from_default.x_xss_protection);
        assert_eq!(
            from_new.strict_transport_security,
            from_default.strict_transport_security
        );
    }
}