Skip to content

pyodide: preserve User-Agent header in httpx and httpx2 requests - #7440

Closed
spideydotjs wants to merge 2 commits into
cloudflare:mainfrom
spideydotjs:fix/python-httpx-user-agent
Closed

spideydotjs wants to merge 2 commits into
cloudflare:mainfrom
spideydotjs:fix/python-httpx-user-agent

Conversation

@spideydotjs

Copy link
Copy Markdown

Fixes #7242

Problem

In Python Workers, outbound requests using httpx or httpx2 silently dropped custom User-Agent headers, whereas direct calls via fetch(req) preserved them.

In the Emscripten fetch transports used by httpx (0.28.1) and httpx2 (via httpx2_jsfetch), HEADERS_TO_IGNORE = ("user-agent",) explicitly stripped the header to avoid browser console warnings, where User-Agent is considered a forbidden request header. In workerd and Node.js, fetch() fully allows setting custom User-Agent headers without restriction.

Solution

  1. Register import execution patches for both httpx._transports.emscripten and httpx2_jsfetch in entropy_import_context_packages.py, clearing module.HEADERS_TO_IGNORE to an empty tuple upon module execution.
  2. Add regression unit test in test_entropy_patches.py and wire the unit-test-entropy-patches target in src/pyodide/BUILD.bazel.
  3. Add dedicated Python runtime test workflow in .github/workflows/python-tests.yml.

In Python Workers, outbound requests using httpx or httpx2 silently
dropped custom User-Agent headers. Direct calls via fetch(req) preserved
the header as expected.

In the Emscripten fetch transports used by httpx (0.28.1) and httpx2
(via httpx2_jsfetch), HEADERS_TO_IGNORE = ("user-agent",) explicitly
stripped the header to avoid browser console warnings, where User-Agent
is considered a forbidden request header. In workerd and Node.js,
fetch() fully allows setting custom User-Agent headers without restriction.

Register import execution patches for both httpx._transports.emscripten
and httpx2_jsfetch in entropy_import_context_packages.py, clearing
module.HEADERS_TO_IGNORE to an empty tuple upon module execution.

Add regression unit test in test_entropy_patches.py and wire the
unit-test-entropy-patches target in src/pyodide/BUILD.bazel.

Fixes cloudflare#7242
Add a dedicated workflow in .github/workflows/python-tests.yml running
on ubuntu-latest.

The workflow executes matrix unit testing across Python 3.12 and 3.13,
runs the containerized test suite in python:3.12-slim via Docker, and
verifies git diff whitespace hygiene.

Add /.context/ to .gitignore to exclude local investigation scratchpads.
@spideydotjs
spideydotjs requested review from a team as code owners September 20, 2026 08:26
@github-actions

github-actions Bot commented Sep 20, 2026

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@spideydotjs

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Sep 20, 2026
@ryanking13

Copy link
Copy Markdown
Contributor

Sorry, we do not want to fix this by patching the package in workerd. Instead, this needs to be upstreamed to httpx2.

@ryanking13 ryanking13 closed this Sep 21, 2026
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.

🐛 Bug Report — httpx via Cloudflare Python Worker drops User-Agent headers

2 participants