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 set trace attributes for django when host is not allowed #1026

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix set trace attributes for django when host is not allowed
`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.
```
  • Loading branch information
sirkonst committed Apr 28, 2021
commit 47e15f205ce04670a74aca967ac249fb2b698d22
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def process_request(self, request):
span.span_kind = span_module.SpanKind.SERVER
tracer.add_attribute_to_current_span(
attribute_key=HTTP_HOST,
attribute_value=request.get_host())
attribute_value=request._get_raw_host())
tracer.add_attribute_to_current_span(
attribute_key=HTTP_METHOD,
attribute_value=request.method)
Expand All @@ -220,7 +220,7 @@ def process_request(self, request):
attribute_value=str(request.path))
tracer.add_attribute_to_current_span(
attribute_key=HTTP_URL,
attribute_value=str(request.build_absolute_uri()))
attribute_value=str(request.get_raw_uri()))

# Add the span to thread local
# in some cases (exceptions, timeouts) currentspan in
Expand Down
55 changes: 54 additions & 1 deletion contrib/opencensus-ext-django/tests/test_django_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import unittest

import mock
from django.test import RequestFactory
from django.test import RequestFactory, override_settings
from django.test.utils import teardown_test_environment

from opencensus.trace import execution_context, print_exporter, samplers
Expand Down Expand Up @@ -129,6 +129,59 @@ def test_process_request(self):

self.assertEqual(span.name, 'mock.mock.Mock')

@override_settings(ALLOWED_HOSTS=['allowedhost'])
def test_process_request_with_disallowed_host(self):
from opencensus.ext.django import middleware

trace_id = '2dd43a1d6b2549c6bc2a1a54c2fc0b05'
span_id = '6e0c63257de34c92'
django_trace_id = '00-{}-{}-00'.format(trace_id, span_id)

django_request = RequestFactory().get('/wiki/Rabbit', **{
'HTTP_HOST': 'notallowedhost',
'HTTP_TRACEPARENT': django_trace_id})

# Force the test request to be sampled
settings = type('Test', (object,), {})
settings.OPENCENSUS = {
'TRACE': {
'SAMPLER': 'opencensus.trace.samplers.AlwaysOnSampler()', # noqa
}
}
patch_settings = mock.patch(
'django.conf.settings',
settings)

with patch_settings:
middleware_obj = middleware.OpencensusMiddleware()

# test process_request
middleware_obj.process_request(django_request)

tracer = middleware._get_current_tracer()

span = tracer.current_span()

expected_attributes = {
'http.host': u'notallowedhost',
'http.method': 'GET',
'http.path': u'/wiki/Rabbit',
'http.route': u'/wiki/Rabbit',
'http.url': u'http://notallowedhost/wiki/Rabbit',
}
self.assertEqual(span.span_kind, span_module.SpanKind.SERVER)
self.assertEqual(span.attributes, expected_attributes)
self.assertEqual(span.parent_span.span_id, span_id)

span_context = tracer.span_context
self.assertEqual(span_context.trace_id, trace_id)

# test process_view
view_func = mock.Mock()
middleware_obj.process_view(django_request, view_func)

self.assertEqual(span.name, 'mock.mock.Mock')

def test_excludelist_path(self):
from opencensus.ext.django import middleware

Expand Down