Skip to content
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

Redact auth from URL in UpdatingDefaultsHelpFormatter #9163

Merged
merged 2 commits into from
Nov 27, 2020
Merged
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
1 change: 1 addition & 0 deletions news/9160.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Redact auth from URL in help message.
13 changes: 12 additions & 1 deletion src/pip/_internal/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pip._internal.cli.status_codes import UNKNOWN_ERROR
from pip._internal.configuration import Configuration, ConfigurationError
from pip._internal.utils.compat import get_terminal_size
from pip._internal.utils.misc import redact_auth_from_url

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -106,12 +107,22 @@ class UpdatingDefaultsHelpFormatter(PrettyHelpFormatter):

This is updates the defaults before expanding them, allowing
them to show up correctly in the help listing.

Also redact auth from url type options
"""

def expand_default(self, option):
default_value = None
if self.parser is not None:
self.parser._update_defaults(self.parser.defaults)
return optparse.IndentedHelpFormatter.expand_default(self, option)
default_value = self.parser.defaults.get(option.dest)
help_text = optparse.IndentedHelpFormatter.expand_default(self, option)

if default_value and option.metavar == 'URL':
help_text = help_text.replace(
default_value, redact_auth_from_url(default_value))

return help_text


class CustomOptionParser(optparse.OptionParser):
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ def test_help_command_should_exit_status_error_when_cmd_does_not_exist(script):
assert result.returncode == ERROR


def test_help_command_redact_auth_from_url(script):
"""
Test `help` on various subcommands redact auth from url
"""
script.environ['PIP_INDEX_URL'] = 'https://user:secret@example.com'
result = script.pip('install', '--help')
assert result.returncode == SUCCESS
assert 'secret' not in result.stdout


def test_help_commands_equally_functional(in_memory_pip):
"""
Test if `pip help` and 'pip --help' behave the same way.
Expand Down