Redact URLs in Collecting... logs

This commit is contained in:
Stéphane Bidoul 2023-10-14 13:50:49 +02:00
parent 2333ef3b53
commit 8f0ed32413
5 changed files with 39 additions and 2 deletions

1
news/12350.bugfix.rst Normal file
View File

@ -0,0 +1 @@
Redact password from URLs in some additional places.

View File

@ -47,6 +47,7 @@ from pip._internal.utils.misc import (
display_path, display_path,
hash_file, hash_file,
hide_url, hide_url,
redact_auth_from_requirement,
) )
from pip._internal.utils.temp_dir import TempDirectory from pip._internal.utils.temp_dir import TempDirectory
from pip._internal.utils.unpacking import unpack_file from pip._internal.utils.unpacking import unpack_file
@ -277,7 +278,7 @@ class RequirementPreparer:
information = str(display_path(req.link.file_path)) information = str(display_path(req.link.file_path))
else: else:
message = "Collecting %s" message = "Collecting %s"
information = str(req.req or req) information = redact_auth_from_requirement(req.req) if req.req else str(req)
# If we used req.req, inject requirement source if available (this # If we used req.req, inject requirement source if available (this
# would already be included if we used req directly) # would already be included if we used req directly)

View File

@ -49,6 +49,7 @@ from pip._internal.utils.misc import (
display_path, display_path,
hide_url, hide_url,
is_installable_dir, is_installable_dir,
redact_auth_from_requirement,
redact_auth_from_url, redact_auth_from_url,
) )
from pip._internal.utils.packaging import safe_extra from pip._internal.utils.packaging import safe_extra
@ -188,7 +189,7 @@ class InstallRequirement:
def __str__(self) -> str: def __str__(self) -> str:
if self.req: if self.req:
s = str(self.req) s = redact_auth_from_requirement(self.req)
if self.link: if self.link:
s += " from {}".format(redact_auth_from_url(self.link.url)) s += " from {}".format(redact_auth_from_url(self.link.url))
elif self.link: elif self.link:

View File

@ -35,6 +35,7 @@ from typing import (
cast, cast,
) )
from pip._vendor.packaging.requirements import Requirement
from pip._vendor.pyproject_hooks import BuildBackendHookCaller from pip._vendor.pyproject_hooks import BuildBackendHookCaller
from pip._vendor.tenacity import retry, stop_after_delay, wait_fixed from pip._vendor.tenacity import retry, stop_after_delay, wait_fixed
@ -578,6 +579,13 @@ def redact_auth_from_url(url: str) -> str:
return _transform_url(url, _redact_netloc)[0] return _transform_url(url, _redact_netloc)[0]
def redact_auth_from_requirement(req: Requirement) -> str:
"""Replace the password in a given requirement url with ****."""
if not req.url:
return str(req)
return str(req).replace(req.url, redact_auth_from_url(req.url))
class HiddenText: class HiddenText:
def __init__(self, secret: str, redacted: str) -> None: def __init__(self, secret: str, redacted: str) -> None:
self.secret = secret self.secret = secret

View File

@ -14,6 +14,7 @@ from typing import Any, Callable, Iterator, List, NoReturn, Optional, Tuple, Typ
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
import pytest import pytest
from pip._vendor.packaging.requirements import Requirement
from pip._internal.exceptions import HashMismatch, HashMissing, InstallationError from pip._internal.exceptions import HashMismatch, HashMissing, InstallationError
from pip._internal.utils.deprecation import PipDeprecationWarning, deprecated from pip._internal.utils.deprecation import PipDeprecationWarning, deprecated
@ -37,6 +38,7 @@ from pip._internal.utils.misc import (
normalize_path, normalize_path,
normalize_version_info, normalize_version_info,
parse_netloc, parse_netloc,
redact_auth_from_requirement,
redact_auth_from_url, redact_auth_from_url,
redact_netloc, redact_netloc,
remove_auth_from_url, remove_auth_from_url,
@ -765,6 +767,30 @@ def test_redact_auth_from_url(auth_url: str, expected_url: str) -> None:
assert url == expected_url assert url == expected_url
@pytest.mark.parametrize(
"req, expected",
[
("pkga", "pkga"),
(
"resolvelib@ "
" git+https://test-user:test-pass@github.com/sarugaku/resolvelib@1.0.1",
"resolvelib@"
" git+https://test-user:****@github.com/sarugaku/resolvelib@1.0.1",
),
(
"resolvelib@"
" git+https://test-user:test-pass@github.com/sarugaku/resolvelib@1.0.1"
" ; python_version>='3.6'",
"resolvelib@"
" git+https://test-user:****@github.com/sarugaku/resolvelib@1.0.1"
' ; python_version >= "3.6"',
),
],
)
def test_redact_auth_from_requirement(req: str, expected: str) -> None:
assert redact_auth_from_requirement(Requirement(req)) == expected
class TestHiddenText: class TestHiddenText:
def test_basic(self) -> None: def test_basic(self) -> None:
""" """