opencensus-python icon indicating copy to clipboard operation
opencensus-python copied to clipboard

Fix set trace attributes for django when host is not allowed

Open sirkonst opened this issue 4 years ago • 0 comments

request.get_host() raises DisallowedHost exception if request came with HOST witch not configured in ALLOWED_HOSTS setting.

Django provides special methods request._get_raw_host() and request.get_raw_uri() to get values like get_host() and build_absolute_uri() but without checking by ALLOWED_HOSTS.

Without fix error will be like:

ERROR    opencensus.ext.django.middleware:middleware.py:235 Failed to trace request
Traceback (most recent call last):
  File "/home/konst/develop/opencensus-python/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py", line 211, in process_request
    attribute_value=request.get_host())
  File "/home/konst/develop/opencensus-python/.nox/unit-2-7/lib/python2.7/site-packages/django/http/request.py", line 113, in get_host
    raise DisallowedHost(msg)
DisallowedHost: Invalid HTTP_HOST header: 'notallowedhost'. You may need to add u'notallowedhost' to ALLOWED_HOSTS.

sirkonst avatar Apr 28 '21 09:04 sirkonst