starlette.datastructures.Headers(raw=...) membership check fails when raw header names aren’t lowercased (ASGI interop) #3104
Unanswered
folex
asked this question in
Potential Issue
Replies: 1 comment
-
|
This is a real edge case, but it comes from passing non-ASGI-normalized raw headers.
If the raw headers come from ASGI, they should already be lower-case. If they come from another source, normalize them before constructing |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! We ran into an interop issue when constructing
starlette.datastructures.Headersfrom arawheader list. If therawlist contains header names that are not lowercased bytes (e.g.b"Content-Encoding"), the membership check viaindoes not work, even thoughHeaders.__repr__shows the header present.This can lead to incorrect behavior in middleware that relies on
"content-encoding" in headers(e.g.GZipMiddlewarepotentially double-compressing a response).Reproduction
Expected
True, orHeadersshould normalizerawheader names to lowercase internally, orHeaders(raw=...)should validate/document thatrawkeys must already be lowercased.Actual
False.Notes / context
ASGI specifies header names should be lowercase bytes. In our case,
prometheus-clientproducedrawheaders with capitalized names. Starlette preservesrawas-is, while__contains__lowercases the lookup key—so membership checks can never match.These headers are produced by prometheus_client's
make_asgi_app: https://github.com/prometheus/client_python/blob/e1cdc203b1cf5f15c7b9a64d79fccc7907a62ca3/prometheus_client/asgi.py#L8Thanks for taking a look—happy to provide additional details!
Beta Was this translation helpful? Give feedback.
All reactions