Skip to content
Open
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>
///
/// Note that this doesn't actually include % itself - see the note in
/// https://url.spec.whatwg.org/#string-percent-encode-after-encoding
const USERINFO_ENCODE_SET: &AsciiSet = &CONTROLS
// 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'|');

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:testpassword@127.0.0.1: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%23@example.com:5432')
assert str(url) == 'postgresql://user%20name:p%40ss%2Fword%3F%23@example.com: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%23__@example.com:5432')
assert str(url) == 'postgresql://user%20name:p%40ss%2Fword%3F%23__@example.com: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%23@example.com:5431,other:pa%25ss@example.org:5432'
assert (
str(url) == 'postgresql://user%20name:p%40ss%2Fword%3F%23__@example.com:5431,other:p%40%ss__@example.org: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%ss__', 'host': 'example.org', 'port': 5432},
]


Expand Down
Loading