Skip to content

Commit 32053c6

Browse files
authored
Merge pull request #28588 from edx/loading-cors-origins-with-shceme
feat: `djang-cor-headers` need schemes with urls. Condition added for future.
2 parents 4efa9b5 + 5e84e37 commit 32053c6

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

cms/envs/production.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
from django.core.exceptions import ImproperlyConfigured
1717
from django.urls import reverse_lazy
1818
from edx_django_utils.plugins import add_plugins
19+
from importlib.metadata import version
1920
from path import Path as path
2021

22+
2123
from openedx.core.djangoapps.plugins.constants import ProjectType, SettingsType
2224

2325
from .common import *
@@ -554,6 +556,13 @@ def get_env_setting(setting):
554556
if FEATURES.get('ENABLE_CORS_HEADERS'):
555557
CORS_ALLOW_CREDENTIALS = True
556558
CORS_ORIGIN_WHITELIST = ENV_TOKENS.get('CORS_ORIGIN_WHITELIST', ())
559+
560+
# values are already updated above with default CORS_ORIGIN_WHITELIST values but in
561+
# case of new version django_cors_headers they will get override.
562+
cors_major_version = int(version('django_cors_headers').split('.')[0])
563+
if cors_major_version >= 3 and CORS_ORIGIN_WHITELIST and ENV_TOKENS.get('CORS_ORIGIN_WHITELIST_WITH_SCHEME'):
564+
CORS_ORIGIN_WHITELIST = ENV_TOKENS.get('CORS_ORIGIN_WHITELIST_WITH_SCHEME')
565+
557566
CORS_ORIGIN_ALLOW_ALL = ENV_TOKENS.get('CORS_ORIGIN_ALLOW_ALL', False)
558567
CORS_ALLOW_INSECURE = ENV_TOKENS.get('CORS_ALLOW_INSECURE', False)
559568
CORS_ALLOW_HEADERS = corsheaders_default_headers + (

lms/djangoapps/experiments/tests/test_views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from django.test.utils import override_settings
1414
from django.urls import reverse
1515
from django.utils.timezone import now
16+
from importlib_metadata import version
1617
from rest_framework.test import APITestCase
1718

1819
from common.djangoapps.student.tests.factories import UserFactory
@@ -260,6 +261,11 @@ def _cross_domain_post(self, csrf_token, data, referer=CROSS_DOMAIN_REFERER):
260261
**kwargs
261262
)
262263

264+
def test_white_list_contents_with_cors_header_version(self, *args): # pylint: disable=unused-argument
265+
""" Verify that with django-cor-header<3 it loads list without scheme. """
266+
assert settings.CORS_ORIGIN_WHITELIST == ['sandbox.edx.org']
267+
assert int(version('django_cors_headers').split('.')[0]) == 2
268+
263269

264270
class ExperimentKeyValueViewSetTests(APITestCase): # lint-amnesty, pylint: disable=missing-class-docstring
265271

lms/envs/production.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from corsheaders.defaults import default_headers as corsheaders_default_headers
2626
from django.core.exceptions import ImproperlyConfigured
2727
from edx_django_utils.plugins import add_plugins
28+
from importlib.metadata import version
2829
from path import Path as path
2930

3031
from openedx.core.djangoapps.plugins.constants import ProjectType, SettingsType
@@ -356,6 +357,13 @@ def get_env_setting(setting):
356357
if FEATURES.get('ENABLE_CORS_HEADERS') or FEATURES.get('ENABLE_CROSS_DOMAIN_CSRF_COOKIE'):
357358
CORS_ALLOW_CREDENTIALS = True
358359
CORS_ORIGIN_WHITELIST = ENV_TOKENS.get('CORS_ORIGIN_WHITELIST', ())
360+
361+
# values are already updated above with default CORS_ORIGIN_WHITELIST values but in
362+
# case of new version of django_cors_headers they will get override.
363+
cors_major_version = int(version('django_cors_headers').split('.')[0])
364+
if cors_major_version >= 3 and CORS_ORIGIN_WHITELIST and ENV_TOKENS.get('CORS_ORIGIN_WHITELIST_WITH_SCHEME'):
365+
CORS_ORIGIN_WHITELIST = ENV_TOKENS.get('CORS_ORIGIN_WHITELIST_WITH_SCHEME')
366+
359367
CORS_ORIGIN_ALLOW_ALL = ENV_TOKENS.get('CORS_ORIGIN_ALLOW_ALL', False)
360368
CORS_ALLOW_INSECURE = ENV_TOKENS.get('CORS_ALLOW_INSECURE', False)
361369
CORS_ALLOW_HEADERS = corsheaders_default_headers + (

lms/envs/test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import openid.oidutil
2121
from django.utils.translation import ugettext_lazy
2222
from edx_django_utils.plugins import add_plugins
23+
from importlib.metadata import version
2324
from path import Path as path
2425

2526
from openedx.core.djangoapps.plugins.constants import ProjectType, SettingsType
@@ -597,3 +598,12 @@
597598

598599
RESET_PASSWORD_TOKEN_VALIDATE_API_RATELIMIT = '2/m'
599600
RESET_PASSWORD_API_RATELIMIT = '2/m'
601+
602+
CORS_ORIGIN_WHITELIST = ['sandbox.edx.org']
603+
CORS_ORIGIN_WHITELIST_WITH_SCHEME = ['https://sandbox.edx.org']
604+
605+
# values are already updated above with default CORS_ORIGIN_WHITELIST values but in
606+
# case of new version django_cors_headers they will get override.
607+
cors_major_version = int(version('django_cors_headers').split('.')[0])
608+
if cors_major_version >= 3 and CORS_ORIGIN_WHITELIST and CORS_ORIGIN_WHITELIST_WITH_SCHEME:
609+
CORS_ORIGIN_WHITELIST = CORS_ORIGIN_WHITELIST_WITH_SCHEME

0 commit comments

Comments
 (0)