From 504abd1584274f29088fee8204daaa2c55df76f3 Mon Sep 17 00:00:00 2001 From: Hannah Stepanek Date: Mon, 10 Jun 2024 15:47:39 -0700 Subject: [PATCH 1/2] Fix bug in asgi when no content-length header Previously, if there was no content length header and browser injection was enabled, the asgi wrapper would fail with a type error as the header_value would be None. Now, the except clause catches both type and value errors. --- newrelic/api/asgi_application.py | 2 +- tests/agent_features/test_asgi_browser.py | 24 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/newrelic/api/asgi_application.py b/newrelic/api/asgi_application.py index 475faa7cbb..dc2fd5bbf4 100644 --- a/newrelic/api/asgi_application.py +++ b/newrelic/api/asgi_application.py @@ -175,7 +175,7 @@ async def send_inject_browser_agent(self, message): try: content_length = int(header_value) - except ValueError: + except (TypeError, ValueError): # Invalid content length results in an abort await self.send_buffered() return diff --git a/tests/agent_features/test_asgi_browser.py b/tests/agent_features/test_asgi_browser.py index 925f186d6e..59ce94c6a2 100644 --- a/tests/agent_features/test_asgi_browser.py +++ b/tests/agent_features/test_asgi_browser.py @@ -653,6 +653,19 @@ async def target_asgi_application_invalid_content_length(scope, receive, send): target_application_invalid_content_length = AsgiTest(target_asgi_application_invalid_content_length) + +@asgi_application() +async def target_asgi_application_no_content_length(scope, receive, send): + output = b"

RESPONSE

" + + response_headers = [(b"content-type", b"text/html; charset=utf-8")] + + await send({"type": "http.response.start", "status": 200, "headers": response_headers}) + await send({"type": "http.response.body", "body": output}) + + +target_application_no_content_length = AsgiTest(target_asgi_application_no_content_length) + _test_html_insertion_invalid_content_length_settings = { "browser_monitoring.enabled": True, "browser_monitoring.auto_instrument": True, @@ -674,6 +687,17 @@ def test_html_insertion_invalid_content_length(): assert b"NREUM.info" not in response.body +@override_application_settings(_test_html_insertion_invalid_content_length_settings) +def test_html_insertion_invalid_content_length(): + response = target_application_no_content_length.get("/") + assert response.status == 200 + + assert "content-type" in response.headers + + assert b"NREUM HEADER" not in response.body + assert b"NREUM.info" not in response.body + + @asgi_application() async def target_asgi_application_content_encoding(scope, receive, send): output = b"

RESPONSE

" From 52a452a2846434e868fce78085906290cebd62f6 Mon Sep 17 00:00:00 2001 From: Hannah Stepanek Date: Wed, 12 Jun 2024 11:46:44 -0700 Subject: [PATCH 2/2] Fixup: de-duplicate test name --- tests/agent_features/test_asgi_browser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/agent_features/test_asgi_browser.py b/tests/agent_features/test_asgi_browser.py index 59ce94c6a2..26c0d56702 100644 --- a/tests/agent_features/test_asgi_browser.py +++ b/tests/agent_features/test_asgi_browser.py @@ -688,7 +688,7 @@ def test_html_insertion_invalid_content_length(): @override_application_settings(_test_html_insertion_invalid_content_length_settings) -def test_html_insertion_invalid_content_length(): +def test_html_insertion_no_content_length(): response = target_application_no_content_length.get("/") assert response.status == 200