Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
fix(core): send AMP requests through the proxy the environment names (#8186)
João Moura0b84f8e18b45
diff --git a/lib/cli/src/crewai_cli/cli.py b/lib/cli/src/crewai_cli/cli.pyindex 35f493e31..b68ba8654 100644--- a/lib/cli/src/crewai_cli/cli.py+++ b/lib/cli/src/crewai_cli/cli.py@@ -6,6 +6,10 @@ import subprocessfrom typing import TYPE_CHECKING, Anyimport click++# Imported first so AMP requests pin the machine's proxy and CA settings before+# any command loads a project's .env (see crewai_core.plus_api).+import crewai_core.plus_api # noqa: F401from crewai_core.token_manager import TokenManagerfrom crewai_cli.config import Settingsdiff --git a/lib/cli/src/crewai_cli/experimental/eval_crew.py b/lib/cli/src/crewai_cli/experimental/eval_crew.pyindex 6bd104379..621437c26 100644--- a/lib/cli/src/crewai_cli/experimental/eval_crew.py+++ b/lib/cli/src/crewai_cli/experimental/eval_crew.py@@ -50,6 +50,7 @@ import webbrowserimport clickfrom crewai_core.constants import DEFAULT_CREWAI_ENTERPRISE_URL+from crewai_core.plus_api import restore_machine_transportfrom crewai_core.settings import Settingsfrom dotenv import dotenv_values, set_keyimport httpx@@ -670,12 +671,16 @@ def _anonymous(client: PlusAPI) -> PlusAPI:def _load_project_env() -> None:"""The project's .env, as `crewai run` loads it — so CREWAI_PLUS_URL here is- the one the run used — with `crewai eval`'s token left as it was."""+ the one the run used — with `crewai eval`'s token left as it was, and the+ machine's proxy and CA: the crew this command runs inherits this+ environment and pins them from it, for AMP requests that carry the saved+ login."""env_file = Path.cwd() / ".env"if env_file.is_file():from crewai_cli.run_crew import load_project_envload_project_env(env_file)+ restore_machine_transport(os.environ)def read_last_run(directory: Path | None = None) -> dict[str, Any] | None:diff --git a/lib/cli/src/crewai_cli/plus_api.py b/lib/cli/src/crewai_cli/plus_api.pyindex 9f099e4b4..401fde87f 100644--- a/lib/cli/src/crewai_cli/plus_api.py+++ b/lib/cli/src/crewai_cli/plus_api.py@@ -132,10 +132,16 @@ class PlusAPI(_CorePlusAPI):request_kwargs["data"] = dataif timeout is not None:request_kwargs["timeout"] = timeout+ # An older crewai-core has no _http_client; its clients ignore proxies.+ http_client = getattr(self, "_http_client", None)with (path.open("rb") as file_handle,- httpx.Client(trust_env=False, verify=verify) as client,+ (+ http_client(verify)+ if http_client+ else httpx.Client(verify=verify, trust_env=False)+ ) as client,):files = {"zip_file": (path.name, file_handle, "application/zip"),diff --git a/lib/cli/src/crewai_cli/run_crew.py b/lib/cli/src/crewai_cli/run_crew.pyindex af825ecda..87c8d8728 100644--- a/lib/cli/src/crewai_cli/run_crew.py+++ b/lib/cli/src/crewai_cli/run_crew.py@@ -58,6 +58,23 @@ source_dir = os.environ.get("CREWAI_RUNNER_SOURCE_DIR")if source_dir:sys.path.insert(0, source_dir)+# This interpreter never imports cli.py, so pin the machine's proxy and CA for+# AMP requests here, before _run_json_crew loads the project's .env. A+# crewai-core too old to have plus_api pins nothing and ignores proxies anyway;+# any other import failure is real and must surface here. `python -c` puts the+# project folder first on sys.path, so the pin is imported with it off the path:+# a project folder named crewai_core must not decide where the saved login+# travels. The path is restored after, so project code still imports its own.+search_path = sys.path[:]+sys.path[:] = [entry for entry in sys.path if entry not in ("", os.getcwd())]+try:+ import crewai_core.plus_api+except ModuleNotFoundError as error:+ if error.name != "crewai_core.plus_api":+ raise+finally:+ sys.path[:] = search_path+package_dir = Path(os.environ["CREWAI_CLI_RUNNER_PACKAGE_DIR"])package_spec = importlib.util.spec_from_file_location("crewai_cli",diff --git a/lib/cli/tests/test_plus_api.py b/lib/cli/tests/test_plus_api.pyindex b797f29b5..671b43312 100644--- a/lib/cli/tests/test_plus_api.py+++ b/lib/cli/tests/test_plus_api.py@@ -300,6 +300,8 @@ class TestPlusAPI(unittest.TestCase):)self.assertEqual(response, mock_response)+ @patch.dict("crewai_core.plus_api._MACHINE_TRANSPORT_ENV", clear=True)+ @patch("crewai_core.plus_api._os_proxies", dict)@patch("crewai_core.plus_api.httpx.Client")def test_make_request(self, mock_client_class):mock_client_instance = MagicMock()@@ -309,7 +311,9 @@ class TestPlusAPI(unittest.TestCase):response = self.api._make_request("GET", "test_endpoint")- mock_client_class.assert_called_once_with(trust_env=False, verify=True)+ mock_client_class.assert_called_once_with(+ verify=True, trust_env=False, mounts={}+ )mock_client_instance.request.assert_called_once_with("GET", f"{self.api.base_url}/test_endpoint", headers=self.api.headers)@@ -547,9 +551,14 @@ def _requests_sent_by(call: Callable[[PlusAPI], object]) -> list[httpx.Request]:return httpx.Response(200, json={})transport = httpx.MockTransport(answer)- with patch(- "crewai_core.plus_api.httpx.Client",- side_effect=lambda **kwargs: _RealHttpxClient(transport=transport, **kwargs),+ # A machine or OS proxy would mount a real transport ahead of the mock one.+ with (+ patch.dict("crewai_core.plus_api._MACHINE_TRANSPORT_ENV", clear=True),+ patch("crewai_core.plus_api._os_proxies", dict),+ patch(+ "crewai_core.plus_api.httpx.Client",+ side_effect=lambda **kwargs: _RealHttpxClient(transport=transport, **kwargs),+ ),):call(PlusAPI("test_api_key"))return sent@@ -611,3 +620,53 @@ def test_reading_an_evaluation_says_this_cli_can_export_it():assert request.method == "GET"assert request.url.path == "/crewai_plus/api/v1/tracing/evaluations/ev-1"assert dict(request.url.params) == {"can_export": "1"}++[email protected]_network(allowed_hosts=["127.0.0.1"])+def test_zip_deploy_uploads_go_through_the_proxy_the_machine_names(+ tmp_path, monkeypatch+):+ # Corporate networks and coding-agent sandboxes only allow traffic through+ # the proxy in HTTP(S)_PROXY; the CLI's own ZIP upload must use it too.+ # The machine's variables are the ones crewai-core pinned at startup.+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+ import threading+ import zipfile++ seen = []++ class Proxy(BaseHTTPRequestHandler):+ def do_POST(self):+ body = self.rfile.read(int(self.headers["Content-Length"]))+ seen.append((self.path, self.headers.get("Authorization"), body))+ self.send_response(201)+ self.send_header("Content-Length", "2")+ self.end_headers()+ self.wfile.write(b"{}")++ def log_message(self, format, *args):+ pass++ server = ThreadingHTTPServer(("127.0.0.1", 0), Proxy)+ threading.Thread(target=server.serve_forever, daemon=True).start()+ monkeypatch.setattr(+ "crewai_core.plus_api._MACHINE_TRANSPORT_ENV",+ {"HTTP_PROXY": f"http://127.0.0.1:{server.server_address[1]}"},+ )+ project_zip = tmp_path / "project.zip"+ with zipfile.ZipFile(project_zip, "w") as archive:+ archive.writestr("pyproject.toml", "[project]\nname = 'demo'\n")++ try:+ response = PlusAPI("amp-token", base_url="http://amp.test").create_crew_from_zip(+ project_zip, name="demo"+ )+ finally:+ server.shutdown()+ server.server_close()++ assert response.status_code == 201+ ((target, authorization, body),) = seen+ assert target == "http://amp.test/crewai_plus/api/v1/crews/zip"+ assert authorization == "Bearer amp-token"+ assert b'filename="project.zip"' in bodydiff --git a/lib/cli/tests/test_run_crew.py b/lib/cli/tests/test_run_crew.pyindex 4d7587b0e..c0f97223a 100644--- a/lib/cli/tests/test_run_crew.py+++ b/lib/cli/tests/test_run_crew.py@@ -1,5 +1,6 @@"""Tests for crewai_cli.run_crew JSON crew handling."""+import jsonimport osfrom pathlib import Pathimport subprocess@@ -232,6 +233,126 @@ def test_json_runner_code_loads_current_cli_package_over_project_env(tmp_path: Passert marker.read_text() == "current:trained.pkl"+def test_json_runner_pins_the_machines_proxy_before_the_project_env_loads(+ tmp_path: Path,+):+ """The runner is a fresh interpreter that never imports cli.py. It reads the+ machine's proxy and CA for AMP before `_run_json_crew` loads the project's+ .env, so that file cannot choose where the saved login travels."""+ from crewai_core import plus_api++ current_pkg = tmp_path / "cli" / "crewai_cli"+ current_pkg.mkdir(parents=True)+ pinned = tmp_path / "pinned.json"+ (current_pkg / "__init__.py").write_text("")+ (current_pkg / "run_crew.py").write_text(+ "import json\n"+ "from pathlib import Path\n"+ "from dotenv import load_dotenv\n"+ "def _run_json_crew(trained_agents_file=None):\n"+ " load_dotenv(Path.cwd() / '.env', override=True)\n"+ " from crewai_core import plus_api\n"+ f" Path({str(pinned)!r}).write_text(json.dumps(plus_api._MACHINE_TRANSPORT_ENV))\n"+ )+ (tmp_path / ".env").write_text(+ "HTTPS_PROXY=http://project-proxy.test:8080\nSSL_CERT_FILE=/project/ca.pem\n"+ )++ env = {+ key: value+ for key, value in os.environ.items()+ if not plus_api._sets_transport(key)+ }+ env["HTTPS_PROXY"] = "http://machine-proxy.test:3128"+ env[run_crew_module._CREWAI_CLI_RUNNER_PACKAGE_DIR_ENV] = str(current_pkg)++ subprocess.run(+ [sys.executable, "-c", run_crew_module._JSON_CREW_RUNNER_CODE],+ check=True,+ env=env,+ cwd=tmp_path,+ )++ assert json.loads(pinned.read_text()) == {+ "HTTPS_PROXY": "http://machine-proxy.test:3128"+ }+++def test_json_runner_ignores_crewai_modules_planted_in_the_project(tmp_path: Path):+ """The runner starts in the project folder; a crewai_core package there must+ not be the module that pins where the saved login travels, while the+ project's own modules stay importable for its code."""+ planted = tmp_path / "crewai_core"+ planted.mkdir()+ (planted / "__init__.py").write_text("")+ (planted / "plus_api.py").write_text(+ "_MACHINE_TRANSPORT_ENV = {'HTTPS_PROXY': 'http://attacker.test:8080'}\n"+ )+ current_pkg = tmp_path / "cli" / "crewai_cli"+ current_pkg.mkdir(parents=True)+ pinned = tmp_path / "pinned.json"+ (current_pkg / "__init__.py").write_text("")+ (tmp_path / "project_helpers.py").write_text("VALUE = 'project code still imports'\n")+ (current_pkg / "run_crew.py").write_text(+ "import json\n"+ "from pathlib import Path\n"+ "def _run_json_crew(trained_agents_file=None):\n"+ " from crewai_core import plus_api\n"+ " import project_helpers\n"+ f" Path({str(pinned)!r}).write_text(json.dumps("+ "{'file': plus_api.__file__, 'env': plus_api._MACHINE_TRANSPORT_ENV, "+ "'project': project_helpers.VALUE}))\n"+ )+ from crewai_core import plus_api++ env = {+ key: value+ for key, value in os.environ.items()+ if not plus_api._sets_transport(key)+ }+ env[run_crew_module._CREWAI_CLI_RUNNER_PACKAGE_DIR_ENV] = str(current_pkg)++ subprocess.run(+ [sys.executable, "-c", run_crew_module._JSON_CREW_RUNNER_CODE],+ check=True,+ env=env,+ cwd=tmp_path,+ )++ loaded = json.loads(pinned.read_text())+ assert loaded["file"] == plus_api.__file__+ assert loaded["env"] == {}+ assert loaded["project"] == "project code still imports"+++def test_json_runner_stops_when_plus_api_fails_to_import(tmp_path: Path):+ """Only a crewai-core without plus_api is skipped; a plus_api that fails to+ import stops the runner with its own error instead of running unpinned."""+ broken = tmp_path / "broken_env" / "crewai_core"+ broken.mkdir(parents=True)+ (broken / "__init__.py").write_text("")+ (broken / "plus_api.py").write_text("raise ImportError('plus_api needs httpx 0.28')\n")+ current_pkg = tmp_path / "cli" / "crewai_cli"+ current_pkg.mkdir(parents=True)+ (current_pkg / "__init__.py").write_text("")+ (current_pkg / "run_crew.py").write_text("def _run_json_crew(trained_agents_file=None):\n pass\n")++ env = os.environ.copy()+ env["PYTHONPATH"] = str(broken.parent)+ env[run_crew_module._CREWAI_CLI_RUNNER_PACKAGE_DIR_ENV] = str(current_pkg)++ result = subprocess.run(+ [sys.executable, "-c", run_crew_module._JSON_CREW_RUNNER_CODE],+ env=env,+ cwd=tmp_path,+ capture_output=True,+ text=True,+ )++ assert result.returncode != 0+ assert "ImportError: plus_api needs httpx 0.28" in result.stderr++def test_json_runner_imports_with_older_project_env_crewai_core(tmp_path: Path):old_parent = tmp_path / "old_env"old_crewai_core = old_parent / "crewai_core"diff --git a/lib/crewai-core/src/crewai_core/plus_api.py b/lib/crewai-core/src/crewai_core/plus_api.pyindex a8ce4dbc0..c54fefd83 100644--- a/lib/crewai-core/src/crewai_core/plus_api.py+++ b/lib/crewai-core/src/crewai_core/plus_api.py@@ -2,10 +2,14 @@from __future__ import annotations+from collections.abc import MutableMapping+import ipaddressimport osfrom pathlib import Path+import sslfrom typing import Any, Final, Literal, TypedDict, castfrom urllib.parse import urljoin+import urllib.requestimport httpxfrom typing_extensions import NotRequired@@ -146,6 +150,108 @@ class RequestKwargs(TypedDict):timeout: NotRequired[float | httpx.Timeout]+def _sets_transport(key: str) -> bool:+ """Whether this variable decides the proxy or CA bundle of AMP requests.++ httpx reads the proxy and CA variables. REQUEST_METHOD counts: urllib drops+ an uppercase HTTP_PROXY when it is set. So does UV_ENV_FILE, for a crewAI+ process started with `uv run`: uv loads that file into it before Python starts.+ """+ return key.lower().endswith("_proxy") or key.upper() in {+ "SSL_CERT_FILE",+ "SSL_CERT_DIR",+ "REQUEST_METHOD",+ "UV_ENV_FILE",+ }+++# The proxy and CA variables as the process had them when this module was first+# imported. AMP requests carry the saved login, so they take their proxy and CA+# from here, never from the live environment: a project's .env, loaded later by+# `crewai run` or by `crewai.llm` on import, must not choose where the login+# travels or which CA vouches for AMP. The CLI imports this module at startup,+# before any command reads a .env.+_MACHINE_TRANSPORT_ENV: dict[str, str] = {+ key: value for key, value in os.environ.items() if _sets_transport(key)+}+++def restore_machine_transport(environ: MutableMapping[str, str]) -> None:+ """Put the pinned proxy and CA variables back in ``environ``, dropping any set since.++ For a process that loads a project's .env and then starts another crewAI+ process: the child pins the variables it inherits as the machine's.+ """+ for key in [key for key in environ if _sets_transport(key)]:+ del environ[key]+ environ.update(_MACHINE_TRANSPORT_ENV)+++def _os_proxies() -> dict[str, str]:+ """The OS proxy settings (macOS System Settings, the Windows registry)."""+ read = getattr(urllib.request, "getproxies_macosx_sysconf", None) or getattr(+ urllib.request, "getproxies_registry", None+ )+ return read() if read else {}+++def _machine_proxies() -> dict[str, str]:+ """Proxies by scheme, read from the pinned variables as urllib's getproxies() reads them."""+ proxies: dict[str, str] = {}+ # Two passes, so a lowercase name wins over its uppercase twin.+ for name, value in _MACHINE_TRANSPORT_ENV.items():+ if value and name.lower().endswith("_proxy"):+ proxies[name[:-6].lower()] = value+ # As urllib does (CVE-2016-1000110, "httpoxy"): in a CGI process a client's+ # Proxy header arrives as HTTP_PROXY, so only a lowercase http_proxy counts.+ if "REQUEST_METHOD" in _MACHINE_TRANSPORT_ENV:+ proxies.pop("http", None)+ for name, value in _MACHINE_TRANSPORT_ENV.items():+ if name.endswith("_proxy"):+ if value:+ proxies[name[:-6].lower()] = value+ else:+ proxies.pop(name[:-6].lower(), None)+ return proxies or _os_proxies()+++def _no_proxy_pattern(host: str) -> str:+ """The httpx mount pattern for one NO_PROXY entry, as httpx builds it."""+ if "://" in host:+ return host+ try:+ address = ipaddress.ip_address(host.split("/")[0])+ except ValueError:+ return f"all://{host}" if host.lower() == "localhost" else f"all://*{host}"+ return f"all://[{host}]" if address.version == 6 else f"all://{host}"+++def _machine_transport(verify: bool) -> dict[str, Any]:+ """httpx.Client arguments for the machine's proxy and CA, as trust_env would read them at startup."""+ ssl_verify: ssl.SSLContext | bool = verify+ if verify and (cafile := _MACHINE_TRANSPORT_ENV.get("SSL_CERT_FILE")):+ ssl_verify = ssl.create_default_context(cafile=cafile)+ elif verify and (capath := _MACHINE_TRANSPORT_ENV.get("SSL_CERT_DIR")):+ ssl_verify = ssl.create_default_context(capath=capath)++ proxies = _machine_proxies()+ mounts: dict[str, httpx.BaseTransport | None] = {}+ for scheme in ("http", "https", "all"):+ if url := proxies.get(scheme):+ mounts[f"{scheme}://"] = httpx.HTTPTransport(+ proxy=url if "://" in url else f"http://{url}",+ verify=ssl_verify,+ trust_env=False,+ )+ for host in (entry.strip() for entry in proxies.get("no", "").split(",")):+ if host == "*":+ mounts = {}+ break+ if host:+ mounts[_no_proxy_pattern(host)] = None+ return {"verify": ssl_verify, "trust_env": False, "mounts": mounts}++class PlusAPI:"""Client for working with the CrewAI+ API."""@@ -186,6 +292,11 @@ class PlusAPI:or DEFAULT_CREWAI_ENTERPRISE_URL)+ @staticmethod+ def _http_client(verify: bool = True) -> httpx.Client:+ """A client that reaches AMP through the machine's proxy and CA, not a project's .env."""+ return httpx.Client(**_machine_transport(verify))+def _make_request(self,method: HttpMethod,@@ -204,7 +315,7 @@ class PlusAPI:request_kwargs["params"] = paramsif timeout is not None:request_kwargs["timeout"] = timeout- with httpx.Client(trust_env=False, verify=verify) as client:+ with self._http_client(verify) as client:return client.request(method, url, **request_kwargs)def _make_multipart_request(@@ -230,7 +341,7 @@ class PlusAPI:with (path.open("rb") as file_handle,- httpx.Client(trust_env=False, verify=verify) as client,+ self._http_client(verify) as client,):files = {"zip_file": (path.name, file_handle, "application/zip"),diff --git a/lib/crewai-core/tests/test_plus_api_proxy.py b/lib/crewai-core/tests/test_plus_api_proxy.pynew file mode 100644index 000000000..761bc1254--- /dev/null+++ b/lib/crewai-core/tests/test_plus_api_proxy.py@@ -0,0 +1,305 @@+"""PlusAPI reaches AMP through the proxy the machine names.++Corporate networks and coding-agent sandboxes (Claude Code, Codex) only allow+outbound traffic through a proxy announced in HTTP(S)_PROXY. Two local servers+stand in for AMP and for that proxy, so the tests show which one a request reached.++The machine's settings are the variables the CLI pinned at startup and the OS+proxy settings. A variable that appears later, as a project's .env loaded by+`crewai run` or by `crewai.llm` on import puts it there, does not count.+"""++from __future__ import annotations++from collections.abc import Iterator+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+import json+from pathlib import Path+import threading+from typing import NamedTuple, cast+import urllib.request+import zipfile++from crewai_core import plus_api+from crewai_core.plus_api import PlusAPI+from dotenv import load_dotenv+import pytest+++PROXY_VARS = ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY")+TRANSPORT_VARS = (+ *PROXY_VARS,+ "SSL_CERT_FILE",+ "SSL_CERT_DIR",+ "REQUEST_METHOD",+ "UV_ENV_FILE",+)+++class Seen(NamedTuple):+ method: str+ target: str+ authorization: str | None+ body: bytes+++class _RecordingServer(ThreadingHTTPServer):+ def __init__(self, name: str) -> None:+ super().__init__(("127.0.0.1", 0), _Recorder)+ self.name = name+ self.seen: list[Seen] = []++ @property+ def url(self) -> str:+ return f"http://127.0.0.1:{self.server_address[1]}"+++class _Recorder(BaseHTTPRequestHandler):+ def _answer(self) -> None:+ server = cast(_RecordingServer, self.server)+ body = self.rfile.read(int(self.headers.get("Content-Length", 0)))+ server.seen.append(+ Seen(self.command, self.path, self.headers.get("Authorization"), body)+ )+ payload = json.dumps({"served_by": server.name}).encode()+ self.send_response(200)+ self.send_header("Content-Type", "application/json")+ self.send_header("Content-Length", str(len(payload)))+ self.end_headers()+ self.wfile.write(payload)++ def do_GET(self) -> None:+ self._answer()++ def do_POST(self) -> None:+ self._answer()++ def log_message(self, format: str, *args: object) -> None:+ pass+++def _serve(name: str) -> Iterator[_RecordingServer]:+ server = _RecordingServer(name)+ thread = threading.Thread(target=server.serve_forever, daemon=True)+ thread.start()+ yield server+ server.shutdown()+ server.server_close()+++def proxy() -> Iterator[_RecordingServer]:+ yield from _serve("proxy")+++def amp() -> Iterator[_RecordingServer]:+ yield from _serve("amp")++[email protected](autouse=True)+def machine(monkeypatch: pytest.MonkeyPatch) -> dict[str, str]:+ """The variables pinned at startup: none, on a machine with no OS proxy.++ The environment starts without transport variables too, and anything a test+ loads into it is removed afterwards.+ """+ pinned: dict[str, str] = {}+ monkeypatch.setattr(plus_api, "_MACHINE_TRANSPORT_ENV", pinned)+ monkeypatch.setattr(plus_api, "_os_proxies", dict)+ for name in TRANSPORT_VARS:+ for variant in {name, name.lower()}:+ monkeypatch.setenv(variant, "") # recorded, so teardown removes it+ monkeypatch.delenv(variant)+ return pinned+++def project_zip(tmp_path: Path) -> Path:+ path = tmp_path / "project.zip"+ with zipfile.ZipFile(path, "w") as archive:+ archive.writestr("pyproject.toml", "[project]\nname = 'demo'\n")+ return path++[email protected]_network(allowed_hosts=["127.0.0.1"])+def test_requests_go_through_the_proxy_the_machine_names(+ proxy: _RecordingServer, machine: dict[str, str]+) -> None:+ machine["HTTP_PROXY"] = proxy.url++ response = PlusAPI("amp-token", base_url="http://amp.test").get_agent("researcher")++ assert response.json() == {"served_by": "proxy"}+ assert proxy.seen == [+ Seen(+ "GET",+ "http://amp.test/crewai_plus/api/v1/agents/researcher",+ "Bearer amp-token",+ b"",+ )+ ]++[email protected]_network(allowed_hosts=["127.0.0.1"])+def test_in_a_cgi_process_an_uppercase_http_proxy_is_ignored(+ proxy: _RecordingServer, amp: _RecordingServer, machine: dict[str, str]+) -> None:+ """A CGI client's Proxy header arrives as HTTP_PROXY ("httpoxy"); the login must not follow it."""+ machine["HTTP_PROXY"] = proxy.url+ machine["REQUEST_METHOD"] = "GET"++ response = PlusAPI("amp-token", base_url=amp.url).get_agent("researcher")++ assert response.json() == {"served_by": "amp"}+ assert proxy.seen == []++[email protected]_network(allowed_hosts=["127.0.0.1"])+def test_in_a_cgi_process_a_lowercase_http_proxy_still_applies(+ proxy: _RecordingServer, machine: dict[str, str]+) -> None:+ machine["http_proxy"] = proxy.url+ machine["REQUEST_METHOD"] = "GET"++ response = PlusAPI("amp-token", base_url="http://amp.test").get_agent("researcher")++ assert response.json() == {"served_by": "proxy"}+ assert proxy.seen[0].authorization == "Bearer amp-token"+++ "variables",+ [+ {"HTTP_PROXY": "http://upper.test:1"},+ {"HTTP_PROXY": "http://upper.test:1", "REQUEST_METHOD": "GET"},+ {+ "HTTP_PROXY": "http://upper.test:1",+ "http_proxy": "http://lower.test:1",+ "REQUEST_METHOD": "POST",+ },+ {"HTTPS_PROXY": "http://secure.test:1", "REQUEST_METHOD": "GET"},+ {"HTTP_PROXY": "http://upper.test:1", "http_proxy": ""},+ ],+)+def test_proxies_are_read_as_urllib_reads_them(+ variables: dict[str, str], machine: dict[str, str], monkeypatch: pytest.MonkeyPatch+) -> None:+ for name, value in variables.items():+ machine[name] = value+ monkeypatch.setenv(name, value)++ assert plus_api._machine_proxies() == urllib.request.getproxies_environment()++[email protected]_network(allowed_hosts=["127.0.0.1"])+def test_hosts_in_no_proxy_are_reached_directly(+ proxy: _RecordingServer, amp: _RecordingServer, machine: dict[str, str]+) -> None:+ machine["HTTP_PROXY"] = proxy.url+ machine["NO_PROXY"] = "127.0.0.1"++ response = PlusAPI("amp-token", base_url=amp.url).get_agent("researcher")++ assert response.json() == {"served_by": "amp"}+ assert [seen.target for seen in amp.seen] == [+ "/crewai_plus/api/v1/agents/researcher"+ ]+ assert proxy.seen == []++[email protected]_network(allowed_hosts=["127.0.0.1"])+def test_without_proxy_settings_requests_go_directly(+ proxy: _RecordingServer, amp: _RecordingServer+) -> None:+ response = PlusAPI("amp-token", base_url=amp.url).get_agent("researcher")++ assert response.json() == {"served_by": "amp"}+ assert amp.seen[0].authorization == "Bearer amp-token"+ assert proxy.seen == []++[email protected]_network(allowed_hosts=["127.0.0.1"])+def test_with_no_proxy_variable_the_os_proxy_settings_apply(+ proxy: _RecordingServer, monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.setattr(plus_api, "_os_proxies", lambda: {"http": proxy.url})++ response = PlusAPI("amp-token", base_url="http://amp.test").get_agent("researcher")++ assert response.json() == {"served_by": "proxy"}+ assert proxy.seen[0].authorization == "Bearer amp-token"++[email protected]_network(allowed_hosts=["127.0.0.1"])+def test_a_proxy_a_project_env_sets_after_startup_is_not_used(+ proxy: _RecordingServer, amp: _RecordingServer, tmp_path: Path+) -> None:+ """`crewai run` on a JSON crew or a declarative flow, and `crewai.llm` on+ import, load the project's .env into the process. A cloned project must not+ route the saved login through its own proxy or have AMP vouched for by its+ own CA: a CA file that does not exist would fail the request if it were read."""+ env_file = tmp_path / ".env"+ env_file.write_text(+ f"HTTP_PROXY={proxy.url}\n"+ f"https_proxy={proxy.url}\n"+ f"SSL_CERT_FILE={tmp_path / 'project-ca.pem'}\n"+ )+ load_dotenv(env_file, override=True)++ response = PlusAPI("amp-token", base_url=amp.url).get_agent("researcher")++ assert response.json() == {"served_by": "amp"}+ assert amp.seen[0].authorization == "Bearer amp-token"+ assert proxy.seen == []+++def test_the_ca_bundle_the_machine_names_is_the_one_trusted(+ machine: dict[str, str], tmp_path: Path+) -> None:+ machine["SSL_CERT_FILE"] = str(tmp_path / "missing-ca.pem")++ with pytest.raises(FileNotFoundError):+ PlusAPI("amp-token", base_url="https://amp.test").get_agent("researcher")++[email protected]_network(allowed_hosts=["127.0.0.1"])+def test_zip_deploy_uploads_go_through_the_proxy(+ proxy: _RecordingServer, machine: dict[str, str], project_zip: Path+) -> None:+ machine["HTTP_PROXY"] = proxy.url++ response = PlusAPI("amp-token", base_url="http://amp.test").create_crew_from_zip(+ project_zip, name="demo"+ )++ assert response.json() == {"served_by": "proxy"}+ (seen,) = proxy.seen+ assert (seen.method, seen.target) == (+ "POST",+ "http://amp.test/crewai_plus/api/v1/crews/zip",+ )+ assert b'filename="project.zip"' in seen.body+ assert seen.authorization == "Bearer amp-token"+++def test_restoring_the_machine_transport_undoes_what_a_project_env_set(+ machine: dict[str, str],+) -> None:+ machine["HTTPS_PROXY"] = "http://machine-proxy.test:3128"+ environ = {+ "HTTPS_PROXY": "http://project-proxy.test:8080",+ "all_proxy": "socks5://project-proxy.test:1080",+ "NO_PROXY": "*",+ "SSL_CERT_FILE": "/project/ca.pem",+ "REQUEST_METHOD": "GET",+ "UV_ENV_FILE": ".env",+ "OPENAI_API_KEY": "project-key",+ }++ plus_api.restore_machine_transport(environ)++ assert environ == {+ "HTTPS_PROXY": "http://machine-proxy.test:3128",+ "OPENAI_API_KEY": "project-key",+ }diff --git a/lib/crewai/src/crewai/__init__.py b/lib/crewai/src/crewai/__init__.pyindex 60a11e090..6c5daae97 100644--- a/lib/crewai/src/crewai/__init__.py+++ b/lib/crewai/src/crewai/__init__.py@@ -3,6 +3,11 @@ import sysfrom typing import TYPE_CHECKING, Annotated, Anyimport warnings+# Pin the machine's proxy and CA for AMP requests first: importing crewai.llm and+# crewai.project loads the project's .env, which must not choose where the saved+# login travels. A crew run in its own process (`uv run run_crew`) never imports+# the CLI, so this import is the pin there.+import crewai_core.plus_api # noqa: F401from pydantic import Field, PydanticUserErrorfrom crewai.agent.core import Agentdiff --git a/lib/crewai/tests/cli/test_plus_api.py b/lib/crewai/tests/cli/test_plus_api.pyindex 5ba215aa5..c1acd382b 100644--- a/lib/crewai/tests/cli/test_plus_api.py+++ b/lib/crewai/tests/cli/test_plus_api.py@@ -252,6 +252,8 @@ class TestPlusAPI(unittest.TestCase):)self.assertEqual(response, mock_response)+ @patch.dict("crewai_core.plus_api._MACHINE_TRANSPORT_ENV", clear=True)+ @patch("crewai_core.plus_api._os_proxies", dict)@patch("crewai_core.plus_api.httpx.Client")def test_make_request(self, mock_client_class):mock_client_instance = MagicMock()@@ -261,7 +263,9 @@ class TestPlusAPI(unittest.TestCase):response = self.api._make_request("GET", "test_endpoint")- mock_client_class.assert_called_once_with(trust_env=False, verify=True)+ mock_client_class.assert_called_once_with(+ verify=True, trust_env=False, mounts={}+ )mock_client_instance.request.assert_called_once_with("GET", f"{self.api.base_url}/test_endpoint", headers=self.api.headers)diff --git a/lib/crewai/tests/test_plus_api_pin.py b/lib/crewai/tests/test_plus_api_pin.pynew file mode 100644index 000000000..0e42cc8e4--- /dev/null+++ b/lib/crewai/tests/test_plus_api_pin.py@@ -0,0 +1,98 @@+"""`import crewai` pins AMP's proxy and CA before it loads a project's .env.++A classic crew runs in its own process (`uv run run_crew`), which never imports the+CLI. There, `import crewai` is the first thing to read the project's `.env`, so the+machine's transport settings have to be captured before that happens. That holds+only if the process starts without the `.env` already in its environment, which+`crewai eval` has to see to when it loads the `.env` and then runs the crew.+"""++import json+import os+from pathlib import Path+import subprocess+import sys++from crewai_core import plus_api+import pytest+++def test_import_crewai_pins_the_machines_proxy_before_the_project_env_loads(+ tmp_path: Path,+) -> None:+ (tmp_path / ".env").write_text(+ "HTTPS_PROXY=http://project-proxy.test:8080\nSSL_CERT_FILE=/project/ca.pem\n"+ )+ env = {key: value for key, value in os.environ.items() if not plus_api._sets_transport(key)}+ probe = (+ "import json, os\n"+ "import crewai\n"+ "from crewai_core import plus_api\n"+ "print(json.dumps({'loaded': os.environ.get('HTTPS_PROXY'),"+ " 'pinned': plus_api._MACHINE_TRANSPORT_ENV}))\n"+ )++ result = subprocess.run(+ [sys.executable, "-c", probe],+ cwd=tmp_path,+ env=env,+ capture_output=True,+ text=True,+ check=True,+ )++ seen = json.loads(result.stdout.strip().splitlines()[-1])+ # crewai loaded the project's .env (so the test means something) ...+ assert seen["loaded"] == "http://project-proxy.test:8080"+ # ... after the pin, which holds only what the machine set: nothing.+ assert seen["pinned"] == {}+++ "machine",+ [{}, {"HTTPS_PROXY": "http://machine-proxy.test:3128"}],+ ids=["machine-sets-none", "machine-sets-its-own"],+)+def test_the_crew_crewai_eval_runs_pins_the_machines_proxy_not_the_project_envs(+ tmp_path: Path, machine: dict[str, str]+) -> None:+ """`crewai eval` loads the project's .env, then starts the crew with `uv run`+ in a child process that inherits its environment and pins from it. The .env+ also names itself as uv's env file, which uv would load before Python starts."""+ (tmp_path / ".env").write_text(+ "HTTPS_PROXY=http://project-proxy.test:8080\n"+ "SSL_CERT_FILE=/project/ca.pem\n"+ "CREWAI_PLUS_URL=https://amp.project.test\n"+ "UV_ENV_FILE=.env\n"+ )+ env = {key: value for key, value in os.environ.items() if not plus_api._sets_transport(key)}+ env.pop("CREWAI_PLUS_URL", None)+ env.update(machine)+ probe = (+ "import json, os, subprocess, sys\n"+ "import crewai_cli.cli\n"+ "from crewai_cli.experimental.eval_crew import _load_project_env\n"+ "from crewai_core.tool_credentials import build_env_with_all_tool_credentials\n"+ "_load_project_env()\n"+ "run = subprocess.run(['uv', 'run', '--no-project', '--python', sys.executable,"+ " 'python', '-c', 'import json, crewai;"+ " from crewai_core import plus_api; print(json.dumps(plus_api._MACHINE_TRANSPORT_ENV))'],"+ " env=build_env_with_all_tool_credentials(), capture_output=True, text=True, check=True)\n"+ "print(json.dumps({'amp': os.environ.get('CREWAI_PLUS_URL'),"+ " 'run_pinned': json.loads(run.stdout.strip().splitlines()[-1])}))\n"+ )++ result = subprocess.run(+ [sys.executable, "-c", probe],+ cwd=tmp_path,+ env=env,+ capture_output=True,+ text=True,+ check=True,+ )++ seen = json.loads(result.stdout.strip().splitlines()[-1])+ # The command loaded the project's .env (so the test means something) ...+ assert seen["amp"] == "https://amp.project.test"+ # ... and the run it started pinned only what the machine set.+ assert seen["run_pinned"] == machine