-
Notifications
You must be signed in to change notification settings - Fork 198
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
Add a Content Security Policy to non-rustdoc pages #1333
Conversation
Globally allowing that warning in |
@pietroalbini this causes |
There's also an error on /features pages (e.g. http://localhost:3000/crate/konst/0.2.0/features). |
Fixed the features page, I'm not exactly sure where you see the |
I think this might be something injected by firefox devtools, it doesn't seem to be affecting the site at all. |
Short note here (without having read the code)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I clicked around some more and everything seems to be working now :)
pub(super) fn suppress(&mut self, suppress: bool) { | ||
self.suppress = suppress; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why make the suppress
field private if you're going to allow setting it anyway?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly out of habit, and because this looks weird:
req.extensions
.get_mut::<Csp>()
.expect("missing CSP")
.suppress = true;
req.extensions | ||
.get_mut::<Csp>() | ||
.expect("missing CSP") | ||
.suppress(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is shared between all threads, isn't it? Won't this surpress the CSP policy for all requests? Or is it per-request because you used link_before
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, all the extensions are added in each request. Most of the others are just cloned Arc<_>
s, instead here it's a fresh instance every time.
// their documentation and 'self' would allow their execution. Instead, every allowed | ||
// script must include the random nonce in it, which an attacker is not able to guess. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh, I just realized while reading this that there's a new nonce for each request, it's not a hash of the file or anything like that. Maybe add a comment to the CSP saying that? And a test that two sequential requests get different nonces?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, the test is already there. I'll add a comment.
I see a few errors from extensions injected scripts, that might be where |
Rebased and addressed all review comments. |
This PR adds a Content Security Policy to all pages except for rustdoc pages, increasing the security of the website. This should not break anything, but in case something breaks there is no need to revert the PR: setting the
DOCSRS_CSP_REPORT_ONLY
environment variable totrue
will switch the CSP to be "report-only", notifying breakages in the console without actually blocking any content.There are a couple of changes here that will affect how docs.rs is developed (cc @rust-lang/docs-rs):
style=""
are not allowed anymore. Using CSS classes is required instead.nonce="{{ csp_nonce }}"
HTML attribute for it to work.eval()
inside JavaScript doesn't work anymore (we weren't using it before, but listing that just to be sure).The best way to test this is to start the server locally and see if anything breaks while visiting pages.
r? @jyn514