Skip to content

Reject trailing newline in percent-decoded URL userinfo - #604

Open
shashank11yadav wants to merge 1 commit into
python-validators:masterfrom
shashank11yadav:fix/url-userinfo-trailing-newline
Open

shashank11yadav wants to merge 1 commit into
python-validators:masterfrom
shashank11yadav:fix/url-userinfo-trailing-newline

Conversation

@shashank11yadav

Copy link
Copy Markdown

Fixes #603

validators.url() percent-decodes a userinfo that has two or more colons before matching it against the username regex. A decoded newline in the middle of that userinfo was rejected, but the same newline at the very end was accepted. The closing $ in two branches of _username_regex also matches just before a trailing newline. So url("http://a:b:c%0A@example.com") returned True, while url("http://a:b:c%0Ad@example.com") returned a ValidationError.

This PR changes those two closing $ anchors to \Z (the dot-atom and non-quoted-string branches in src/validators/url.py). Only a value that ends in exactly one newline changes behaviour. The extended-Latin branch is not touched.

This is a consistency fix in the same class as #492 and #513, which made slug() and the hostname and port checks reject a trailing newline. It is not a claim that RFC 3986 forbids %0A in userinfo. Path and query still accept percent-encoded controls, and this PR does not change that.

Tests: one parametrized test, test_returns_failed_validation_on_newline_in_decoded_userinfo in tests/test_url.py, with six cases. Three trailing-newline cases fail before the fix and pass after it. The other three (a newline in the middle, at the start, and two trailing newlines) were already rejected and stay rejected.

How it was tested (commit 36c82f4):

  • uv run --no-sync pytest tests/test_url.py: 334 passed.
  • uv run --no-sync pytest tests/test_url.py -k test_returns_failed_validation_on_newline_in_decoded_userinfo: 6 passed.
  • The new test run without the fix: 3 failed, 3 passed (the three trailing-newline cases fail).
  • uv run --no-sync ruff check .: pass.
  • uv run --no-sync ruff format --check .: pass.

Not run: pyright (uv run --no-sync pyright ., which CI runs) and bandit (uv run --no-sync bandit -c pyproject.toml -r src/validators, which CI runs). Neither was run for this change.

A userinfo with two or more colons is percent-decoded before it is
matched against the username regex. A decoded newline in the middle was
rejected, but one at the very end was accepted, because `$` also matches
just before a trailing newline. Anchor the dot-atom and non-quoted-string
branches with `\Z` so both positions agree.

This branch has not been deployed

No deployments
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.

url(): userinfo with two or more colons accepts a trailing newline (%0A)

1 participant