Skip to content

Commit eacc739

Browse files
authored
Merge pull request #10998 from q0w/handle-netrc
2 parents d5e1eee + b953f9a commit eacc739

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

news/10979.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prioritize URL credentials over netrc.

src/pip/_internal/network/auth.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def _get_index_url(self, url: str) -> Optional[str]:
109109
def _get_new_credentials(
110110
self,
111111
original_url: str,
112-
allow_netrc: bool = True,
112+
*,
113+
allow_netrc: bool = False,
113114
allow_keyring: bool = False,
114115
) -> AuthInfo:
115116
"""Find and return credentials for the specified URL."""
@@ -260,7 +261,7 @@ def handle_401(self, resp: Response, **kwargs: Any) -> Response:
260261
# Query the keyring for credentials:
261262
username, password = self._get_new_credentials(
262263
resp.url,
263-
allow_netrc=False,
264+
allow_netrc=True,
264265
allow_keyring=True,
265266
)
266267

tests/functional/test_install_config.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,46 @@ def get_credential(url, username):
415415
assert "get_credential was called" in result.stderr
416416
else:
417417
assert "get_credential was called" not in result.stderr
418+
419+
420+
def test_prioritize_url_credentials_over_netrc(
421+
script: PipTestEnvironment,
422+
data: TestData,
423+
cert_factory: CertFactory,
424+
) -> None:
425+
cert_path = cert_factory()
426+
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
427+
ctx.load_cert_chain(cert_path, cert_path)
428+
ctx.load_verify_locations(cafile=cert_path)
429+
ctx.verify_mode = ssl.CERT_REQUIRED
430+
431+
server = make_mock_server(ssl_context=ctx)
432+
server.mock.side_effect = [
433+
package_page(
434+
{
435+
"simple-3.0.tar.gz": "/files/simple-3.0.tar.gz",
436+
}
437+
),
438+
authorization_response(str(data.packages / "simple-3.0.tar.gz")),
439+
]
440+
441+
url = f"https://USERNAME:PASSWORD@{server.host}:{server.port}/simple"
442+
443+
netrc = script.scratch_path / ".netrc"
444+
netrc.write_text(
445+
f"machine {server.host} login wrongusername password wrongpassword"
446+
)
447+
with server_running(server):
448+
script.environ["NETRC"] = netrc
449+
script.pip(
450+
"install",
451+
"--no-cache-dir",
452+
"--index-url",
453+
url,
454+
"--cert",
455+
cert_path,
456+
"--client-cert",
457+
cert_path,
458+
"simple",
459+
)
460+
script.assert_installed(simple="3.0")

0 commit comments

Comments
 (0)