Crates.io | content-security-policy |
lib.rs | content-security-policy |
version | 0.5.2 |
source | src |
created_at | 2017-10-02 02:23:17.700655 |
updated_at | 2024-07-29 15:42:53.797538 |
description | Will parse and validate Content-Security-Policy level 3 |
homepage | |
repository | https://github.com/rust-ammonia/rust-content-security-policy |
max_upload_size | |
id | 34122 |
size | 121,095 |
This function parses a CSP string into a data structure, and provides a bunch of functions you can call on it (basically all of the "hooks" defined in the CSP standard). It directly uses the url
crate, but it's intentionally agnostic to your HTML parser and your networking stack, so there are a few things it doesn't do:
rust-url
, it is intentionally not entangled with any particular networking stack, HTML parser, or DOM implementation.Vec<>
of objects that the library's users should push to the event loop. Just iterate over the vec, convert to your internal event representation, and push them to the event loop after calling the rust-content-security-policy function. Since the CSP specification never spins the event loop in the middle of any of its algorithms, that will be spec compliant anyway.To use content-security-policy
, add it to your project's Cargo.toml
file:
[dependencies]
content-security-policy = "0.5.2"
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);
}