Skip to content

Fix wsgi invalid request uri#4551

Open
IonDragos2003 wants to merge 6 commits intoopen-telemetry:mainfrom
IonDragos2003:fix-wsgi-invalid-request-uri
Open

Fix wsgi invalid request uri#4551
IonDragos2003 wants to merge 6 commits intoopen-telemetry:mainfrom
IonDragos2003:fix-wsgi-invalid-request-uri

Conversation

@IonDragos2003
Copy link
Copy Markdown

@IonDragos2003 IonDragos2003 commented May 7, 2026

Description

Fixes #4447

WSGI Instrumentation currently parses RAW_URI / REQUEST_URI to derive url.path & url.query. Malformed request URIs can cause urllib.parse.urlparse to raise a ValueError, which breaks instrumentation and may result in a 500 response.

This change avoids parsing the raw request URI and instead derives url.path and url.query directly from the WSGI environ (PATH_INFO and QUERY_STRING).

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Added a regression test:
    • test_request_attributes_with_invalid_request_uri_uses_wsgi_environ
    • Verifies that malformed REQUEST_URI does not raise and that URL_PATH / URL_QUERY are derived from WSGI environ.
  • Updated existing test:
    • test_request_attributes_with_full_request_uri to reflect new behavior (no parsing of REQUEST_URI).

Ran tests:
uv run python -m pytest instrumentation/opentelemetry-instrumentation-wsgi/tests

All tests pass.

Does This PR Require a Core Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Copy link
Copy Markdown
Member

@emdneto emdneto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example.com/ still valid no? I don't see the exception running this test against main

Comment thread instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py Outdated
@IonDragos2003 IonDragos2003 force-pushed the fix-wsgi-invalid-request-uri branch from 99e5faf to a1ca22a Compare May 8, 2026 12:42
@IonDragos2003
Copy link
Copy Markdown
Author

IonDragos2003 commented May 8, 2026

Hi @emdneto, seems that these changes are now breaking some Falcon tests.

Should I do something like

if target:
    try:
        path, query = _parse_url_query(target)
    except ValueError:
        path = environ.get("PATH_INFO")
        query = environ.get("QUERY_STRING")
    _set_http_target(result, target, path, query, sem_conv_opt_in_mode)

@emdneto
Copy link
Copy Markdown
Member

emdneto commented May 8, 2026

Hi @emdneto, seems that these changes are now breaking some Falcon tests.

Should I do something like

if target:
    try:
        path, query = _parse_url_query(target)
    except ValueError:
        path = environ.get("PATH_INFO")
        query = environ.get("QUERY_STRING")
    _set_http_target(result, target, path, query, sem_conv_opt_in_mode)

@IonDragos2003

I would fix the Falcon tests instead, since the target is no longer used to parse the path and query. Because of that, _has_fixed_http_target in the Falcon tests no longer has the same effect it used to.

Also, please fix precommit by running tox -e precommit and add a changelog entry! Thanks

}
expected_new = {
URL_PATH: "/3/library/urllib.parse.html",
URL_QUERY: "highlight=params",
Copy link
Copy Markdown
Member

@emdneto emdneto May 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the issue with the URL_QUERY here? Can we update the test to still reflect that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

WSGI instrumentation returns 500 when given invalid url

2 participants