Skip to content

ngx_mruby v2.7.7

The line of error.log for an exception whose class redefines inspect or backtrace (#803)

Section titled “The line of error.log for an exception whose class redefines inspect or backtrace (#803)”
  • Before: when Ruby code raised an exception whose class redefines inspect in Ruby, ngx_mruby wrote no line for the exception to error.log, whatever the redefined method did. ngx_mruby called the method while the exception was still pending in the mruby state. In that state the bundled mruby does not run the method to its end: it goes on with the pending exception, so neither the String that the method would return nor an exception of the method itself reached ngx_mruby. In a build with --with-debug, a backtrace redefined in Ruby went the same way, and ngx_mruby wrote backtrace must be Array. mrb_type=18 at the debug level and no frame. In the content handler of a location that Nginx.redirect ran from an mruby_set handler, this left the request unfinished, whatever the redefined inspect did, or in a build with --with-debug the redefined backtrace, also when it returned a String or an Array: the pending exception went on to the mruby_set handler, past the nginx functions in between. The mruby_set handler logged that exception again, which for a redefined inspect again wrote no line of the exception, and for a redefined backtrace wrote the line of the exception a second time and no frame. nginx then answered 404 from the static files of the target location instead of 500, and the connection stayed open until the client gave up.
  • Now: ngx_mruby calls inspect and backtrace with the pending exception set aside, and stops an exception that they raise in the logging. When inspect returns a String, the line has that String, as for an exception whose class does not redefine it. When inspect raises, the line has #<Class> (inspect raised Class), with the class of the exception and the class of the exception of inspect; when it returns another value, the line has #<Class> (inspect did not return a String). In a build with --with-debug, the String elements of the result of a redefined backtrace are logged, one line each, and its other elements are skipped; a backtrace that raises or does not return an Array is logged at the debug level as before. Inside another mruby call, the request ends with 500 as it does directly under nginx, and the worker goes on to the next request.
  • Why it changed:
    • The defect: inspect and backtrace were called while the exception was pending, so a method written in Ruby never gave its result. Inside another mruby call, the pending exception left the logging and the request unfinished, whatever the method did. The same calls are fixed on next by #796 and #799; this pull request brings the fix to 2.x.
    • Why the old behavior could not be kept: when an exception leaves the logging and jumps past the nginx functions that handle the request, the request and its connection stay open. When no line of the exception is written, the operator cannot learn from the log that the error happened. The class name in place of an inspect that cannot give a String is the line that 3.x writes for the same exception, so 2.x and 3.x write the same line.
    • What the documentation promised: the documentation said nothing about exceptions whose class redefines inspect or backtrace.
  • Affected: scripts of every kind of handler (HTTP and stream, and the code that runs when nginx reads the configuration or a worker starts or exits) that raise an exception whose class redefines inspect or backtrace in Ruby. An exception whose class does not redefine them gets the same line as before, byte for byte.
  • What to do: nothing, unless you parse error.log for these exceptions: the line now exists, with the text of the redefined inspect or the class name.

A non-String argument to a Request setter, Nginx::Async::HTTP.sub_request or Nginx::Headers_in/out#[]=

Section titled “A non-String argument to a Request setter, Nginx::Async::HTTP.sub_request or Nginx::Headers_in/out#[]=”

Security advisory: GHSA-cjxv-4q9m-62pf.

  • Before: Nginx::Request#uri=, #args=, #method=, #protocol=, #request_line=, #unparsed_uri= and #content_type= (which share one macro), Nginx::Async::HTTP.sub_request (the request path), and Nginx::Headers_in#[]= and Nginx::Headers_out#[]= (the header name, and each value) did not check that the argument was a String, and read a non-String argument as if it were a String.
  • Now: these methods raise TypeError for a non-String argument, as Nginx.rputs, Nginx.echo, Nginx.redirect, Nginx::Filter#body= and the Nginx::Var setter already do (#775). A nil argument keeps its meaning: a setter leaves the member unchanged and returns, and Headers#[]= removes the header.
  • Why it changed:
    • The defect: these methods read their argument as a String without checking its type. The advisory linked above has the details.
    • Why the old behavior could not be kept: a value that is not a String has no string content, so reading it as a String has no defined result.
    • What the documentation promised: the documentation of these methods describes a String argument.
  • Affected: scripts that pass to one of these methods a value that may not be a String. A script that passes only a String (a literal, an interpolated string, or a value it has converted) is not affected.
  • What to do: where a script may pass a non-String to one of these methods, convert it (for example with .to_s) or handle the TypeError.

None.

  • When the class of an exception redefines inspect (or, in a build with --with-debug, backtrace) in Ruby and the exception is raised in the content handler of a location that Nginx.redirect runs from an mruby_set handler, nginx now finishes the request with 500, whatever the method does. Before, the exception went on to the mruby_set handler, nginx answered 404, and the connection stayed open until the client gave up (#803)
  • In a build with --with-debug, an element of the result of a redefined backtrace that is not a String is skipped, as on 3.x. Before, the code read every element as a String without checking its type (#803)
  • Nginx::Upstream#server= and the stream Nginx::Stream::Connection#upstream_server= reset their per-peer store when the nginx cycle pool is freed, so a later cycle after a reload in master_process off does not reuse the store of an earlier cycle (GHSA-pmjr-f94r-j47g)
  • New test case: test/t/cases/exception_log_script_methods.rb, with the server of test/conf/conf.d/94-exception-log-script-methods.conf on port 18091 and the stream servers of test/conf/conf.d/stream/52-exception-log-script-methods.conf on ports 12383 and 12389 (#803)
  • New test case: test/t/cases/type_unchecked_reads.rb, with the server of test/conf/conf.d/95-type-unchecked-reads.conf on port 18192
  • No directive was added, renamed or removed. Rebuild the module with this release the way you built 2.7.6, 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.

  • Tools that parse error.log for exceptions whose class redefines inspect or backtrace: the line of such an exception now exists, with the text of the redefined inspect or the class name.
  • Scripts that pass a value that may not be a String to a Request setter, Nginx::Async::HTTP.sub_request or Nginx::Headers_in/out#[]=: convert it (for example with .to_s) or handle the TypeError that these methods now raise for a non-String.