Skip to content

Commit b25992c

Browse files
Fix incorrect netrc format assumption
According to the netrc specification (see [1] and [2]), the `machine` part should not be a full URL, but only a host name. Before, using the correct netrc format with only a host name did not work for authentication purposes in Python Inspector. Fix this by using urllib.parse to find the matching host name. [1]: https://www.ibm.com/docs/en/aix/7.2.0?topic=formats-netrc-file-format-tcpip [2]: https://docs.python.org/3/library/netrc.html#netrc.netrc.hosts Resolves: #176. Signed-off-by: Marcel Bochtler <[email protected]>
1 parent af339f8 commit b25992c

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

src/python_inspector/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616
from typing import NamedTuple
1717
from typing import Optional
1818

19+
from urllib.parse import urlparse
20+
1921
import aiohttp
2022
import requests
2123

2224

2325
def get_netrc_auth(url, netrc):
2426
"""
25-
Return login and password if url is in netrc
27+
Return login and password if the hostname is in netrc
2628
else return login and password as None
2729
"""
30+
hostname = urlparse(url).hostname
2831
hosts = netrc.hosts
29-
if url in hosts:
30-
url_auth = hosts.get(url)
32+
if hostname in hosts:
33+
url_auth = hosts.get(hostname)
3134
# netrc returns a tuple of (login, account, password)
3235
return (url_auth[0], url_auth[2])
3336
return (None, None)

tests/data/test-commented.netrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
machine https://pyp2.org/simple login test password test123
2-
# machine https://pyp1.org/simple login test password test123
1+
machine pyp2.org login test password test123
2+
# machine pyp1.org login test password test123

tests/data/test.netrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
machine https://pyp1.org/simple login test password test123
1+
machine pyp1.org login test password test123
2+
machine subdomain.example.com login subdomain-user password subdomain-secret

tests/test_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ def test_get_netrc_auth():
3535
netrc_file = test_env.get_test_loc("test.netrc")
3636
parsed_netrc = netrc(netrc_file)
3737
assert get_netrc_auth(url="https://pyp1.org/simple", netrc=parsed_netrc) == ("test", "test123")
38+
assert get_netrc_auth(url="https://pyp1.org/different/path", netrc=parsed_netrc) == ("test", "test123")
39+
assert get_netrc_auth(url="https://pyp1.org", netrc=parsed_netrc) == ("test", "test123")
40+
41+
42+
def test_get_netrc_auth_with_ports_and_schemes():
43+
netrc_file = test_env.get_test_loc("test.netrc")
44+
parsed_netrc = netrc(netrc_file)
45+
46+
assert get_netrc_auth(url="https://pyp1.org:443/path", netrc=parsed_netrc) == ("test", "test123")
47+
assert get_netrc_auth(url="http://pyp1.org:80/simple", netrc=parsed_netrc) == ("test", "test123")
3848

3949

4050
def test_get_commented_netrc_auth():
@@ -49,6 +59,14 @@ def test_get_netrc_auth_with_no_matching_url():
4959
assert get_netrc_auth(url="https://pypi2.org/simple", netrc=parsed_netrc) == (None, None)
5060

5161

62+
def test_get_netrc_auth_with_with_subdomains():
63+
netrc_file = test_env.get_test_loc("test.netrc")
64+
parsed_netrc = netrc(netrc_file)
65+
66+
assert get_netrc_auth(url="https://subdomain.example.com/simple", netrc=parsed_netrc) == ("subdomain-user", "subdomain-secret")
67+
assert get_netrc_auth(url="https://another.example.com/simple", netrc=parsed_netrc) == (None, None)
68+
69+
5270
@pytest.mark.asyncio
5371
@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
5472
@mock.patch("python_inspector.utils_pypi.CACHE.get")

0 commit comments

Comments
 (0)