Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

187 safe url string already encoded user pass #196

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,37 @@ def test_safe_url_string_userinfo_unsafe_chars(
"ftp://admin:%7C%25@example.com",
)

def test_safe_url_string_user_and_pass_percentage_encoded(self):
self.assertEqual(
safe_url_string("http://%25user:%25pass@host"),
"http://%25user:%25pass@host",
)

self.assertEqual(
safe_url_string("http://%user:%pass@host"),
"http://%25user:%25pass@host",
)

self.assertEqual(
safe_url_string("http://%26user:%26pass@host"),
"http://&user:&pass@host",
)

self.assertEqual(
safe_url_string("http://%2525user:%2525pass@host"),
"http://%2525user:%2525pass@host",
)

self.assertEqual(
safe_url_string("http://%2526user:%2526pass@host"),
"http://%2526user:%2526pass@host",
)

self.assertEqual(
safe_url_string("http://%25%26user:%25%26pass@host"),
"http://%25&user:%25&pass@host",
)

def test_safe_download_url(self):
self.assertEqual(
safe_download_url("http://www.example.org"), "http://www.example.org/"
Expand Down
5 changes: 3 additions & 2 deletions w3lib/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
urlsplit,
urlunparse,
urlunsplit,
unquote,
)
from urllib.parse import _coerce_args # type: ignore
from urllib.request import pathname2url, url2pathname
Expand Down Expand Up @@ -105,11 +106,11 @@ def safe_url_string(
netloc_bytes = b""
if username is not None or password is not None:
if username is not None:
safe_username = quote(username, RFC3986_USERINFO_SAFE_CHARS)
safe_username = quote(unquote(username), RFC3986_USERINFO_SAFE_CHARS)
netloc_bytes += safe_username.encode(encoding)
if password is not None:
netloc_bytes += b":"
safe_password = quote(password, RFC3986_USERINFO_SAFE_CHARS)
safe_password = quote(unquote(password), RFC3986_USERINFO_SAFE_CHARS)
netloc_bytes += safe_password.encode(encoding)
netloc_bytes += b"@"
if hostname is not None:
Expand Down