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ยง
pub extern crate percent_encoding;
pub extern crate url;
Modulesยง
Structsยง
- CspList
- https://www.w3.org/TR/CSP/#csp-list
- Directive
- https://www.w3.org/TR/CSP/#directives
- Element
- Hash
Function - https://www.w3.org/TR/SRI/#integrity-metadata
- Invalid
Destination - Policy
- A single parsed content security policy.
- Request
- request to be validated
- Response
- response to be validated https://fetch.spec.whatwg.org/#concept-response
- Source
List ๐ - https://www.w3.org/TR/CSP/#framework-directive-source-list
- Url
- A parsed URL record.
- Violation
- violation information
Enumsยง
- Allow
Result ๐ - https://www.w3.org/TR/CSP/#allow-all-inline
- Check
Result - Many algorithms are allowed to return either โAllowedโ or โBlockedโ. The spec describes these as strings.
- Destination
- Hash
Algorithm - Initiator
- Inline
Check Type - The valid values for type are โscriptโ, โscript attributeโ, โstyleโ, and โstyle attributeโ.
- Match
Result - https://www.w3.org/TR/CSP/#match-element-to-source-list
- Navigation
Check Type - The valid values for type are โform-submissionโ and โotherโ.
- Origin
- The origin of an URL
- Parser
Metadata - Policy
Disposition - https://www.w3.org/TR/CSP/#policy-disposition
- Policy
Source - https://www.w3.org/TR/CSP/#policy-source
- Position
- Indicates a position within a URL based on its components.
- Subresource
Integrity Metadata - https://www.w3.org/TR/SRI/#parse-metadata
- Violates
- https://www.w3.org/TR/CSP/#does-request-violate-policy
- Violation
Resource - violation information
Staticsยง
- DIRECTIVE_
NAME_ ๐GRAMMAR - https://www.w3.org/TR/CSP/#grammardef-directive-name
- DIRECTIVE_
VALUE_ ๐TOKEN_ GRAMMAR - https://www.w3.org/TR/CSP/#grammardef-directive-value
- HASH_
SOURCE_ ๐GRAMMAR - https://www.w3.org/TR/CSP/#grammardef-hash-source
- HOST_
SOURCE_ ๐GRAMMAR - https://www.w3.org/TR/CSP/#grammardef-host-source
- NONCE_
SOURCE_ ๐GRAMMAR - https://www.w3.org/TR/CSP/#grammardef-nonce-source
- NONE_
SOURCE_ ๐GRAMMAR - SCHEME_
SOURCE_ ๐GRAMMAR - https://www.w3.org/TR/CSP/#grammardef-scheme-source
- SUBRESOURCE_
METADATA_ ๐GRAMMAR - https://www.w3.org/TR/SRI/#the-integrity-attribute This corresponds to the โhash-expressionโ grammar.
- TRUSTED_
POLICY_ ๐SOURCE_ GRAMMAR - https://www.w3.org/TR/trusted-types/#trusted-types-csp-directive
Functionsยง
- default_
port ๐ - default_
port_ ๐str - does_
url_ ๐match_ expression_ in_ origin_ with_ redirect_ count - https://www.w3.org/TR/CSP/#match-url-to-source-expression
- get_
fetch_ ๐directive_ fallback_ list - https://www.w3.org/TR/CSP/#directive-fallback-list
- get_
the_ ๐effective_ directive_ for_ inline_ checks - https://www.w3.org/TR/CSP/#effective-directive-for-inline-check
- get_
the_ ๐effective_ directive_ for_ request - https://www.w3.org/TR/CSP/#effective-directive-for-a-request
- host_
part_ ๐match - https://www.w3.org/TR/CSP/#match-hosts
- origin_
scheme_ ๐part_ match - parse_
subresource_ integrity_ metadata - https://www.w3.org/TR/SRI/#parse-metadata
- path_
part_ ๐match - https://www.w3.org/TR/CSP/#match-paths
- port_
part_ ๐match - https://www.w3.org/TR/CSP/#match-ports
- request_
is_ ๐script_ like - https://fetch.spec.whatwg.org/#request-destination-script-like
- scheme_
is_ ๐httpx - scheme_
is_ ๐network - scheme_
part_ ๐match - https://www.w3.org/TR/CSP/#match-schemes
- script_
directives_ ๐postrequest_ check - https://www.w3.org/TR/CSP/#script-post-request
- script_
directives_ ๐prerequest_ check - https://www.w3.org/TR/CSP/#script-pre-request
- should_
fetch_ ๐directive_ execute - https://www.w3.org/TR/CSP/#should-directive-execute
- url_
port ๐