Skip to content

Commit 7c49d06

Browse files
authored
Merge pull request #11091 from q0w/override-proxy
2 parents 02ad0f3 + f5e0c6c commit 7c49d06

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

news/10685.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make the ``--proxy`` parameter take precedence over environment variables.

src/pip/_internal/cli/req_command.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def _build_session(
145145
"http": options.proxy,
146146
"https": options.proxy,
147147
}
148+
session.trust_env = False
148149

149150
# Determine if we can prompt the user for authentication or not
150151
session.auth.prompting = not options.no_input

tests/functional/test_proxy.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import ssl
2+
from pathlib import Path
3+
from typing import Any, Dict
4+
5+
import proxy
6+
import pytest
7+
from proxy.http.proxy import HttpProxyBasePlugin
8+
9+
from tests.conftest import CertFactory
10+
from tests.lib import PipTestEnvironment, TestData
11+
from tests.lib.server import (
12+
authorization_response,
13+
make_mock_server,
14+
package_page,
15+
server_running,
16+
)
17+
18+
19+
class AccessLogPlugin(HttpProxyBasePlugin):
20+
def on_access_log(self, context: Dict[str, Any]) -> None:
21+
print(context)
22+
23+
24+
@pytest.mark.network
25+
def test_proxy_overrides_env(
26+
script: PipTestEnvironment, capfd: pytest.CaptureFixture[str]
27+
) -> None:
28+
with proxy.Proxy(
29+
port=8899,
30+
num_acceptors=1,
31+
), proxy.Proxy(plugins=[AccessLogPlugin], port=8888, num_acceptors=1):
32+
script.environ["http_proxy"] = "127.0.0.1:8888"
33+
script.environ["https_proxy"] = "127.0.0.1:8888"
34+
result = script.pip(
35+
"download",
36+
"--proxy",
37+
"http://127.0.0.1:8899",
38+
"--trusted-host",
39+
"127.0.0.1",
40+
"-d",
41+
"pip_downloads",
42+
"INITools==0.1",
43+
)
44+
result.did_create(Path("scratch") / "pip_downloads" / "INITools-0.1.tar.gz")
45+
out, _ = capfd.readouterr()
46+
assert "CONNECT" not in out
47+
48+
49+
def test_proxy_does_not_override_netrc(
50+
script: PipTestEnvironment,
51+
data: TestData,
52+
cert_factory: CertFactory,
53+
) -> None:
54+
cert_path = cert_factory()
55+
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
56+
ctx.load_cert_chain(cert_path, cert_path)
57+
ctx.load_verify_locations(cafile=cert_path)
58+
ctx.verify_mode = ssl.CERT_REQUIRED
59+
60+
server = make_mock_server(ssl_context=ctx)
61+
server.mock.side_effect = [
62+
package_page(
63+
{
64+
"simple-3.0.tar.gz": "/files/simple-3.0.tar.gz",
65+
}
66+
),
67+
authorization_response(data.packages / "simple-3.0.tar.gz"),
68+
authorization_response(data.packages / "simple-3.0.tar.gz"),
69+
]
70+
71+
url = f"https://{server.host}:{server.port}/simple"
72+
73+
netrc = script.scratch_path / ".netrc"
74+
netrc.write_text(f"machine {server.host} login USERNAME password PASSWORD")
75+
with proxy.Proxy(port=8888, num_acceptors=1), server_running(server):
76+
script.environ["NETRC"] = netrc
77+
script.pip(
78+
"install",
79+
"--proxy",
80+
"http://127.0.0.1:8888",
81+
"--trusted-host",
82+
"127.0.0.1",
83+
"--no-cache-dir",
84+
"--index-url",
85+
url,
86+
"--cert",
87+
cert_path,
88+
"--client-cert",
89+
cert_path,
90+
"simple",
91+
)
92+
script.assert_installed(simple="3.0")

tests/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ virtualenv >= 20.0 ; python_version >= '3.10'
1212
werkzeug
1313
wheel
1414
tomli-w
15+
proxy.py

0 commit comments

Comments
 (0)