request-rescue 0.1.7 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +6 -4
- data/lib/request_rescue/controller.rb +20 -1
- data/lib/request_rescue/engine.rb +3 -1
- data/lib/request_rescue/error_response.rb +12 -0
- data/lib/request_rescue/middleware.rb +2 -1
- data/lib/request_rescue/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 59dc2a5dbe71b9afb1ce2d61b9a4651a78ec016eea63a777c5186352f03761e1
|
|
4
|
+
data.tar.gz: 4e0f5c640001225a151490a91d0b38eb1596fe55243e6bacd4799c12de66fd66
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 850b212990488b3f3cb5778811760d0d6c96fade7181f76ce61f3f5a0c0b82ad05c113191258faf917c394e617a130dc3cd68288809b775a04b40a5dc8ee6181
|
|
7
|
+
data.tar.gz: e515cac2e291c561bb0340488bdcaf71988dab6577d5d39a9b66aecf186ba5e5775156174a1d9dd6f1915608d699730c695a280bef79e595e60d2e7027127194
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- Install the middleware after `ActionDispatch::RemoteIp`, as feedmob-rescue did, so malformed requests are rejected before the Rails request log and application middleware. Check the query before the HTTP method.
|
|
6
|
+
- Raise a template that is missing in every format when migration mode is off, so the application reports a 500. A template that exists only in other formats still returns 404.
|
|
7
|
+
- Use route format defaults and the `format` parameter to choose JSON error responses.
|
|
8
|
+
|
|
9
|
+
## 0.1.4
|
|
10
|
+
|
|
5
11
|
- Restore HTTP 400 in migration mode for query values with invalid UTF-8, including requests to mounted error pages.
|
|
6
12
|
|
|
7
13
|
## 0.1.1
|
data/README.md
CHANGED
|
@@ -35,12 +35,15 @@ config.exceptions_app = RequestRescue::ExceptionsApp
|
|
|
35
35
|
| --- | --- | --- |
|
|
36
36
|
| Conflicting nested query parameter types | 400 | 400 |
|
|
37
37
|
| HTTP method unrecognized by Rails | 405 JSON | 405 JSON with `{"errors":["Method not allowed"]}` |
|
|
38
|
-
| Unsupported controller format
|
|
38
|
+
| Unsupported controller format, including a template that exists only in other formats | 404 | 302 to the mounted `not_found` page |
|
|
39
|
+
| Template missing in every format | Raised, so Rails returns 500 | 302 to the mounted `not_found` page |
|
|
39
40
|
| Invalid cross-origin JavaScript request | 403 | Empty 200 |
|
|
40
41
|
| Missing or invalid Apipie parameter | 422 | 302 to the mounted `invalid_params` page |
|
|
41
42
|
| Mounted `/not_found`, `/invalid_params`, `/404`, `/500` | 404, 422, 404, 500 respectively | 200 with the corresponding error page |
|
|
42
43
|
|
|
43
|
-
HTML applications render error pages by default. Clients can request JSON with `Accept: application/json`. API-only applications default to JSON. HEAD requests return the status and headers with an empty body.
|
|
44
|
+
HTML applications render error pages by default. Clients can request JSON with a `.json` path, a `format` parameter, a route's format default or `Accept: application/json`. API-only applications default to JSON. HEAD requests return the status and headers with an empty body. Apipie integration is available when the host loads `apipie-rails`.
|
|
45
|
+
|
|
46
|
+
The middleware is installed automatically after `ActionDispatch::RemoteIp`, where feedmob-rescue checked requests. Malformed queries and unrecognized methods are rejected before the Rails request log and any middleware the application adds.
|
|
44
47
|
|
|
45
48
|
Development re-raises handled controller exceptions for debugging. Cross-origin script exceptions also propagate in the test environment.
|
|
46
49
|
|
|
@@ -56,12 +59,11 @@ With `self.routes`, a routing miss or an application exception renders the 404 o
|
|
|
56
59
|
Check these differences when testing a migration.
|
|
57
60
|
|
|
58
61
|
- The old error pages redirected requests that accept JSON to `/not_found`, including requests for `/not_found` itself, so a client following redirects never got a response. With `exceptions_app = self.routes`, routing misses, `.json` paths and application exceptions did the same. In migration mode the new pages answer these requests with status 200 and a JSON body such as `{"errors":["Page not found"]}`. Controller redirects for unsupported formats and Apipie errors still apply to JSON requests.
|
|
59
|
-
|
|
60
62
|
- The old engine rendered ERB views through the host's `ApplicationController`. The new engine serves standalone Rack responses. Check any error-page branding, layout overrides or controller callbacks your application relies on.
|
|
61
63
|
- Migration mode validates the encoding of query values before routing. In version 0.1.3, a mounted page that ignores its query can return 200 for invalid UTF-8. See the [changelog](CHANGELOG.md) for the correction. With migration mode off, Rails can apply per-action parameter encodings.
|
|
62
64
|
- The new middleware returns 400 for conflicting nested parameter types. On the Rails 6.1 migration baseline the old middleware let the Rack parsing exception propagate. The new method check also requires the verb casing Rails recognizes.
|
|
63
65
|
|
|
64
|
-
Before switching a production application, exercise its error routes, an Apipie endpoint if applicable, and any clients that follow redirects. Once clients can consume direct 404, 403 and 422 responses, disable migration mode. To roll back, restore the old gem and lockfile, concern, engine and exception app together.
|
|
66
|
+
Before switching a production application, exercise its error routes, an Apipie endpoint if applicable, and any clients that follow redirects. Once clients can consume direct 404, 403 and 422 responses, disable migration mode. Without migration mode, a template that is missing in every format raises and reaches your error reporting as a 500. To roll back, restore the old gem and lockfile, concern, engine and exception app together.
|
|
65
67
|
|
|
66
68
|
## Verify a package locally
|
|
67
69
|
|
|
@@ -3,7 +3,8 @@ module RequestRescue
|
|
|
3
3
|
extend ActiveSupport::Concern
|
|
4
4
|
|
|
5
5
|
included do
|
|
6
|
-
rescue_from ActionController::UnknownFormat,
|
|
6
|
+
rescue_from ActionController::UnknownFormat, with: :request_rescue_not_found
|
|
7
|
+
rescue_from ActionView::MissingTemplate, with: :request_rescue_missing_template
|
|
7
8
|
rescue_from ActionController::InvalidCrossOriginRequest, with: :request_rescue_cross_origin
|
|
8
9
|
# Rails resolves string class names when handling an exception, so Apipie is optional.
|
|
9
10
|
rescue_from "Apipie::ParamMissing", "Apipie::ParamInvalid", with: :request_rescue_invalid_params
|
|
@@ -18,6 +19,24 @@ module RequestRescue
|
|
|
18
19
|
request_rescue_render(404)
|
|
19
20
|
end
|
|
20
21
|
|
|
22
|
+
def request_rescue_missing_template(error)
|
|
23
|
+
raise error if Rails.env.development?
|
|
24
|
+
return redirect_to(request_rescue.not_found_path) if Rails.application.config.request_rescue.legacy_responses
|
|
25
|
+
# A template that exists in another format means the client asked for an unsupported one.
|
|
26
|
+
# Otherwise the template is missing, so let the application report a 500.
|
|
27
|
+
raise error unless request_rescue_other_format?(error)
|
|
28
|
+
|
|
29
|
+
request_rescue_render(404)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def request_rescue_other_format?(error)
|
|
33
|
+
return false unless respond_to?(:lookup_context, true) && error.path.present?
|
|
34
|
+
return false if error.respond_to?(:partial) && error.partial
|
|
35
|
+
|
|
36
|
+
prefixes = error.path.include?("/") ? [] : _prefixes
|
|
37
|
+
lookup_context.any_templates?(error.path, prefixes)
|
|
38
|
+
end
|
|
39
|
+
|
|
21
40
|
def request_rescue_invalid_params(error)
|
|
22
41
|
raise error if Rails.env.development?
|
|
23
42
|
return redirect_to(request_rescue.invalid_params_path) if Rails.application.config.request_rescue.legacy_responses
|
|
@@ -4,8 +4,10 @@ module RequestRescue
|
|
|
4
4
|
config.request_rescue = ActiveSupport::OrderedOptions.new
|
|
5
5
|
config.request_rescue.legacy_responses = false
|
|
6
6
|
|
|
7
|
+
# feedmob-rescue checked requests beside RemoteIp, before the Rails logger,
|
|
8
|
+
# exception pages and any middleware the application adds.
|
|
7
9
|
initializer "request_rescue.middleware" do |app|
|
|
8
|
-
app.middleware.
|
|
10
|
+
app.middleware.insert_after ActionDispatch::RemoteIp, RequestRescue::Middleware
|
|
9
11
|
end
|
|
10
12
|
end
|
|
11
13
|
end
|
|
@@ -33,6 +33,9 @@ module RequestRescue
|
|
|
33
33
|
private
|
|
34
34
|
|
|
35
35
|
def json_request?(env)
|
|
36
|
+
format = explicit_format(env)
|
|
37
|
+
return format == "json" if %w[json html].include?(format)
|
|
38
|
+
|
|
36
39
|
path = env["action_dispatch.original_path"] || env["PATH_INFO"]
|
|
37
40
|
return true if path.to_s.end_with?(".json")
|
|
38
41
|
|
|
@@ -42,6 +45,15 @@ module RequestRescue
|
|
|
42
45
|
rescue ActionDispatch::Http::MimeNegotiation::InvalidType
|
|
43
46
|
Rails.application.config.api_only
|
|
44
47
|
end
|
|
48
|
+
|
|
49
|
+
# Route defaults and the format parameter take precedence, as they do in controllers.
|
|
50
|
+
def explicit_format(env)
|
|
51
|
+
format = env["action_dispatch.request.path_parameters"]&.fetch(:format, nil)
|
|
52
|
+
format ||= Rack::Request.new(env).GET["format"]
|
|
53
|
+
format.to_s if format.is_a?(String) || format.is_a?(Symbol)
|
|
54
|
+
rescue Rack::QueryParser::ParameterTypeError, Rack::QueryParser::InvalidParameterError, RangeError
|
|
55
|
+
nil
|
|
56
|
+
end
|
|
45
57
|
end
|
|
46
58
|
|
|
47
59
|
# Rails rewrites PATH_INFO to the numeric HTTP status before dispatch.
|
|
@@ -4,9 +4,10 @@ module RequestRescue
|
|
|
4
4
|
@app = app
|
|
5
5
|
end
|
|
6
6
|
|
|
7
|
+
# Check the query before the method, in the same order as feedmob-rescue.
|
|
7
8
|
def call(env)
|
|
8
|
-
return ErrorResponse.new(405).call(env) unless ActionDispatch::Request::HTTP_METHODS.include?(env["REQUEST_METHOD"])
|
|
9
9
|
return ErrorResponse.new(400).call(env) unless valid_query?(env)
|
|
10
|
+
return ErrorResponse.new(405).call(env) unless ActionDispatch::Request::HTTP_METHODS.include?(env["REQUEST_METHOD"])
|
|
10
11
|
|
|
11
12
|
@app.call(env)
|
|
12
13
|
end
|