summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2020-01-16 18:11:29 -0800
committerJeremy Evans <code@jeremyevans.net>2020-01-16 18:16:51 -0800
commitba2b9b4cce6c7a4a8e64b128bb7b5af95e01ba4e (patch)
treec4c391f860978116a7075077e37e9ee04c2cd27f
parent167a94b12b407d432199ca15bf0703115ce28aca (diff)
downloadrack-ba2b9b4cce6c7a4a8e64b128bb7b5af95e01ba4e.tar.gz
Make Rack::ShowExceptions handle invalid POST data
Otherwise you get an exception trying to render the exception page. Fixes #991
-rw-r--r--lib/rack/show_exceptions.rb4
-rw-r--r--test/spec_show_exceptions.rb21
2 files changed, 23 insertions, 2 deletions
diff --git a/lib/rack/show_exceptions.rb b/lib/rack/show_exceptions.rb
index 843af607..8ca96ef0 100644
--- a/lib/rack/show_exceptions.rb
+++ b/lib/rack/show_exceptions.rb
@@ -313,7 +313,7 @@ module Rack
<% end %>
<h3 id="post-info">POST</h3>
- <% if req.POST and not req.POST.empty? %>
+ <% if ((req.POST and not req.POST.empty?) rescue (no_post_data = "Invalid POST data"; nil)) %>
<table class="req">
<thead>
<tr>
@@ -331,7 +331,7 @@ module Rack
</tbody>
</table>
<% else %>
- <p>No POST data.</p>
+ <p><%= no_post_data || "No POST data" %>.</p>
<% end %>
diff --git a/test/spec_show_exceptions.rb b/test/spec_show_exceptions.rb
index a4ade121..73a0536f 100644
--- a/test/spec_show_exceptions.rb
+++ b/test/spec_show_exceptions.rb
@@ -25,6 +25,27 @@ describe Rack::ShowExceptions do
assert_match(res, /RuntimeError/)
assert_match(res, /ShowExceptions/)
+ assert_match(res, /No GET data/)
+ assert_match(res, /No POST data/)
+ end
+
+ it "handles invalid POST data exceptions" do
+ res = nil
+
+ req = Rack::MockRequest.new(
+ show_exceptions(
+ lambda{|env| raise RuntimeError }
+ ))
+
+ res = req.post("/", "HTTP_ACCEPT" => "text/html", "rack.input" => StringIO.new(String.new << '(%bad-params%)'))
+
+ res.must_be :server_error?
+ res.status.must_equal 500
+
+ assert_match(res, /RuntimeError/)
+ assert_match(res, /ShowExceptions/)
+ assert_match(res, /No GET data/)
+ assert_match(res, /Invalid POST data/)
end
it "works with binary data in the Rack environment" do