Struct ContentSecurityPolicy
pub struct ContentSecurityPolicy { /* private fields */ }http only.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§
§impl ContentSecurityPolicy
impl ContentSecurityPolicy
pub const fn empty() -> ContentSecurityPolicy
pub const fn empty() -> ContentSecurityPolicy
Empty policy. Build from this when you want to add directives one at a time rather than starting from a baseline.
pub fn strict_self() -> ContentSecurityPolicy
pub fn strict_self() -> ContentSecurityPolicy
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'pub fn directives(&self) -> impl Iterator<Item = &Directive>
pub fn directives(&self) -> impl Iterator<Item = &Directive>
Iterate the policy’s directives in encoding order.
pub fn with(
self,
name: impl Into<DirectiveName>,
sources: SourceList,
) -> ContentSecurityPolicy
pub fn with( self, name: impl Into<DirectiveName>, sources: SourceList, ) -> ContentSecurityPolicy
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.
pub fn set(
&mut self,
name: impl Into<DirectiveName>,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set( &mut self, name: impl Into<DirectiveName>, sources: SourceList, ) -> &mut ContentSecurityPolicy
In-place sibling of with.
pub fn with_default_src(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_default_src(self, sources: SourceList) -> ContentSecurityPolicy
Set default-src.
pub fn set_default_src(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_default_src( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set default-src.
pub fn with_script_src(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_script_src(self, sources: SourceList) -> ContentSecurityPolicy
Set script-src.
pub fn set_script_src(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_script_src( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set script-src.
pub fn with_style_src(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_style_src(self, sources: SourceList) -> ContentSecurityPolicy
Set style-src.
pub fn set_style_src(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_style_src( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set style-src.
pub fn with_img_src(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_img_src(self, sources: SourceList) -> ContentSecurityPolicy
Set img-src.
pub fn set_img_src(&mut self, sources: SourceList) -> &mut ContentSecurityPolicy
pub fn set_img_src(&mut self, sources: SourceList) -> &mut ContentSecurityPolicy
Set img-src.
pub fn with_font_src(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_font_src(self, sources: SourceList) -> ContentSecurityPolicy
Set font-src.
pub fn set_font_src(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_font_src( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set font-src.
pub fn with_connect_src(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_connect_src(self, sources: SourceList) -> ContentSecurityPolicy
Set connect-src.
pub fn set_connect_src(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_connect_src( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set connect-src.
pub fn with_media_src(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_media_src(self, sources: SourceList) -> ContentSecurityPolicy
Set media-src.
pub fn set_media_src(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_media_src( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set media-src.
pub fn with_object_src(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_object_src(self, sources: SourceList) -> ContentSecurityPolicy
Set object-src.
pub fn set_object_src(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_object_src( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set object-src.
pub fn with_frame_src(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_frame_src(self, sources: SourceList) -> ContentSecurityPolicy
Set frame-src.
pub fn set_frame_src(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_frame_src( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set frame-src.
pub fn with_frame_ancestors(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_frame_ancestors(self, sources: SourceList) -> ContentSecurityPolicy
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).
pub fn set_frame_ancestors(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_frame_ancestors( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
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).
pub fn with_child_src(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_child_src(self, sources: SourceList) -> ContentSecurityPolicy
Set child-src.
pub fn set_child_src(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_child_src( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set child-src.
pub fn with_worker_src(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_worker_src(self, sources: SourceList) -> ContentSecurityPolicy
Set worker-src.
pub fn set_worker_src(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_worker_src( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set worker-src.
pub fn with_manifest_src(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_manifest_src(self, sources: SourceList) -> ContentSecurityPolicy
Set manifest-src.
pub fn set_manifest_src(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_manifest_src( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set manifest-src.
pub fn with_form_action(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_form_action(self, sources: SourceList) -> ContentSecurityPolicy
Set form-action.
pub fn set_form_action(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_form_action( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set form-action.
pub fn with_base_uri(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_base_uri(self, sources: SourceList) -> ContentSecurityPolicy
Set base-uri.
pub fn set_base_uri(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_base_uri( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set base-uri.
Set navigate-to.
Set navigate-to.
pub fn with_report_to(self, sources: SourceList) -> ContentSecurityPolicy
pub fn with_report_to(self, sources: SourceList) -> ContentSecurityPolicy
Set report-to.
pub fn set_report_to(
&mut self,
sources: SourceList,
) -> &mut ContentSecurityPolicy
pub fn set_report_to( &mut self, sources: SourceList, ) -> &mut ContentSecurityPolicy
Set report-to.
pub fn with_upgrade_insecure_requests(self) -> ContentSecurityPolicy
pub fn with_upgrade_insecure_requests(self) -> ContentSecurityPolicy
Set the valueless upgrade-insecure-requests directive.
pub fn set_upgrade_insecure_requests(&mut self) -> &mut ContentSecurityPolicy
pub fn set_upgrade_insecure_requests(&mut self) -> &mut ContentSecurityPolicy
Set the valueless upgrade-insecure-requests directive.
Trait Implementations§
§impl Clone for ContentSecurityPolicy
impl Clone for ContentSecurityPolicy
§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 more§impl Debug for ContentSecurityPolicy
impl Debug for ContentSecurityPolicy
§impl Default for ContentSecurityPolicy
impl Default for ContentSecurityPolicy
§fn default() -> ContentSecurityPolicy
fn default() -> ContentSecurityPolicy
§impl Display for ContentSecurityPolicy
impl Display for ContentSecurityPolicy
§impl HeaderDecode for ContentSecurityPolicy
impl HeaderDecode for ContentSecurityPolicy
§fn decode<'i, I>(values: &mut I) -> Result<ContentSecurityPolicy, Error>where
I: Iterator<Item = &'i HeaderValue>,
fn decode<'i, I>(values: &mut I) -> Result<ContentSecurityPolicy, Error>where
I: Iterator<Item = &'i HeaderValue>,
HeaderValues.§impl HeaderEncode for ContentSecurityPolicy
impl HeaderEncode for ContentSecurityPolicy
§fn encode<E>(&self, values: &mut E)where
E: Extend<HeaderValue>,
fn encode<E>(&self, values: &mut E)where
E: Extend<HeaderValue>,
HeaderValue, and add it to a container
which has HeaderValue type as each element. Read more§fn encode_to_value(&self) -> Option<HeaderValue>
fn encode_to_value(&self) -> Option<HeaderValue>
HeaderValue. Read more§impl PartialEq for ContentSecurityPolicy
impl PartialEq for ContentSecurityPolicy
§fn eq(&self, other: &ContentSecurityPolicy) -> bool
fn eq(&self, other: &ContentSecurityPolicy) -> bool
self and other values to be equal, and is used by ==.§impl TypedHeader for ContentSecurityPolicy
impl TypedHeader for ContentSecurityPolicy
§fn name() -> &'static HeaderName
fn name() -> &'static HeaderName
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 UnsafeUnpin for ContentSecurityPolicy
impl UnwindSafe for ContentSecurityPolicy
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§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§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a rama_grpc::Request§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more§impl<T, U> RamaTryFrom<T> for Uwhere
U: TryFrom<T>,
impl<T, U> RamaTryFrom<T> for Uwhere
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
fn rama_try_from(value: T) -> Result<U, <U as RamaTryFrom<T>>::Error>
§impl<T, U, CrateMarker> RamaTryInto<U, CrateMarker> for Twhere
U: RamaTryFrom<T, CrateMarker>,
impl<T, U, CrateMarker> RamaTryInto<U, CrateMarker> for Twhere
U: RamaTryFrom<T, CrateMarker>,
type Error = <U as RamaTryFrom<T, CrateMarker>>::Error
fn rama_try_into(self) -> Result<U, <U as RamaTryFrom<T, CrateMarker>>::Error>
§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.
§impl<V, F> ValueFormatter<&V> for F
impl<V, F> ValueFormatter<&V> for F
§fn format_value(writer: impl ValueWriter, value: &&V)
fn format_value(writer: impl ValueWriter, value: &&V)
value to writer§impl<V, F> ValueFormatter<Arc<V>> for F
impl<V, F> ValueFormatter<Arc<V>> for F
§fn format_value(writer: impl ValueWriter, value: &Arc<V>)
fn format_value(writer: impl ValueWriter, value: &Arc<V>)
value to writer§impl<V, F> ValueFormatter<Box<V>> for F
impl<V, F> ValueFormatter<Box<V>> for F
§fn format_value(writer: impl ValueWriter, value: &Box<V>)
fn format_value(writer: impl ValueWriter, value: &Box<V>)
value to writer§impl<V, F> ValueFormatter<Cow<'_, V>> for F
impl<V, F> ValueFormatter<Cow<'_, V>> for F
§fn format_value(writer: impl ValueWriter, value: &Cow<'_, V>)
fn format_value(writer: impl ValueWriter, value: &Cow<'_, V>)
value to writer§impl<V, F> ValueFormatter<Option<V>> for Fwhere
F: ValueFormatter<V> + ?Sized,
impl<V, F> ValueFormatter<Option<V>> for Fwhere
F: ValueFormatter<V> + ?Sized,
§fn format_value(writer: impl ValueWriter, value: &Option<V>)
fn format_value(writer: impl ValueWriter, value: &Option<V>)
value to writer