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 URL to hide password #6295

Merged
merged 10 commits into from Mar 1, 2019
13 changes: 12 additions & 1 deletion src/pip/_internal/index.py
Expand Up @@ -74,6 +74,17 @@
logger = logging.getLogger(__name__)


def _redacted_url(url):
"""Redact the URL to remove the basic auth password
"""
parsed = urllib_parse.urlparse(url)

replaced = parsed._replace(netloc="{}:{}@{}".format(parsed.username, "<pwd>", parsed.hostname))
replaced_url = replaced.geturl()

return replaced_url


def _match_vcs_scheme(url):
# type: (str) -> Optional[str]
"""Look for VCS schemes in the URL.
Expand Down Expand Up @@ -155,7 +166,7 @@ def _get_html_response(url, session):
if _is_url_like_archive(url):
_ensure_html_response(url, session=session)

logger.debug('Getting page %s', url)
logger.debug('Getting page %s', _redacted_url(url))
Copy link
Member

@cjerdonek cjerdonek Feb 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use the redact_password_from_url() function which has this purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know!, Fixed in the next commit


resp = session.get(
url,
Expand Down
13 changes: 13 additions & 0 deletions tests/functional/test_install_config.py
Expand Up @@ -40,6 +40,19 @@ def test_command_line_options_override_env_vars(script, virtualenv):
assert "Getting page https://download.zope.org/ppix" in result.stdout


def test_no_password_in_debug_message(script, virtualenv):
"""
Test that password in the URL is not logged
"""
script.environ['PIP_INDEX_URL'] = 'https://user:my_password@example.com/simple/'
result = script.pip('install', '-vvv', 'INITools', expect_error=True)
assert (
"Getting page https://user:<pwd>@example.com/simple/initools"
in result.stdout
)
Copy link
Member

@cjerdonek cjerdonek Feb 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, you might as well assert that my_password is not in the output. (And I would assign my_password to a variable so that that portion of the test doesn't pass because of a typo.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

virtualenv.clear()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? If so, should it be in a finally block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used test_command_line_options_override_env_vars as a template and I assumed that you need to cleanup manually. Removed.



@pytest.mark.network
def test_env_vars_override_config_file(script, virtualenv):
"""
Expand Down