Skip to content
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

Merged
merged 3 commits into from
Apr 8, 2021

Conversation

pietroalbini
Copy link
Member

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 to true 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):

  • Inline styles with style="" are not allowed anymore. Using CSS classes is required instead.
  • Any JavaScript will need to have the 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

@jyn514
Copy link
Member

jyn514 commented Mar 26, 2021

error: name `IO` contains a capitalized acronym
Error:   --> crates/metadata/lib.rs:74:5
   |
74 |     IO(#[from] io::Error),
   |     ^^ help: consider making the acronym lowercase, except the initial letter (notice the capitalization): `Io`
   |
   = note: `-D clippy::upper-case-acronyms` implied by `-D warnings`

Globally allowing that warning in crates/metadata seems fine to me.

@jyn514
Copy link
Member

jyn514 commented Mar 26, 2021

@pietroalbini this causes utils.js to fail to load:

image

@jyn514
Copy link
Member

jyn514 commented Mar 26, 2021

There's also an error on /features pages (e.g. http://localhost:3000/crate/konst/0.2.0/features).
image
I can't seem to figure out how to get firefox to tell me what line it's on, though.

@jyn514 jyn514 added S-waiting-on-author Status: This PR is incomplete or needs to address review comments A-frontend Area: Web frontend labels Mar 26, 2021
@pietroalbini
Copy link
Member Author

Fixed the features page, I'm not exactly sure where you see the utils.js error though.

@jyn514
Copy link
Member

jyn514 commented Mar 27, 2021

Fixed the features page, I'm not exactly sure where you see the utils.js error though.

I think this might be something injected by firefox devtools, it doesn't seem to be affecting the site at all.

@jyn514 jyn514 added S-waiting-on-review Status: This pull request has been implemented and needs to be reviewed and removed S-waiting-on-author Status: This PR is incomplete or needs to address review comments labels Mar 27, 2021
@syphar
Copy link
Member

syphar commented Mar 28, 2021

Fixed the features page, I'm not exactly sure where you see the utils.js error though.

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)

  • chrome and safari don't show the error, so it seems to be related to FF (and IMHO dev tools)
  • I do see an error when checking rustdoc pages.

Copy link
Member

@jyn514 jyn514 left a 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 :)

src/web/csp.rs Show resolved Hide resolved
Comment on lines +19 to +24
pub(super) fn suppress(&mut self, suppress: bool) {
self.suppress = suppress;
}
Copy link
Member

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?

Copy link
Member Author

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; 

Comment on lines +257 to +260
req.extensions
.get_mut::<Csp>()
.expect("missing CSP")
.suppress(true);
Copy link
Member

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?

Copy link
Member Author

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.

Comment on lines +65 to +69
// 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.
Copy link
Member

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?

Copy link
Member Author

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.

@Nemo157
Copy link
Member

Nemo157 commented Apr 7, 2021

I see a few errors from extensions injected scripts, that might be where utils.js is coming from too, I don't get that specific one in FF devtools.

@pietroalbini
Copy link
Member Author

pietroalbini commented Apr 8, 2021

Rebased and addressed all review comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-frontend Area: Web frontend S-waiting-on-review Status: This pull request has been implemented and needs to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants