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 moresource§impl Debug for ContentSecurityPolicy
impl Debug for ContentSecurityPolicy
source§impl Default for ContentSecurityPolicy
impl Default for ContentSecurityPolicy
source§impl PartialEq for ContentSecurityPolicy
impl PartialEq for ContentSecurityPolicy
impl Eq for ContentSecurityPolicy
impl StructuralPartialEq for ContentSecurityPolicy
Auto Trait Implementations§
impl Freeze for ContentSecurityPolicy
impl RefUnwindSafe for ContentSecurityPolicy
impl Send for ContentSecurityPolicy
impl Sync for ContentSecurityPolicy
impl Unpin for ContentSecurityPolicy
impl UnwindSafe for ContentSecurityPolicy
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)