Skip to main content

page_policy

Function page_policy 

Source
pub fn page_policy(html: &str) -> Option<String>
Expand description

Computes the per-page Content-Security-Policy for a built HTML page, or None when the page has no inline blocks and the global computed_policy applies unchanged (spec B4).

The returned policy is DEFAULT_CSP_POLICY_TEMPLATE rendered with the page’s inline SHA-256 source hashes — hash-strict, never containing 'unsafe-inline'.

§Examples

use ssg::csp::page_policy;

assert!(page_policy("<html><head></head><body></body></html>").is_none());

let html = r#"<script type="application/ld+json">{"@type":"Thing"}</script>"#;
let policy = page_policy(html).expect("inline JSON-LD yields a policy");
assert!(policy.contains("'sha256-"));
assert!(!policy.contains("unsafe-inline"));