Crate content_security_policy

source ยท
Expand description

Parse and validate Web Content-Security-Policy level 3

ยงExample

extern crate content_security_policy;
use content_security_policy::*;
fn main() {
    let csp_list = CspList::parse("script-src *.notriddle.com", PolicySource::Header, PolicyDisposition::Enforce);
    let (check_result, _) = csp_list.should_request_be_blocked(&Request {
        url: Url::parse("https://www.notriddle.com/script.js").unwrap(),
        origin: Origin::Tuple("https".to_string(), url::Host::Domain("notriddle.com".to_owned()), 443),
        redirect_count: 0,
        destination: Destination::Script,
        initiator: Initiator::None,
        nonce: String::new(),
        integrity_metadata: String::new(),
        parser_metadata: ParserMetadata::None,
    });
    assert_eq!(check_result, CheckResult::Allowed);
    let (check_result, _) = csp_list.should_request_be_blocked(&Request {
        url: Url::parse("https://www.evil.example/script.js").unwrap(),
        origin: Origin::Tuple("https".to_string(), url::Host::Domain("notriddle.com".to_owned()), 443),
        redirect_count: 0,
        destination: Destination::Script,
        initiator: Initiator::None,
        nonce: String::new(),
        integrity_metadata: String::new(),
        parser_metadata: ParserMetadata::None,
    });
    assert_eq!(check_result, CheckResult::Blocked);
}

Re-exportsยง

Modulesยง

Structsยง

  • https://www.w3.org/TR/CSP/#csp-list
  • https://www.w3.org/TR/CSP/#directives
  • https://www.w3.org/TR/SRI/#integrity-metadata
  • A single parsed content security policy.
  • request to be validated
  • response to be validated https://fetch.spec.whatwg.org/#concept-response
  • SourceList ๐Ÿ”’
    https://www.w3.org/TR/CSP/#framework-directive-source-list
  • A parsed URL record.
  • violation information

Enumsยง

Staticsยง

Functionsยง