Skip to content

Commit 8b4652e

Browse files
authored
Merge pull request #9163 from NoahGorny/redact-url-from-help
Redact auth from URL in UpdatingDefaultsHelpFormatter
2 parents 254414b + 4f8dfcf commit 8b4652e

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

news/9160.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Redact auth from URL in help message.

src/pip/_internal/cli/parser.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pip._internal.cli.status_codes import UNKNOWN_ERROR
1818
from pip._internal.configuration import Configuration, ConfigurationError
1919
from pip._internal.utils.compat import get_terminal_size
20+
from pip._internal.utils.misc import redact_auth_from_url
2021

2122
logger = logging.getLogger(__name__)
2223

@@ -106,12 +107,22 @@ class UpdatingDefaultsHelpFormatter(PrettyHelpFormatter):
106107
107108
This is updates the defaults before expanding them, allowing
108109
them to show up correctly in the help listing.
110+
111+
Also redact auth from url type options
109112
"""
110113

111114
def expand_default(self, option):
115+
default_value = None
112116
if self.parser is not None:
113117
self.parser._update_defaults(self.parser.defaults)
114-
return optparse.IndentedHelpFormatter.expand_default(self, option)
118+
default_value = self.parser.defaults.get(option.dest)
119+
help_text = optparse.IndentedHelpFormatter.expand_default(self, option)
120+
121+
if default_value and option.metavar == 'URL':
122+
help_text = help_text.replace(
123+
default_value, redact_auth_from_url(default_value))
124+
125+
return help_text
115126

116127

117128
class CustomOptionParser(optparse.OptionParser):

tests/functional/test_help.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ def test_help_command_should_exit_status_error_when_cmd_does_not_exist(script):
6464
assert result.returncode == ERROR
6565

6666

67+
def test_help_command_redact_auth_from_url(script):
68+
"""
69+
Test `help` on various subcommands redact auth from url
70+
"""
71+
script.environ['PIP_INDEX_URL'] = 'https://user:[email protected]'
72+
result = script.pip('install', '--help')
73+
assert result.returncode == SUCCESS
74+
assert 'secret' not in result.stdout
75+
76+
6777
def test_help_commands_equally_functional(in_memory_pip):
6878
"""
6979
Test if `pip help` and 'pip --help' behave the same way.

0 commit comments

Comments
 (0)