pub enum ContentSecurityPolicyDirective<'a> {
Show 25 variants
ChildSrc(Vec<&'a str>),
ConnectSrc(Vec<&'a str>),
DefaultSrc(Vec<&'a str>),
FontSrc(Vec<&'a str>),
FrameSrc(Vec<&'a str>),
ImgSrc(Vec<&'a str>),
ManifestSrc(Vec<&'a str>),
MediaSrc(Vec<&'a str>),
ObjectSrc(Vec<&'a str>),
PrefetchSrc(Vec<&'a str>),
ScriptSrc(Vec<&'a str>),
ScriptSrcElem(Vec<&'a str>),
ScriptSrcAttr(Vec<&'a str>),
StyleSrc(Vec<&'a str>),
StyleSrcElem(Vec<&'a str>),
StyleSrcAttr(Vec<&'a str>),
WorkerSrc(Vec<&'a str>),
BaseUri(Vec<&'a str>),
Sandbox(Vec<&'a str>),
FormAction(Vec<&'a str>),
FrameAncestors(Vec<&'a str>),
ReportTo(Vec<&'a str>),
RequireTrustedTypesFor(Vec<&'a str>),
TrustedTypes(Vec<&'a str>),
UpgradeInsecureRequests,
}
Expand description
Manages Content-Security-Policy
header
The HTTP Content-Security-Policy response header allows web site administrators to control resources the user agent is allowed to load for a given page. With a few exceptions, policies mostly involve specifying server origins and script endpoints. This helps guard against cross-site scripting attacks (XSS).
§Directives
- child-src: Defines valid sources for web workers and nested browsing contexts loaded using elements such as
<frame>
and<iframe>
. - connect-src: Applies to XMLHttpRequest (AJAX), WebSocket or EventSource. If not allowed the browser emulates a 400 HTTP status code.
- default-src: The default-src is the default policy for loading content such as JavaScript, Images, CSS, Font’s, AJAX requests, Frames, HTML5 Media. See the list of directives to see which values are allowed as default.
- font-src: Defines valid sources for fonts loaded using @font-face.
- frame-src: Defines valid sources for nested browsing contexts loading using elements such as
<frame>
and<iframe>
. - img-src: Defines valid sources of images and favicons.
- manifest-src: Specifies which manifest can be applied to the resource.
- media-src: Defines valid sources for loading media using the
<audio>
and<video>
elements. - object-src: Defines valid sources for the
<object>
,<embed>
, and<applet>
elements. - prefetch-src: Specifies which referrer to use when fetching the resource.
- script-src: Defines valid sources for JavaScript.
- script-src-elem: Defines valid sources for JavaScript inline event handlers.
- script-src-attr: Defines valid sources for JavaScript inline event handlers.
- style-src: Defines valid sources for stylesheets.
- style-src-elem: Defines valid sources for stylesheets inline event handlers.
- style-src-attr: Defines valid sources for stylesheets inline event handlers.
- worker-src: Defines valid sources for Worker, SharedWorker, or ServiceWorker scripts.
- base-uri: Restricts the URLs which can be used in a document’s
<base>
element. - sandbox: Enables a sandbox for the requested resource similar to the iframe sandbox attribute. The sandbox applies a same origin policy, prevents popups, plugins and script execution is blocked. You can keep the sandbox value empty to keep all restrictions in place, or add values: allow-forms allow-same-origin allow-scripts allow-popups, allow-modals, allow-orientation-lock, allow-pointer-lock, allow-presentation, allow-popups-to-escape-sandbox, allow-top-navigation, allow-top-navigation-by-user-activation.
- form-action: Restricts the URLs which can be used as the target of a form submissions from a given context.
- frame-ancestors: Specifies valid parents that may embed a page using
<frame>
,<iframe>
,<object>
,<embed>
, or<applet>
. - report-to: Enables reporting of violations.
- require-trusted-types-for: Specifies which trusted types are required by a resource.
- trusted-types: Specifies which trusted types are defined by a resource.
- upgrade-insecure-requests: Block HTTP requests on insecure elements.
§Examples
use helmet_core::ContentSecurityPolicy;
let content_security_policy = ContentSecurityPolicy::default()
.child_src(vec!["'self'", "https://youtube.com"])
.connect_src(vec!["'self'", "https://youtube.com"])
.default_src(vec!["'self'", "https://youtube.com"])
.font_src(vec!["'self'", "https://youtube.com"]);
Variants§
ChildSrc(Vec<&'a str>)
Warning: Instead of child-src, if you want to regulate nested browsing contexts and workers, you should use the frame-src and worker-src directives, respectively.
ConnectSrc(Vec<&'a str>)
Applies to XMLHttpRequest (AJAX), WebSocket or EventSource. If not allowed the browser emulates a 400 HTTP status code.
DefaultSrc(Vec<&'a str>)
The default-src is the default policy for loading content such as JavaScript, Images, CSS, Font’s, AJAX requests, Frames, HTML5 Media. See the list of directives to see which values are allowed as default.
FontSrc(Vec<&'a str>)
Defines valid sources for fonts loaded using @font-face.
FrameSrc(Vec<&'a str>)
Defines valid sources for nested browsing contexts loading using elements such as <frame>
and <iframe>
.
ImgSrc(Vec<&'a str>)
Defines valid sources of images and favicons.
ManifestSrc(Vec<&'a str>)
Specifies which manifest can be applied to the resource.
MediaSrc(Vec<&'a str>)
Defines valid sources for loading media using the <audio>
and <video>
elements.
ObjectSrc(Vec<&'a str>)
Defines valid sources for the <object>
, <embed>
, and <applet>
elements.
PrefetchSrc(Vec<&'a str>)
Specifies which referrer to use when fetching the resource.
ScriptSrc(Vec<&'a str>)
Defines valid sources for JavaScript.
ScriptSrcElem(Vec<&'a str>)
Defines valid sources for JavaScript inline event handlers.
ScriptSrcAttr(Vec<&'a str>)
Defines valid sources for JavaScript inline event handlers.
StyleSrc(Vec<&'a str>)
Defines valid sources for stylesheets.
StyleSrcElem(Vec<&'a str>)
Defines valid sources for stylesheets inline event handlers.
StyleSrcAttr(Vec<&'a str>)
Defines valid sources for stylesheets inline event handlers.
WorkerSrc(Vec<&'a str>)
Defines valid sources for Worker, SharedWorker, or ServiceWorker scripts.
BaseUri(Vec<&'a str>)
Restricts the URLs which can be used in a document’s <base>
element.
Sandbox(Vec<&'a str>)
Enables a sandbox for the requested resource similar to the iframe sandbox attribute. The sandbox applies a same origin policy, prevents popups, plugins and script execution is blocked. You can keep the sandbox value empty to keep all restrictions in place, or add values: allow-forms allow-same-origin allow-scripts allow-popups, allow-modals, allow-orientation-lock, allow-pointer-lock, allow-presentation, allow-popups-to-escape-sandbox, allow-top-navigation, allow-top-navigation-by-user-activation.
FormAction(Vec<&'a str>)
Restricts the URLs which can be used as the target of a form submissions from a given context.
FrameAncestors(Vec<&'a str>)
Specifies valid parents that may embed a page using <frame>
, <iframe>
, <object>
, <embed>
, or <applet>
.
ReportTo(Vec<&'a str>)
Enables reporting of violations.
report-uri is deprecated, however, it is still supported by browsers that don’t yet support report-to. ReportTo will apply both to report-uri and report-to with the same values, to support browsers that support both.
RequireTrustedTypesFor(Vec<&'a str>)
Specifies which trusted types are required by a resource.
TrustedTypes(Vec<&'a str>)
Specifies which trusted types are defined by a resource.
UpgradeInsecureRequests
Block HTTP requests on insecure elements.
Implementations§
Source§impl<'a> ContentSecurityPolicyDirective<'a>
impl<'a> ContentSecurityPolicyDirective<'a>
Sourcepub fn child_src(values: Vec<&'a str>) -> Self
pub fn child_src(values: Vec<&'a str>) -> Self
child-src: Defines valid sources for web workers and nested browsing contexts loaded using elements such as <frame>`` and
Sourcepub fn connect_src(values: Vec<&'a str>) -> Self
pub fn connect_src(values: Vec<&'a str>) -> Self
connect-src: Applies to XMLHttpRequest (AJAX), WebSocket or EventSource. If not allowed the browser emulates a 400 HTTP status code.
Sourcepub fn default_src(values: Vec<&'a str>) -> Self
pub fn default_src(values: Vec<&'a str>) -> Self
default-src: The default-src is the default policy for loading content such as JavaScript, Images, CSS, Font’s, AJAX requests, Frames, HTML5 Media. See the list of directives to see which values are allowed as default.
Sourcepub fn font_src(values: Vec<&'a str>) -> Self
pub fn font_src(values: Vec<&'a str>) -> Self
font-src: Defines valid sources for fonts loaded using @font-face.
Sourcepub fn frame_src(values: Vec<&'a str>) -> Self
pub fn frame_src(values: Vec<&'a str>) -> Self
frame-src: Defines valid sources for nested browsing contexts loading using elements such as <frame>
and <iframe>
.
Sourcepub fn img_src(values: Vec<&'a str>) -> Self
pub fn img_src(values: Vec<&'a str>) -> Self
img-src: Defines valid sources of images and favicons.
Sourcepub fn manifest_src(values: Vec<&'a str>) -> Self
pub fn manifest_src(values: Vec<&'a str>) -> Self
manifest-src: Specifies which manifest can be applied to the resource.
Sourcepub fn media_src(values: Vec<&'a str>) -> Self
pub fn media_src(values: Vec<&'a str>) -> Self
media-src: Defines valid sources for loading media using the <audio>
and <video>
elements.
Sourcepub fn object_src(values: Vec<&'a str>) -> Self
pub fn object_src(values: Vec<&'a str>) -> Self
object-src: Defines valid sources for the <object>
, <embed>
, and <applet>
elements.
Sourcepub fn prefetch_src(values: Vec<&'a str>) -> Self
pub fn prefetch_src(values: Vec<&'a str>) -> Self
prefetch-src: Specifies which referrer to use when fetching the resource.
Sourcepub fn script_src(values: Vec<&'a str>) -> Self
pub fn script_src(values: Vec<&'a str>) -> Self
script-src: Defines valid sources for JavaScript.
Sourcepub fn script_src_elem(values: Vec<&'a str>) -> Self
pub fn script_src_elem(values: Vec<&'a str>) -> Self
script-src-elem: Defines valid sources for JavaScript inline event handlers.
Sourcepub fn script_src_attr(values: Vec<&'a str>) -> Self
pub fn script_src_attr(values: Vec<&'a str>) -> Self
script-src-attr: Defines valid sources for JavaScript inline event handlers.
Sourcepub fn style_src(values: Vec<&'a str>) -> Self
pub fn style_src(values: Vec<&'a str>) -> Self
style-src: Defines valid sources for stylesheets.
Sourcepub fn style_src_elem(values: Vec<&'a str>) -> Self
pub fn style_src_elem(values: Vec<&'a str>) -> Self
style-src-elem: Defines valid sources for stylesheets inline event handlers.
Sourcepub fn style_src_attr(values: Vec<&'a str>) -> Self
pub fn style_src_attr(values: Vec<&'a str>) -> Self
style-src-attr: Defines valid sources for stylesheets inline event handlers.
Sourcepub fn worker_src(values: Vec<&'a str>) -> Self
pub fn worker_src(values: Vec<&'a str>) -> Self
worker-src: Defines valid sources for Worker, SharedWorker, or ServiceWorker scripts.
Sourcepub fn base_uri(values: Vec<&'a str>) -> Self
pub fn base_uri(values: Vec<&'a str>) -> Self
base-uri: Restricts the URLs which can be used in a document’s <base>
element.
Sourcepub fn sandbox(values: Vec<&'a str>) -> Self
pub fn sandbox(values: Vec<&'a str>) -> Self
sandbox: Enables a sandbox for the requested resource similar to the iframe sandbox attribute. The sandbox applies a same origin policy, prevents popups, plugins and script execution is blocked. You can keep the sandbox value empty to keep all restrictions in place, or add values: allow-forms allow-same-origin allow-scripts allow-popups, allow-modals, allow-orientation-lock, allow-pointer-lock, allow-presentation, allow-popups-to-escape-sandbox, allow-top-navigation, allow-top-navigation-by-user-activation.
Sourcepub fn form_action(values: Vec<&'a str>) -> Self
pub fn form_action(values: Vec<&'a str>) -> Self
form-action: Restricts the URLs which can be used as the target of a form submissions from a given context.
Sourcepub fn frame_ancestors(values: Vec<&'a str>) -> Self
pub fn frame_ancestors(values: Vec<&'a str>) -> Self
frame-ancestors: Specifies valid parents that may embed a page using <frame>
, <iframe>
, <object>
, <embed>
, or <applet>
.
Sourcepub fn require_trusted_types_for(values: Vec<&'a str>) -> Self
pub fn require_trusted_types_for(values: Vec<&'a str>) -> Self
require-trusted-types-for: Specifies which trusted types are required by a resource.
Sourcepub fn trusted_types(values: Vec<&'a str>) -> Self
pub fn trusted_types(values: Vec<&'a str>) -> Self
trusted-types: Specifies which trusted types are defined by a resource.
Sourcepub fn upgrade_insecure_requests() -> Self
pub fn upgrade_insecure_requests() -> Self
Block HTTP requests on insecure elements.
Trait Implementations§
Source§impl<'a> Clone for ContentSecurityPolicyDirective<'a>
impl<'a> Clone for ContentSecurityPolicyDirective<'a>
Source§fn clone(&self) -> ContentSecurityPolicyDirective<'a>
fn clone(&self) -> ContentSecurityPolicyDirective<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more