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

Content Security Policy #3620

Closed
rudolphfroger opened this issue Apr 12, 2017 · 8 comments
Closed

Content Security Policy #3620

rudolphfroger opened this issue Apr 12, 2017 · 8 comments
Labels
builder:html html theme type:enhancement enhance or introduce a new feature
Milestone

Comments

@rudolphfroger
Copy link

Subject: adjust themes to allow a sufficient strict Content-Security-Policy

Problem

  • Settings a sufficiently strict Content-Security-Policy raises many blocked actions

Also see: https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP

Procedure to reproduce the problem

Serving the generated HTML using a Content Security Policy header like below gives many exceptions:

Content-Security-Policy: "media-src 'self'; img-src 'self' data:; connect-src 'self'; child-src 'self'; style-src 'self'; default-src 'none'; font-src 'self'; script-src 'self'"

Expected results

I expected to be able to set a Content-Security-Policy header to protect the site against XSS and injection attacks.

Environment info

  • OS: any
  • Python version: n.a.
  • Sphinx version:any
@shimizukawa
Copy link
Member

@rudolphfroger I think you mentioned about HTTP header, however sphinx IS NOT a server application. Sphinx just generate html files. So I think there is nothing todo from sphinx side.

@rudolphfroger
Copy link
Author

I understand that Sphinx won't be able to set an HTTP header. At the moment the inline Javascript in the Sphinx themes prevent anyone from setting a reasonable Content Security Policy header without breaking Sphinx functionality. Moving all inline Javascript and CSS into the separate JS and CSS files would enable people to serve Sphinx in a better secured setup.

@shimizukawa
Copy link
Member

Thanks for your explanation. Indeed, current templates are not good.

@ghost
Copy link

ghost commented Nov 27, 2017

I think you mentioned about HTTP header, however sphinx IS NOT a server application. Sphinx just generate html files. So I think there is nothing todo from sphinx side.

CSP can either be served via a header (i.e, via Apache) or put into a meta tag (i.e, in a Sphinx theme). The point of CSP is that it can be configured to disallow inline scripts/styles to prevent XSS/CSRF attacks so it is a Sphinx problem.

Currently the RTD theme sets DOCUMENTATION_OPTIONS expected by the basic theme (doctools, searchtools, etc.) by doing this:

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
             URL_ROOT:'{{ url_root }}',
             VERSION:'{{ release|e }}',
             COLLAPSE_INDEX:false,
             FILE_SUFFIX:'{{ '' if no_search_suffix else file_suffix }}',
             HAS_SOURCE:  {{ has_source|lower }},
             SOURCELINK_SUFFIX: '{{ sourcelink_suffix }}'
         };
     </script>

IMO there should be a better way to set these properties than JavaScript, i.e through some type of middleware.

@tk0miya
Copy link
Member

tk0miya commented Jan 14, 2018

Fixed by #4295.
Thank you for reporting!

@jumarko
Copy link

jumarko commented Feb 19, 2019

Because of this issue I've updated my sphinx to 1.8.4 and it seems that the problem with DOCUMENTATION_OPTIONS is gone.
However, I got other errors when using the search box because of inlined scripts used in search.html

[Report Only] Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'nonce-w90MR9FaEMvvIqq+E32iGfCOojWBnzSzADCmOrNSung64Inv9lUYU9aaA6pLc131GP294iM50jiiqfmP' https://js.stripe.com". Either the 'unsafe-inline' keyword, a hash ('sha256-Ia59rEZC7ZJO4TTGcWqtwZBVaOfxSvdArdpB0AaSzbk='), or a nonce ('nonce-...') is required to enable inline execution.

[Report Only] Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'nonce-w90MR9FaEMvvIqq+E32iGfCOojWBnzSzADCmOrNSung64Inv9lUYU9aaA6pLc131GP294iM50jiiqfmP' https://js.stripe.com". Either the 'unsafe-inline' keyword, a hash ('sha256-p98tulYY6PuvLZreTlWz42v29aGELSPpkZGnACGsP28='), or a nonce ('nonce-...') is required to enable inline execution.

[Report Only] Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'nonce-w90MR9FaEMvvIqq+E32iGfCOojWBnzSzADCmOrNSung64Inv9lUYU9aaA6pLc131GP294iM50jiiqfmP' https://js.stripe.com". Either the 'unsafe-inline' keyword, a hash ('sha256-euFOlnAZ5DTyYbwKER1+neBcumHLVJsd7mDPngZMMbU='), or a nonce ('nonce-...') is required to enable inline execution.

image

@ghost
Copy link

ghost commented Feb 21, 2019

As I understand it, the purpose of the original AJAX code is to defer loading searchindex.js because of its size. In 2019, it should be possible to use something like async or defer. Further, caching should not be a problem either, assuming a properly configured server (and many people are using Cloudflare anyway).

@ghost
Copy link

ghost commented Feb 21, 2019

@jumarko I've opened a PR if you would like to pull it down and test it.

tk0miya added a commit that referenced this issue Feb 25, 2019
#3620: Defer searchindex.js rather than loading it via ajax
tchaikov added a commit to tchaikov/ceph that referenced this issue Aug 7, 2019
to address sphinx-doc/sphinx#3620, we need to
use sphinx with its fix at
sphinx-doc/sphinx@e049f86
in other words, we need to use sphinx v2.0.0 and up. but sphinx 2.0
requires python >= 3.5, so we have to use python3 for building the
documents.

in this change:

* doc-requirements.txt: install python3 packages on debian derivatives
* build-doc: install python3.6 packages from EPEL7, and use python3
  venv for using sphinx2
* doc-requirements.txt: bump up all python packages to latest
  stable.

Signed-off-by: Kefu Chai <kchai@redhat.com>
tchaikov added a commit to tchaikov/ceph that referenced this issue Aug 7, 2019
to address sphinx-doc/sphinx#3620, we need to
use sphinx with its fix at
sphinx-doc/sphinx@e049f86
in other words, we need to use sphinx v2.0.0 and up. but sphinx 2.0
requires python >= 3.5, so we have to use python3 for building the
documents.

in this change:

* doc-requirements.txt: install python3 packages on debian derivatives
* build-doc: install python3.6 packages from EPEL7, and use python3
  venv for using sphinx2
* doc-requirements.txt: bump up all python packages to latest
  stable.

Signed-off-by: Kefu Chai <kchai@redhat.com>
tchaikov added a commit to tchaikov/ceph that referenced this issue Aug 8, 2019
to address sphinx-doc/sphinx#3620, we need to
use sphinx with its fix at
sphinx-doc/sphinx@e049f86
in other words, we need to use sphinx v2.0.0 and up. but sphinx 2.0
requires python >= 3.5, so we have to use python3 for building the
documents.

in this change:

* doc-requirements.txt: install python3 packages on debian derivatives
* build-doc: install python3.6 packages from EPEL7, and use python3
  venv for using sphinx2
* doc-requirements.txt: bump up all python packages to latest
  stable.

Signed-off-by: Kefu Chai <kchai@redhat.com>
neha-ojha pushed a commit to ceph/ceph-ci that referenced this issue Aug 13, 2019
to address sphinx-doc/sphinx#3620, we need to
use sphinx with its fix at
sphinx-doc/sphinx@e049f86
in other words, we need to use sphinx v2.0.0 and up. but sphinx 2.0
requires python >= 3.5, so we have to use python3 for building the
documents.

in this change:

* doc-requirements.txt: install python3 packages on debian derivatives
* build-doc: install python3.6 packages from EPEL7, and use python3
  venv for using sphinx2
* doc-requirements.txt: bump up all python packages to latest
  stable.

Signed-off-by: Kefu Chai <kchai@redhat.com>
smithfarm pushed a commit to smithfarm/ceph that referenced this issue Oct 1, 2019
to address sphinx-doc/sphinx#3620, we need to
use sphinx with its fix at
sphinx-doc/sphinx@e049f86
in other words, we need to use sphinx v2.0.0 and up. but sphinx 2.0
requires python >= 3.5, so we have to use python3 for building the
documents.

in this change:

* doc-requirements.txt: install python3 packages on debian derivatives
* build-doc: install python3.6 packages from EPEL7, and use python3
  venv for using sphinx2
* doc-requirements.txt: bump up all python packages to latest
  stable.

Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit ace8cb1)

Conflicts:
	admin/doc-requirements.txt
smithfarm pushed a commit to smithfarm/ceph that referenced this issue Oct 1, 2019
to address sphinx-doc/sphinx#3620, we need to
use sphinx with its fix at
sphinx-doc/sphinx@e049f86
in other words, we need to use sphinx v2.0.0 and up. but sphinx 2.0
requires python >= 3.5, so we have to use python3 for building the
documents.

in this change:

* doc-requirements.txt: install python3 packages on debian derivatives
* build-doc: install python3.6 packages from EPEL7, and use python3
  venv for using sphinx2
* doc-requirements.txt: bump up all python packages to latest
  stable.

Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit ace8cb1)

Conflicts:
	admin/doc-requirements.txt
smithfarm pushed a commit to smithfarm/ceph that referenced this issue Oct 1, 2019
to address sphinx-doc/sphinx#3620, we need to
use sphinx with its fix at
sphinx-doc/sphinx@e049f86
in other words, we need to use sphinx v2.0.0 and up. but sphinx 2.0
requires python >= 3.5, so we have to use python3 for building the
documents.

in this change:

* doc-requirements.txt: install python3 packages on debian derivatives
* build-doc: install python3.6 packages from EPEL7, and use python3
  venv for using sphinx2
* doc-requirements.txt: bump up all python packages to latest
  stable.

Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit ace8cb1)
smithfarm pushed a commit to smithfarm/ceph that referenced this issue Oct 1, 2019
to address sphinx-doc/sphinx#3620, we need to
use sphinx with its fix at
sphinx-doc/sphinx@e049f86
in other words, we need to use sphinx v2.0.0 and up. but sphinx 2.0
requires python >= 3.5, so we have to use python3 for building the
documents.

in this change:

* doc-requirements.txt: install python3 packages on debian derivatives
* build-doc: install python3.6 packages from EPEL7, and use python3
  venv for using sphinx2
* doc-requirements.txt: bump up all python packages to latest
  stable.

Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit ace8cb1)

Conflicts:
	admin/doc-requirements.txt
	admin/build-doc
jkromwijk added a commit to jkromwijk/matplotlib that referenced this issue Nov 10, 2019
Hopefully a more future-proof fix, removing the dependency on ajax and
searchtools.js altogether. (After loading, searchindex.js itself does
depend on searchtools.js, but both are updated with the version of
sphinx that is used, unlike this template.)

Because the script is loaded with `defer` and also right at the end of
the body, the size of searchindex.js should not block loading the rest
of the page (one of the arguments for using ajax).
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
builder:html html theme type:enhancement enhance or introduce a new feature
Projects
None yet
Development

No branches or pull requests

4 participants