Skip to content

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

Backlog

Description

@shashank11yadav

Describe the bug

validators.url handles %0A differently depending on where it sits in the userinfo. When the userinfo has two or more colons, the value goes through unquote and then _username_regex().match(...). The non-quoted-string branch of that regex ends in $, which also matches before a trailing newline. A decoded \n at the end is therefore never checked, while the same %0A in the middle is rejected.

To reproduce

import validators

mid = "http://a:b:c%0Ad@example.com"  # newline in the middle
end = "http://a:b:c%0A@example.com"  # newline at the end
print("mid", validators.url(mid) is True, "end", validators.url(end) is True)

Output on master (22786cc) and on validators 0.36.0:

mid False end True

Expected behavior

end should be rejected like mid, so the script should print mid False end False. The non-quoted-string branch of _username_regex (url.py:22) excludes the newline character, but the trailing $ lets one through at the end of the string.

Versions

  • validators 0.36.0 and master @ 22786cc
  • Python 3.12, Linux

Additional context

Cause: src/validators/url.py:81, _username_regex().match(unquote(value)), combined with $ at the end of the pattern. Using \Z instead of $ would make both cases behave the same. The same kind of trailing-newline problem was fixed for slug() in #492.

I know %0A is a legal percent-encoding in a URL, so this is about consistency rather than a spec violation.

I have a fix with a regression test ready and will open a pull request for it right after filing this issue; it will link back here.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions