Skip to content
Open
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
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Nodeenv changelog

Version [unreleased]
--------------------
- Hide mirror values in verbose configuration output to keep credentials
out of captured diagnostics. The configured mirror is unchanged.
Follow-up to `#426 <https://github.com/ekalinin/nodeenv/pull/426>`_.
- `env_dir` in the `[nodeenv]` section of the config file sets DEST_DIR for
runs that pass neither DEST_DIR nor `-p`. DEST_DIR on the command line and
`-p` take precedence over it
Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,8 @@ Other options
^^^^^^^^^^^^^

``-v, --verbose``
Verbose mode.
Verbose mode. Mirror configuration values are shown as ``<redacted>``
because they may contain credentials; the configured mirror is unchanged.

``-q, --quiet``
Quiet mode.
Expand Down
4 changes: 3 additions & 1 deletion nodeenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ def _load(cls, configfiles, verbose=False):
val = val[1:-1]

if verbose:
# Mirrors may contain credentials, including query tokens.
display_val = '<redacted>' if attr == 'mirror' else val
print('CONFIG {0}: {1} = {2}'.format(
os.path.basename(configfile), attr, val))
os.path.basename(configfile), attr, display_val))
setattr(cls, attr, val)

if os.path.exists(".node-version"):
Expand Down
62 changes: 62 additions & 0 deletions tests/nodeenv_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,45 @@ def test_config_keeps_percent_signs(tmpdir, mirror, expected):
nodeenv.Config.mirror = nodeenv.Config._default['mirror']


@pytest.mark.parametrize('mirror, expected', [
('https://user:secret@mirror.example/nodejs',
'https://user:secret@mirror.example/nodejs'),
('https://user:p%40ss@mirror.example/nodejs',
'https://user:p%40ss@mirror.example/nodejs'),
('https://user:p%%40ss@mirror.example/nodejs',
'https://user:p%40ss@mirror.example/nodejs'),
('user:secret@mirror.example/nodejs',
'user:secret@mirror.example/nodejs'),
('https://user:secret@[::1]:8080/nodejs',
'https://user:secret@[::1]:8080/nodejs'),
('https://mirror.example/nodejs?token=secret',
'https://mirror.example/nodejs?token=secret'),
('https://user:secret@[invalid', 'https://user:secret@[invalid'),
('https://mirror.example/nodejs', 'https://mirror.example/nodejs'),
])
@pytest.mark.parametrize('verbose', (False, True))
def test_config_verbose_redacts_mirror(tmpdir, capsys, monkeypatch,
mirror, expected, verbose):
"""Diagnostics must not expose credentials or change the loaded mirror."""
rc = tmpdir.join('nodeenvrc')
rc.write('[nodeenv]\nnode = 22.14.0\nmirror = "%s"\n' % mirror)
monkeypatch.setattr(nodeenv.Config, 'node', 'latest')
monkeypatch.setattr(nodeenv.Config, 'mirror', None)
nodeenv.Config._load([str(rc)], verbose=verbose)
assert nodeenv.Config.mirror == expected
assert nodeenv.Config.node == '22.14.0'
out, err = capsys.readouterr()
assert err == ''
if verbose:
assert 'mirror = <redacted>' in out
assert 'node = 22.14.0' in out
assert expected not in out
assert 'secret' not in out
assert 'p%40ss' not in out
else:
assert out == ''


@pytest.mark.parametrize('mirror, error', [
('%(bsae)s/nodejs', configparser.InterpolationMissingOptionError),
('%(mirror)s/nodejs', configparser.InterpolationDepthError),
Expand Down Expand Up @@ -719,6 +758,29 @@ def test_mirror_credentials_are_sent_as_basic_auth(auth_mirror,
assert set(seen) == {('/index.json', header)}


def test_config_mirror_credentials_stay_out_of_verbose_output(
auth_mirror, tmpdir, capsys, monkeypatch):
"""Redacting diagnostics must not remove authentication from requests."""
port, seen = auth_mirror
rc = tmpdir.join('nodeenvrc')
rc.write('[nodeenv]\nmirror = http://user:secret@127.0.0.1:%d\n'
% port)
monkeypatch.setattr(nodeenv.Config, 'mirror', None)
monkeypatch.setattr(nodeenv, 'src_base_url', None)
monkeypatch.setattr(nodeenv, 'src_auth', None)
monkeypatch.setattr(sys, 'argv', [
__file__, '--list', '--verbose', '--config-file', str(rc),
])
with mock.patch.object(nodeenv.logger, 'info') as mock_logger:
nodeenv.main()
mock_logger.assert_called_with('99.0.0')
assert set(seen) == {('/index.json', 'Basic dXNlcjpzZWNyZXQ=')}
out, err = capsys.readouterr()
assert 'mirror = <redacted>' in out
for secret in ('user:secret', 'secret', 'dXNlcjpzZWNyZXQ='):
assert secret not in out + err


def test_mirror_credentials_do_not_follow_redirects(auth_mirror):
"""A redirect may lead to another host, the password stays behind"""
port, seen = auth_mirror
Expand Down