Skip to content

v2.7.13

Latest

Choose a tag to compare

@matsumotory matsumotory released this 24 Nov 13:46
e0b17ec

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).

更新前に確認する挙動の変更

1. サブリクエストでスクリプトが変えたヘッダは、メインリクエストが中継先へ送るヘッダに届かなくなる(逆も同じ)

  • 変更前:スクリプトがNginx::Headers_in#deleteか#[]=でサブリクエスト(auth_requestのサブリクエストや、Nginx::Async::HTTP.sub_requestで始めたサブリクエスト)でリクエストのヘッダを変えると、その変更がメインリクエストの中継先へ送るヘッダに届くことがあり、メインリクエストでの変更がサブリクエストに届くこともありました。どの変更が届くかは、変更の種類と、削除では消すヘッダの位置で決まりました。nginxは、サブリクエストに、メインリクエストのヘッダのリストの浅いコピーを渡します(sr->headers_in = r->headers_in)。先頭のpartは各リクエストで別の構造体になりますが、その後ろのpartは両方で同じ構造体のままで、リストのlastポインタもコピーされるので、サブリクエストのlastはメインリクエストのpartを指します。
    • サブリクエストの[]=による追加(既存のヘッダを置き換える[]=の追加側も含む)は、ヘッダの数によらずメインリクエストの中継先へ送るヘッダに届きました。サブリクエストのコピーされたlastがメインリクエストのpartを指すので、pushがそこに入るためです。
    • サブリクエストのdelete(置き換えの削除側も含む)がメインリクエストに届くのは、消すヘッダが先頭から21個目以降のときだけでした。1つのpartには20個のヘッダが入り、共有されるのは先頭のpartより後ろだけなので、先頭のpart(ヘッダが20個以下のリクエストや、先頭から20個目まで)での変更は共有されませんでした。このため、置き換えは、置き換えるヘッダが先頭から20個目までにあるときはメインリクエストの中継先に古い値と新しい値の両方を残し、21個目以降にあるときは新しい値だけを残しました。内部向けのヘッダを消すauth_requestのサブリクエストも、消すヘッダが21個目以降にあるときだけ、そのヘッダをメインリクエストの中継先から消していました。
    • 逆向き(メインリクエストがサブリクエストを始めた後に行った変更がサブリクエストに届くこと)も、先頭より後ろのpartで同じように起きました(ヘッダが20個を超えるリクエスト)。
    • ヘッダが20個以下のリクエストでは、サブリクエストが足したヘッダはサブリクエスト自身のpartではなくメインリクエストのpartに入るので、サブリクエスト自身が中継先へ送るヘッダにはそれが入りませんでした。
  • 変更後:スクリプトがリクエストのヘッダを変えると、そのリクエストへ、共有していたpartの自分用のコピーと、自分用のlastを渡します。これにより、deleteや[]=はそのリクエスト自身のリストを変え、サブリクエストでのヘッダの変更は、ヘッダの数や、サブリクエストを作るもの(auth_requestのサブリクエスト、Nginx::Async::HTTP.sub_request、mirrorなどのディレクティブ)によらず、メインリクエストが中継先へ送るヘッダに届かなくなります。(ヘッダが20個以下のリクエストは、埋め込みのpartの配列をサブリクエストと共有したままです。配列を複製しないのは、nginxがキャッシュする要素のアドレスを保つためです。そのため、メインリクエストと、バックグラウンドで動くmirrorのサブリクエストが、そのようなリクエストにそれぞれヘッダを足すと、以前と同じく、その配列の同じ場所に書きます。)
  • 変更の理由:
    • 不具合:先頭より後ろのpartを共有していたため、スクリプトが片方のリクエストでヘッダを変えると、もう一方のリクエストが読むリストの構造が変わり、もう一方のリクエストが、自分のスクリプトが設定していないヘッダを中継先へ送ってしまったり、送るはずのヘッダを落としてしまったりしました。同じ共有によって、もう一方のリクエストが、リストのpartの配列の末尾の1つ先を読み書きすることもありました。詳細はGHSA-xc5m-h3wm-hcq6にあります。
    • 前の挙動を残せなかった理由:この伝わり方は、メモリを共有していたことの副作用で、意図した機能ではありませんでした。結果はヘッダの数と変えたヘッダの位置で変わりました。伝わり方を残すと、あるリクエストが別のリクエストのヘッダのリストに書き込む形が残ります。各リクエストにpartの自分用のコピーを持たせるほうが、挙動がはっきり決まり、各リクエストのリストがそのリクエストだけのものになります。先頭のpartは元から各リクエストで別の構造体なので、そこでのdeleteは伝わりませんでした。ただし追加は、共有しているlastがメインリクエストのpartを指していたため、伝わりました。修正は各リクエストに自分用のlastを渡すので、追加もそのリクエスト自身のpartに入ります。
    • ドキュメントが示していた挙動:2.7.12のNginx::Headers_inは、deleteと[]=がリクエストのヘッダを変えると書いていました。サブリクエストでの変更がメインリクエストに届くことや、その逆向きの伝わり方については、何も書いていませんでした。
  • 影響の範囲:サブリクエストのヘッダの変更がメインリクエストの中継先へ届くこと、または逆向きに頼っている設定です。メインリクエストの中継先へ向けてサブリクエストでヘッダを追加または置き換える設定は、ヘッダの数によらず影響を受けます。サブリクエストでヘッダを削除する設定と、逆向き(メインリクエストの変更がサブリクエストに届くこと)に頼る設定は、ヘッダが20個を超えるリクエストで影響を受けます。中継先へ反映したいリクエストの側で変更するスクリプトは、影響を受けません。
  • 必要な対応:中継先へ反映したいリクエストの側で変更するか、nginx自身の仕組みで値を渡してください。
    • 内部向けのヘッダを中継先へ送らないようにするには、サブリクエストではなく、メインリクエスト自身のハンドラ(中継するlocationのrewriteフェーズかaccessフェーズのハンドラ)で消すか、nginx自身のディレクティブで送らないようにします。後者は、proxy_set_header <名前> "";(空の値)か、リクエストのヘッダを1つも送らないproxy_pass_request_headers off;です。
    • auth_requestのサブリクエストからメインリクエストの中継先へ値を渡すには、サブリクエストの応答に値を載せ、auth_request_setで読んでください(たとえば、サブリクエストがNginx::Request.new.headers_out["X-Name"]を設定し、中継するlocationでauth_request_set $name $sent_http_x_name;としてからproxy_set_header X-Name $name;とします)。サブリクエストがNginx::Var#setで設定する変数も使えますが、その変数はlocationのrewriteフェーズの外(serverブロックのsetかmap)で宣言してください。そうしないと、mrubyのハンドラが既定値より先に走り、値が上書きされます。
    • Nginx::Async::HTTP.sub_requestでは、サブリクエストが戻った後に、メインリクエスト自身のハンドラでヘッダを足せます(サブリクエストの応答はNginx::Async::HTTP.last_responseで読めます)。各リクエストは自分の変更を中継先へ送り続けるからです。
  • テスト:ポート18106と18109です(test/conf/conf.d/75-headers-in-subrequest.conf、test/t/cases/headers_in_subrequest.rb)。