Skip to content

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

Open
joshuaswanson wants to merge 13 commits into
sphinx-doc:masterfrom
joshuaswanson:fix/intersphinx-auth-leak
Open

Fix intersphinx leaking basic auth credentials in error and info messages#14366
joshuaswanson wants to merge 13 commits into
sphinx-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.

@jayaddison jayaddison left a comment

Copy link
Copy Markdown
Contributor

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

@jayaddison

Copy link
Copy Markdown
Contributor

@joshuaswanson sorry, one more thing that I forgot: you should also update the CHANGES.rst (changelog) file to describe the fix (and to add yourself a credit for it!).

@jayaddison

Copy link
Copy Markdown
Contributor

👍 thanks again

Comment thread CHANGES.rst Outdated
def do_GET(self):
if '/new/' not in self.path:
self.send_response(302)
new_url = f'http://localhost:{self.server.server_port}/new/{INVENTORY_FILENAME}'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There are two issues with this line:

  1. The production redirect log message contains two independently redacted values: the original inventory URL (inv_location) and the final URL after following the redirect (new_inv_location). The current test gives only the original URL a password:

    original:    http://user:secret@localhost/objects.inv
    destination: http://localhost/new/objects.inv
    

    That verifies redaction of inv_location, but it cannot verify redaction of new_inv_location. For example, if someone accidentally changed the implementation to the following, the current test would still pass:

    LOGGER.info(msg, _get_safe_url(inv_location), new_inv_location)

    The second URL is now logged without redaction, but it has no password to leak, so the assertion looking for secret detects nothing.

    Giving the destination its own credentials closes that coverage gap:

    destination: http://redirect-user:redirect-secret@localhost/new/objects.inv
    

    The correct implementation logs redirect-user@localhost; an implementation that omits _get_safe_url(new_inv_location) logs redirect-secret and fails the existing assertion. This makes the test protect both halves of the redirect message from future regressions.

    - new_url = f'http://localhost:{self.server.server_port}/new/{INVENTORY_FILENAME}'
    + new_url = f'http://redirect-user:redirect-secret@localhost:{self.server.server_port}/new/{INVENTORY_FILENAME}'
  2. The ty check fails here because BaseHTTPRequestHandler.server is typed as BaseServer, which does not expose a server_port attribute. An explicit type narrowing should preserve the current behavior while allowing ty to recognize the concrete server type:

    + assert isinstance(self.server, http.server.HTTPServer)
      new_url = f'http://localhost:{self.server.server_port}/new/{INVENTORY_FILENAME}'

This suggestion addresses both:

Suggested change
new_url = f'http://localhost:{self.server.server_port}/new/{INVENTORY_FILENAME}'
assert isinstance(self.server, http.server.HTTPServer)
new_url = f'http://redirect-user:redirect-secret@localhost:{self.server.server_port}/new/{INVENTORY_FILENAME}'

Comment thread sphinx/ext/intersphinx/_load.py Outdated
safe_url,
err.__class__,
str(err),
str(err).replace(inv_location, safe_url),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Exact-string replacement does not reliably redact the URL after Requests has normalized it. For example, a valid URL may encode ~ as %7E, so inv_location contains the password s%7Eecret, but Requests constructs the error using the equivalent normalized form s~ecret. The replacement therefore does not match, leaving the password in the error output.

For example, an HTTP 500 currently produces output equivalent to:

intersphinx inventory 'http://user@example.com/objects.inv' not fetchable:
500 Server Error for url: http://user:s~ecret@example.com/objects.inv

The first occurrence is redacted, but the normalized URL in the underlying Requests error still contains the password. After redacting the URL retained by the exception, both occurrences would be safe:

intersphinx inventory 'http://user@example.com/objects.inv' not fetchable:
500 Server Error for url: http://user@example.com/objects.inv

Could we redact the URL from err.request.url or err.response.url when available, while retaining the original URL as a fallback?

@jdillard jdillard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

intersphinx exposes basic auth password if connection fails

3 participants