Skip to content

Fix url_prefix failing to match request path when it contains non-ASCII characters - #508

Open
cristianchiriac wants to merge 2 commits into
Pylons:mainfrom
cristianchiriac:fix/url-prefix-unicode-encoding
Open

cristianchiriac wants to merge 2 commits into
Pylons:mainfrom
cristianchiriac:fix/url-prefix-unicode-encoding

Conversation

@cristianchiriac

Copy link
Copy Markdown

Fixes #492.

Bug

url_prefix fails to match the request path whenever it contains a character outside ASCII. The request path is decoded in parser.py as uri.decode("latin-1"), i.e. the raw UTF-8 bytes read back as latin-1 (the usual WSGI trick for carrying arbitrary bytes through str). url_prefix, on the other hand, is stored as whatever unicode string was passed in configuration, with no equivalent re-encoding. task.py's get_environment() then compares the two directly, so a url_prefix with non-ASCII characters never matches the corresponding (differently-encoded) path, and SCRIPT_NAME/PATH_INFO never get split correctly for it.

Fix

slash_fixed_str() in adjustments.py (used only for normalizing url_prefix) now re-encodes the string as UTF-8 bytes decoded as latin-1 after the existing slash normalization, matching the encoding the path comparison in task.py expects.

Test plan

  • Added test_url_prefix_non_ascii to TestAdjustments in tests/test_adjustments.py, checking that a non-ASCII url_prefix comes out re-encoded to match the path's encoding convention.
  • Verified the new test fails against the unmodified code and passes with the fix.
  • pytest tests/: 669 passed, 70 skipped (skips are pre-existing, unrelated to this change).
  • black --check and isort --check-only on the changed files: clean.

The request path is decoded from raw bytes as "UTF-8 bytes read as
latin-1", following the WSGI convention for PATH_INFO/SCRIPT_NAME (see
uri.decode("latin-1") in parser.py). url_prefix was left as the plain
unicode string passed in configuration, so comparing it against the
path in task.py's get_environment() never matched once url_prefix
contained a non-ASCII character, and SCRIPT_NAME/PATH_INFO were never
split correctly.

Apply the same UTF-8-as-latin-1 re-encoding to url_prefix at
configuration time, in slash_fixed_str(), so it lines up with the path
it's compared against.

Fixes Pylons#492
@cristianchiriac

Copy link
Copy Markdown
Author

Hi! Gentle ping on this fix. It's ready for review whenever you have a moment, and I'm happy to make any changes you'd like. Thanks!

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ce8Hr56xHurqiV5wEkVmxj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Waitress fails to match unicode url_prefix

1 participant