Skip to main content

SecurityHeadersConfig

Struct SecurityHeadersConfig 

Source
pub struct SecurityHeadersConfig {
    pub x_content_type_options: Option<&'static str>,
    pub x_frame_options: Option<XFrameOptions>,
    pub x_xss_protection: Option<&'static str>,
    pub content_security_policy: Option<String>,
    pub hsts: Option<(u64, bool, bool)>,
    pub referrer_policy: Option<ReferrerPolicy>,
    pub permissions_policy: Option<String>,
}
Expand description

Configuration for the Security Headers middleware.

All headers are optional. Set a value to Some(...) to include the header, or None to skip it.

§Defaults

The default configuration provides secure defaults:

  • X-Content-Type-Options: nosniff
  • X-Frame-Options: DENY
  • X-XSS-Protection: 0 (disabled as modern browsers have built-in protection)
  • Referrer-Policy: strict-origin-when-cross-origin

§Example

use fastapi_core::middleware::{SecurityHeadersConfig, XFrameOptions, ReferrerPolicy};

let config = SecurityHeadersConfig::default()
    .x_frame_options(XFrameOptions::SameOrigin)
    .content_security_policy("default-src 'self'")
    .hsts(31536000, true);  // 1 year, includeSubDomains

Fields§

§x_content_type_options: Option<&'static str>

X-Content-Type-Options header. Default: Some("nosniff")

§x_frame_options: Option<XFrameOptions>

X-Frame-Options header. Default: Some(XFrameOptions::Deny)

§x_xss_protection: Option<&'static str>

X-XSS-Protection header. Default: Some("0") (disabled - modern browsers have built-in protection)

Note: This header is largely obsolete. Setting it to “0” is recommended to prevent potential security issues in older browsers.

§content_security_policy: Option<String>

Content-Security-Policy header. Default: None (should be configured based on your application)

§hsts: Option<(u64, bool, bool)>

Strict-Transport-Security (HSTS) header. Tuple of (max_age_seconds, include_sub_domains, preload) Default: None (only set this for HTTPS-only sites)

§referrer_policy: Option<ReferrerPolicy>

Referrer-Policy header. Default: Some(ReferrerPolicy::StrictOriginWhenCrossOrigin)

§permissions_policy: Option<String>

Permissions-Policy header (formerly Feature-Policy). Default: None (should be configured based on your application)

Implementations§

Source§

impl SecurityHeadersConfig

Source

pub fn new() -> Self

Creates a new configuration with secure defaults.

Source

pub fn none() -> Self

Creates an empty configuration (no headers).

Source

pub fn strict() -> Self

Creates a strict configuration for high-security applications.

Includes:

  • All default headers
  • HSTS with 1 year max-age and includeSubDomains
  • A basic CSP that only allows same-origin resources
Source

pub fn x_content_type_options(self, value: Option<&'static str>) -> Self

Sets the X-Content-Type-Options header.

Source

pub fn x_frame_options(self, value: Option<XFrameOptions>) -> Self

Sets the X-Frame-Options header.

Source

pub fn x_xss_protection(self, value: Option<&'static str>) -> Self

Sets the X-XSS-Protection header.

Source

pub fn content_security_policy(self, value: impl Into<String>) -> Self

Sets the Content-Security-Policy header.

Source

pub fn no_content_security_policy(self) -> Self

Clears the Content-Security-Policy header.

Source

pub fn hsts( self, max_age: u64, include_sub_domains: bool, preload: bool, ) -> Self

Sets the Strict-Transport-Security (HSTS) header.

§Arguments
  • max_age: Maximum time (in seconds) the browser should remember HTTPS
  • include_sub_domains: Whether to apply to all subdomains
  • preload: Whether to include in browser preload lists (use with caution)
§Warning

Only enable HSTS for sites that are HTTPS-only. Enabling HSTS incorrectly can make your site inaccessible.

Source

pub fn no_hsts(self) -> Self

Clears the HSTS header.

Source

pub fn referrer_policy(self, value: Option<ReferrerPolicy>) -> Self

Sets the Referrer-Policy header.

Source

pub fn permissions_policy(self, value: impl Into<String>) -> Self

Sets the Permissions-Policy header.

Source

pub fn no_permissions_policy(self) -> Self

Clears the Permissions-Policy header.

Trait Implementations§

Source§

impl Clone for SecurityHeadersConfig

Source§

fn clone(&self) -> SecurityHeadersConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SecurityHeadersConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SecurityHeadersConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ResponseProduces<T> for T