-
Notifications
You must be signed in to change notification settings - Fork 5.1k
fix: sanitize newlines in NO_PROXY env var before httpx client init #3519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rkfshakti
wants to merge
2
commits into
openai:main
Choose a base branch
from
rkfshakti:fix/no-proxy-newline-sanitize
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+296
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| # Regression tests for NO_PROXY newline sanitization (issue #3303). | ||
| # | ||
| # httpx's ``get_environment_proxies()`` only splits on commas, so a trailing | ||
| # newline in ``NO_PROXY`` becomes part of the hostname and httpx raises | ||
| # ``InvalidURL``. The SDK temporarily normalizes the env var during client | ||
| # construction and restores it afterwards, so unrelated clients are unaffected. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| def _set_no_proxy(monkeypatch: pytest.MonkeyPatch, value: str | None) -> None: | ||
| """Set both NO_PROXY and no_proxy via monkeypatch for automatic cleanup.""" | ||
| if value is None: | ||
| monkeypatch.delenv("NO_PROXY", raising=False) | ||
| monkeypatch.delenv("no_proxy", raising=False) | ||
| else: | ||
| monkeypatch.setenv("NO_PROXY", value) | ||
| monkeypatch.setenv("no_proxy", value) | ||
|
|
||
|
|
||
| def _mount_patterns(client: object) -> list[str]: | ||
| return [k.pattern for k in client._mounts] # type: ignore[attr-defined] | ||
|
|
||
|
|
||
| def test_sync_client_construction_with_newline_no_proxy(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """A sync default client can be constructed when NO_PROXY has newlines.""" | ||
| from openai._base_client import _DefaultHttpxClient | ||
|
|
||
| _set_no_proxy(monkeypatch, "localhost\n127.0.0.1") | ||
| # Should not raise InvalidURL | ||
| client = _DefaultHttpxClient() | ||
| patterns = _mount_patterns(client) | ||
| assert any("localhost" in p for p in patterns) | ||
| assert any("127.0.0.1" in p for p in patterns) | ||
| client.close() | ||
|
|
||
|
|
||
| def test_async_client_construction_with_newline_no_proxy(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """An async default client can be constructed when NO_PROXY has newlines.""" | ||
| from openai._base_client import _DefaultAsyncHttpxClient | ||
|
|
||
| _set_no_proxy(monkeypatch, "localhost\n127.0.0.1") | ||
| # Should not raise InvalidURL | ||
| client = _DefaultAsyncHttpxClient() | ||
| patterns = _mount_patterns(client) | ||
| assert any("localhost" in p for p in patterns) | ||
| assert any("127.0.0.1" in p for p in patterns) | ||
|
|
||
|
|
||
| def test_env_restored_after_sync_client_construction(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """os.environ is restored to its original value after client construction.""" | ||
| from openai._base_client import _DefaultHttpxClient | ||
|
|
||
| original = "localhost\n127.0.0.1" | ||
| _set_no_proxy(monkeypatch, original) | ||
| client = _DefaultHttpxClient() | ||
| client.close() | ||
| import os | ||
|
|
||
| assert os.environ.get("NO_PROXY") == original | ||
| assert os.environ.get("no_proxy") == original | ||
|
|
||
|
|
||
| def test_env_restored_after_async_client_construction(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """os.environ is restored after async client construction.""" | ||
| from openai._base_client import _DefaultAsyncHttpxClient | ||
|
|
||
| original = "localhost\n127.0.0.1" | ||
| _set_no_proxy(monkeypatch, original) | ||
| _DefaultAsyncHttpxClient() | ||
| import os | ||
|
|
||
| assert os.environ.get("NO_PROXY") == original | ||
| assert os.environ.get("no_proxy") == original | ||
|
|
||
|
|
||
| def test_trust_env_false_skips_sanitization(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """When trust_env=False, NO_PROXY is not touched and no InvalidURL is raised.""" | ||
| from openai._base_client import _DefaultHttpxClient | ||
|
|
||
| _set_no_proxy(monkeypatch, "localhost\n127.0.0.1") | ||
| client = _DefaultHttpxClient(trust_env=False) | ||
| import os | ||
|
|
||
| # env should be untouched | ||
| assert os.environ.get("NO_PROXY") == "localhost\n127.0.0.1" | ||
| # no proxy mounts should be configured since trust_env=False | ||
| assert client._mounts == {} | ||
| client.close() | ||
|
|
||
|
|
||
| def test_trust_env_false_async_skips_sanitization(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """Async client with trust_env=False skips NO_PROXY sanitization.""" | ||
| from openai._base_client import _DefaultAsyncHttpxClient | ||
|
|
||
| _set_no_proxy(monkeypatch, "localhost\n127.0.0.1") | ||
| client = _DefaultAsyncHttpxClient(trust_env=False) | ||
| import os | ||
|
|
||
| assert os.environ.get("NO_PROXY") == "localhost\n127.0.0.1" | ||
| assert client._mounts == {} | ||
|
|
||
|
|
||
| def test_no_newline_no_mutation(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """When NO_PROXY has no newlines, the env var is not modified at all.""" | ||
| from openai._base_client import _DefaultHttpxClient | ||
|
|
||
| _set_no_proxy(monkeypatch, "localhost,127.0.0.1") | ||
| client = _DefaultHttpxClient() | ||
| client.close() | ||
| import os | ||
|
|
||
| assert os.environ.get("NO_PROXY") == "localhost,127.0.0.1" | ||
|
|
||
|
|
||
| def test_lowercase_no_proxy_sanitized(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """Lowercase no_proxy is also sanitized.""" | ||
| from openai._base_client import _DefaultHttpxClient | ||
|
|
||
| monkeypatch.delenv("NO_PROXY", raising=False) | ||
| monkeypatch.setenv("no_proxy", "localhost\n127.0.0.1") | ||
| client = _DefaultHttpxClient() | ||
| client.close() | ||
| import os | ||
|
|
||
| # restored after construction | ||
| assert os.environ.get("no_proxy") == "localhost\n127.0.0.1" | ||
|
|
||
|
|
||
| def test_multiple_newlines_sanitized(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """Multiple newlines and whitespace are handled correctly.""" | ||
| from openai._base_client import _DefaultHttpxClient | ||
|
|
||
| _set_no_proxy(monkeypatch, "localhost\n\n127.0.0.1\n.example.com\n") | ||
| client = _DefaultHttpxClient() | ||
| patterns = _mount_patterns(client) | ||
| assert any("localhost" in p for p in patterns) | ||
| assert any("127.0.0.1" in p for p in patterns) | ||
| assert any("example.com" in p for p in patterns) | ||
| client.close() | ||
| import os | ||
|
|
||
| # restored | ||
| assert os.environ.get("NO_PROXY") == "localhost\n\n127.0.0.1\n.example.com\n" | ||
|
|
||
|
|
||
| def test_carriage_return_sanitized(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """A lone \\r (from CRLF files where \\n was stripped) is also sanitized.""" | ||
| from openai._base_client import _DefaultHttpxClient | ||
|
|
||
| _set_no_proxy(monkeypatch, "localhost\r127.0.0.1") | ||
| client = _DefaultHttpxClient() | ||
| patterns = _mount_patterns(client) | ||
| assert any("localhost" in p for p in patterns) | ||
| assert any("127.0.0.1" in p for p in patterns) | ||
| client.close() | ||
| import os | ||
|
|
||
| assert os.environ.get("NO_PROXY") == "localhost\r127.0.0.1" | ||
|
|
||
|
|
||
| def test_crlf_sanitized(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """CRLF (\\r\\n) line endings are sanitized correctly.""" | ||
| from openai._base_client import _DefaultHttpxClient | ||
|
|
||
| _set_no_proxy(monkeypatch, "localhost\r\n127.0.0.1\r\n") | ||
| client = _DefaultHttpxClient() | ||
| patterns = _mount_patterns(client) | ||
| assert any("localhost" in p for p in patterns) | ||
| assert any("127.0.0.1" in p for p in patterns) | ||
| client.close() | ||
|
|
||
|
|
||
| def test_aiohttp_client_construction_with_newline_no_proxy(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """The aiohttp transport client also sanitizes NO_PROXY newlines.""" | ||
| pytest.importorskip("httpx_aiohttp") | ||
| from openai._base_client import _DefaultAioHttpClient | ||
|
|
||
| _set_no_proxy(monkeypatch, "localhost\n127.0.0.1") | ||
| # Should not raise InvalidURL | ||
| client = _DefaultAioHttpClient() | ||
| patterns = _mount_patterns(client) | ||
| assert any("localhost" in p for p in patterns) | ||
| assert any("127.0.0.1" in p for p in patterns) | ||
|
|
||
|
|
||
| def test_aiohttp_client_trust_env_false_skips_sanitization(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """The aiohttp transport client respects trust_env=False.""" | ||
| pytest.importorskip("httpx_aiohttp") | ||
| from openai._base_client import _DefaultAioHttpClient | ||
|
|
||
| _set_no_proxy(monkeypatch, "localhost\n127.0.0.1") | ||
| client = _DefaultAioHttpClient(trust_env=False) | ||
| import os | ||
|
|
||
| assert os.environ.get("NO_PROXY") == "localhost\n127.0.0.1" | ||
| assert client._mounts == {} | ||
|
|
||
|
|
||
| def test_concurrent_client_construction_serializes_sanitization(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """Concurrent client constructions must not race on the env mutation. | ||
|
|
||
| Without the lock, one call could restore the original (invalid) NO_PROXY | ||
| value while another call's ``super().__init__()`` is still reading the | ||
| environment, exposing the second client to InvalidURL. The lock | ||
| serializes the sanitize-construct-restore window so each call sees a | ||
| consistent environment. | ||
| """ | ||
| import threading | ||
|
|
||
| from openai._base_client import _DefaultHttpxClient | ||
|
|
||
| _set_no_proxy(monkeypatch, "localhost\n127.0.0.1") | ||
| errors: list[Exception] = [] | ||
|
|
||
| def construct() -> None: | ||
| try: | ||
| _DefaultHttpxClient() | ||
| except Exception as exc: | ||
| errors.append(exc) | ||
|
|
||
| threads = [threading.Thread(target=construct) for _ in range(10)] | ||
| for t in threads: | ||
| t.start() | ||
| for t in threads: | ||
| t.join() | ||
|
|
||
| assert not errors, f"Concurrent constructions failed: {errors}" | ||
| # The original (invalid) value must be restored after all constructions | ||
| assert os.environ.get("NO_PROXY") == "localhost\n127.0.0.1" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When users opt into the documented aiohttp transport with
AsyncOpenAI(http_client=DefaultAioHttpClient()), this new guard never runs:_DefaultAioHttpClientbelow still delegates directly to itshttpx.AsyncClient-compatible superclass, soNO_PROXY/no_proxyvalues containing newlines can still raise during client construction. Please wrap that constructor with the same_sanitized_no_proxy()logic, while preserving thetrust_env=Falseskip, so the regression fix applies to all SDK-provided clients.Useful? React with 👍 / 👎.