Repository navigation
feat(handler): send response cookies returned by a handler - #139
Merged
Merged
Conversation
`set-cookie` is the one response header cowboy does not take from the headers map. A response may carry several of them, and a map holds one value per name, so cowboy keeps cookies in the request's `resp_cookies` field and fills the map key itself at reply time, letting each protocol write one header per cookie. minirest passed the handler's header map straight to `cowboy_req:reply/4` and never touched `resp_cookies`, so a handler had no way to set a cookie. Returning the header exits with `response_error` on cowboy 2.10 and later, and silently clobbers any cookie already set on the request before that. Move any `set-cookie` entry out of the header map and into `resp_cookies` before replying. The value is one cookie as a binary, or several as a list of binaries, each already formatted by `cow_cookie:setcookie/3`. Cookies are keyed by name, as cowboy does, so setting the same cookie twice replaces it instead of sending two headers the browser cannot reconcile.
zmstone
added a commit
to zmstone/emqx
that referenced
this pull request
Sep 5, 2026
Temporary. Replace with a released tag once emqx/minirest#139 lands. The SSO fix needs minirest to send a `set-cookie` returned by a handler; without it every OIDC login start answers 500.
3 tasks done
zmstone
marked this pull request as ready for review
September 6, 2026 15:39
zmstone
marked this pull request as draft
September 7, 2026 12:44
zmstone
marked this pull request as ready for review
September 7, 2026 19:24
hjianbo
approved these changes
Sep 8, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
A minirest handler cannot set a response cookie today.
reply_with_body/4passes thehandler's header map straight to
cowboy_req:reply/4, and cowboy does not acceptset-cookiethere:The reason is that
set-cookieis the one response header that must not be folded intoa single line, so a response may carry several of them, while a map holds one value per
name. Cowboy keeps cookies in the request's
resp_cookiesfield and fills the map keyitself at reply time, giving it a list value that each protocol expands into one header
per cookie (
cowboy_http:headers_to_list/1,cowboy_http2:headers_to_list/1).So the key is reserved and type-overloaded. A handler that returns a plain binary there
exits with
response_erroron cowboy 2.10 and later. On 2.9, which this repo buildsagainst, there is no guard: the value reaches
headers_to_list/1, the list comprehensiongets a binary generator and raises
bad_generatorin the connection process, and acookie set earlier on the request is silently clobbered by
response_headers/2.This moves any
set-cookieentry out of the header map intoresp_cookiesbeforereplying, which is the translation cowboy expects a framework layered on it to do.
Interface
Return the formatted cookie under the lower case
set-cookiekey:The value is one cookie as a binary, or several as a list of binaries, each already
formatted by
cow_cookie:setcookie/3. Anything else raises{invalid_set_cookie_header, Value}at the point of the mistake, rather than deep in theprotocol. Cookies are keyed by name, as cowboy does, so setting the same cookie twice
replaces it instead of sending two headers the browser cannot reconcile, and a cookie a
middleware already set on the request is preserved.
The header name must be lower case. That matches how cowboy keys headers, and HTTP/2
requires it. A differently cased key passes through as an ordinary header, exactly as it
does today.
Compatibility
Handlers that do not return
set-cookieare unaffected: one extramaps:take/2perresponse. Handlers that do return it currently crash, so there is no behaviour to keep.
Tests
t_set_cookieandt_set_cookiesinminirest_handler_SUITEdrive real requests throughthe listener and assert the cookies arrive as separate
set-cookieheaders. Theassertions check the
name=valuepair and the requested attributes rather than the wholestring, because the attribute list differs between cowlib versions (2.9's cowlib appends
Version=1).make ct(13 passed),make xrefandmake fmt-checkare clean on OTP 27.2.Why
Needed by an EMQX Dashboard SSO fix that binds each OIDC and SAML login round-trip to the
browser that started it with a login-start cookie checked at the callback. There is no
route from a minirest handler to a cookie without this.