Skip to content
These are 2.x maintenance-branch docs. Latest release tag: v2.7.12. Check the version targeted by each example. For development, read the 3.x documentation.

ngx_mruby v2.7.8

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)

Section titled “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)

Section titled “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.

None.

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