From 586fc991ac0d55c4b17e97528f79e76ab96a4b2b Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Tue, 25 Jan 2022 14:24:07 -0800 Subject: [PATCH] Cache Rack::Request#POST result if input content type is not parseable (Fixes #749) In all other cases, the result was cached, so not caching in this case is inconsistent, and can result in unexpected behavior if POST is called multiple times on the same request. --- CHANGELOG.md | 1 + lib/rack/request.rb | 3 ++- test/spec_request.rb | 4 +++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57cc42cf4..3ed7442d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ All notable changes to this project will be documented in this file. For info on - Decrease default allowed parameter recursion level from 100 to 32. ([#1640](https://github.com/rack/rack/issues/1640), [@jeremyevans](https://github.com/jeremyevans)) - Attempting to parse a multipart response with an empty body now raises Rack::Multipart::EmptyContentError. ([#1603](https://github.com/rack/rack/issues/1603), [@jeremyevans](https://github.com/jeremyevans)) - `Rack::Utils.secure_compare` uses OpenSSL's faster implementation if available. ([#1711](https://github.com/rack/rack/pull/1711), [@bdewater](https://github.com/bdewater)) +- `Rack::Request#POST` now caches an empty hash if input content type is not parseable. ([#749](https://github.com/rack/rack/pull/749), [@jeremyevans](https://github.com/jeremyevans)) - BREAKING CHANGE: Updated `trusted_proxy?` to match full 127.0.0.0/8 network. ([#1781](https://github.com/rack/rack/pull/1781), [@snbloch](https://github.com/snbloch)) ### Fixed diff --git a/lib/rack/request.rb b/lib/rack/request.rb index 722a06b8d..2df93ebd9 100644 --- a/lib/rack/request.rb +++ b/lib/rack/request.rb @@ -471,7 +471,8 @@ def POST set_header RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT) get_header RACK_REQUEST_FORM_HASH else - {} + set_header RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT) + set_header(RACK_REQUEST_FORM_HASH, {}) end end diff --git a/test/spec_request.rb b/test/spec_request.rb index c2c71dc4b..7371823a3 100644 --- a/test/spec_request.rb +++ b/test/spec_request.rb @@ -434,7 +434,9 @@ def initialize(*) req.media_type.must_equal 'text/plain' req.media_type_params['charset'].must_equal 'utf-8' req.content_charset.must_equal 'utf-8' - req.POST.must_be :empty? + post = req.POST + post.must_be_empty + req.POST.must_be_same_as post req.params.must_equal "foo" => "quux" req.body.read.must_equal "foo=bar&quux=bla" end