2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-21 09:18:17 -04:00
|
|
|
require "action_dispatch/http/request"
|
|
|
|
require "action_dispatch/middleware/exception_wrapper"
|
2009-06-08 16:33:18 -04:00
|
|
|
|
2009-05-03 00:02:22 -04:00
|
|
|
module ActionDispatch
|
2011-12-01 15:15:42 -05:00
|
|
|
# This middleware rescues any exception returned by the application
|
2011-12-16 03:59:36 -05:00
|
|
|
# and calls an exceptions app that will wrap it in a format for the end user.
|
2011-12-16 03:45:14 -05:00
|
|
|
#
|
2011-12-16 03:59:36 -05:00
|
|
|
# The exceptions app should be passed as parameter on initialization
|
2012-09-28 16:32:27 -04:00
|
|
|
# of ShowExceptions. Every time there is an exception, ShowExceptions will
|
2011-12-16 03:45:14 -05:00
|
|
|
# store the exception in env["action_dispatch.exception"], rewrite the
|
2017-03-12 12:51:26 -04:00
|
|
|
# PATH_INFO to the exception status code and call the Rack app.
|
2012-03-16 22:23:00 -04:00
|
|
|
#
|
2011-12-16 03:59:36 -05:00
|
|
|
# If the application returns a "X-Cascade" pass response, this middleware
|
|
|
|
# will send an empty response as result with the correct status code.
|
|
|
|
# If any exception happens inside the exceptions app, this middleware
|
|
|
|
# catches the exceptions and returns a FAILSAFE_RESPONSE.
|
2009-05-03 00:02:22 -04:00
|
|
|
class ShowExceptions
|
2016-08-06 12:51:43 -04:00
|
|
|
FAILSAFE_RESPONSE = [500, { "Content-Type" => "text/plain" },
|
2013-01-06 17:36:11 -05:00
|
|
|
["500 Internal Server Error\n" \
|
|
|
|
"If you are the administrator of this website, then please read this web " \
|
|
|
|
"application's log file and/or the web server's log file to find out what " \
|
2012-07-06 01:39:15 -04:00
|
|
|
"went wrong."]]
|
2009-05-03 00:02:22 -04:00
|
|
|
|
2011-12-20 09:12:38 -05:00
|
|
|
def initialize(app, exceptions_app)
|
2009-05-03 00:02:22 -04:00
|
|
|
@app = app
|
2011-12-16 03:29:37 -05:00
|
|
|
@exceptions_app = exceptions_app
|
2009-05-03 00:02:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2015-08-06 19:12:06 -04:00
|
|
|
request = ActionDispatch::Request.new env
|
2013-01-07 07:58:55 -05:00
|
|
|
@app.call(env)
|
|
|
|
rescue Exception => exception
|
2015-08-06 19:12:06 -04:00
|
|
|
if request.show_exceptions?
|
2015-08-23 20:25:13 -04:00
|
|
|
render_exception(request, exception)
|
2015-08-06 19:12:06 -04:00
|
|
|
else
|
|
|
|
raise exception
|
2013-10-26 13:55:22 -04:00
|
|
|
end
|
2009-05-17 13:24:42 -04:00
|
|
|
end
|
2009-05-11 20:07:05 -04:00
|
|
|
|
2009-05-17 13:24:42 -04:00
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def render_exception(request, exception)
|
|
|
|
backtrace_cleaner = request.get_header "action_dispatch.backtrace_cleaner"
|
|
|
|
wrapper = ExceptionWrapper.new(backtrace_cleaner, exception)
|
|
|
|
status = wrapper.status_code
|
2019-01-24 18:48:39 -05:00
|
|
|
request.set_header "action_dispatch.exception", wrapper.unwrapped_exception
|
2016-08-06 13:55:02 -04:00
|
|
|
request.set_header "action_dispatch.original_path", request.path_info
|
change request method to a `GET` when passing failed requests to `config.exceptions_app`
Similar to #38998 (fixed in #40246), HTTP method validation occurring whenever methods are called on `ActionDispatch::Request` can cause some weird unintended consequences. For example, if `config.exceptions_app = self.routes`, you get an exception raised via the `ActionDispatch::ShowExceptions` middleware failsafe:
```
Started TEST "/" for 127.0.0.1 at 2020-11-05 15:40:31 -0500
(1.0ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
TEST, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH excluded from capture: DSN not set
ActionController::UnknownHttpMethod (TEST, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH):
actionpack (6.0.3.4) lib/action_dispatch/http/request.rb:431:in `check_method'
actionpack (6.0.3.4) lib/action_dispatch/http/request.rb:143:in `request_method'
rack (2.2.3) lib/rack/request.rb:187:in `head?'
actionpack (6.0.3.4) lib/action_dispatch/journey/router.rb:113:in `find_routes'
actionpack (6.0.3.4) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.0.3.4) lib/action_dispatch/routing/route_set.rb:834:in `call'
Error during failsafe response: TEST, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/http/request.rb:431:in `check_method'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/http/request.rb:143:in `request_method'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rack-2.2.3/lib/rack/request.rb:187:in `head?'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/journey/router.rb:113:in `find_routes'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/journey/router.rb:32:in `serve'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/routing/route_set.rb:834:in `call'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/middleware/show_exceptions.rb:50:in `render_exception'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/middleware/show_exceptions.rb:36:in `rescue in call'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
# ...
```
Now, to prevent the redundant exception, we overwrite `request_method` before passing `env` down to `config.exceptions_app`. `action_dispatch.original_request_method` is set to keep the original request method available for inspection.
2020-12-16 20:48:09 -05:00
|
|
|
request.set_header "action_dispatch.original_request_method", request.raw_request_method
|
2016-08-06 13:55:02 -04:00
|
|
|
request.path_info = "/#{status}"
|
change request method to a `GET` when passing failed requests to `config.exceptions_app`
Similar to #38998 (fixed in #40246), HTTP method validation occurring whenever methods are called on `ActionDispatch::Request` can cause some weird unintended consequences. For example, if `config.exceptions_app = self.routes`, you get an exception raised via the `ActionDispatch::ShowExceptions` middleware failsafe:
```
Started TEST "/" for 127.0.0.1 at 2020-11-05 15:40:31 -0500
(1.0ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
TEST, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH excluded from capture: DSN not set
ActionController::UnknownHttpMethod (TEST, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH):
actionpack (6.0.3.4) lib/action_dispatch/http/request.rb:431:in `check_method'
actionpack (6.0.3.4) lib/action_dispatch/http/request.rb:143:in `request_method'
rack (2.2.3) lib/rack/request.rb:187:in `head?'
actionpack (6.0.3.4) lib/action_dispatch/journey/router.rb:113:in `find_routes'
actionpack (6.0.3.4) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.0.3.4) lib/action_dispatch/routing/route_set.rb:834:in `call'
Error during failsafe response: TEST, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/http/request.rb:431:in `check_method'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/http/request.rb:143:in `request_method'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rack-2.2.3/lib/rack/request.rb:187:in `head?'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/journey/router.rb:113:in `find_routes'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/journey/router.rb:32:in `serve'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/routing/route_set.rb:834:in `call'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/middleware/show_exceptions.rb:50:in `render_exception'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/middleware/show_exceptions.rb:36:in `rescue in call'
/usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
# ...
```
Now, to prevent the redundant exception, we overwrite `request_method` before passing `env` down to `config.exceptions_app`. `action_dispatch.original_request_method` is set to keep the original request method available for inspection.
2020-12-16 20:48:09 -05:00
|
|
|
request.request_method = "GET"
|
2016-08-06 13:55:02 -04:00
|
|
|
response = @exceptions_app.call(request.env)
|
|
|
|
response[1]["X-Cascade"] == "pass" ? pass_response(status) : response
|
|
|
|
rescue Exception => failsafe_error
|
|
|
|
$stderr.puts "Error during failsafe response: #{failsafe_error}\n #{failsafe_error.backtrace * "\n "}"
|
|
|
|
FAILSAFE_RESPONSE
|
|
|
|
end
|
2011-12-16 03:59:36 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def pass_response(status)
|
2016-08-16 03:30:11 -04:00
|
|
|
[status, { "Content-Type" => "text/html; charset=#{Response.default_charset}", "Content-Length" => "0" }, []]
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2009-05-03 00:02:22 -04:00
|
|
|
end
|
|
|
|
end
|