newrelic / newrelic-python-agent

New Relic Python Agent
https://docs.newrelic.com/docs/agents/python-agent
Apache License 2.0
178 stars 102 forks source link

Content length calculation raises TypeError #1164

Closed AlexanderArvidsson closed 5 months ago

AlexanderArvidsson commented 5 months ago

We've got alerts from the browser agent injection code in New Relic.

TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
  File "newrelic/api/asgi_application.py", line 307, in nr_async_asgi
    return await wrapped(scope, receive, send)
  File "django/core/handlers/asgi.py", line 170, in __call__
    await self.handle(scope, receive, send)
  File "django/core/handlers/asgi.py", line 216, in handle
    task.result()
  File "django/core/handlers/asgi.py", line 195, in process_request
    await self.send_response(response, send)
  File "django/core/handlers/asgi.py", line 357, in send_response
    await send(
  File "newrelic/api/asgi_application.py", line 177, in send_inject_browser_agent
    content_length = int(header_value)

Here's the offending code: https://github.com/newrelic/newrelic-python-agent/blob/45b575f8c5966b0a53216ad25bde6a659eff6e73/newrelic/api/asgi_application.py#L176-L183

The code does not take into consideration that content-length can be None, but it does check for None after the int conversion. The try-except should catch TypeError.


I'm using Django with Gunicorn, which to be fair is not really a supported framework, but it still works for us except for this piece of code that raises an error. So if that piece of code can just safely handle possibility of None in the header value, it should work fine.

workato-integration[bot] commented 5 months ago

https://new-relic.atlassian.net/browse/NR-280243

lrafeei commented 5 months ago

Hello--thanks for bringing this to our attention. There is actually a PR to fix this issue so this should be fixed in our next release

AlexanderArvidsson commented 5 months ago

Hello--thanks for bringing this to our attention. There is actually a PR to fix this issue so this should be fixed in our next release

Ouch, my apologies for jumping the gun and not checking PRs (or other issues for that matter), I was in a bit of a rush. This is a duplicate of #1150!