-
-
Notifications
You must be signed in to change notification settings - Fork 154
Extend CSP Configuration #911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f405e05
Extend CSP Configuration to handle user-supplied values that can cont…
guspower b82ddfb
fix RequestContext to use CSP value from AppConfig
guspower e2d44d7
update documentation to describe usage
guspower 28907f6
generate nonce per request; config is a string (again); added playwri…
guspower 36d10e1
fix js lint `let` -> `const`
guspower 4ec8b51
Merge branch 'main' into pr/guspower/911
lovasoa 93aa1b7
format
lovasoa aadce60
remove some useless string copies
lovasoa f08fa57
implement a proper csp template struct
lovasoa 8e913b8
parse the content-security-policy just once
lovasoa a88f350
fix merge issue.
lovasoa d7ecf8f
fix docs
lovasoa 29d7c99
fix docs
lovasoa 4877a25
clippy
lovasoa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,101 @@ | ||
| use std::fmt::Display; | ||
|
|
||
| use actix_web::http::header::{ | ||
| HeaderName, HeaderValue, TryIntoHeaderPair, CONTENT_SECURITY_POLICY, | ||
| }; | ||
| use actix_web::HttpResponseBuilder; | ||
| use awc::http::header::InvalidHeaderValue; | ||
| use rand::random; | ||
| use std::fmt::{Display, Formatter}; | ||
|
|
||
| pub const DEFAULT_CONTENT_SECURITY_POLICY: &str = "script-src 'self' 'nonce-{NONCE}'"; | ||
|
|
||
| #[derive(Debug, Clone, Copy)] | ||
| #[derive(Debug, Clone)] | ||
| pub struct ContentSecurityPolicy { | ||
| pub nonce: u64, | ||
| policy: String, | ||
| } | ||
|
|
||
| impl Default for ContentSecurityPolicy { | ||
| fn default() -> Self { | ||
| Self { nonce: random() } | ||
| impl ContentSecurityPolicy { | ||
| pub fn new<S: Into<String>>(policy: S) -> Self { | ||
| Self { | ||
| nonce: random(), | ||
| policy: policy.into(), | ||
| } | ||
| } | ||
|
|
||
| pub fn apply_to_response(&self, response: &mut HttpResponseBuilder) { | ||
| if self.is_enabled() { | ||
| response.insert_header(self); | ||
| } | ||
| } | ||
|
|
||
| fn is_enabled(&self) -> bool { | ||
| !self.policy.is_empty() | ||
| } | ||
|
|
||
| #[allow(dead_code)] | ||
| fn set_nonce(&mut self, nonce: u64) { | ||
| self.nonce = nonce; | ||
| } | ||
| } | ||
|
|
||
| impl Display for ContentSecurityPolicy { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "script-src 'self' 'nonce-{}'", self.nonce) | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| let value = self.policy.replace("{NONCE}", &self.nonce.to_string()); | ||
|
|
||
| write!(f, "{value}") | ||
| } | ||
| } | ||
|
|
||
| impl actix_web::http::header::TryIntoHeaderPair for &ContentSecurityPolicy { | ||
| impl TryIntoHeaderPair for &ContentSecurityPolicy { | ||
| type Error = InvalidHeaderValue; | ||
|
|
||
| fn try_into_pair( | ||
| self, | ||
| ) -> Result< | ||
| ( | ||
| actix_web::http::header::HeaderName, | ||
| actix_web::http::header::HeaderValue, | ||
| ), | ||
| Self::Error, | ||
| > { | ||
| fn try_into_pair(self) -> Result<(HeaderName, HeaderValue), Self::Error> { | ||
| Ok(( | ||
| actix_web::http::header::CONTENT_SECURITY_POLICY, | ||
| actix_web::http::header::HeaderValue::from_str(&self.to_string())?, | ||
| CONTENT_SECURITY_POLICY, | ||
| HeaderValue::from_str(&self.to_string())?, | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn default_csp_response_contains_random_nonce() { | ||
| let mut csp = ContentSecurityPolicy::new(DEFAULT_CONTENT_SECURITY_POLICY); | ||
| csp.set_nonce(0); | ||
|
|
||
| assert!(csp.is_enabled()); | ||
| assert_eq!(&csp.to_string(), "script-src 'self' 'nonce-0'"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn custom_csp_response_without_nonce() { | ||
| let csp = ContentSecurityPolicy::new("object-src 'none';"); | ||
|
|
||
| assert!(csp.is_enabled()); | ||
| assert_eq!("object-src 'none';", &csp.to_string()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn blank_csp_response() { | ||
| let csp = ContentSecurityPolicy::new(""); | ||
|
|
||
| assert!(!csp.is_enabled()); | ||
| assert_eq!("", &csp.to_string()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn custom_csp_with_nonce() { | ||
| let mut csp = | ||
| ContentSecurityPolicy::new("script-src 'self' 'nonce-{NONCE}'; object-src 'none';"); | ||
| csp.set_nonce(0); | ||
|
|
||
| assert!(csp.is_enabled()); | ||
| assert_eq!( | ||
| "script-src 'self' 'nonce-0'; object-src 'none';", | ||
| csp.to_string().as_str() | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.