From d14e2e5a217c5dfbc0de1796f2821ea1efd07fe4 Mon Sep 17 00:00:00 2001 From: Szymon Nowak Date: Fri, 24 Aug 2012 18:08:06 +0200 Subject: [PATCH 1/3] Raise generic ParseError exception when ActionDispatch::ParamsParser fails parsing request params. --- actionpack/lib/action_dispatch/middleware/params_parser.rb | 7 +++++-- .../test/dispatch/request/json_params_parsing_test.rb | 2 +- .../test/dispatch/request/xml_params_parsing_test.rb | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/params_parser.rb b/actionpack/lib/action_dispatch/middleware/params_parser.rb index 1cb803ffb9..7f38c6d4f3 100644 --- a/actionpack/lib/action_dispatch/middleware/params_parser.rb +++ b/actionpack/lib/action_dispatch/middleware/params_parser.rb @@ -4,6 +4,8 @@ require 'active_support/core_ext/hash/indifferent_access' module ActionDispatch class ParamsParser + class ParseError < StandardError; end + DEFAULT_PARSERS = { Mime::XML => :xml_simple, Mime::JSON => :json @@ -52,9 +54,10 @@ module ActionDispatch false end rescue Exception => e # YAML, XML or Ruby code block errors - logger(env).debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}" + message = "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}" + logger(env).debug message - raise e + raise ParseError, message end def content_type_from_legacy_post_data_format_header(env) diff --git a/actionpack/test/dispatch/request/json_params_parsing_test.rb b/actionpack/test/dispatch/request/json_params_parsing_test.rb index 302bff0696..f7f621df71 100644 --- a/actionpack/test/dispatch/request/json_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/json_params_parsing_test.rb @@ -46,7 +46,7 @@ class JsonParamsParsingTest < ActionDispatch::IntegrationTest begin $stderr = StringIO.new # suppress the log json = "[\"person]\": {\"name\": \"David\"}}" - assert_raise(MultiJson::DecodeError) { post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => false} } + assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => false} } ensure $stderr = STDERR end diff --git a/actionpack/test/dispatch/request/xml_params_parsing_test.rb b/actionpack/test/dispatch/request/xml_params_parsing_test.rb index 84823e2896..0fcae1e650 100644 --- a/actionpack/test/dispatch/request/xml_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/xml_params_parsing_test.rb @@ -68,7 +68,7 @@ class XmlParamsParsingTest < ActionDispatch::IntegrationTest begin $stderr = StringIO.new # suppress the log xml = "David" - assert_raise(REXML::ParseException) { post "/parse", xml, default_headers.merge('action_dispatch.show_exceptions' => false) } + assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", xml, default_headers.merge('action_dispatch.show_exceptions' => false) } ensure $stderr = STDERR end From b6ba012032c0c53e4539228b06f3da6d6a5a69bc Mon Sep 17 00:00:00 2001 From: Szymon Nowak Date: Fri, 24 Aug 2012 21:55:41 +0200 Subject: [PATCH 2/3] Fix ActionDispatch::ParamsParser::ParseError message for XML and JSON parsers. --- actionpack/lib/action_dispatch/middleware/params_parser.rb | 6 ++---- .../test/dispatch/request/json_params_parsing_test.rb | 3 ++- actionpack/test/dispatch/request/xml_params_parsing_test.rb | 3 ++- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/params_parser.rb b/actionpack/lib/action_dispatch/middleware/params_parser.rb index 7f38c6d4f3..46e33484a4 100644 --- a/actionpack/lib/action_dispatch/middleware/params_parser.rb +++ b/actionpack/lib/action_dispatch/middleware/params_parser.rb @@ -40,14 +40,12 @@ module ActionDispatch when Proc strategy.call(request.raw_post) when :xml_simple, :xml_node - data = Hash.from_xml(request.body.read) || {} - request.body.rewind if request.body.respond_to?(:rewind) + data = Hash.from_xml(request.raw_post) || {} data.with_indifferent_access when :yaml YAML.load(request.raw_post) when :json - data = ActiveSupport::JSON.decode(request.body) - request.body.rewind if request.body.respond_to?(:rewind) + data = ActiveSupport::JSON.decode(request.raw_post) data = {:_json => data} unless data.is_a?(Hash) data.with_indifferent_access else diff --git a/actionpack/test/dispatch/request/json_params_parsing_test.rb b/actionpack/test/dispatch/request/json_params_parsing_test.rb index f7f621df71..2022586912 100644 --- a/actionpack/test/dispatch/request/json_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/json_params_parsing_test.rb @@ -46,7 +46,8 @@ class JsonParamsParsingTest < ActionDispatch::IntegrationTest begin $stderr = StringIO.new # suppress the log json = "[\"person]\": {\"name\": \"David\"}}" - assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => false} } + exception = assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => false} } + assert_match json, exception.message ensure $stderr = STDERR end diff --git a/actionpack/test/dispatch/request/xml_params_parsing_test.rb b/actionpack/test/dispatch/request/xml_params_parsing_test.rb index 0fcae1e650..1ef2195810 100644 --- a/actionpack/test/dispatch/request/xml_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/xml_params_parsing_test.rb @@ -68,7 +68,8 @@ class XmlParamsParsingTest < ActionDispatch::IntegrationTest begin $stderr = StringIO.new # suppress the log xml = "David" - assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", xml, default_headers.merge('action_dispatch.show_exceptions' => false) } + exception = assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", xml, default_headers.merge('action_dispatch.show_exceptions' => false) } + assert_match xml, exception.message ensure $stderr = STDERR end From fd99bb892652b1ffffdd7f9271184235d6040111 Mon Sep 17 00:00:00 2001 From: Szymon Nowak Date: Mon, 27 Aug 2012 23:46:53 +0200 Subject: [PATCH 3/3] Make ActionDispatch::ParamsParser::ParseError#original_exception return the original exception. --- .../action_dispatch/middleware/params_parser.rb | 14 ++++++++++---- .../dispatch/request/json_params_parsing_test.rb | 3 ++- .../dispatch/request/xml_params_parsing_test.rb | 3 ++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/params_parser.rb b/actionpack/lib/action_dispatch/middleware/params_parser.rb index 46e33484a4..2c98ca03a8 100644 --- a/actionpack/lib/action_dispatch/middleware/params_parser.rb +++ b/actionpack/lib/action_dispatch/middleware/params_parser.rb @@ -4,7 +4,14 @@ require 'active_support/core_ext/hash/indifferent_access' module ActionDispatch class ParamsParser - class ParseError < StandardError; end + class ParseError < StandardError + attr_reader :original_exception + + def initialize(message, original_exception) + super(message) + @original_exception = original_exception + end + end DEFAULT_PARSERS = { Mime::XML => :xml_simple, @@ -52,10 +59,9 @@ module ActionDispatch false end rescue Exception => e # YAML, XML or Ruby code block errors - message = "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}" - logger(env).debug message + logger(env).debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}" - raise ParseError, message + raise ParseError.new(e.message, e) end def content_type_from_legacy_post_data_format_header(env) diff --git a/actionpack/test/dispatch/request/json_params_parsing_test.rb b/actionpack/test/dispatch/request/json_params_parsing_test.rb index 2022586912..c0c3147e37 100644 --- a/actionpack/test/dispatch/request/json_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/json_params_parsing_test.rb @@ -47,7 +47,8 @@ class JsonParamsParsingTest < ActionDispatch::IntegrationTest $stderr = StringIO.new # suppress the log json = "[\"person]\": {\"name\": \"David\"}}" exception = assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => false} } - assert_match json, exception.message + assert_equal MultiJson::DecodeError, exception.original_exception.class + assert_equal exception.original_exception.message, exception.message ensure $stderr = STDERR end diff --git a/actionpack/test/dispatch/request/xml_params_parsing_test.rb b/actionpack/test/dispatch/request/xml_params_parsing_test.rb index 1ef2195810..cb68667002 100644 --- a/actionpack/test/dispatch/request/xml_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/xml_params_parsing_test.rb @@ -69,7 +69,8 @@ class XmlParamsParsingTest < ActionDispatch::IntegrationTest $stderr = StringIO.new # suppress the log xml = "David" exception = assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", xml, default_headers.merge('action_dispatch.show_exceptions' => false) } - assert_match xml, exception.message + assert_equal REXML::ParseException, exception.original_exception.class + assert_equal exception.original_exception.message, exception.message ensure $stderr = STDERR end