-
Notifications
You must be signed in to change notification settings - Fork 517
Fixes how Python Workers treats duplicate headers #5915
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
base: main
Are you sure you want to change the base?
Changes from all commits
2be488a
ce5bbb7
a4a6651
3b930d8
78b49ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -318,6 +318,15 @@ def _is_js_instance(val, js_cls_name): | |||||
| return hasattr(val, "constructor") and val.constructor.name == js_cls_name | ||||||
|
|
||||||
|
|
||||||
| def _preserve_request_header_commas() -> bool: | ||||||
| try: | ||||||
| import _cloudflare_compat_flags as compat_flags | ||||||
| except Exception: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return False | ||||||
|
|
||||||
| return bool(getattr(compat_flags, "python_request_headers_preserve_commas", False)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need the
Suggested change
|
||||||
|
|
||||||
|
|
||||||
| def _to_js_headers(headers: Headers): | ||||||
| if isinstance(headers, list): | ||||||
| # We should have a list[tuple[str, str]] | ||||||
|
|
@@ -775,10 +784,26 @@ def headers(self): | |||||
| import http.client | ||||||
|
|
||||||
| result = http.client.HTTPMessage() | ||||||
| if not _preserve_request_header_commas(): | ||||||
| for key, val in self.js_object.headers: | ||||||
| result[key] = val.strip() | ||||||
|
|
||||||
| return result | ||||||
|
|
||||||
| for key, val in self.js_object.headers: | ||||||
| for subval in val.split(","): | ||||||
| result[key] = subval.strip() | ||||||
| # With the exception of Set-Cookie, duplicate headers can and are combined with a comma | ||||||
| # in the JS Headers API. We do the same when returning the headers to Python. | ||||||
| # | ||||||
| # See https://httpwg.org/specs/rfc9110.html#rfc.section.5.3. | ||||||
| js_headers = self.js_object.headers | ||||||
| set_cookie_headers = js_headers.getSetCookie() | ||||||
| if set_cookie_headers: | ||||||
| for value in set_cookie_headers: | ||||||
| result.add_header("Set-Cookie", value.strip()) | ||||||
|
|
||||||
| for key, val in js_headers: | ||||||
| if key.lower() == "set-cookie": | ||||||
| continue | ||||||
| result.add_header(key, val.strip()) | ||||||
|
|
||||||
| return result | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1335,4 +1335,11 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef { | |
| # checking for null will break. To migrate, either: | ||
| # 1. Add a null check: if (controller.byobRequest) { ... } | ||
| # 2. Explicitly set autoAllocateChunkSize when creating the stream | ||
|
|
||
| pythonRequestHeadersPreserveCommas @155 :Bool | ||
| $compatEnableFlag("python_request_headers_preserve_commas") | ||
| $compatDisableFlag("disable_python_request_headers_preserve_commas") | ||
| $compatEnableDate("2026-02-04"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am curious why are we setting the compat date to a lot future? |
||
| # Preserve commas in Python Request headers rather than treating them as separators, | ||
| # while still exposing multiple Set-Cookie headers as distinct values. | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Why not make this into a helper method like this: