-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redact basic authentication passwords from log messages (#5773)
Redact basic authentication passwords from URLs.
- Loading branch information
Showing
8 changed files
with
84 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Redact the password from the URL in various log messages. |
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -890,15 +890,24 @@ def split_auth_from_netloc(netloc): | |
return netloc, user_pass | ||
|
||
|
||
def remove_auth_from_url(url): | ||
# Return a copy of url with 'username:password@' removed. | ||
# username/pass params are passed to subversion through flags | ||
# and are not recognized in the url. | ||
def redact_netloc(netloc): | ||
""" | ||
Replace the password in a netloc with "****", if it exists. | ||
For example, "user:[email protected]" returns "user:****@example.com". | ||
""" | ||
netloc, (user, password) = split_auth_from_netloc(netloc) | ||
if user is None: | ||
return netloc | ||
password = '' if password is None else ':****' | ||
return '{user}{password}@{netloc}'.format(user=user, | ||
password=password, | ||
netloc=netloc) | ||
|
||
# parsed url | ||
purl = urllib_parse.urlsplit(url) | ||
netloc, user_pass = split_auth_from_netloc(purl.netloc) | ||
|
||
def _transform_url(url, transform_netloc): | ||
purl = urllib_parse.urlsplit(url) | ||
netloc = transform_netloc(purl.netloc) | ||
# stripped url | ||
url_pieces = ( | ||
purl.scheme, netloc, purl.path, purl.query, purl.fragment | ||
|
@@ -907,6 +916,22 @@ def remove_auth_from_url(url): | |
return surl | ||
|
||
|
||
def _get_netloc(netloc): | ||
return split_auth_from_netloc(netloc)[0] | ||
|
||
|
||
def remove_auth_from_url(url): | ||
# Return a copy of url with 'username:password@' removed. | ||
# username/pass params are passed to subversion through flags | ||
# and are not recognized in the url. | ||
return _transform_url(url, _get_netloc) | ||
|
||
|
||
def redact_password_from_url(url): | ||
"""Replace the password in a given url with ****.""" | ||
return _transform_url(url, redact_netloc) | ||
|
||
|
||
def protect_pip_from_modification_on_windows(modifying_pip): | ||
"""Protection of pip.exe from modification on Windows | ||
|
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -24,8 +24,9 @@ | |
from pip._internal.utils.hashes import Hashes, MissingHashes | ||
from pip._internal.utils.misc import ( | ||
call_subprocess, egg_link_path, ensure_dir, get_installed_distributions, | ||
get_prog, make_vcs_requirement_url, normalize_path, remove_auth_from_url, | ||
rmtree, split_auth_from_netloc, untar_file, unzip_file, | ||
get_prog, make_vcs_requirement_url, normalize_path, redact_netloc, | ||
redact_password_from_url, remove_auth_from_url, rmtree, | ||
split_auth_from_netloc, untar_file, unzip_file, | ||
) | ||
from pip._internal.utils.packaging import check_dist_requires_python | ||
from pip._internal.utils.temp_dir import TempDirectory | ||
|
@@ -662,6 +663,25 @@ def test_split_auth_from_netloc(netloc, expected): | |
assert actual == expected | ||
|
||
|
||
@pytest.mark.parametrize('netloc, expected', [ | ||
# Test a basic case. | ||
('example.com', 'example.com'), | ||
# Test with username and no password. | ||
('[email protected]', '[email protected]'), | ||
# Test with username and password. | ||
('user:[email protected]', 'user:****@example.com'), | ||
# Test with username and empty password. | ||
('user:@example.com', 'user:****@example.com'), | ||
# Test the password containing an @ symbol. | ||
('user:pass@[email protected]', 'user:****@example.com'), | ||
# Test the password containing a : symbol. | ||
('user:pass:[email protected]', 'user:****@example.com'), | ||
]) | ||
def test_redact_netloc(netloc, expected): | ||
actual = redact_netloc(netloc) | ||
assert actual == expected | ||
|
||
|
||
@pytest.mark.parametrize('auth_url, expected_url', [ | ||
('https://user:[email protected]/project/tags/v0.2', | ||
'https://domain.tld/project/tags/v0.2'), | ||
|
@@ -681,3 +701,14 @@ def test_split_auth_from_netloc(netloc, expected): | |
def test_remove_auth_from_url(auth_url, expected_url): | ||
url = remove_auth_from_url(auth_url) | ||
assert url == expected_url | ||
|
||
|
||
@pytest.mark.parametrize('auth_url, expected_url', [ | ||
('https://[email protected]/abc', 'https://[email protected]/abc'), | ||
('https://user:[email protected]', 'https://user:****@example.com'), | ||
('https://user:@example.com', 'https://user:****@example.com'), | ||
('https://example.com', 'https://example.com') | ||
]) | ||
def test_redact_password_from_url(auth_url, expected_url): | ||
url = redact_password_from_url(auth_url) | ||
assert url == expected_url |