Skip to content

fix: sanitize NO_PROXY env vars containing newline characters#3308

Open
paulxg12 wants to merge 1 commit into
openai:mainfrom
paulxg12:fix/no-proxy-newline-sanitize
Open

fix: sanitize NO_PROXY env vars containing newline characters#3308
paulxg12 wants to merge 1 commit into
openai:mainfrom
paulxg12:fix/no-proxy-newline-sanitize

Conversation

@paulxg12
Copy link
Copy Markdown

What

Sanitize NO_PROXY / no_proxy environment variables by replacing embedded newlines with commas before httpx reads them.

Why

Fixes #3303. When NO_PROXY contains newline characters (common in Docker, .env files, and shell scripts), httpx splits only by commas. The newline becomes part of the hostname, triggering:

httpx.InvalidURL: Invalid non-printable ASCII character in URL, \n at position 16

How

Add a _sanitize_proxy_env_vars() helper that normalises newline-separated entries into comma-separated ones. Call it from both _DefaultHttpxClient.__init__ and _DefaultAsyncHttpxClient.__init__ so that sync and async clients are both covered.

Testing

  • Existing test suite continues to pass (the helper is a no-op when NO_PROXY has no newlines, which is the common case).
  • Manually verified the reproducer from the issue:
import os
os.environ["NO_PROXY"] = "localhost\n192.168.1.1"
from openai import OpenAI  # no longer raises InvalidURL

Checklist

  • Follows existing code style
  • Tests pass locally
  • No breaking changes
  • Documentation updated if needed

When NO_PROXY contains newlines (common in Docker, .env files, or
shell scripts), httpx's get_environment_proxies() splits only by
commas, causing the newline to become part of the hostname and
triggering InvalidURL on client creation.

This adds a sanitization step in both _DefaultHttpxClient and
_DefaultAsyncHttpxClient that replaces newlines with commas so
each line becomes a separate NO_PROXY entry.

Fixes openai#3303
@paulxg12 paulxg12 requested a review from a team as a code owner May 26, 2026 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InvalidURL error when NO_PROXY environment variable contains newline characters

1 participant