diff --git a/CHANGELOG.md b/CHANGELOG.md index cd3a0aa9..fa9251b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - [GHSA-mr52-m5rh-frmq](https://github.com/Shopify/pitchfork/security/advisories/GHSA-mr52-m5rh-frmq) Fix CRLF injection in response trailers. - Fix response headers with non-ASCII bytes raising `ArgumentError` (invalid UTF-8) or `Encoding::CompatibilityError` (values in different encodings). Header values are now checked and written as bytes. - [GHSA-p9r3-wjcg-rwv5](https://github.com/Shopify/pitchfork/security/advisories/GHSA-p9r3-wjcg-rwv5) Fix chunked body parse errors that could disclose unread data. +- Avoid stale body source pointers when Ruby compacts strings. - `RACK_ENV` now default to `"production"` instead of `"development"`. - Fix workers surviving a soft timeout when the application swallows the `SystemExit` (e.g. Rails' `ActionDispatch::ShowExceptions`). The worker now exits once the response is written, and `after_request_complete` isn't called for that request. diff --git a/ext/pitchfork_http/pitchfork_http.c b/ext/pitchfork_http/pitchfork_http.c index dc3e0cdc..dd60e0af 100644 --- a/ext/pitchfork_http/pitchfork_http.c +++ b/ext/pitchfork_http/pitchfork_http.c @@ -4098,13 +4098,10 @@ static VALUE HttpParser_hijacked_bang(VALUE self) static VALUE HttpParser_filter_body(VALUE self, VALUE dst, VALUE src) { struct http_parser *hp = data_get(self); - char *srcptr; long srclen; - srcptr = RSTRING_PTR(src); - srclen = RSTRING_LEN(src); - StringValue(dst); + srclen = RSTRING_LEN(src); if (HP_FL_TEST(hp, CHUNKED)) { if (!chunked_eof(hp)) { @@ -4115,7 +4112,7 @@ static VALUE HttpParser_filter_body(VALUE self, VALUE dst, VALUE src) hp->s.dest_offset = 0; RB_OBJ_WRITE(self, &hp->cont, dst); RB_OBJ_WRITE(self, &hp->buf, src); - http_parser_execute(self, hp, srcptr, srclen); + http_parser_execute(self, hp, RSTRING_PTR(src), srclen); if (hp->cs == http_parser_error) parser_raise(eHttpParserError, "Invalid HTTP format, parsing fails."); @@ -4150,7 +4147,7 @@ static VALUE HttpParser_filter_body(VALUE self, VALUE dst, VALUE src) * when the +src+ buffer is modified (when reading off the socket). */ RB_OBJ_WRITE(self, &hp->buf, src); - memcpy(RSTRING_PTR(dst), srcptr, nr); + memcpy(RSTRING_PTR(dst), RSTRING_PTR(src), nr); hp->len.content -= nr; if (hp->len.content == 0) { HP_FL_SET(hp, REQEOF); diff --git a/ext/pitchfork_http/pitchfork_http.rl b/ext/pitchfork_http/pitchfork_http.rl index 5fcc6c7a..07211cf4 100644 --- a/ext/pitchfork_http/pitchfork_http.rl +++ b/ext/pitchfork_http/pitchfork_http.rl @@ -911,13 +911,10 @@ static VALUE HttpParser_hijacked_bang(VALUE self) static VALUE HttpParser_filter_body(VALUE self, VALUE dst, VALUE src) { struct http_parser *hp = data_get(self); - char *srcptr; long srclen; - srcptr = RSTRING_PTR(src); - srclen = RSTRING_LEN(src); - StringValue(dst); + srclen = RSTRING_LEN(src); if (HP_FL_TEST(hp, CHUNKED)) { if (!chunked_eof(hp)) { @@ -928,7 +925,7 @@ static VALUE HttpParser_filter_body(VALUE self, VALUE dst, VALUE src) hp->s.dest_offset = 0; RB_OBJ_WRITE(self, &hp->cont, dst); RB_OBJ_WRITE(self, &hp->buf, src); - http_parser_execute(self, hp, srcptr, srclen); + http_parser_execute(self, hp, RSTRING_PTR(src), srclen); if (hp->cs == http_parser_error) parser_raise(eHttpParserError, "Invalid HTTP format, parsing fails."); @@ -963,7 +960,7 @@ static VALUE HttpParser_filter_body(VALUE self, VALUE dst, VALUE src) * when the +src+ buffer is modified (when reading off the socket). */ RB_OBJ_WRITE(self, &hp->buf, src); - memcpy(RSTRING_PTR(dst), srcptr, nr); + memcpy(RSTRING_PTR(dst), RSTRING_PTR(src), nr); hp->len.content -= nr; if (hp->len.content == 0) { HP_FL_SET(hp, REQEOF); diff --git a/test/unit/test_http_parser_ng.rb b/test/unit/test_http_parser_ng.rb index 3a94b198..88125bef 100644 --- a/test/unit/test_http_parser_ng.rb +++ b/test/unit/test_http_parser_ng.rb @@ -471,6 +471,29 @@ def test_bad_chunk_clears_destination end end + def test_filter_body_reloads_source_after_destination_coercion + length = 2048 + expected = "B" * length + [ + ["Transfer-Encoding: chunked", "1\r\nA\r\n", "800\r\n#{expected}\r\n"], + ["Content-Length: #{length}", "A", expected], + ].each do |header, original, replacement| + parser = HttpParser.new + parser.buf << "POST / HTTP/1.1\r\n#{header}\r\n\r\n" + parser.parse + src = original.dup + dst = +"" + coercible = Object.new + coercible.define_singleton_method(:to_str) do + src.replace(replacement) + dst + end + + parser.filter_body(coercible, src) + assert_equal expected, dst + end + end + # The decoded length must be set before anything can allocate: GC # compaction re-embeds Strings and only copies their logical length. # Runs in a subprocess so GC.stress and GC.auto_compact can't leak.