Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
9 changes: 3 additions & 6 deletions ext/pitchfork_http/pitchfork_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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.");

Expand Down Expand Up @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions ext/pitchfork_http/pitchfork_http.rl
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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.");

Expand Down Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions test/unit/test_http_parser_ng.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading