Skip to content

Fix intersphinx leaking basic auth credentials in error and info messages#14366

Open
joshuaswanson wants to merge 9 commits intosphinx-doc:masterfrom
joshuaswanson:fix/intersphinx-auth-leak
Open

Fix intersphinx leaking basic auth credentials in error and info messages#14366
joshuaswanson wants to merge 9 commits intosphinx-doc:masterfrom
joshuaswanson:fix/intersphinx-auth-leak

Conversation

@joshuaswanson
Copy link
Copy Markdown

Fixes #14342.

When intersphinx fails to fetch an inventory or detects a redirect, the error and info messages include the raw URL with basic auth credentials. The _get_safe_url helper already exists in this file (and is used for the initial "loading inventory" info message) but wasn't applied to the error path in _fetch_inventory_url or the "inventory has moved" log message.

@jayaddison
Copy link
Copy Markdown
Contributor

Thanks for working on a fix for this @joshuaswanson - please could you add some test coverage to accompany it?

I would recommend copying and adapting the test_inspect_main_url test case from tests/test_ext_intersphinx/test_ext_intersphinx.py, into test cases for the HTTP-error case and HTTP-redirection case respectively.


get_request.side_effect = ConnectionError('connection refused')
with pytest.raises(ConnectionError, match='connection refused'):
_fetch_inventory_url(

This comment was marked as resolved.

cache_path=None,
)
status_output = app.status.getvalue()
assert 'secret' not in status_output

This comment was marked as resolved.

This comment was marked as resolved.

Comment on lines +688 to +695
@mock.patch('sphinx.ext.intersphinx._load.InventoryFile')
@mock.patch('sphinx.ext.intersphinx._load.requests.get')
@pytest.mark.sphinx('html', testroot='root')
def test_fetch_inventory_redirect_hides_credentials(get_request, InventoryFile, app):
"""Credentials should not appear in redirect log messages."""
mocked_get = get_request.return_value.__enter__.return_value
intersphinx_setup(app)
mocked_get.content = b'# Sphinx inventory version 2'

This comment was marked as resolved.

jayaddison

This comment was marked as outdated.

@jayaddison
Copy link
Copy Markdown
Contributor

@picnixz are you able to review this pull request too?

Comment on lines +683 to +684
_, stderr = capsys.readouterr()
assert 'secret' not in stderr

This comment was marked as resolved.

This comment was marked as resolved.

Comment on lines +721 to +723
status_output = app.status.getvalue()
assert 'secret' not in status_output
assert 'user@localhost' in status_output

This comment was marked as resolved.

Comment on lines +712 to +715
stdout, stderr = capsys.readouterr()
full_output = stdout + stderr + '\n'.join(caplog.messages)
assert 'secret' not in full_output
assert 'user@localhost' in full_output

This comment was marked as resolved.

This comment was marked as resolved.

Comment on lines +707 to +710
with http_server(RedirectHandler) as server:
port = server.server_port
url = f'http://user:secret@localhost:{port}/{INVENTORY_FILENAME}'
inspect_main([url])

This comment was marked as resolved.

assert actual == expected


def test_fetch_inventory_url_error_hides_credentials(capsys):

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

@jayaddison jayaddison left a comment

Choose a reason for hiding this comment

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

Looks great - thanks for this and for your responsiveness to codereview, @joshuaswanson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

intersphinx exposes basic auth password if connection fails

2 participants