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

Terms and conditions error #1153

Merged
merged 8 commits into from
Feb 21, 2025
Merged

Terms and conditions error #1153

merged 8 commits into from
Feb 21, 2025

Conversation

swainn
Copy link
Member

@swainn swainn commented Feb 7, 2025

Description

This PR addresses an issue with loading error templates when the middleware hasn't been processed.

The specific issue was caused by a missing host in ALLOWED_HOSTS, which would end up raising this error:

ERROR Invalid HTTP_HOST header: '127.0.0.1:8000'. You may need to add '127.0.0.1' to ALLOWED_HOSTS.
Traceback (most recent call last):
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/utils/deprecation.py", line 128, in __call__
    response = self.process_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/middleware/common.py", line 48, in process_request
    host = request.get_host()
           ^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/http/request.py", line 151, in get_host
    raise DisallowedHost(msg)
django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: '127.0.0.1:8000'. You may need to add '127.0.0.1' to ALLOWED_HOSTS.
Exception inside application: 'ASGIRequest' object has no attribute 'user'
Traceback (most recent call last):
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/utils/deprecation.py", line 128, in __call__
    response = self.process_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/middleware/common.py", line 48, in process_request
    host = request.get_host()
           ^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/http/request.py", line 151, in get_host
    raise DisallowedHost(msg)
django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: '127.0.0.1:8000'. You may need to add '127.0.0.1' to ALLOWED_HOSTS.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/core/handlers/exception.py", line 164, in get_exception_response
    response = callback(request, exception=exception)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/tethys/tethys_portal/views/error.py", line 25, in handler_400
    return render(request, "tethys_portal/error.html", context, status=400)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/shortcuts.py", line 25, in render
    content = loader.render_to_string(template_name, context, request, using=using)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/template/backends/django.py", line 107, in render
    return self.template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/template/base.py", line 169, in render
    with context.bind_template(self):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/home/tethysdev/miniconda3/envs/tethys/lib/python3.12/site-packages/django/template/context.py", line 256, in bind_template
    context = processor(self.request)
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tethysdev/tethys/tethys_portal/context_processors.py", line 38, in tethys_portal_context
    or (request.user.is_authenticated and request.user.is_active)
        ^^^^^^^^^^^^
AttributeError: 'ASGIRequest' object has no attribute 'user'

<------------- AttributeError repeated 4-5 times ------------->

I suspect this was caused by the following chain of events:

  • ALLOWED_HOST error was raised
  • The error prevented the Middleware from running that adds the user to the request
  • Django tried to load the 400 error page of Tethys
  • The 400 page of Tethys inherits from base.html
  • The base.html includes a template from terms and conditions
  • Terms and conditions requires the user to be added to the request and raises an Attribute Error if it is not found.
  • This causes Django to try to load the 500 error page which triggers the Terms and Conditions Attribute Error again
  • It cycles a few more times and then dies

Changes Made to Code

  • Added check for request.user in the if block that includes the TOS template in the base.html and app_base.html templates.
  • Adds {% block tos_override %}{% endblock %} override to the error.html base template for the error pages, so terms and conditions is never loaded on the error pages.
  • Add check for request.user to context processor that could cause similar issue

Related PRs, Issues, and Discussions

  • N/A

Additional Notes

  • N/A

Quality Checks

  • At least one new test has been written for new code
  • New code has 100% test coverage
  • Code has been formatted with Black
  • Code has been linted with flake8
  • Docstrings for new methods have been added
  • The documentation has been updated appropriately

@swainn swainn added the bugfix label Feb 7, 2025
@swainn swainn requested review from sdc50 and shawncrawley February 7, 2025 16:59
@swainn swainn self-assigned this Feb 7, 2025
@coveralls
Copy link

coveralls commented Feb 7, 2025

Coverage Status

coverage: 100.0%. remained the same
when pulling 33c9326 on terms_and_conditions_error
into 9b8ea21 on main.

@swainn swainn marked this pull request as ready for review February 7, 2025 17:07
@swainn swainn force-pushed the terms_and_conditions_error branch from 56a9696 to c3c3aad Compare February 7, 2025 17:17
@sdc50 sdc50 force-pushed the terms_and_conditions_error branch from 2f2ab02 to 33c9326 Compare February 21, 2025 19:37
@sdc50 sdc50 merged commit 1182270 into main Feb 21, 2025
42 checks passed
swainn added a commit that referenced this pull request Mar 5, 2025
* Exploration - appears to be issue with allowed_hosts

* Don't load terms and conditions middleware when there is not user attached to the request.

* Override the terms of service block in the error templates
The error pages need to load when middleware hasn't been fully processed sometimes and TOS can't handle not having the user added to the request.

* Revert settings changes

* blacken and lint

* Add test and and addition request.user check form tos

* Fix error with has_terms check

* Add explanation of commented out error routes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants