-
Notifications
You must be signed in to change notification settings - Fork 312
fix: only percent-encode characters in the userinfo encode set #1852
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
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| // 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 { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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%ss__', 'host': 'example.org', 'port': 5432}, | ||
| ] | ||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RFC 3986 says:
And the note in the WHATWG spec isn't clear to me:
As I understand it, it says that some encoding sets don't encode
%, and as such%need special-casing and must be percent-encoded?Before #1829, it seems that
%wasn't encoded:But following what I said previously this should be a bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ad320a7