ngx_mruby v2.7.8
Behavior changes: read before upgrading
Section titled “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)
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.redirectread 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 theLocationheader and a status line with a number that is not a status code and no reason phrase. Withnil, the client got no response, and the connection was closed.Nginx::Server#add_listenerandNginx::Stream.add_listenerread the:addressof their Hash as a String without checking its type. With a missing:address,nilor a value that is not a String, such as an Integer, nginx ended with a segmentation fault while it read the configuration, also withnginx -t. - Now: for a redirect to a URL,
Nginx.redirectraisesTypeError(String cannot be converted to Integer, with the class of the value, ornil 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: arescuein the script catches it, and otherwise ngx_mruby logs it and the request gets 500. No redirect is made and noLocationheader is set. An internal redirect has no status code and still ignores the second argument.add_listenerraisesTypeError(Integer cannot be converted to String, with the class of the value, ornil cannot be converted to Stringfor a missing ornil:address). The exception reaches the code ofmruby_server_context_handler_codeormruby_stream_server_context_code, and nginx stops reading the configuration:nginx -tfails and prints the exception aftermrb_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_listenercheck in this entry is operator-facing hardening from the same review that a client cannot reach. The Request setters,Nginx::Async::HTTP.sub_requestandNginx::Headers_in/out#[]=got the same check in 2.7.7, andNginx.redirectchecks 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_statuswith an Integer constant as its default, and said nothing about other values. Nginx::Server and Nginx::#add_ listener Stream showed a String.add_ listener :addressin every example, and said nothing about other values.
- 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
- Affected: calls of
Nginx.redirectto a URL with a second argument that is not an Integer, and calls ofNginx::Server#add_listenerandNginx::Stream.add_listenerwhose Hash has no:addressor an:addressthat is not a String. An internal redirect, a status code that is an Integer and an:addressthat is a String behave as before. - What to do: pass an Integer status code to
Nginx.redirect, for exampleNginx::HTTP_MOVED_PERMANENTLY, or leave it out for 302, and pass the:addressofadd_listeneras a String, for exampleport.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.redirecttakes 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 raisedArgumentError, 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: arescuein 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_redirectnamed 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
ArgumentErrorwhen 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.
- The defect: the format of the arguments of
- Affected: calls of
Nginx.redirectwith 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
Section titled “New features”None.
Nginx.redirectto a URL checks that its status code is an Integer, andNginx::Server#add_listenerandNginx::Stream.add_listenercheck that the:addressis a String; a value of another type raisesTypeError. Before, a status code of another type gave a response without a valid status or no response, and an:addressof another type, or none, ended nginx while it read the configuration (#813)Nginx.redirecttakes one or two arguments, and a third raisesArgumentError. 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
Section titled “Build and test changes”- New test case:
test/t/cases/argument_types.rb, with the server oftest/conf/conf.d/96-argument-types.confon port 18200 (#813)
Upgrading
Section titled “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.redirectto a URL with a status code that is not an Integer, and calls ofadd_listenerwithout a String:address: pass an Integer status code, or leave it out for 302, and a String:address. - Calls of
Nginx.redirectwith three arguments: pass one or two.