plume/content_security_policy
Content-Security-Policy (CSP)
This response header lets sites declare which resources the browser is
allowed to load for a given page, mitigating cross-site scripting (XSS)
and data-injection attacks. plume.default() ships a sensible starter
policy.
Most directives expect at least one source. Passing an empty list (e.g.
DefaultSrc([])) renders an incomplete directive — omit it entirely
instead. Sandbox([]) is the exception; an empty token list applies the
maximum restrictions.
Examples
Policy([
DefaultSrc([Self]),
ScriptSrc([Self]),
ImgSrc([Self, Scheme("data")]),
StyleSrc([Self, UnsafeInline]),
])
See the MDN docs.
Types
A single Content-Security-Policy directive.
pub type Directive {
DefaultSrc(List(Source))
ScriptSrc(List(Source))
ScriptSrcAttr(List(Source))
ScriptSrcElem(List(Source))
StyleSrc(List(Source))
StyleSrcAttr(List(Source))
StyleSrcElem(List(Source))
ImgSrc(List(Source))
ConnectSrc(List(Source))
FontSrc(List(Source))
ObjectSrc(List(Source))
MediaSrc(List(Source))
FrameSrc(List(Source))
FencedFrameSrc(List(Source))
FrameAncestors(List(Source))
ChildSrc(List(Source))
ManifestSrc(List(Source))
WorkerSrc(List(Source))
BaseUri(List(Source))
FormAction(List(Source))
Sandbox(List(SandboxToken))
UpgradeInsecureRequests
RequireTrustedTypesFor(List(TrustedTypesSink))
TrustedTypes(List(TrustedTypesPolicy))
}
Constructors
-
DefaultSrc(List(Source))Serves as a fallback for the other fetch directives.
-
ScriptSrc(List(Source))Valid sources for JavaScript and WebAssembly resources.
-
ScriptSrcAttr(List(Source))Valid sources for inline event handlers (e.g.
onclick). -
ScriptSrcElem(List(Source))Valid sources for JavaScript
<script>elements. -
StyleSrc(List(Source))Valid sources for stylesheets.
-
StyleSrcAttr(List(Source))Valid sources for inline styles applied to elements (e.g.
styleattributes). -
StyleSrcElem(List(Source))Valid sources for stylesheet
<style>elements and<link>elements withrel="stylesheet". -
ImgSrc(List(Source))Valid sources of images and favicons.
-
ConnectSrc(List(Source))Restricts the URLs which can be loaded using script interfaces.
-
FontSrc(List(Source))Valid sources for fonts loaded using
@font-face. -
ObjectSrc(List(Source))Valid sources for the
<object>and<embed>elements. -
MediaSrc(List(Source))Valid sources for loading media using the
<audio>,<video>and<track>elements. -
FrameSrc(List(Source))Valid sources for nested browsing contexts loaded into elements such as
<frame>and<iframe>. -
FencedFrameSrc(List(Source))Valid sources for nested browsing contexts loaded into
<fencedframe>elements. Does not fall back todefault-srcorframe-src; if omitted, any URL is allowed. -
FrameAncestors(List(Source))Valid parents that may embed a page using
<frame>,<iframe>,<object>, or<embed>. -
ChildSrc(List(Source))Valid sources for web workers and nested browsing contexts loaded using elements such as
<frame>and<iframe>. Falls back todefault-src. -
ManifestSrc(List(Source))Valid sources for application manifest files.
-
WorkerSrc(List(Source))Valid sources for
Worker,SharedWorker, orServiceWorkerscripts. -
BaseUri(List(Source))Restricts the URLs which can be used in a document’s
<base>element. -
FormAction(List(Source))Restricts the URLs which can be used as the target of form submissions.
-
Sandbox(List(SandboxToken))Enables a sandbox for the requested resource, similar to the
<iframe>sandboxattribute. Pass an empty list to apply the maximum restrictions. -
UpgradeInsecureRequestsInstructs the browser to upgrade insecure requests (HTTP) to secure requests (HTTPS) before fetching.
-
RequireTrustedTypesFor(List(TrustedTypesSink))Controls data passed to DOM XSS sink functions (e.g.
Element.innerHTML). -
TrustedTypes(List(TrustedTypesPolicy))Restricts which Trusted Types policies may be created and used by scripts.
A capability token allowed within the sandbox directive.
pub type SandboxToken {
AllowDownloads
AllowForms
AllowModals
AllowOrientationLock
AllowPointerLock
AllowPopups
AllowPopupsToEscapeSandbox
AllowPresentation
AllowSameOrigin
AllowScripts
AllowTopNavigation
AllowTopNavigationByUserActivation
AllowTopNavigationToCustomProtocols
AllowStorageAccessByUserActivation
}
Constructors
-
AllowDownloadsAllows downloads to be initiated by the sandboxed content.
-
AllowFormsAllows the content to submit forms.
-
AllowModalsAllows the content to open modal dialogs (e.g.
alert(),confirm(),prompt(),print()). -
AllowOrientationLockAllows the content to disable the ability to lock the screen orientation.
-
AllowPointerLockAllows the content to use the Pointer Lock API.
-
AllowPopupsAllows the content to open popups (e.g.
window.open(),target="_blank"). -
AllowPopupsToEscapeSandboxAllows popups opened by the sandboxed content to escape the sandbox.
-
AllowPresentationAllows the content to start a presentation session.
-
AllowSameOriginTreats the content as same-origin rather than a unique opaque origin, allowing access to cookies and storage.
-
AllowScriptsAllows the content to execute scripts.
-
AllowTopNavigationAllows the content to navigate the top-level browsing context.
-
AllowTopNavigationByUserActivationAllows the content to navigate the top-level browsing context, but only in response to a user gesture.
-
AllowTopNavigationToCustomProtocolsAllows the content to navigate the top-level browsing context to non-
http/httpsURL schemes. -
AllowStorageAccessByUserActivationLets the sandboxed content request access to the parent’s storage via the Storage Access API.
A source expression used in fetch directives.
pub type Source {
Self
None
UnsafeInline
UnsafeEval
StrictDynamic
WasmUnsafeEval
UnsafeHashes
InlineSpeculationRules
Wildcard
Host(String)
Scheme(String)
Nonce(String)
Sha256(String)
Sha384(String)
Sha512(String)
}
Constructors
-
SelfSame scheme, host, and port as the document. Rendered as
'self'. -
NoneMatches no URLs. Rendered as
'none'. -
UnsafeInlineAllows the use of inline resources such as inline
<script>elements,javascript:URLs, and inline event handlers. Rendered as'unsafe-inline'. -
UnsafeEvalAllows the use of
eval()and similar methods for creating code from strings. Rendered as'unsafe-eval'. -
StrictDynamicPropagates trust from a nonced or hashed root script to scripts it loads. Rendered as
'strict-dynamic'. -
WasmUnsafeEvalAllows the loading and execution of WebAssembly modules without needing to also allow
'unsafe-eval'. Rendered as'wasm-unsafe-eval'. -
UnsafeHashesAllows hashes (e.g.
'sha256-...') to match against inline event handlers andstyleattributes, which are otherwise excluded from hash matching. Rendered as'unsafe-hashes'. -
InlineSpeculationRulesAllows inline
<script type="speculationrules">blocks used by the Speculation Rules API. Rendered as'inline-speculation-rules'. -
WildcardMatches any URL, except those with the
data:,blob:, orfilesystem:schemes. Rendered as*. -
Host(String)A host source, e.g.
https://cdn.example.comor*.example.com. -
Scheme(String)A scheme source, e.g.
"https"(rendered ashttps:). Pass the scheme name without the trailing colon. -
Nonce(String)A base64-encoded nonce that matches the
nonceattribute on an inline element. Rendered as'nonce-<value>'. -
Sha256(String)A base64-encoded SHA-256 hash of an inline resource. Rendered as
'sha256-<value>'. -
Sha384(String)A base64-encoded SHA-384 hash of an inline resource. Rendered as
'sha384-<value>'. -
Sha512(String)A base64-encoded SHA-512 hash of an inline resource. Rendered as
'sha512-<value>'.
A policy name or wildcard used in the trusted-types directive.
pub type TrustedTypesPolicy {
PolicyName(String)
AllowDuplicates
NoPolicy
AnyPolicy
}
Constructors
-
PolicyName(String)A policy name that may be created (e.g.
default,dompurify). -
AllowDuplicatesAllows the same policy name to be created more than once. Rendered as
'allow-duplicates'. -
NoPolicyDisables Trusted Types policy creation entirely. Rendered as
'none'. -
AnyPolicyAllows any policy name. Rendered as
*.
A DOM XSS injection sink group used with the require-trusted-types-for
directive.
pub type TrustedTypesSink {
Script
}
Constructors
-
ScriptThe DOM XSS injection sink group. The only sink group currently defined by the spec. Rendered as
'script'.
Values
pub fn to_string(value: ContentSecurityPolicy) -> String
Encode as the Content-Security-Policy header value.