pub struct ContentSecurityPolicy { /* private fields */ }Expand description
Content-Security-Policy response header.
Adding a directive that already exists in the policy replaces its source-list in place (preserving declared order). The user agent would ignore a second occurrence anyway, so we keep the value the caller most recently supplied.
§Examples
use rama_http_headers::{ContentSecurityPolicy, HostSource, SourceList};
let csp = ContentSecurityPolicy::strict_self().with_img_src(
SourceList::self_origin()
.with_data()
.with_host(HostSource::try_parse("https://raw.githubusercontent.com").unwrap()),
);
let rendered = csp.to_string();
assert!(rendered.contains("img-src 'self' data: https://raw.githubusercontent.com"));
assert!(rendered.contains("frame-ancestors 'none'"));Implementations§
Source§impl ContentSecurityPolicy
impl ContentSecurityPolicy
Sourcepub const fn empty() -> Self
pub const fn empty() -> Self
Empty policy. Build from this when you want to add directives one at a time rather than starting from a baseline.
Sourcepub fn strict_self() -> Self
pub fn strict_self() -> Self
Strict same-origin baseline:
default-src 'self'; script-src 'self'; style-src 'self';
img-src 'self'; font-src 'self'; connect-src 'self';
form-action 'self'; base-uri 'self'; frame-ancestors 'none'Sourcepub fn directives(&self) -> impl Iterator<Item = &Directive> + '_
pub fn directives(&self) -> impl Iterator<Item = &Directive> + '_
Iterate the policy’s directives in encoding order.
Sourcepub fn with(self, name: impl Into<DirectiveName>, sources: SourceList) -> Self
pub fn with(self, name: impl Into<DirectiveName>, sources: SourceList) -> Self
Generic escape hatch: append or replace any directive by name. If the directive already exists, its source-list is overwritten in place (order preserved); otherwise it’s appended.
Sourcepub fn set(
&mut self,
name: impl Into<DirectiveName>,
sources: SourceList,
) -> &mut Self
pub fn set( &mut self, name: impl Into<DirectiveName>, sources: SourceList, ) -> &mut Self
In-place sibling of with.
Sourcepub fn with_default_src(self, sources: SourceList) -> Self
pub fn with_default_src(self, sources: SourceList) -> Self
Set default-src.
Sourcepub fn set_default_src(&mut self, sources: SourceList) -> &mut Self
pub fn set_default_src(&mut self, sources: SourceList) -> &mut Self
Set default-src.
Sourcepub fn with_script_src(self, sources: SourceList) -> Self
pub fn with_script_src(self, sources: SourceList) -> Self
Set script-src.
Sourcepub fn set_script_src(&mut self, sources: SourceList) -> &mut Self
pub fn set_script_src(&mut self, sources: SourceList) -> &mut Self
Set script-src.
Sourcepub fn with_style_src(self, sources: SourceList) -> Self
pub fn with_style_src(self, sources: SourceList) -> Self
Set style-src.
Sourcepub fn set_style_src(&mut self, sources: SourceList) -> &mut Self
pub fn set_style_src(&mut self, sources: SourceList) -> &mut Self
Set style-src.
Sourcepub fn with_img_src(self, sources: SourceList) -> Self
pub fn with_img_src(self, sources: SourceList) -> Self
Set img-src.
Sourcepub fn set_img_src(&mut self, sources: SourceList) -> &mut Self
pub fn set_img_src(&mut self, sources: SourceList) -> &mut Self
Set img-src.
Sourcepub fn with_font_src(self, sources: SourceList) -> Self
pub fn with_font_src(self, sources: SourceList) -> Self
Set font-src.
Sourcepub fn set_font_src(&mut self, sources: SourceList) -> &mut Self
pub fn set_font_src(&mut self, sources: SourceList) -> &mut Self
Set font-src.
Sourcepub fn with_connect_src(self, sources: SourceList) -> Self
pub fn with_connect_src(self, sources: SourceList) -> Self
Set connect-src.
Sourcepub fn set_connect_src(&mut self, sources: SourceList) -> &mut Self
pub fn set_connect_src(&mut self, sources: SourceList) -> &mut Self
Set connect-src.
Sourcepub fn with_media_src(self, sources: SourceList) -> Self
pub fn with_media_src(self, sources: SourceList) -> Self
Set media-src.
Sourcepub fn set_media_src(&mut self, sources: SourceList) -> &mut Self
pub fn set_media_src(&mut self, sources: SourceList) -> &mut Self
Set media-src.
Sourcepub fn with_object_src(self, sources: SourceList) -> Self
pub fn with_object_src(self, sources: SourceList) -> Self
Set object-src.
Sourcepub fn set_object_src(&mut self, sources: SourceList) -> &mut Self
pub fn set_object_src(&mut self, sources: SourceList) -> &mut Self
Set object-src.
Sourcepub fn with_frame_src(self, sources: SourceList) -> Self
pub fn with_frame_src(self, sources: SourceList) -> Self
Set frame-src.
Sourcepub fn set_frame_src(&mut self, sources: SourceList) -> &mut Self
pub fn set_frame_src(&mut self, sources: SourceList) -> &mut Self
Set frame-src.
Sourcepub fn with_frame_ancestors(self, sources: SourceList) -> Self
pub fn with_frame_ancestors(self, sources: SourceList) -> Self
Set frame-ancestors. Note that nonces, hashes, and
'unsafe-inline' are not valid sources here per CSP3 § 6.1.2
(we don’t enforce that — just be aware).
Sourcepub fn set_frame_ancestors(&mut self, sources: SourceList) -> &mut Self
pub fn set_frame_ancestors(&mut self, sources: SourceList) -> &mut Self
Set frame-ancestors. Note that nonces, hashes, and
'unsafe-inline' are not valid sources here per CSP3 § 6.1.2
(we don’t enforce that — just be aware).
Sourcepub fn with_child_src(self, sources: SourceList) -> Self
pub fn with_child_src(self, sources: SourceList) -> Self
Set child-src.
Sourcepub fn set_child_src(&mut self, sources: SourceList) -> &mut Self
pub fn set_child_src(&mut self, sources: SourceList) -> &mut Self
Set child-src.
Sourcepub fn with_worker_src(self, sources: SourceList) -> Self
pub fn with_worker_src(self, sources: SourceList) -> Self
Set worker-src.
Sourcepub fn set_worker_src(&mut self, sources: SourceList) -> &mut Self
pub fn set_worker_src(&mut self, sources: SourceList) -> &mut Self
Set worker-src.
Sourcepub fn with_manifest_src(self, sources: SourceList) -> Self
pub fn with_manifest_src(self, sources: SourceList) -> Self
Set manifest-src.
Sourcepub fn set_manifest_src(&mut self, sources: SourceList) -> &mut Self
pub fn set_manifest_src(&mut self, sources: SourceList) -> &mut Self
Set manifest-src.
Sourcepub fn with_form_action(self, sources: SourceList) -> Self
pub fn with_form_action(self, sources: SourceList) -> Self
Set form-action.
Sourcepub fn set_form_action(&mut self, sources: SourceList) -> &mut Self
pub fn set_form_action(&mut self, sources: SourceList) -> &mut Self
Set form-action.
Sourcepub fn with_base_uri(self, sources: SourceList) -> Self
pub fn with_base_uri(self, sources: SourceList) -> Self
Set base-uri.
Sourcepub fn set_base_uri(&mut self, sources: SourceList) -> &mut Self
pub fn set_base_uri(&mut self, sources: SourceList) -> &mut Self
Set base-uri.
Set navigate-to.
Set navigate-to.
Sourcepub fn with_report_to(self, sources: SourceList) -> Self
pub fn with_report_to(self, sources: SourceList) -> Self
Set report-to.
Sourcepub fn set_report_to(&mut self, sources: SourceList) -> &mut Self
pub fn set_report_to(&mut self, sources: SourceList) -> &mut Self
Set report-to.
Sourcepub fn with_upgrade_insecure_requests(self) -> Self
pub fn with_upgrade_insecure_requests(self) -> Self
Set the valueless upgrade-insecure-requests directive.
Sourcepub fn set_upgrade_insecure_requests(&mut self) -> &mut Self
pub fn set_upgrade_insecure_requests(&mut self) -> &mut Self
Set the valueless upgrade-insecure-requests directive.
Trait Implementations§
Source§impl Clone for ContentSecurityPolicy
impl Clone for ContentSecurityPolicy
Source§fn clone(&self) -> ContentSecurityPolicy
fn clone(&self) -> ContentSecurityPolicy
1.0.0 (const: unstable) · 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§fn default() -> ContentSecurityPolicy
fn default() -> ContentSecurityPolicy
Source§impl Display for ContentSecurityPolicy
impl Display for ContentSecurityPolicy
impl Eq for ContentSecurityPolicy
Source§impl HeaderDecode for ContentSecurityPolicy
impl HeaderDecode for ContentSecurityPolicy
Source§fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(
values: &mut I,
) -> Result<Self, Error>
fn decode<'i, I: Iterator<Item = &'i HeaderValue>>( values: &mut I, ) -> Result<Self, Error>
HeaderValues.Source§impl HeaderEncode for ContentSecurityPolicy
impl HeaderEncode for ContentSecurityPolicy
Source§fn encode<E: Extend<HeaderValue>>(&self, values: &mut E)
fn encode<E: Extend<HeaderValue>>(&self, values: &mut E)
HeaderValue, and add it to a container
which has HeaderValue type as each element. Read moreSource§fn encode_to_value(&self) -> Option<HeaderValue>
fn encode_to_value(&self) -> Option<HeaderValue>
HeaderValue. Read moreSource§impl PartialEq for ContentSecurityPolicy
impl PartialEq for ContentSecurityPolicy
Source§fn eq(&self, other: &ContentSecurityPolicy) -> bool
fn eq(&self, other: &ContentSecurityPolicy) -> bool
self and other values to be equal, and is used by ==.impl StructuralPartialEq for ContentSecurityPolicy
Source§impl TypedHeader for ContentSecurityPolicy
impl TypedHeader for ContentSecurityPolicy
Source§fn name() -> &'static HeaderName
fn name() -> &'static HeaderName
Auto Trait Implementations§
impl Freeze for ContentSecurityPolicy
impl RefUnwindSafe for ContentSecurityPolicy
impl Send for ContentSecurityPolicy
impl Sync for ContentSecurityPolicy
impl Unpin for ContentSecurityPolicy
impl UnsafeUnpin 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more