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
§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§
Source§impl ContentSecurityPolicy
impl ContentSecurityPolicy
Sourcepub fn base_uri<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn base_uri<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy base-uri
directive
Sourcepub fn block_all_mixed_content(&mut self) -> &mut Self
pub fn block_all_mixed_content(&mut self) -> &mut Self
Defines the Content-Security-Policy block-all-mixed-content
directive
Sourcepub fn connect_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn connect_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy connect-src
directive
Sourcepub fn default_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn default_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy default-src
directive
Sourcepub fn font_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn font_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy font-src
directive
Sourcepub fn form_action<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn form_action<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy form-action
directive
Sourcepub fn frame_ancestors<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn frame_ancestors<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy frame-ancestors
directive
Sourcepub fn frame_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn frame_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy frame-src
directive
Sourcepub fn img_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn img_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy img-src
directive
Sourcepub fn media_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn media_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy media-src
directive
Sourcepub fn object_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn object_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy object-src
directive
Sourcepub fn plugin_types<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn plugin_types<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy plugin-types
directive
Sourcepub fn require_sri_for<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn require_sri_for<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy require-sri-for
directive
Sourcepub fn report_uri<T: AsRef<str>>(&mut self, uri: T) -> &mut Self
pub fn report_uri<T: AsRef<str>>(&mut self, uri: T) -> &mut Self
Defines the Content-Security-Policy report-uri
directive
Sourcepub fn report_to(&mut self, endpoints: Vec<ReportTo>) -> &mut Self
pub fn report_to(&mut self, endpoints: Vec<ReportTo>) -> &mut Self
Defines the Content-Security-Policy report-to
directive
Sourcepub fn sandbox<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn sandbox<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy sandbox
directive
Sourcepub fn script_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn script_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy script-src
directive
Sourcepub fn style_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn style_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy style-src
directive
Sourcepub fn upgrade_insecure_requests(&mut self) -> &mut Self
pub fn upgrade_insecure_requests(&mut self) -> &mut Self
Defines the Content-Security-Policy upgrade-insecure-requests
directive
Sourcepub fn worker_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
pub fn worker_src<T: AsRef<str>>(&mut self, source: T) -> &mut Self
Defines the Content-Security-Policy worker-src
directive
Sourcepub fn report_only(&mut self) -> &mut Self
pub fn report_only(&mut self) -> &mut Self
Change the header to Content-Security-Policy-Report-Only
Trait Implementations§
Source§impl Clone for ContentSecurityPolicy
impl Clone for ContentSecurityPolicy
Source§fn clone(&self) -> ContentSecurityPolicy
fn clone(&self) -> ContentSecurityPolicy
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more