Skip to content

Commit 2995261

Browse files
Add try-except to web request parsing (#1449)
* Add try-except to web request parsing * Fix parsing logic * Add clarifying comment --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent e7c4c02 commit 2995261

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

newrelic/api/web_transaction.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -665,13 +665,22 @@ def __init__(self, application, environ, source=None):
665665
self._request_uri = request_uri
666666

667667
if self._request_uri is not None:
668-
# Need to make sure we drop off any query string
669-
# arguments on the path if we have to fallback
670-
# to using the original REQUEST_URI. Can't use
671-
# attribute access on result as only support for
672-
# Python 2.5+.
673-
674-
self._request_uri = urlparse.urlparse(self._request_uri)[2]
668+
# This try/except logic is to handle cases where
669+
# `REQUEST_URI` is malformed or contains invalid
670+
# characters. This can happen at this point if
671+
# a malformed request is passed in and for versions
672+
# of `urllib` in the Python standard library older
673+
# than what has been released Jan 31, 2025.
674+
try:
675+
# Need to make sure we drop off any query string
676+
# arguments on the path if we have to fallback
677+
# to using the original REQUEST_URI. Can't use
678+
# attribute access on result as only support for
679+
# Python 2.5+.
680+
self._request_uri = urlparse.urlparse(self._request_uri)[2]
681+
except:
682+
# If `self._request_uri` is invalid, set to `None`
683+
self._request_uri = None
675684

676685
if script_name is not None or path_info is not None:
677686
if path_info is None:

0 commit comments

Comments
 (0)