pub struct ContentSecurityPolicy { /* private fields */ }
Expand description

Build a Content-Security-Policy header.

Content-Security-Policy (CSP) HTTP headers are used to prevent cross-site injections. Read more

Mozilla Developer Network

Examples

use http_types::{headers, security, Response, StatusCode};

let mut policy = security::ContentSecurityPolicy::new();
policy
    .default_src(security::Source::SameOrigin)
    .default_src("areweasyncyet.rs")
    .script_src(security::Source::SameOrigin)
    .script_src(security::Source::UnsafeInline)
    .object_src(security::Source::None)
    .base_uri(security::Source::None)
    .upgrade_insecure_requests();

let mut res = Response::new(StatusCode::Ok);
res.set_body("Hello, Chashu!");

security::default(&mut res);
policy.apply(&mut res);

assert_eq!(res["content-security-policy"], "base-uri 'none'; default-src 'self' areweasyncyet.rs; object-src 'none'; script-src 'self' 'unsafe-inline'; upgrade-insecure-requests");

Implementations

Create a new instance.

Defines the Content-Security-Policy base-uri directive

MDN | base-uri

Defines the Content-Security-Policy block-all-mixed-content directive

MDN | block-all-mixed-content

Defines the Content-Security-Policy connect-src directive

MDN | connect-src

Defines the Content-Security-Policy default-src directive

MDN | default-src

Defines the Content-Security-Policy font-src directive

MDN | font-src

Defines the Content-Security-Policy form-action directive

MDN | form-action

Defines the Content-Security-Policy frame-ancestors directive

MDN | frame-ancestors

Defines the Content-Security-Policy frame-src directive

MDN | frame-src

Defines the Content-Security-Policy img-src directive

MDN | img-src

Defines the Content-Security-Policy media-src directive

MDN | media-src

Defines the Content-Security-Policy object-src directive

MDN | object-src

Defines the Content-Security-Policy plugin-types directive

MDN | plugin-types

Defines the Content-Security-Policy require-sri-for directive

MDN | require-sri-for

Defines the Content-Security-Policy report-uri directive

MDN | report-uri

Defines the Content-Security-Policy report-to directive

MDN | report-to

Defines the Content-Security-Policy sandbox directive

MDN | sandbox

Defines the Content-Security-Policy script-src directive

MDN | script-src

Defines the Content-Security-Policy style-src directive

MDN | style-src

Defines the Content-Security-Policy upgrade-insecure-requests directive

MDN | upgrade-insecure-requests

Defines the Content-Security-Policy worker-src directive

MDN | worker-src

Change the header to Content-Security-Policy-Report-Only

Sets the Content-Security-Policy (CSP) HTTP header to prevent cross-site injections

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Sets the Content-Security-Policy default to “script-src ‘self’; object-src ‘self’”

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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.

Calls U::from(self).

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

Should always be Self

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

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.