Skip to content

v2.7.8

Choose a tag to compare

@matsumotory matsumotory released this 15 Nov 05:48
976015c

Behavior changes: read before upgrading

Nginx.redirect raises TypeError for a status code that is not an Integer, and add_listener for an :address that is not a String (#813)

Security advisory (the status code of Nginx.redirect to a URL): GHSA-8j8w-mcp5-xp3h.

  • Before: Nginx.redirect read its second argument, the status code of a redirect to a URL, as an Integer without checking its type. With a String or a Float, the response had the Location header and a status line with a number that is not a status code and no reason phrase. With nil, the client got no response, and the connection was closed. Nginx::Server#add_listener and Nginx::Stream.add_listener read the :address of their Hash as a String without checking its type. With a missing :address, nil or a value that is not a String, such as an Integer, nginx ended with a segmentation fault while it read the configuration, also with nginx -t.
  • Now: for a redirect to a URL, Nginx.redirect raises TypeError (String cannot be converted to Integer, with the class of the value, or nil cannot be converted to Integer) when the status code is not an Integer. The exception ends the handler like any other exception of the script: a rescue in the script catches it, and otherwise ngx_mruby logs it and the request gets 500. No redirect is made and no Location header is set. An internal redirect has no status code and still ignores the second argument. add_listener raises TypeError (Integer cannot be converted to String, with the class of the value, or nil cannot be converted to String for a missing or nil :address). The exception reaches the code of mruby_server_context_handler_code or mruby_stream_server_context_code, and nginx stops reading the configuration: nginx -t fails and prints the exception after mrb_run failed.
  • Why it changed:
    • The defect: these methods read an argument without checking its type, and a value of another type gave a response without a valid status, no response, or the end of the nginx process. For a redirect to a URL, a heap-allocated status code such as a String put a heap address of the worker process into the status line, which weakens ASLR; the advisory linked above has the details, and the add_listener check in this entry is operator-facing hardening from the same review that a client cannot reach. The Request setters, Nginx::Async::HTTP.sub_request and Nginx::Headers_in/out#[]= got the same check in 2.7.7, and Nginx.redirect checks its first argument since 2.7.6 (#775).
    • Why the old behavior could not be kept: a value that is not an Integer has no status code, and a value that is not a String has no address, so reading them that way has no defined result. Converting them would accept types that these methods did not accept before, which the 2.x line does not do.
    • What the documentation promised: Nginx.redirect in 2.7.7 named the second argument http_status with an Integer constant as its default, and said nothing about other values. Nginx::Server#add_listener and Nginx::Stream.add_listener showed a String :address in every example, and said nothing about other values.
  • Affected: calls of Nginx.redirect to a URL with a second argument that is not an Integer, and calls of Nginx::Server#add_listener and Nginx::Stream.add_listener whose Hash has no :address or an :address that is not a String. An internal redirect, a status code that is an Integer and an :address that is a String behave as before.
  • What to do: pass an Integer status code to Nginx.redirect, for example Nginx::HTTP_MOVED_PERMANENTLY, or leave it out for 302, and pass the :address of add_listener as a String, for example port.to_s. Code that passed other values did not work on 2.7.7 either.

Nginx.redirect raises ArgumentError for a third argument (#813)

  • Before: Nginx.redirect takes a URL or an internal location and an optional status code, but a call with three arguments raised no error. The format of the arguments that it gave mruby allowed three, and it passed two variables to receive them, so the format and the number of variables did not match, and the result of a call with three arguments was not defined. A call with four or more arguments raised ArgumentError, as it does now.
  • Now: a call with three arguments raises ArgumentError (wrong number of arguments (given 3, expected 1..2)) before it does anything, for a URL and for an internal location. The exception ends the handler like any other exception of the script: a rescue in the script catches it, and otherwise ngx_mruby logs it and the request gets 500. No redirect is made.
  • Why it changed:
    • The defect: the format of the arguments of ngx_mrb_redirect named three arguments, and the function passed two variables to receive them (#813).
    • Why the old behavior could not be kept: the result of a call with three arguments was not defined, so it could not be kept as it was. The method has no third argument to give a meaning to, and mruby raises ArgumentError when a method gets more arguments than it takes.
    • What the documentation promised: the heading of Nginx.redirect in 2.7.7 names two arguments, and the documentation said nothing about a third.
  • Affected: calls of Nginx.redirect with three arguments.
  • What to do: pass one or two arguments to Nginx.redirect: the URL or the internal location, and for a URL the status code if it is not 302.

New features

None.

Fixes

  • Nginx.redirect to a URL checks that its status code is an Integer, and Nginx::Server#add_listener and Nginx::Stream.add_listener check that the :address is a String; a value of another type raises TypeError. Before, a status code of another type gave a response without a valid status or no response, and an :address of another type, or none, ended nginx while it read the configuration (#813)
  • Nginx.redirect takes one or two arguments, and a third raises ArgumentError. Before, the format of its arguments did not match the number of variables that received them, and the result of a call with three arguments was not defined (#813)

Build and test changes

  • New test case: test/t/cases/argument_types.rb, with the server of test/conf/conf.d/96-argument-types.conf on port 18200 (#813)

Upgrading

  • No directive was added, renamed or removed. Rebuild the module with this
    release the way you built 2.7.7, and replace the binary or the dynamic
    module as usual.

Check these before upgrading. Each line is the "What to do" of its entry
above.

  • Calls of Nginx.redirect to a URL with a status code that is not an
    Integer, and calls of add_listener without a String :address: pass an
    Integer status code, or leave it out for 302, and a String :address.
  • Calls of Nginx.redirect with three arguments: pass one or two.

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

Nginx.redirectはIntegerでないステータスコードに、add_listenerはStringでない:addressにTypeErrorを出します(#813)

アドバイザリ(URLへのNginx.redirectのステータスコードについて):GHSA-8j8w-mcp5-xp3h

  • 変更前:Nginx.redirectは、URLへのリダイレクトのステータスコードである第2引数を、型を確かめずにIntegerとして読んでいました。StringかFloatを渡すと、応答にはLocationヘッダが付き、ステータスラインにはステータスコードではない数が入り、理由のフレーズはありませんでした。nilを渡すと、クライアントには応答が返らず、接続が閉じられました。Nginx::Server#add_listenerとNginx::Stream.add_listenerは、Hashの:addressを、型を確かめずにStringとして読んでいました。:addressが無いか、nilか、IntegerのようにStringでない値だと、nginxは設定を読み込む途中でセグメンテーション違反で終わりました。nginx -tでも同じでした。
  • 変更後:URLへのリダイレクトで、ステータスコードがIntegerでないと、Nginx.redirectはTypeErrorを出します。メッセージは、値のクラスを入れたString cannot be converted to Integerか、nil cannot be converted to Integerです。スクリプトのほかの例外と同じに、この例外でもハンドラは終わります。スクリプトはrescueでこの例外を捕まえられます。捕まえなければ、ngx_mrubyは例外をログに書き、リクエストは500になります。リダイレクトはせず、Locationヘッダも付けません。内部リダイレクトにはステータスコードが無く、これまでどおり第2引数を使いません。add_listenerはTypeErrorを出します。メッセージは、値のクラスを入れたInteger cannot be converted to Stringか、:addressが無いかnilのときのnil cannot be converted to Stringです。この例外はmruby_server_context_handler_codeかmruby_stream_server_context_codeのコードに届き、nginxは設定の読み込みを止めます。nginx -tは失敗し、mrb_run failedの後に例外を表示します。
  • 変更の理由:
    • 不具合:これらのメソッドは、引数を型を確かめずに読んでいました。そのため、ほかの型の値を渡すと、有効なステータスの無い応答になるか、応答が返らないか、nginxのプロセスが終わりました。URLへのリダイレクトでは、Stringのようなヒープに置かれたステータスコードを渡すと、ワーカープロセスのヒープのアドレスがステータスラインに入り、ASLRが弱まりました。詳細は、上にリンクしたアドバイザリが示します。この項目のadd_listenerの確認は、同じ見直しによる運用者向けの堅牢化で、クライアントからは到達しません。Requestのセッター、Nginx::Async::HTTP.sub_request、Nginx::Headers_in/out#[]=は、2.7.7で同じ確認をするようになりました。Nginx.redirectは、2.7.6から第1引数を確かめています(#775)。
    • 前の挙動を残せなかった理由:Integerでない値にはステータスコードが無く、Stringでない値にはアドレスが無いので、そのように読んだ結果は決まりません。変換して受け付けると、これらのメソッドがこれまで受け付けていなかった型を受け付けることになります。2.x系では、そのような変更はしません。
    • ドキュメントが示していた挙動:2.7.7のNginx.redirectは、第2引数をhttp_statusと呼び、既定の値にIntegerの定数を示していました。ほかの値については、何も書いていませんでした。Nginx::Server#add_listenerとNginx::Stream.add_listenerは、どの例でもStringの:addressを示していました。ほかの値については、何も書いていませんでした。
  • 影響の範囲:Integerでない第2引数を渡す、URLへのNginx.redirectの呼び出しと、Hashに:addressが無いか、Stringでない:addressを渡す、Nginx::Server#add_listenerとNginx::Stream.add_listenerの呼び出しです。内部リダイレクト、Integerのステータスコード、Stringの:addressは、前と同じに動きます。
  • 必要な対応:Nginx.redirectには、Nginx::HTTP_MOVED_PERMANENTLYのようなIntegerのステータスコードを渡すか、302にするなら省いてください。add_listenerの:addressは、port.to_sのようにStringで渡してください。ほかの値を渡すコードは、2.7.7でも動いていませんでした。

Nginx.redirectは3つ目の引数にArgumentErrorを出します(#813)

  • 変更前:Nginx.redirectは、URLか内部のlocationと、省略できるステータスコードを取ります。しかし、引数を3つ渡してもエラーになりませんでした。このメソッドがmrubyに渡す引数の形式は3つの引数を許し、受け取る変数は2つしか渡していませんでした。形式と変数の数が合っておらず、引数を3つ渡したときの結果は定まっていませんでした。引数を4つ以上渡すと、今と同じくArgumentErrorが出ていました。
  • 変更後:引数を3つ渡すと、Nginx.redirectは何もする前にArgumentErrorを出します。メッセージはwrong number of arguments (given 3, expected 1..2)です。URLでも内部のlocationでも同じです。スクリプトのほかの例外と同じに、この例外でもハンドラは終わります。スクリプトはrescueでこの例外を捕まえられます。捕まえなければ、ngx_mrubyは例外をログに書き、リクエストは500になります。リダイレクトはしません。
  • 変更の理由:
    • 不具合:ngx_mrb_redirectの引数の形式は3つの引数を挙げていて、受け取る変数は2つしか渡していませんでした(#813)。
    • 前の挙動を残せなかった理由:引数を3つ渡したときの結果は定まっていなかったので、残すことはできませんでした。このメソッドには、意味を与えられる3つ目の引数がありません。また、メソッドが取るより多い引数を渡されたとき、mrubyはArgumentErrorを出します。
    • ドキュメントが示していた挙動:2.7.7のNginx.redirectの見出しは、2つの引数を挙げていました。3つ目の引数については、何も書いていませんでした。
  • 影響の範囲:引数を3つ渡すNginx.redirectの呼び出しです。
  • 必要な対応:Nginx.redirectには、引数を1つか2つ渡してください。URLか内部のlocationと、URLで302以外にするならステータスコードです。

新機能

ありません。

不具合の修正

  • URLへのNginx.redirectはステータスコードがIntegerであることを、Nginx::Server#add_listenerとNginx::Stream.add_listenerは:addressがStringであることを確かめるようになりました。ほかの型の値にはTypeErrorを出します。これまでは、ほかの型のステータスコードでは有効なステータスの無い応答になるか応答が返らず、ほかの型の:addressか:addressが無いと、設定を読み込む途中でnginxが終わりました(#813)。
  • Nginx.redirectは引数を1つか2つ取り、3つ目の引数にはArgumentErrorを出します。これまでは、引数の形式と受け取る変数の数が合っておらず、引数を3つ渡したときの結果は定まっていませんでした(#813)。

ビルドとテストの変更

  • 新しいテストのケースとして、test/t/cases/argument_types.rbと、ポート18200のtest/conf/conf.d/96-argument-types.confのサーバーを加えました(#813)。

アップグレード

  • ディレクティブの追加、名前の変更、削除はありません。2.7.7と同じ方法でこのリリースのモジュールをビルドし、バイナリか動的モジュールを置き換えてください。

アップグレードの前に、次の点を確かめてください。各行は、項目の「必要な対応」です。

  • URLへのNginx.redirectにIntegerでないステータスコードを渡す呼び出しと、Stringの:addressを渡さないadd_listenerの呼び出し:Integerのステータスコードを渡すか、302にするなら省いてください。:addressはStringで渡してください。
  • 引数を3つ渡すNginx.redirectの呼び出し:1つか2つにしてください。