diff --git a/openedx/core/lib/x_forwarded_for/middleware.py b/openedx/core/lib/x_forwarded_for/middleware.py index 4a9d287dc9..034c1a7838 100644 --- a/openedx/core/lib/x_forwarded_for/middleware.py +++ b/openedx/core/lib/x_forwarded_for/middleware.py @@ -51,6 +51,12 @@ class XForwardedForMiddleware(MiddlewareMixin): # Give some observability into IP chain length and composition. Useful # for monitoring in case of unexpected network config changes, etc. ip_chain = ip.get_raw_ip_chain(request) + + # .. custom_attribute_name: ip_chain.raw + # .. custom_attribute_description: The actual contents of the raw IP chain. Could + # be used to correlate authenticated and unauthenticated requests for the same + # user. + set_custom_attribute('ip_chain.raw', ', '.join(ip_chain)) set_custom_attribute('ip_chain.count', len(ip_chain)) set_custom_attribute('ip_chain.types', '-'.join(_ip_type(s) for s in ip_chain)) diff --git a/openedx/core/lib/x_forwarded_for/tests/test_middleware.py b/openedx/core/lib/x_forwarded_for/tests/test_middleware.py index fae3840a26..5b5d28bc92 100644 --- a/openedx/core/lib/x_forwarded_for/tests/test_middleware.py +++ b/openedx/core/lib/x_forwarded_for/tests/test_middleware.py @@ -60,12 +60,12 @@ class TestXForwardedForMiddleware(TestCase): @ddt.unpack @ddt.data( - (None, 1, 'priv'), - ('1.2.3.4', 2, 'pub-priv'), - ('XXXXXXXX, 1.2.3.4, 5.5.5.5', 4, 'unknown-pub-pub-priv'), + (None, '127.0.0.1', 1, 'priv'), + ('1.2.3.4', '1.2.3.4, 127.0.0.1', 2, 'pub-priv'), + ('XXXXXXXX, 1.2.3.4, 5.5.5.5', 'XXXXXXXX, 1.2.3.4, 5.5.5.5, 127.0.0.1', 4, 'unknown-pub-pub-priv'), ) @patch("openedx.core.lib.x_forwarded_for.middleware.set_custom_attribute") - def test_xff_metrics(self, xff, expected_count, expected_types, mock_set_custom_attribute): + def test_xff_metrics(self, xff, expected_raw, expected_count, expected_types, mock_set_custom_attribute): request = RequestFactory().get('/somewhere') if xff is not None: request.META['HTTP_X_FORWARDED_FOR'] = xff @@ -73,6 +73,7 @@ class TestXForwardedForMiddleware(TestCase): XForwardedForMiddleware().process_request(request) mock_set_custom_attribute.assert_has_calls([ + call('ip_chain.raw', expected_raw), call('ip_chain.count', expected_count), call('ip_chain.types', expected_types), ])