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
Adopt a Content Security Policy #242
Merged
Conversation
This file contains 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 one was especially tricky, because editcomp makes heavy use of gridform, which generates HTML in JS as a string and then injects it with innerHTML. There's no way to attach <script> tags with innerHTML, even if the <script> contains the appropriate nonces. So, to refactor, I added a new hook for gridform, activateListeners, designed to re-activate event handlers for all of the gridform generated HTML, and moved all event handlers in there. This was especially painful for comboboxes, because I had to manually re-attach equivalent event listeners on all of the generated comboboxes.
This was referenced Jul 11, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
In this PR, I'm attempting to add a Content Security Policy, to provide broad, general defense against XSS attacks.
https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
XSS security issues occur when an attacker constructs a special link to IFDB, like this:
https://ifdb.org/allreviews?id=40ru852yszsizxsv&ratings=yes&pg=all&sortby=%3C/script%3E%27%22%3E%3Cimg%20src=x%20onError=prompt(1)%3E
sortby
is supposed to be a single word, likenew
to sort by "Newest First", but the code foolishly assumes that we can insert any text from the URL directly into the web page.The attacker can then write their own JavaScript code that runs on our server, logged in with the current user's credentials. In this example, the attacker just runs
prompt(1)
, as a proof of concept, but if the user is an admin, the code could just as easily delete users, promote themselves to an administrator, or do anything else than an administrator can do.Fixing XSS issues can be a game of whack-a-mole, trying to catch every case of these, but the better fix is to add a Content Security Policy that forbids this entire kind of bug.
As I write this, there are 44 commits in this PR. 😳
I start by adding a very basic CSP header, allowing too much:
This header blocks all
<script>
tags from working unless they have a validnonce
attribute. https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce…so then I went through and added nonces to as many
<script>
tags as I could find. I think I got them all.That then left three bugs to fix:
unsafe-eval
: Removed all calls toeval()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/evalscript-src-attr 'unsafe-inline'
: To fix this, I had to remove allonclick
attributes from the entire site, replacing them with nonce-enabled<script>
tags. I did over the course of 39 other commits. 😖 (This fixed the XSS issue above.)style-src 'unsafe-inline'
: To fix this, I'll have to remove allstyle=
attributes, of which there are 235 in the code base, and remove (or add nonces) to all<style>
elements, of which there are only 9 in the code base.… so, uh, yeah, a lot of work still to do. But I wanted to get some of this code off of my machine so people could begin reviewing it.