logo
pub struct Shield { /* private fields */ }
Expand description

A Fairing that injects browser security and privacy headers into all outgoing responses.

Usage

To use Shield, first construct an instance of it. To use the default set of headers, construct with Shield::default(). For an instance with no preset headers, use Shield::new(). To enable an additional header, use enable(), and to disable a header, use disable():

use rocket::shield::Shield;
use rocket::shield::{XssFilter, ExpectCt};

// A `Shield` with the default headers:
let shield = Shield::default();

// A `Shield` with the default headers minus `XssFilter`:
let shield = Shield::default().disable::<XssFilter>();

// A `Shield` with the default headers plus `ExpectCt`.
let shield = Shield::default().enable(ExpectCt::default());

// A `Shield` with only `XssFilter` and `ExpectCt`.
let shield = Shield::default()
    .enable(XssFilter::default())
    .enable(ExpectCt::default());

Then, attach the instance of Shield to your application’s instance of Rocket:

rocket::build()
    // ...
    .attach(shield)

The fairing will inject all enabled headers into all outgoing responses unless the response already contains a header with the same name. If it does contain the header, a warning is emitted, and the header is not overwritten.

TLS and HSTS

If TLS is configured and enabled when the application is launched in a non-debug profile, HSTS is automatically enabled with its default policy and a warning is logged.

To get rid of this warning, explicitly Shield::enable() an Hsts policy.

Implementations

Returns an instance of Shield with no headers enabled.

Example
use rocket::shield::Shield;

let shield = Shield::new();

Enables the policy header policy.

If the poliicy was previously enabled, the configuration is replaced with that of policy.

Example
use rocket::shield::Shield;
use rocket::shield::NoSniff;

let shield = Shield::new().enable(NoSniff::default());

Disables the policy header policy.

Example
use rocket::shield::Shield;
use rocket::shield::NoSniff;

let shield = Shield::default().disable::<NoSniff>();

Returns true if the policy P is enabled.

Example
use rocket::shield::Shield;
use rocket::shield::{Permission, NoSniff, Frame};
use rocket::shield::{Prefetch, ExpectCt, Referrer};

let shield = Shield::default();

assert!(shield.is_enabled::<NoSniff>());
assert!(shield.is_enabled::<Frame>());
assert!(shield.is_enabled::<Permission>());

assert!(!shield.is_enabled::<Prefetch>());
assert!(!shield.is_enabled::<ExpectCt>());
assert!(!shield.is_enabled::<Referrer>());

Trait Implementations

Returns a new Shield instance. See the table for a description of the policies used by default.

Example
use rocket::shield::Shield;

let shield = Shield::default();

Returns an Info structure containing the name and Kind of this fairing. The name can be any arbitrary string. Kind must be an ord set of Kind variants. Read more

The liftoff callback. Read more

The response callback. Read more

The ignite callback. Returns Ok if ignition should proceed and Err if ignition and launch should be aborted. Read more

The request callback. Read more

The shutdown callback. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

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

Calls U::from(self).

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

Converts self into a collection.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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