Skip to content
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
32 changes: 30 additions & 2 deletions src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::OnceLock;

use idna::punycode::decode_to_string;
use jiter::{PartialMode, StringCacheMode};
use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
use percent_encoding::{percent_encode, AsciiSet, CONTROLS};
use pyo3::exceptions::PyValueError;
use pyo3::pyclass::CompareOp;
use pyo3::sync::OnceLockExt;
Expand Down Expand Up @@ -602,8 +602,36 @@ fn is_punnycode_domain(lib_url: &Url, domain: &str) -> bool {
scheme_is_special(lib_url.scheme()) && domain.split('.').any(|part| part.starts_with(PUNYCODE_PREFIX))
}

/// See <https://url.spec.whatwg.org/#userinfo-percent-encode-set>
const USERINFO_ENCODE_SET: &AsciiSet = &CONTROLS
Copy link
Member

Choose a reason for hiding this comment

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

Note to self: ~ is no included in the set. RFC 1738 used to consider it an unsafe character, but RFC 3986 doesn't.

// query percent-encodes is controls plus the below
.add(b' ')
.add(b'"')
.add(b'#')
.add(b'<')
.add(b'>')
// path percent-encodes is query percent-encodes plus the below
.add(b'?')
.add(b'^')
.add(b'`')
.add(b'{')
.add(b'}')
// userinfo percent-encodes is path percent-encodes plus the below
.add(b'/')
.add(b':')
.add(b';')
.add(b'=')
.add(b'@')
.add(b'[')
.add(b'\\')
.add(b']')
.add(b'|')
// https://datatracker.ietf.org/doc/html/rfc3986.html#section-2.4
// we must also percent-encode '%'
.add(b'%');

fn encode_userinfo_component(value: &str) -> Cow<'_, str> {
let encoded = percent_encode(value.as_bytes(), NON_ALPHANUMERIC).to_string();
let encoded = percent_encode(value.as_bytes(), USERINFO_ENCODE_SET).to_string();
if encoded == value {
Cow::Borrowed(value)
} else {
Expand Down
33 changes: 20 additions & 13 deletions tests/validators/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,31 +1318,38 @@ def test_multi_url_build() -> None:
assert str(url) == 'postgresql://testuser:[email protected]:5432/database?sslmode=require#test'


def test_multi_url_build_encodes_credentials() -> None:
url = MultiHostUrl.build(
@pytest.mark.parametrize('url_type', [Url, MultiHostUrl])
def test_url_build_encodes_credentials(url_type: type[Union[Url, MultiHostUrl]]) -> None:
url = url_type.build(
scheme='postgresql',
username='user name',
password='p@ss/word?#',
password='p@ss/word?#__',
host='example.com',
port=5432,
)
assert url == MultiHostUrl('postgresql://user%20name:p%40ss%2Fword%3F%[email protected]:5432')
assert str(url) == 'postgresql://user%20name:p%40ss%2Fword%3F%[email protected]:5432'
assert url.hosts() == [
{'username': 'user%20name', 'password': 'p%40ss%2Fword%3F%23', 'host': 'example.com', 'port': 5432}
]
assert url == url_type('postgresql://user%20name:p%40ss%2Fword%3F%[email protected]:5432')
assert str(url) == 'postgresql://user%20name:p%40ss%2Fword%3F%[email protected]:5432'
if url_type is Url:
assert url.username == 'user%20name'
assert url.password == 'p%40ss%2Fword%3F%23__'
else:
assert url.hosts() == [
{'username': 'user%20name', 'password': 'p%40ss%2Fword%3F%23__', 'host': 'example.com', 'port': 5432}
]


def test_multi_url_build_hosts_encodes_credentials() -> None:
hosts = [
{'host': 'example.com', 'password': 'p@ss/word?#', 'username': 'user name', 'port': 5431},
{'host': 'example.org', 'password': 'pa%ss', 'username': 'other', 'port': 5432},
{'host': 'example.com', 'password': 'p@ss/word?#__', 'username': 'user name', 'port': 5431},
{'host': 'example.org', 'password': 'p@%ss__', 'username': 'other', 'port': 5432},
]
url = MultiHostUrl.build(scheme='postgresql', hosts=hosts)
assert str(url) == 'postgresql://user%20name:p%40ss%2Fword%3F%[email protected]:5431,other:pa%[email protected]:5432'
assert (
str(url) == 'postgresql://user%20name:p%40ss%2Fword%3F%[email protected]:5431,other:p%40%[email protected]:5432'
)
assert url.hosts() == [
{'username': 'user%20name', 'password': 'p%40ss%2Fword%3F%23', 'host': 'example.com', 'port': 5431},
{'username': 'other', 'password': 'pa%25ss', 'host': 'example.org', 'port': 5432},
{'username': 'user%20name', 'password': 'p%40ss%2Fword%3F%23__', 'host': 'example.com', 'port': 5431},
{'username': 'other', 'password': 'p%40%25ss__', 'host': 'example.org', 'port': 5432},
]


Expand Down
Loading