Skip to content
This repository was archived by the owner on Feb 26, 2026. It is now read-only.
This repository was archived by the owner on Feb 26, 2026. It is now read-only.

Logging fails with DisallowedHost exception with Django + invalid HTTP_HOST header #757

@davidwtbuxton

Description

@davidwtbuxton

Environment details

  • OS type and version: any
  • Python version: python --version python3.9
  • pip version: pip --version any
  • google-cloud-logging version: pip show google-cloud-logging 3.5.0

Steps to reproduce

  1. Django 3.2.19 on App Engine standard Python 3.9.
  2. google-cloud-logging 3.5.0, with google.cloud.logging.handlers.middleware.RequestMiddleware middleware added to project's settings.py
  3. Request has an invalid Host header, e.g. example.com%7d

Stack trace

 Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/runtime/middlewares.py", line 140, in ErrorLoggingMiddleware
    return app(wsgi_env, start_response)
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/runtime/middlewares.py", line 82, in <lambda>
    lambda app: lambda wsgi_env, start_resp: f(app, wsgi_env, start_resp),
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/runtime/middlewares.py", line 378, in BackgroundAndShutdownMiddleware
    return app(wsgi_env, start_response)
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/runtime/middlewares.py", line 82, in <lambda>
    lambda app: lambda wsgi_env, start_resp: f(app, wsgi_env, start_resp),
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/runtime/middlewares.py", line 405, in SetNamespaceFromHeader
    return app(wsgi_env, start_response)
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/core/handlers/wsgi.py", line 133, in __call__
    response = self.get_response(request)
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/core/handlers/base.py", line 130, in get_response
    response = self._middleware_chain(request)
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/core/handlers/exception.py", line 49, in inner
    response = response_for_exception(request, exc)
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/core/handlers/exception.py", line 99, in response_for_exception
    security_logger.error(
  File "/layers/google.python.runtime/python/lib/python3.9/logging/__init__.py", line 1475, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/layers/google.python.runtime/python/lib/python3.9/logging/__init__.py", line 1589, in _log
    self.handle(record)
  File "/layers/google.python.runtime/python/lib/python3.9/logging/__init__.py", line 1599, in handle
    self.callHandlers(record)
  File "/layers/google.python.runtime/python/lib/python3.9/logging/__init__.py", line 1661, in callHandlers
    hdlr.handle(record)
  File "/layers/google.python.runtime/python/lib/python3.9/logging/__init__.py", line 948, in handle
    rv = self.filter(record)
  File "/layers/google.python.runtime/python/lib/python3.9/logging/__init__.py", line 806, in filter
    result = f.filter(record)
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/handlers/handlers.py", line 90, in filter
    ) = get_request_data()
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/handlers/_helpers.py", line 202, in get_request_data
    http_request, trace_id, span_id, trace_sampled = checker()
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/handlers/_helpers.py", line 110, in get_request_data_from_django
    "requestUrl": request.build_absolute_uri(),
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/http/request.py", line 238, in build_absolute_uri
    location = self._current_scheme_host + location
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/http/request.py", line 248, in _current_scheme_host
    return '{}://{}'.format(self.scheme, self.get_host())
  File "/layers/google.python.pip/pip/lib/python3.9/site-packages/django/http/request.py", line 151, in get_host
    raise DisallowedHost(msg)
django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: 'example.com%7d'. The domain name provided is not valid according to RFC 1034/1035. 

Metadata

Metadata

Assignees

Labels

api: loggingIssues related to the googleapis/python-logging API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions