SecurityHeadersBuilder

Struct SecurityHeadersBuilder 

Source
pub struct SecurityHeadersBuilder { /* private fields */ }
Expand description

Builder for SecurityHeaders.

Provides a fluent interface for configuring security headers.

Implementations§

Source§

impl SecurityHeadersBuilder

Source

pub fn content_security_policy(self, policy: ContentSecurityPolicy) -> Self

Sets the Content-Security-Policy.

§Examples
use http_security_headers::{SecurityHeaders, ContentSecurityPolicy};

let csp = ContentSecurityPolicy::new()
    .default_src(vec!["'self'"])
    .script_src(vec!["'self'", "'unsafe-inline'"]);

let headers = SecurityHeaders::builder()
    .content_security_policy(csp)
    .build()
    .unwrap();
Source

pub fn strict_transport_security( self, max_age: Duration, include_subdomains: bool, preload: bool, ) -> Self

Sets the Strict-Transport-Security header.

§Arguments
  • max_age - Duration for the max-age directive
  • include_subdomains - Whether to include the includeSubDomains directive
  • preload - Whether to include the preload directive
§Examples
use http_security_headers::SecurityHeaders;
use std::time::Duration;

let headers = SecurityHeaders::builder()
    .strict_transport_security(Duration::from_secs(31536000), true, false)
    .build()
    .unwrap();
Source

pub fn strict_transport_security_policy( self, policy: StrictTransportSecurity, ) -> Self

Sets the Strict-Transport-Security header with a custom policy.

Source

pub fn x_frame_options_deny(self) -> Self

Sets X-Frame-Options to DENY.

§Examples
use http_security_headers::SecurityHeaders;

let headers = SecurityHeaders::builder()
    .x_frame_options_deny()
    .build()
    .unwrap();
Source

pub fn x_frame_options_sameorigin(self) -> Self

Sets X-Frame-Options to SAMEORIGIN.

§Examples
use http_security_headers::SecurityHeaders;

let headers = SecurityHeaders::builder()
    .x_frame_options_sameorigin()
    .build()
    .unwrap();
Source

pub fn x_frame_options(self, policy: XFrameOptions) -> Self

Sets the X-Frame-Options header with a custom value.

Source

pub fn x_content_type_options_nosniff(self) -> Self

Enables X-Content-Type-Options: nosniff.

This is enabled by default in preset configurations.

§Examples
use http_security_headers::SecurityHeaders;

let headers = SecurityHeaders::builder()
    .x_content_type_options_nosniff()
    .build()
    .unwrap();
Source

pub fn referrer_policy(self, policy: ReferrerPolicy) -> Self

Sets the Referrer-Policy header.

Source

pub fn referrer_policy_no_referrer(self) -> Self

Sets Referrer-Policy to no-referrer.

Source

pub fn referrer_policy_strict_origin_when_cross_origin(self) -> Self

Sets Referrer-Policy to strict-origin-when-cross-origin.

Source

pub fn cross_origin_opener_policy(self, policy: CrossOriginOpenerPolicy) -> Self

Sets the Cross-Origin-Opener-Policy header.

Source

pub fn cross_origin_embedder_policy( self, policy: CrossOriginEmbedderPolicy, ) -> Self

Sets the Cross-Origin-Embedder-Policy header.

Source

pub fn cross_origin_resource_policy( self, policy: CrossOriginResourcePolicy, ) -> Self

Sets the Cross-Origin-Resource-Policy header.

Source

pub fn build(self) -> Result<SecurityHeaders>

Builds the SecurityHeaders configuration.

§Errors

Returns an error if the configuration is invalid.

Trait Implementations§

Source§

impl Debug for SecurityHeadersBuilder

Source§

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

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

impl Default for SecurityHeadersBuilder

Source§

fn default() -> SecurityHeadersBuilder

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.