You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit was created on GitHub.com and signed with GitHub’s verified signature.
Behavior changes: read before upgrading
1. A header change a script makes in a subrequest no longer reaches the main request's forwarded headers, or the reverse
Before: when a script changed the request headers with Nginx::Headers_in#delete or #[]= in a subrequest (an auth_request subrequest, or one started with Nginx::Async::HTTP.sub_request), the change could reach the main request's forwarded headers, and a change the main request made could reach the subrequest. Which changes reached depended on the kind of change, and for a delete on the position of the removed header. nginx gives a subrequest a shallow copy of the main request's header list (sr->headers_in = r->headers_in): the embedded first part is a separate struct in each request, but the parts after it are the same structs in both, and the list's last pointer is copied as well, so the subrequest's last points at the main request's part.
A subrequest's []= that added a header (and the add half of a []= that replaced an existing one) reached the main request's forwarded headers for any header count, because the subrequest's copied last pointed at the main request's part, so the push landed there.
A subrequest's delete (and the delete half of a replace) reached the main request only when the removed header was beyond the first 20. A list part holds 20 entries, and only the parts after the embedded first one are shared; a change within the embedded first part (a request of 20 headers or fewer, or one of the first 20 headers) was not shared. So a replace left the main request's upstream with both the old and the new value when the replaced header was within the first 20, and with the new value alone when it was beyond them; and an auth_request subrequest that deleted an internal header kept it from the main request's upstream only when the deleted header was beyond the first 20.
The reverse happened for the parts after the first in the same way (a request of more than 20 headers): a change the main request made after starting a subrequest could reach that subrequest.
For a request of 20 headers or fewer, a header a subrequest added went into the main request's part, not the subrequest's own, so the subrequest's own forwarded headers did not even include it.
Now: when a script changes a request's headers, that request is given its own copies of the header-list parts it shares, and its own last, so a delete or []= changes that request's own list, and a subrequest's header change no longer reaches the main request's forwarded headers, for any header count and whichever makes the subrequest (an auth_request subrequest, Nginx::Async::HTTP.sub_request, or a directive such as mirror). (A request of 20 headers or fewer still shares the single embedded part's backing array with its subrequests (the array is not copied, so the element addresses nginx caches stay valid), so a main request and a background mirror subrequest that each add a header to such a request write into the same slot of that array, within the array as before.)
Why it changed:
The defect: because the parts after the first were shared, a change a script made to the headers in one request changed the list structure the other request then read, so the other request could forward headers that its own script had not set, or lose headers it had. The same sharing could also make the other request read or write one element past the end of a list part's array; GHSA-xc5m-h3wm-hcq6 has the details.
Why the old behavior could not be kept: the propagation was a side effect of the shared memory, not a defined feature. Its result depended on the header count and the position of the changed header. Keeping the propagation would mean a request still writing into another request's header list. Giving each request its own copies of the parts is both well-defined and keeps each request's list to itself. The embedded first part was already a separate struct in each request, so a delete there never propagated, but an add did, because the shared last pointed into the main request's part; the fix gives each request its own last, so its add goes to its own part too.
What the documentation promised: Nginx::Headers_in of 2.7.12 said that delete and []= change the request's headers, and nothing about a change in a subrequest reaching the main request, or the reverse.
Affected: a configuration that relied on a subrequest's header change reaching the main request's forwarded headers, or on the reverse. A subrequest that added or replaced a header for the main request's upstream is affected for any header count. A subrequest that deleted a header, and a configuration that relied on the reverse direction (a main request's change reaching a subrequest), is affected for a request of more than 20 headers. A script that makes the change in the request whose forwarded headers it is meant for is unaffected.
What to do: make the change in the request whose forwarded headers it is meant for, or pass the value with nginx's own mechanisms.
To keep an internal header from the upstream, delete it in the main request's own handler (the rewrite or access phase handler of the proxied location) instead of in the subrequest, or keep it from the upstream with nginx's own directives: proxy_set_header <name> ""; (empty value), or proxy_pass_request_headers off; to forward none of the request's headers.
To let an auth_request subrequest pass a value to the main request's upstream, set it on the subrequest's response and read it with auth_request_set (for example the subrequest sets Nginx::Request.new.headers_out["X-Name"], and the proxied location has auth_request_set $name $sent_http_x_name; then proxy_set_header X-Name $name;). A variable the subrequest sets with Nginx::Var#set also works, if it is declared outside the location's rewrite phase (a set in the server block, or a map), so the mruby handler does not run before the default and get overwritten.
With Nginx::Async::HTTP.sub_request, the main request's own handler can add the header after the subrequest returns (it can read the subrequest's response with Nginx::Async::HTTP.last_response), because a request still forwards its own changes.
Tests: ports 18106 and 18109 (test/conf/conf.d/75-headers-in-subrequest.conf, test/t/cases/headers_in_subrequest.rb).