Skip to content

Avoid Django's ALLOWED_HOSTS check #3651

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

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

Conversation

jourdanrodrigues
Copy link

@jourdanrodrigues jourdanrodrigues commented Jul 25, 2025

Description

Django (eventually) calls HttpRequest.get_host when calling HttpRequest.build_absolute_uri, which we use in the middleware.

In the context I'm at, we have a case for setting up a health check as early as possible in a middleware that needs to bypass that check since Kubernetes hits the containers directly via IP.

I can put the middleware after the health check one but would be good to also instrument it to collect traces if that ever fails.

These changes will bring up the implementation of build_absolute_uri without any of the checks.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py, all tests under TestMiddleware.

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Copy link

linux-foundation-easycla bot commented Jul 25, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@jourdanrodrigues jourdanrodrigues changed the title Avoid Django's "ALLOWED_HOSTS" check Avoid Django's ALLOWED_HOSTS check Jul 25, 2025
Comment on lines +460 to +462
url = "{}://{}{}".format(
request.scheme, request._get_raw_host(), request.path
)
Copy link
Member

Choose a reason for hiding this comment

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

Will this break anyone who was relying on the previous behavior of request.build_absolute_uri("?")

Copy link
Author

@jourdanrodrigues jourdanrodrigues Jul 28, 2025

Choose a reason for hiding this comment

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

In my experience (not proof of anything, more as an example), host checks are intentional, not an accidental feature of any particular lib (reason why I'm proposing this).

The consequence of these changes is that the host won't be checked anymore, everything else works normally.

I can put these behind an environment variable, Django settings or just the current code under that method abstraction so that it can more easily be overridden, without having to copy over the whole code.

Let me know what you think!

Copy link
Member

Choose a reason for hiding this comment

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

I defer to the python maintainers on how they would want this configurable, if they do. Thanks for the explanation!

Copy link
Member

Choose a reason for hiding this comment

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

It's not clear to me why the previous implementation appended a ? to the URL being checked, but I'm wondering if we should keep it to minimize changes in behavior. What do you think @jourdanrodrigues?

@jourdanrodrigues jourdanrodrigues requested a review from a team as a code owner July 29, 2025 13:01
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.

3 participants