pub struct SecurityHeaders { /* private fields */ }Expand description
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 to enable it in production.
§Example
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")
);Implementations§
Source§impl SecurityHeaders
impl SecurityHeaders
Sourcepub fn new() -> Self
pub fn new() -> Self
Create with OWASP-recommended defaults.
All headers except HSTS are enabled. HSTS is off by default to avoid breaking development over HTTP.
Sourcepub fn with_hsts(self) -> Self
pub fn with_hsts(self) -> Self
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.
Sourcepub fn with_hsts_preload(self) -> Self
pub fn with_hsts_preload(self) -> 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.
Sourcepub fn without_hsts(self) -> Self
pub fn without_hsts(self) -> Self
Disable HSTS (same as default, for explicitness).
Sourcepub fn x_frame_options(self, value: impl Into<String>) -> Self
pub fn x_frame_options(self, value: impl Into<String>) -> Self
Override the X-Frame-Options header value.
Default is DENY. Use SAMEORIGIN to allow framing from the same origin.
Sourcepub fn content_security_policy(self, value: impl Into<String>) -> Self
pub fn content_security_policy(self, value: impl Into<String>) -> Self
Override the Content-Security-Policy header value.
Sourcepub fn referrer_policy(self, value: impl Into<String>) -> Self
pub fn referrer_policy(self, value: impl Into<String>) -> Self
Override the Referrer-Policy header value.
Sourcepub fn permissions_policy(self, value: impl Into<String>) -> Self
pub fn permissions_policy(self, value: impl Into<String>) -> Self
Override the Permissions-Policy header value.
Sourcepub fn cross_origin_opener_policy(self, value: impl Into<String>) -> Self
pub fn cross_origin_opener_policy(self, value: impl Into<String>) -> Self
Override the Cross-Origin-Opener-Policy header value.
Trait Implementations§
Source§impl Default for SecurityHeaders
impl Default for SecurityHeaders
Auto Trait Implementations§
impl Freeze for SecurityHeaders
impl RefUnwindSafe for SecurityHeaders
impl Send for SecurityHeaders
impl Sync for SecurityHeaders
impl Unpin for SecurityHeaders
impl UnsafeUnpin for SecurityHeaders
impl UnwindSafe for SecurityHeaders
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more