use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{ready, Context, Poll};
use axum::http::{header, HeaderName, HeaderValue, Request, Response};
use base64::engine::general_purpose::STANDARD_NO_PAD;
use base64::Engine as _;
use bytes::Bytes;
use pin_project::pin_project;
use rand::Rng as _;
use tower::{Layer, Service};
use proa_core::CspNonce;
const NONCE_BYTES: usize = 16;
const SCRIPT_SRC_PREFIX: &str = "'nonce-";
const SCRIPT_SRC_SUFFIX: &str = "' 'strict-dynamic'";
const DEFAULT_DIRECTIVES: &[(&str, &str)] = &[
("default-src", "'self'"),
("base-uri", "'self'"),
("object-src", "'none'"),
("frame-ancestors", "'none'"),
("style-src", "'self' 'unsafe-inline'"),
];
#[derive(Debug, Clone)]
pub struct CspPolicy {
directives: BTreeMap<Cow<'static, str>, String>,
explicit: BTreeSet<Cow<'static, str>>,
script_src_extra: Vec<String>,
report_only: bool,
}
impl Default for CspPolicy {
fn default() -> Self {
Self::strict()
}
}
impl CspPolicy {
pub fn strict() -> Self {
Self {
directives: DEFAULT_DIRECTIVES
.iter()
.map(|(name, value)| (Cow::Borrowed(*name), (*value).to_owned()))
.collect(),
explicit: BTreeSet::new(),
script_src_extra: Vec::new(),
report_only: false,
}
}
pub fn with_directive(
mut self,
name: impl Into<Cow<'static, str>>,
value: impl Into<String>,
) -> Self {
let name = name.into();
if name.eq_ignore_ascii_case("script-src") {
tracing::warn!(
"CspPolicy::with_directive cannot set script-src; the nonce and \
'strict-dynamic' are managed by the layer. Use \
CspPolicy::allow_script_source to add sources."
);
return self;
}
self.explicit.insert(name.clone());
self.directives.insert(name, value.into());
self
}
pub fn without_directive(mut self, name: &str) -> Self {
self.directives.remove(name);
self.explicit.remove(name);
self
}
pub fn allow_script_source(mut self, source: impl Into<String>) -> Self {
self.script_src_extra.push(source.into());
self
}
pub fn allow_asset_origin(mut self, origin: &str) -> Self {
let spelled = |directives: &BTreeMap<Cow<'static, str>, String>, name: &str| {
directives
.keys()
.find(|key| key.eq_ignore_ascii_case(name))
.cloned()
};
let fallback = spelled(&self.directives, "default-src")
.and_then(|key| self.directives.get(&key).cloned());
for (directive, only_if_set) in [
("style-src", false),
("style-src-elem", true),
("img-src", false),
("font-src", false),
("media-src", false),
("connect-src", false),
("manifest-src", false),
] {
let key = spelled(&self.directives, directive);
let own = key.as_ref().and_then(|key| self.directives.get(key));
if own.is_some_and(|value| value.trim().eq_ignore_ascii_case("'none'")) {
continue;
}
let Some(current) = own.or(if only_if_set { None } else { fallback.as_ref() }) else {
continue;
};
if current
.split_ascii_whitespace()
.any(|source| source == origin || source == "*")
{
continue;
}
let value = if current.trim().eq_ignore_ascii_case("'none'") {
origin.to_owned()
} else {
format!("{current} {origin}")
};
self.directives
.insert(key.unwrap_or(Cow::Borrowed(directive)), value);
}
self
}
pub fn report_only(mut self, report_only: bool) -> Self {
self.report_only = report_only;
self
}
pub(crate) fn is_report_only(&self) -> bool {
self.report_only
}
pub(crate) fn frame_ancestors_is_default(&self) -> bool {
!self.explicit.contains("frame-ancestors")
&& self.directives.contains_key("frame-ancestors")
}
fn resolve(self) -> ResolvedCsp {
let header_name = if self.report_only {
HeaderName::from_static("content-security-policy-report-only")
} else {
header::CONTENT_SECURITY_POLICY
};
let mut prefix = String::new();
let mut suffix = String::new();
let mut before_script = true;
for (name, value) in &self.directives {
if before_script && name.as_ref() > "script-src" {
before_script = false;
}
let out = if before_script {
&mut prefix
} else {
&mut suffix
};
if !out.is_empty() {
out.push_str("; ");
}
out.push_str(name);
out.push(' ');
out.push_str(value);
}
if !prefix.is_empty() {
prefix.push_str("; ");
}
prefix.push_str("script-src ");
prefix.push_str(SCRIPT_SRC_PREFIX);
let mut tail = String::from(SCRIPT_SRC_SUFFIX);
for source in &self.script_src_extra {
tail.push(' ');
tail.push_str(source);
}
if !suffix.is_empty() {
tail.push_str("; ");
tail.push_str(&suffix);
}
ResolvedCsp {
header_name,
prefix: prefix.into_boxed_str(),
suffix: tail.into_boxed_str(),
valid: true,
}
.validated()
}
}
#[derive(Debug)]
struct ResolvedCsp {
header_name: HeaderName,
prefix: Box<str>,
suffix: Box<str>,
valid: bool,
}
impl ResolvedCsp {
fn validated(mut self) -> Self {
let sample = CspNonce::new("c2FtcGxlLW5vbmNlLXZhbHVl").expect("sample nonce is base64");
if self.header_value(&sample).is_none() {
tracing::warn!(
"assembled Content-Security-Policy contains characters that are \
illegal in a header value; the header will not be sent"
);
self.valid = false;
}
self
}
fn header_value(&self, nonce: &CspNonce) -> Option<HeaderValue> {
let nonce = nonce.as_str();
let mut policy = String::with_capacity(self.prefix.len() + nonce.len() + self.suffix.len());
policy.push_str(&self.prefix);
policy.push_str(nonce);
policy.push_str(&self.suffix);
HeaderValue::from_maybe_shared(Bytes::from(policy)).ok()
}
}
fn fresh_nonce() -> CspNonce {
let mut random = [0_u8; NONCE_BYTES];
rand::rng().fill_bytes(&mut random);
CspNonce::new(STANDARD_NO_PAD.encode(random)).expect("base64 output is a valid CSP nonce")
}
#[derive(Debug, Clone)]
pub struct CspNonceLayer {
resolved: Arc<ResolvedCsp>,
}
impl CspNonceLayer {
pub fn new(policy: CspPolicy) -> Self {
Self {
resolved: Arc::new(policy.resolve()),
}
}
pub fn strict() -> Self {
Self::new(CspPolicy::strict())
}
}
impl Default for CspNonceLayer {
fn default() -> Self {
Self::strict()
}
}
impl<S> Layer<S> for CspNonceLayer {
type Service = CspNonceService<S>;
fn layer(&self, inner: S) -> Self::Service {
CspNonceService {
inner,
resolved: Arc::clone(&self.resolved),
}
}
}
#[derive(Debug, Clone)]
pub struct CspNonceService<S> {
inner: S,
resolved: Arc<ResolvedCsp>,
}
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for CspNonceService<S>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
{
type Response = Response<ResBody>;
type Error = S::Error;
type Future = CspNonceFuture<S::Future>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, mut req: Request<ReqBody>) -> Self::Future {
let nonce = fresh_nonce();
let header = self
.resolved
.valid
.then(|| self.resolved.header_value(&nonce))
.flatten()
.map(|value| (self.resolved.header_name.clone(), value));
req.extensions_mut().insert(nonce);
CspNonceFuture {
inner: self.inner.call(req),
header,
}
}
}
#[pin_project]
pub struct CspNonceFuture<F> {
#[pin]
inner: F,
header: Option<(HeaderName, HeaderValue)>,
}
impl<F, ResBody, E> Future for CspNonceFuture<F>
where
F: Future<Output = Result<Response<ResBody>, E>>,
{
type Output = Result<Response<ResBody>, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let mut result = ready!(this.inner.poll(cx));
if let Ok(response) = &mut result {
if let Some((name, value)) = this.header.take() {
let headers = response.headers_mut();
if !headers.contains_key(&name) {
headers.insert(name, value);
}
}
}
Poll::Ready(result)
}
}
#[cfg(test)]
mod tests {
use super::*;
use axum::body::Body;
use std::convert::Infallible;
use tower::{service_fn, ServiceExt};
fn policy_string(policy: CspPolicy) -> String {
let resolved = policy.resolve();
let nonce = CspNonce::new("dGVzdC1ub25jZQ").unwrap();
resolved
.header_value(&nonce)
.unwrap()
.to_str()
.unwrap()
.to_owned()
}
#[test]
fn an_asset_origin_is_added_wherever_a_host_list_decides() {
const CDN: &str = "https://cdn.example";
let has = |policy: &str, directive: &str| policy.split("; ").any(|part| part == directive);
let strict = policy_string(CspPolicy::strict().allow_asset_origin(CDN));
for directive in [
"style-src 'self' 'unsafe-inline' https://cdn.example",
"img-src 'self' https://cdn.example",
"font-src 'self' https://cdn.example",
"media-src 'self' https://cdn.example",
"connect-src 'self' https://cdn.example",
"manifest-src 'self' https://cdn.example",
"script-src 'nonce-dGVzdC1ub25jZQ' 'strict-dynamic'",
] {
assert!(
has(&strict, directive),
"missing `{directive}` in `{strict}`"
);
}
assert!(!strict.contains("style-src-elem"));
let custom = policy_string(
CspPolicy::strict()
.with_directive("Img-Src", "'self' data:")
.with_directive("style-src-elem", "'self'")
.with_directive("media-src", "'NONE'")
.with_directive("font-src", "https://cdn.example")
.allow_asset_origin(CDN),
);
for directive in [
"Img-Src 'self' data: https://cdn.example",
"style-src-elem 'self' https://cdn.example",
"media-src 'NONE'",
"font-src https://cdn.example",
] {
assert!(
has(&custom, directive),
"missing `{directive}` in `{custom}`"
);
}
assert_eq!(custom.to_ascii_lowercase().matches("img-src").count(), 1);
let closed = policy_string(
CspPolicy::strict()
.with_directive("default-src", "'none'")
.without_directive("style-src")
.allow_asset_origin(CDN),
);
assert!(has(&closed, "img-src https://cdn.example"), "{closed}");
assert!(has(&closed, "style-src https://cdn.example"), "{closed}");
}
#[test]
fn strict_policy_carries_the_nonce_and_strict_dynamic() {
assert_eq!(
policy_string(CspPolicy::strict()),
"base-uri 'self'; default-src 'self'; frame-ancestors 'none'; \
object-src 'none'; script-src 'nonce-dGVzdC1ub25jZQ' 'strict-dynamic'; \
style-src 'self' 'unsafe-inline'"
);
}
#[test]
fn strict_policy_never_allows_unsafe_inline_script() {
let policy = policy_string(CspPolicy::strict());
let script_src = policy
.split("; ")
.find(|directive| directive.starts_with("script-src "))
.unwrap();
assert!(!script_src.contains("'unsafe-inline'"));
assert!(!script_src.contains("'unsafe-eval'"));
}
#[test]
fn directives_can_be_overridden_added_and_removed() {
let policy = policy_string(
CspPolicy::strict()
.with_directive("style-src", "'self'")
.with_directive("img-src", "'self' data:")
.without_directive("frame-ancestors"),
);
assert!(policy.contains("style-src 'self'"));
assert!(!policy.contains("'unsafe-inline'"));
assert!(policy.contains("img-src 'self' data:"));
assert!(!policy.contains("frame-ancestors"));
}
#[test]
fn extra_script_sources_follow_the_nonce() {
let policy = policy_string(CspPolicy::strict().allow_script_source("'wasm-unsafe-eval'"));
assert!(policy
.contains("script-src 'nonce-dGVzdC1ub25jZQ' 'strict-dynamic' 'wasm-unsafe-eval';"));
}
#[test]
fn script_src_cannot_be_replaced_wholesale() {
let policy = policy_string(CspPolicy::strict().with_directive("script-src", "'self'"));
assert!(policy.contains("script-src 'nonce-dGVzdC1ub25jZQ' 'strict-dynamic'"));
}
#[test]
fn a_directive_sorting_after_script_src_still_renders() {
let policy = policy_string(CspPolicy::strict().with_directive("worker-src", "'self'"));
assert!(policy.contains("'strict-dynamic'; style-src"));
assert!(policy.ends_with("worker-src 'self'"));
}
#[test]
fn report_only_uses_the_report_only_header_name() {
let resolved = CspPolicy::strict().report_only(true).resolve();
assert_eq!(resolved.header_name, "content-security-policy-report-only");
assert!(resolved.valid);
}
#[test]
fn a_malformed_directive_drops_the_header_instead_of_panicking() {
let resolved = CspPolicy::strict()
.with_directive("img-src", "bad\nvalue")
.resolve();
assert!(!resolved.valid);
}
#[test]
fn frame_ancestors_default_is_distinguishable_from_an_explicit_value() {
assert!(CspPolicy::strict().frame_ancestors_is_default());
assert!(!CspPolicy::strict()
.with_directive("frame-ancestors", "https://embedder.example")
.frame_ancestors_is_default());
assert!(!CspPolicy::strict()
.without_directive("frame-ancestors")
.frame_ancestors_is_default());
}
#[test]
fn generated_nonces_are_unique_and_carry_128_bits() {
let first = fresh_nonce();
let second = fresh_nonce();
assert_ne!(first.as_str(), second.as_str());
assert_eq!(first.as_str().len(), 22);
}
async fn call_layer<F>(layer: CspNonceLayer, handler: F) -> Response<Body>
where
F: Fn(Request<Body>) -> Response<Body> + Clone + Send + 'static,
{
let service = layer.layer(service_fn(move |req: Request<Body>| {
let handler = handler.clone();
async move { Ok::<_, Infallible>(handler(req)) }
}));
service
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
.await
.unwrap()
}
#[tokio::test]
async fn the_response_header_matches_the_nonce_in_the_request_extensions() {
let response = call_layer(CspNonceLayer::strict(), |req| {
let nonce = req
.extensions()
.get::<CspNonce>()
.expect("layer installs the request nonce");
Response::new(Body::from(nonce.as_str().to_owned()))
})
.await;
let header = response
.headers()
.get(header::CONTENT_SECURITY_POLICY)
.unwrap()
.to_str()
.unwrap()
.to_owned();
let body = axum::body::to_bytes(response.into_body(), 64)
.await
.unwrap();
let seen_by_handler = std::str::from_utf8(&body).unwrap();
assert!(header.contains(&format!("'nonce-{seen_by_handler}'")));
}
#[tokio::test]
async fn a_handler_authored_policy_is_not_overwritten() {
let response = call_layer(CspNonceLayer::strict(), |_req| {
let mut response = Response::new(Body::empty());
response.headers_mut().insert(
header::CONTENT_SECURITY_POLICY,
HeaderValue::from_static("default-src 'none'"),
);
response
})
.await;
assert_eq!(
response.headers().get(header::CONTENT_SECURITY_POLICY),
Some(&HeaderValue::from_static("default-src 'none'"))
);
}
}