-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Use new maintained django-cors-headers package #10000
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# pylint: disable=missing-docstring | ||
|
||
import os | ||
import re | ||
import subprocess | ||
import socket | ||
|
||
|
@@ -9,6 +10,7 @@ | |
from celery.schedules import crontab | ||
|
||
from readthedocs.core.logs import shared_processors | ||
from corsheaders.defaults import default_headers | ||
from readthedocs.core.settings import Settings | ||
|
||
|
||
|
@@ -277,14 +279,14 @@ def MIDDLEWARE(self): | |
'readthedocs.core.middleware.NullCharactersMiddleware', | ||
'readthedocs.core.middleware.ReadTheDocsSessionMiddleware', | ||
'django.middleware.locale.LocaleMiddleware', | ||
'corsheaders.middleware.CorsMiddleware', | ||
'django.middleware.common.CommonMiddleware', | ||
'django.middleware.security.SecurityMiddleware', | ||
'django.middleware.csrf.CsrfViewMiddleware', | ||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'django.contrib.messages.middleware.MessageMiddleware', | ||
'dj_pagination.middleware.PaginationMiddleware', | ||
'corsheaders.middleware.CorsMiddleware', | ||
'csp.middleware.CSPMiddleware', | ||
'readthedocs.core.middleware.ReferrerPolicyMiddleware', | ||
'simple_history.middleware.HistoryRequestMiddleware', | ||
|
@@ -734,15 +736,17 @@ def DOCKER_LIMITS(self): | |
# users to CSRF attacks. The sustainability API is the only view that requires | ||
# cookies to be send cross-site, we override that for that view only. | ||
CORS_ALLOW_CREDENTIALS = False | ||
CORS_ALLOW_HEADERS = ( | ||
'x-requested-with', | ||
'content-type', | ||
'accept', | ||
'origin', | ||
'authorization', | ||
|
||
# Allow cross-site requests from any origin, | ||
# all information from our allowed endpoits is public. | ||
# | ||
# NOTE: We don't use `CORS_ALLOW_ALL_ORIGINS=True`, | ||
# since that will set the `Access-Control-Allow-Origin` header to `*`, | ||
# we won't be able to pass credentials fo the sustainability API with that value. | ||
CORS_ALLOWED_ORIGIN_REGEXES = [re.compile(".+")] | ||
CORS_ALLOW_HEADERS = list(default_headers) + [ | ||
'x-hoverxref-version', | ||
'x-csrftoken' | ||
) | ||
] | ||
# Additional protection to allow only idempotent methods. | ||
CORS_ALLOW_METHODS = [ | ||
'GET', | ||
|
@@ -751,14 +755,19 @@ def DOCKER_LIMITS(self): | |
] | ||
|
||
# URLs to allow CORS to read from unauthed. | ||
CORS_URLS_ALLOW_ALL_REGEX = [ | ||
r"^/api/v2/footer_html", | ||
r"^/api/v2/search", | ||
r"^/api/v2/docsearch", | ||
r"^/api/v2/embed", | ||
r"^/api/v3/embed", | ||
r"^/api/v2/sustainability", | ||
] | ||
CORS_URLS_REGEX = re.compile( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the docs it says this is a string. However, it also says There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, that's the type https://docs.python.org/3/library/typing.html#typing.Pattern. |
||
r""" | ||
^( | ||
/api/v2/footer_html | ||
|/api/v2/search | ||
|/api/v2/docsearch | ||
|/api/v2/embed | ||
|/api/v3/embed | ||
|/api/v2/sustainability | ||
) | ||
""", | ||
re.VERBOSE, | ||
) | ||
|
||
# RTD Settings | ||
ALLOW_PRIVATE_REPOS = False | ||
|
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.
Just to note we are adding 3 extra allowed headers with this change: