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

fix: Updated settings.py to dynamically populate ALLOWED_HOSTS using domains from the Django sites. #1384

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

amirtds
Copy link

@amirtds amirtds commented Nov 15, 2023

Change description

We received a security report highlighting a Host Header Injection vulnerability due to the use of a wildcard '*' in our ALLOWED_HOSTS setting. This configuration could lead to open redirects and other security risks.

I have modified settings.py to dynamically construct the ALLOWED_HOSTS list using domain names from our Django sites to ensures that only valid domains are served.

Changes:

  • Removed the wildcard '*' from ALLOWED_HOSTS.
  • Populated ALLOWED_HOSTS with domain names fetched from the Site model.
  • Ensured ENV_TOKENS['LMS_BASE'] and FEATURES['PREVIEW_LMS_BASE'] are included in ALLOWED_HOSTS if they are valid.
  • Added checks to prevent empty strings in ALLOWED_HOSTS.

Type of change

  • Bug fix (fixes an issue)
  • New feature (adds functionality)

Related issues

Fix #1

Checklists

Development

  • Lint rules pass locally
  • Application changes have been tested thoroughly
  • Automated tests covering modified code pass

Security

  • Security impact of change has been considered
  • Code follows company security practices and guidelines

Code review

  • Pull request has a descriptive title and context useful to a reviewer. Screenshots or screencasts are attached as necessary
  • "Ready for review" label attached and reviewers assigned
  • Changes have been reviewed by at least one other contributor
  • Pull request linked to task tracker where applicable

@amirtds amirtds changed the title fix: Updated settings.py to dynamically populate ALLOWED_HOSTS using domains from the Django sites framework. This change addresses a reported security vulnerability related to Host Header Injection, ensuring that only known and trusted domains are allowed. Removed the wildcard '*' and added robust checks for domain validity. fix: Updated settings.py to dynamically populate ALLOWED_HOSTS using domains from the Django sites. Nov 15, 2023

This comment has been minimized.

@amirtds amirtds force-pushed the fix/host-header-injection-vulnerabilit branch from cf74c4f to e390966 Compare November 15, 2023 22:48

This comment has been minimized.

FEATURES['PREVIEW_LMS_BASE'],
# Fetching all domain names from the Site model
site_domains = [site.domain for site in Site.objects.all()]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amirtds We will also need to cover custom domains

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bryanlandia I think the Sites has the custom domain too. I checked cockroach labs domain and could find it there. If not we can load it from somewhere else
Screenshot 2023-11-15 at 19 54 42

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right, the code actually switches Site.domain to be university.cockroachlabs.com and then sets alt domain to the original Tahoe one, but it's good to cover both in case some custom JS or something somewhere still uses the orig .tahoe.appsembler.com domain

@@ -27,7 +27,9 @@
import yaml
from corsheaders.defaults import default_headers as corsheaders_default_headers
from django.core.exceptions import ImproperlyConfigured
from django.contrib.sites.models import Site

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will apps have loaded by this point? I don't think so. We might need to do this in in an app.ready()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bryanlandia Moved it to openedx -> core -> djangoapps -> appsembler -> sites -> apps.py. Let me know wdyt

This comment has been minimized.

@amirtds amirtds force-pushed the fix/host-header-injection-vulnerabilit branch from 8027cfc to cfdb47a Compare November 16, 2023 00:57

This comment has been minimized.

@amirtds amirtds force-pushed the fix/host-header-injection-vulnerabilit branch 2 times, most recently from 3731306 to 75d177d Compare November 16, 2023 01:05

This comment has been minimized.

1 similar comment

This comment has been minimized.

@amirtds amirtds force-pushed the fix/host-header-injection-vulnerabilit branch from 75d177d to d345708 Compare November 16, 2023 01:08

This comment has been minimized.

Copy link

@bryanlandia bryanlandia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change is also needed in cms/envs/production.py
and we could include the alternative domains

openedx/core/djangoapps/appsembler/sites/apps.py Outdated Show resolved Hide resolved

This comment has been minimized.

@bryanlandia bryanlandia self-requested a review December 11, 2023 21:59

This comment has been minimized.

@bryanlandia
Copy link

@amirtds

Looks like the Docker build for checks is failing because py2neo is now End of Life and there are no longer any releases in GitHub for https://github.com/technige/py2neo

We'll need to update in another PR first. Maybe been fixed upstream so will check

@bryanlandia
Copy link

Yes will cherrypick openedx@1db6867

@bryanlandia
Copy link

Waiting on merge to main of #1387

@bryanlandia bryanlandia force-pushed the fix/host-header-injection-vulnerabilit branch from 5bebaf4 to 4d817ae Compare December 14, 2023 05:22

This comment has been minimized.

@amirtds
Copy link
Author

amirtds commented Jan 27, 2024

Hi @bryanlandia I added same settings for CMS as well, could you please take a look when you have some time

This comment has been minimized.

@amirtds amirtds force-pushed the fix/host-header-injection-vulnerabilit branch from 3c756b0 to 88217c3 Compare January 29, 2024 17:58
Copy link

Checking git merge conflicts against https://github.com/edx/edx-platform.git

Comparing with open-release/nutmeg.master
Benchmark conflicts with main 299
Current conflicts 299
Summary Good work! No added conflicts.
Comparing with master
Benchmark conflicts with main 318
Current conflicts 318
Summary Good work! No added conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants