Skip to content

Commit 3212119

Browse files
committed
Don't fall back to ~/.netrc if the NETRC env var specified doesn't exist, thus allowing you to disable netrc by setting a non-existent path
1 parent dc5ede5 commit 3212119

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

simple_repository_server/__main__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,23 @@ def is_url(url: str) -> bool:
3232

3333
def get_netrc_path() -> typing.Optional[Path]:
3434
"""
35-
Get the netrc file path if it exists.
35+
Get the netrc file path if it exists and is a regular file.
3636
Checks NETRC environment variable first, then ~/.netrc.
37-
Returns None if no netrc file is found.
37+
Returns None if no valid netrc file is found.
38+
39+
If NETRC is explicitly set but points to a non-existent or invalid file,
40+
returns None.
3841
"""
39-
# Check if NETRC environment variable is set
4042
netrc_env = os.environ.get('NETRC')
4143
if netrc_env:
4244
netrc_path = Path(netrc_env)
43-
if netrc_path.exists():
45+
if netrc_path.exists() and netrc_path.is_file():
4446
return netrc_path
47+
# If NETRC is explicitly set but invalid, don't fall back to ~/.netrc
48+
return None
4549

46-
# Check for default ~/.netrc file
4750
default_netrc = Path.home() / '.netrc'
48-
if default_netrc.exists():
51+
if default_netrc.exists() and default_netrc.is_file():
4952
return default_netrc
5053

5154
return None

simple_repository_server/tests/test_netrc_auth.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,26 @@ def test_get_netrc__path_in_home(tmp_home: Path, netrc_file: Path):
4646

4747

4848
def test_get_netrc__netrc_env_var(netrc_file: Path, monkeypatch: pytest.MonkeyPatch):
49-
"""Test get_netrc_path returns None when no netrc file exists."""
49+
"""Test get_netrc_path uses NETRC environment variable when file exists."""
5050
monkeypatch.setenv('NETRC', str(netrc_file))
5151
result = get_netrc_path()
5252
assert result == netrc_file
5353

5454

55+
def test_get_netrc__netrc_env_var_nonexistent(tmp_home: Path, netrc_file: Path, monkeypatch: pytest.MonkeyPatch):
56+
"""Test get_netrc_path returns None when NETRC points to non-existent file (no fallback)."""
57+
# Create ~/.netrc in home directory
58+
home_netrc = tmp_home / '.netrc'
59+
netrc_file.rename(home_netrc)
60+
61+
# Set NETRC to non-existent file
62+
monkeypatch.setenv('NETRC', str(tmp_home / 'doesnt_exist'))
63+
result = get_netrc_path()
64+
65+
# Should return None, NOT fall back to ~/.netrc
66+
assert result is None
67+
68+
5569
def test_create_app__with_netrc(netrc_file: Path, monkeypatch: pytest.MonkeyPatch):
5670
monkeypatch.setenv('NETRC', str(netrc_file))
5771
with mock.patch(

0 commit comments

Comments
 (0)