actionpack 4.2.10 → 5.2.4.6
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of actionpack might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +300 -466
- data/MIT-LICENSE +1 -1
- data/README.rdoc +6 -7
- data/lib/abstract_controller/asset_paths.rb +2 -0
- data/lib/abstract_controller/base.rb +45 -49
- data/lib/{action_controller → abstract_controller}/caching/fragments.rb +78 -15
- data/lib/abstract_controller/caching.rb +66 -0
- data/lib/abstract_controller/callbacks.rb +47 -31
- data/lib/abstract_controller/collector.rb +8 -11
- data/lib/abstract_controller/error.rb +6 -0
- data/lib/abstract_controller/helpers.rb +25 -25
- data/lib/abstract_controller/logger.rb +2 -0
- data/lib/abstract_controller/railties/routes_helpers.rb +4 -2
- data/lib/abstract_controller/rendering.rb +42 -41
- data/lib/abstract_controller/translation.rb +10 -7
- data/lib/abstract_controller/url_for.rb +2 -0
- data/lib/abstract_controller.rb +12 -5
- data/lib/action_controller/api/api_rendering.rb +16 -0
- data/lib/action_controller/api.rb +149 -0
- data/lib/action_controller/base.rb +27 -19
- data/lib/action_controller/caching.rb +14 -57
- data/lib/action_controller/form_builder.rb +50 -0
- data/lib/action_controller/log_subscriber.rb +10 -15
- data/lib/action_controller/metal/basic_implicit_render.rb +13 -0
- data/lib/action_controller/metal/conditional_get.rb +118 -44
- data/lib/action_controller/metal/content_security_policy.rb +52 -0
- data/lib/action_controller/metal/cookies.rb +3 -3
- data/lib/action_controller/metal/data_streaming.rb +27 -46
- data/lib/action_controller/metal/etag_with_flash.rb +18 -0
- data/lib/action_controller/metal/etag_with_template_digest.rb +20 -13
- data/lib/action_controller/metal/exceptions.rb +8 -14
- data/lib/action_controller/metal/flash.rb +4 -3
- data/lib/action_controller/metal/force_ssl.rb +23 -21
- data/lib/action_controller/metal/head.rb +21 -19
- data/lib/action_controller/metal/helpers.rb +24 -14
- data/lib/action_controller/metal/http_authentication.rb +65 -58
- data/lib/action_controller/metal/implicit_render.rb +62 -8
- data/lib/action_controller/metal/instrumentation.rb +19 -21
- data/lib/action_controller/metal/live.rb +90 -106
- data/lib/action_controller/metal/mime_responds.rb +33 -46
- data/lib/action_controller/metal/parameter_encoding.rb +51 -0
- data/lib/action_controller/metal/params_wrapper.rb +61 -53
- data/lib/action_controller/metal/redirecting.rb +49 -28
- data/lib/action_controller/metal/renderers.rb +87 -44
- data/lib/action_controller/metal/rendering.rb +72 -50
- data/lib/action_controller/metal/request_forgery_protection.rb +229 -93
- data/lib/action_controller/metal/rescue.rb +9 -16
- data/lib/action_controller/metal/streaming.rb +12 -10
- data/lib/action_controller/metal/strong_parameters.rb +583 -164
- data/lib/action_controller/metal/testing.rb +2 -17
- data/lib/action_controller/metal/url_for.rb +19 -10
- data/lib/action_controller/metal.rb +98 -83
- data/lib/action_controller/railtie.rb +28 -10
- data/lib/action_controller/railties/helpers.rb +2 -0
- data/lib/action_controller/renderer.rb +117 -0
- data/lib/action_controller/template_assertions.rb +11 -0
- data/lib/action_controller/test_case.rb +280 -411
- data/lib/action_controller.rb +29 -21
- data/lib/action_dispatch/http/cache.rb +93 -47
- data/lib/action_dispatch/http/content_security_policy.rb +272 -0
- data/lib/action_dispatch/http/filter_parameters.rb +26 -20
- data/lib/action_dispatch/http/filter_redirect.rb +10 -11
- data/lib/action_dispatch/http/headers.rb +55 -22
- data/lib/action_dispatch/http/mime_negotiation.rb +60 -41
- data/lib/action_dispatch/http/mime_type.rb +134 -121
- data/lib/action_dispatch/http/mime_types.rb +20 -6
- data/lib/action_dispatch/http/parameter_filter.rb +25 -11
- data/lib/action_dispatch/http/parameters.rb +98 -39
- data/lib/action_dispatch/http/rack_cache.rb +2 -0
- data/lib/action_dispatch/http/request.rb +200 -118
- data/lib/action_dispatch/http/response.rb +225 -110
- data/lib/action_dispatch/http/upload.rb +12 -6
- data/lib/action_dispatch/http/url.rb +110 -28
- data/lib/action_dispatch/journey/formatter.rb +55 -32
- data/lib/action_dispatch/journey/gtg/builder.rb +7 -5
- data/lib/action_dispatch/journey/gtg/simulator.rb +3 -9
- data/lib/action_dispatch/journey/gtg/transition_table.rb +17 -16
- data/lib/action_dispatch/journey/nfa/builder.rb +5 -3
- data/lib/action_dispatch/journey/nfa/dot.rb +13 -13
- data/lib/action_dispatch/journey/nfa/simulator.rb +3 -1
- data/lib/action_dispatch/journey/nfa/transition_table.rb +5 -48
- data/lib/action_dispatch/journey/nodes/node.rb +18 -6
- data/lib/action_dispatch/journey/parser.rb +23 -22
- data/lib/action_dispatch/journey/parser.y +3 -2
- data/lib/action_dispatch/journey/parser_extras.rb +12 -4
- data/lib/action_dispatch/journey/path/pattern.rb +50 -44
- data/lib/action_dispatch/journey/route.rb +106 -28
- data/lib/action_dispatch/journey/router/utils.rb +20 -11
- data/lib/action_dispatch/journey/router.rb +35 -23
- data/lib/action_dispatch/journey/routes.rb +18 -16
- data/lib/action_dispatch/journey/scanner.rb +18 -15
- data/lib/action_dispatch/journey/visitors.rb +99 -52
- data/lib/action_dispatch/journey.rb +7 -5
- data/lib/action_dispatch/middleware/callbacks.rb +1 -2
- data/lib/action_dispatch/middleware/cookies.rb +304 -193
- data/lib/action_dispatch/middleware/debug_exceptions.rb +152 -57
- data/lib/action_dispatch/middleware/debug_locks.rb +124 -0
- data/lib/action_dispatch/middleware/exception_wrapper.rb +68 -69
- data/lib/action_dispatch/middleware/executor.rb +21 -0
- data/lib/action_dispatch/middleware/flash.rb +78 -54
- data/lib/action_dispatch/middleware/public_exceptions.rb +27 -25
- data/lib/action_dispatch/middleware/reloader.rb +5 -91
- data/lib/action_dispatch/middleware/remote_ip.rb +41 -31
- data/lib/action_dispatch/middleware/request_id.rb +17 -9
- data/lib/action_dispatch/middleware/session/abstract_store.rb +41 -25
- data/lib/action_dispatch/middleware/session/cache_store.rb +24 -14
- data/lib/action_dispatch/middleware/session/cookie_store.rb +72 -67
- data/lib/action_dispatch/middleware/session/mem_cache_store.rb +8 -2
- data/lib/action_dispatch/middleware/show_exceptions.rb +26 -22
- data/lib/action_dispatch/middleware/ssl.rb +114 -36
- data/lib/action_dispatch/middleware/stack.rb +31 -44
- data/lib/action_dispatch/middleware/static.rb +57 -50
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb +2 -14
- data/lib/action_dispatch/middleware/templates/rescues/{_source.erb → _source.html.erb} +0 -0
- data/lib/action_dispatch/middleware/templates/rescues/_source.text.erb +8 -0
- data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.html.erb +21 -0
- data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.text.erb +13 -0
- data/lib/action_dispatch/middleware/templates/rescues/layout.erb +1 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +1 -1
- data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +1 -1
- data/lib/action_dispatch/middleware/templates/routes/_route.html.erb +4 -4
- data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +64 -64
- data/lib/action_dispatch/railtie.rb +19 -11
- data/lib/action_dispatch/request/session.rb +106 -59
- data/lib/action_dispatch/request/utils.rb +67 -24
- data/lib/action_dispatch/routing/endpoint.rb +9 -2
- data/lib/action_dispatch/routing/inspector.rb +58 -67
- data/lib/action_dispatch/routing/mapper.rb +733 -447
- data/lib/action_dispatch/routing/polymorphic_routes.rb +166 -140
- data/lib/action_dispatch/routing/redirection.rb +36 -26
- data/lib/action_dispatch/routing/route_set.rb +321 -291
- data/lib/action_dispatch/routing/routes_proxy.rb +32 -5
- data/lib/action_dispatch/routing/url_for.rb +65 -25
- data/lib/action_dispatch/routing.rb +17 -18
- data/lib/action_dispatch/system_test_case.rb +147 -0
- data/lib/action_dispatch/system_testing/browser.rb +49 -0
- data/lib/action_dispatch/system_testing/driver.rb +59 -0
- data/lib/action_dispatch/system_testing/server.rb +31 -0
- data/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +96 -0
- data/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb +31 -0
- data/lib/action_dispatch/system_testing/test_helpers/undef_methods.rb +26 -0
- data/lib/action_dispatch/testing/assertion_response.rb +47 -0
- data/lib/action_dispatch/testing/assertions/response.rb +45 -20
- data/lib/action_dispatch/testing/assertions/routing.rb +30 -26
- data/lib/action_dispatch/testing/assertions.rb +6 -4
- data/lib/action_dispatch/testing/integration.rb +347 -209
- data/lib/action_dispatch/testing/request_encoder.rb +55 -0
- data/lib/action_dispatch/testing/test_process.rb +28 -22
- data/lib/action_dispatch/testing/test_request.rb +27 -34
- data/lib/action_dispatch/testing/test_response.rb +35 -7
- data/lib/action_dispatch.rb +27 -19
- data/lib/action_pack/gem_version.rb +5 -3
- data/lib/action_pack/version.rb +3 -1
- data/lib/action_pack.rb +4 -2
- metadata +59 -42
- data/lib/action_controller/metal/hide_actions.rb +0 -40
- data/lib/action_controller/metal/rack_delegation.rb +0 -32
- data/lib/action_controller/middleware.rb +0 -39
- data/lib/action_controller/model_naming.rb +0 -12
- data/lib/action_dispatch/journey/backwards.rb +0 -5
- data/lib/action_dispatch/journey/router/strexp.rb +0 -27
- data/lib/action_dispatch/middleware/params_parser.rb +0 -60
- data/lib/action_dispatch/testing/assertions/dom.rb +0 -3
- data/lib/action_dispatch/testing/assertions/selector.rb +0 -3
- data/lib/action_dispatch/testing/assertions/tag.rb +0 -3
@@ -1,8 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActionController
|
2
4
|
module Rendering
|
3
5
|
extend ActiveSupport::Concern
|
4
6
|
|
5
|
-
RENDER_FORMATS_IN_PRIORITY = [:body, :
|
7
|
+
RENDER_FORMATS_IN_PRIORITY = [:body, :plain, :html]
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
# Documentation at ActionController::Renderer#render
|
11
|
+
delegate :render, to: :renderer
|
12
|
+
|
13
|
+
# Returns a renderer instance (inherited from ActionController::Renderer)
|
14
|
+
# for the controller.
|
15
|
+
attr_reader :renderer
|
16
|
+
|
17
|
+
def setup_renderer! # :nodoc:
|
18
|
+
@renderer = Renderer.for(self)
|
19
|
+
end
|
20
|
+
|
21
|
+
def inherited(klass)
|
22
|
+
klass.setup_renderer!
|
23
|
+
super
|
24
|
+
end
|
25
|
+
end
|
6
26
|
|
7
27
|
# Before processing, set the request formats in current controller formats.
|
8
28
|
def process_action(*) #:nodoc:
|
@@ -12,15 +32,15 @@ module ActionController
|
|
12
32
|
|
13
33
|
# Check for double render errors and set the content_type after rendering.
|
14
34
|
def render(*args) #:nodoc:
|
15
|
-
raise ::AbstractController::DoubleRenderError if
|
35
|
+
raise ::AbstractController::DoubleRenderError if response_body
|
16
36
|
super
|
17
37
|
end
|
18
38
|
|
19
|
-
# Overwrite render_to_string because body can now be set to a
|
39
|
+
# Overwrite render_to_string because body can now be set to a Rack body.
|
20
40
|
def render_to_string(*)
|
21
41
|
result = super
|
22
42
|
if result.respond_to?(:each)
|
23
|
-
string = ""
|
43
|
+
string = "".dup
|
24
44
|
result.each { |r| string << r }
|
25
45
|
string
|
26
46
|
else
|
@@ -29,72 +49,74 @@ module ActionController
|
|
29
49
|
end
|
30
50
|
|
31
51
|
def render_to_body(options = {})
|
32
|
-
super || _render_in_priorities(options) ||
|
52
|
+
super || _render_in_priorities(options) || " "
|
33
53
|
end
|
34
54
|
|
35
55
|
private
|
36
56
|
|
37
|
-
|
38
|
-
|
39
|
-
|
57
|
+
def _process_variant(options)
|
58
|
+
if defined?(request) && !request.nil? && request.variant.present?
|
59
|
+
options[:variant] = request.variant
|
60
|
+
end
|
40
61
|
end
|
41
62
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
super
|
63
|
+
def _render_in_priorities(options)
|
64
|
+
RENDER_FORMATS_IN_PRIORITY.each do |format|
|
65
|
+
return options[format] if options.key?(format)
|
66
|
+
end
|
47
67
|
|
48
|
-
|
49
|
-
self.content_type = Mime::TEXT
|
50
|
-
else
|
51
|
-
self.content_type ||= format.to_s
|
68
|
+
nil
|
52
69
|
end
|
53
|
-
end
|
54
70
|
|
55
|
-
|
56
|
-
|
57
|
-
options = super
|
58
|
-
options[:update] = blk if block_given?
|
59
|
-
options
|
60
|
-
end
|
61
|
-
|
62
|
-
# Normalize both text and status options.
|
63
|
-
def _normalize_options(options) #:nodoc:
|
64
|
-
_normalize_text(options)
|
65
|
-
|
66
|
-
if options[:html]
|
67
|
-
options[:html] = ERB::Util.html_escape(options[:html])
|
71
|
+
def _set_html_content_type
|
72
|
+
self.content_type = Mime[:html].to_s
|
68
73
|
end
|
69
74
|
|
70
|
-
|
71
|
-
|
75
|
+
def _set_rendered_content_type(format)
|
76
|
+
if format && !response.content_type
|
77
|
+
self.content_type = format.to_s
|
78
|
+
end
|
72
79
|
end
|
73
80
|
|
74
|
-
|
75
|
-
|
81
|
+
# Normalize arguments by catching blocks and setting them on :update.
|
82
|
+
def _normalize_args(action = nil, options = {}, &blk)
|
83
|
+
options = super
|
84
|
+
options[:update] = blk if block_given?
|
85
|
+
options
|
76
86
|
end
|
77
87
|
|
78
|
-
|
79
|
-
|
88
|
+
# Normalize both text and status options.
|
89
|
+
def _normalize_options(options)
|
90
|
+
_normalize_text(options)
|
91
|
+
|
92
|
+
if options[:html]
|
93
|
+
options[:html] = ERB::Util.html_escape(options[:html])
|
94
|
+
end
|
80
95
|
|
81
|
-
|
82
|
-
|
83
|
-
if options.key?(format) && options[format].respond_to?(:to_text)
|
84
|
-
options[format] = options[format].to_text
|
96
|
+
if options[:status]
|
97
|
+
options[:status] = Rack::Utils.status_code(options[:status])
|
85
98
|
end
|
99
|
+
|
100
|
+
super
|
86
101
|
end
|
87
|
-
end
|
88
102
|
|
89
|
-
|
90
|
-
|
91
|
-
|
103
|
+
def _normalize_text(options)
|
104
|
+
RENDER_FORMATS_IN_PRIORITY.each do |format|
|
105
|
+
if options.key?(format) && options[format].respond_to?(:to_text)
|
106
|
+
options[format] = options[format].to_text
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
92
110
|
|
93
|
-
|
94
|
-
|
95
|
-
|
111
|
+
# Process controller specific options, as status, content-type and location.
|
112
|
+
def _process_options(options)
|
113
|
+
status, content_type, location = options.values_at(:status, :content_type, :location)
|
96
114
|
|
97
|
-
|
98
|
-
|
115
|
+
self.status = status if status
|
116
|
+
self.content_type = content_type if content_type
|
117
|
+
headers["Location"] = url_for(location) if location
|
118
|
+
|
119
|
+
super
|
120
|
+
end
|
99
121
|
end
|
100
122
|
end
|
@@ -1,6 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rack/session/abstract/id"
|
4
|
+
require "action_controller/metal/exceptions"
|
5
|
+
require "active_support/security_utils"
|
6
|
+
require "active_support/core_ext/string/strip"
|
4
7
|
|
5
8
|
module ActionController #:nodoc:
|
6
9
|
class InvalidAuthenticityToken < ActionControllerError #:nodoc:
|
@@ -13,9 +16,14 @@ module ActionController #:nodoc:
|
|
13
16
|
# by including a token in the rendered HTML for your application. This token is
|
14
17
|
# stored as a random string in the session, to which an attacker does not have
|
15
18
|
# access. When a request reaches your application, \Rails verifies the received
|
16
|
-
# token with the token in the session.
|
17
|
-
#
|
18
|
-
#
|
19
|
+
# token with the token in the session. All requests are checked except GET requests
|
20
|
+
# as these should be idempotent. Keep in mind that all session-oriented requests
|
21
|
+
# should be CSRF protected, including JavaScript and HTML requests.
|
22
|
+
#
|
23
|
+
# Since HTML and JavaScript requests are typically made from the browser, we
|
24
|
+
# need to ensure to verify request authenticity for the web browser. We can
|
25
|
+
# use session-oriented authentication for these types of requests, by using
|
26
|
+
# the <tt>protect_from_forgery</tt> method in our controllers.
|
19
27
|
#
|
20
28
|
# GET requests are not protected since they don't have side effects like writing
|
21
29
|
# to the database and don't leak sensitive information. JavaScript requests are
|
@@ -26,22 +34,21 @@ module ActionController #:nodoc:
|
|
26
34
|
# Ajax) requests are allowed to make GET requests for JavaScript responses.
|
27
35
|
#
|
28
36
|
# It's important to remember that XML or JSON requests are also affected and if
|
29
|
-
# you're building an API you
|
37
|
+
# you're building an API you should change forgery protection method in
|
38
|
+
# <tt>ApplicationController</tt> (by default: <tt>:exception</tt>):
|
30
39
|
#
|
31
40
|
# class ApplicationController < ActionController::Base
|
32
|
-
# protect_from_forgery
|
33
|
-
# skip_before_action :verify_authenticity_token, if: :json_request?
|
34
|
-
#
|
35
|
-
# protected
|
36
|
-
#
|
37
|
-
# def json_request?
|
38
|
-
# request.format.json?
|
39
|
-
# end
|
41
|
+
# protect_from_forgery unless: -> { request.format.json? }
|
40
42
|
# end
|
41
43
|
#
|
42
|
-
# CSRF protection is turned on with the <tt>protect_from_forgery</tt> method
|
43
|
-
#
|
44
|
-
#
|
44
|
+
# CSRF protection is turned on with the <tt>protect_from_forgery</tt> method.
|
45
|
+
# By default <tt>protect_from_forgery</tt> protects your session with
|
46
|
+
# <tt>:null_session</tt> method, which provides an empty session
|
47
|
+
# during request.
|
48
|
+
#
|
49
|
+
# We may want to disable CSRF protection for APIs since they are typically
|
50
|
+
# designed to be state-less. That is, the request API client will handle
|
51
|
+
# the session for you instead of Rails.
|
45
52
|
#
|
46
53
|
# The token parameter is named <tt>authenticity_token</tt> by default. The name and
|
47
54
|
# value of this token must be added to every layout that renders forms by including
|
@@ -73,6 +80,18 @@ module ActionController #:nodoc:
|
|
73
80
|
config_accessor :log_warning_on_csrf_failure
|
74
81
|
self.log_warning_on_csrf_failure = true
|
75
82
|
|
83
|
+
# Controls whether the Origin header is checked in addition to the CSRF token.
|
84
|
+
config_accessor :forgery_protection_origin_check
|
85
|
+
self.forgery_protection_origin_check = false
|
86
|
+
|
87
|
+
# Controls whether form-action/method specific CSRF tokens are used.
|
88
|
+
config_accessor :per_form_csrf_tokens
|
89
|
+
self.per_form_csrf_tokens = false
|
90
|
+
|
91
|
+
# Controls whether forgery protection is enabled by default.
|
92
|
+
config_accessor :default_protect_from_forgery
|
93
|
+
self.default_protect_from_forgery = false
|
94
|
+
|
76
95
|
helper_method :form_authenticity_token
|
77
96
|
helper_method :protect_against_forgery?
|
78
97
|
end
|
@@ -86,13 +105,21 @@ module ActionController #:nodoc:
|
|
86
105
|
#
|
87
106
|
# class FooController < ApplicationController
|
88
107
|
# protect_from_forgery except: :index
|
108
|
+
# end
|
109
|
+
#
|
110
|
+
# You can disable forgery protection on controller by skipping the verification before_action:
|
89
111
|
#
|
90
|
-
# You can disable CSRF protection on controller by skipping the verification before_action:
|
91
112
|
# skip_before_action :verify_authenticity_token
|
92
113
|
#
|
93
114
|
# Valid Options:
|
94
115
|
#
|
95
|
-
# * <tt>:only/:except</tt> -
|
116
|
+
# * <tt>:only/:except</tt> - Only apply forgery protection to a subset of actions. For example <tt>only: [ :create, :create_all ]</tt>.
|
117
|
+
# * <tt>:if/:unless</tt> - Turn off the forgery protection entirely depending on the passed Proc or method reference.
|
118
|
+
# * <tt>:prepend</tt> - By default, the verification of the authentication token will be added at the position of the
|
119
|
+
# protect_from_forgery call in your application. This means any callbacks added before are run first. This is useful
|
120
|
+
# when you want your forgery protection to depend on other callbacks, like authentication methods (Oauth vs Cookie auth).
|
121
|
+
#
|
122
|
+
# If you need to add verification to the beginning of the callback chain, use <tt>prepend: true</tt>.
|
96
123
|
# * <tt>:with</tt> - Set the method to handle unverified request.
|
97
124
|
#
|
98
125
|
# Valid unverified request handling methods are:
|
@@ -100,19 +127,30 @@ module ActionController #:nodoc:
|
|
100
127
|
# * <tt>:reset_session</tt> - Resets the session.
|
101
128
|
# * <tt>:null_session</tt> - Provides an empty session during request but doesn't reset it completely. Used as default if <tt>:with</tt> option is not specified.
|
102
129
|
def protect_from_forgery(options = {})
|
130
|
+
options = options.reverse_merge(prepend: false)
|
131
|
+
|
103
132
|
self.forgery_protection_strategy = protection_method_class(options[:with] || :null_session)
|
104
133
|
self.request_forgery_protection_token ||= :authenticity_token
|
105
|
-
|
134
|
+
before_action :verify_authenticity_token, options
|
106
135
|
append_after_action :verify_same_origin_request
|
107
136
|
end
|
108
137
|
|
138
|
+
# Turn off request forgery protection. This is a wrapper for:
|
139
|
+
#
|
140
|
+
# skip_before_action :verify_authenticity_token
|
141
|
+
#
|
142
|
+
# See +skip_before_action+ for allowed options.
|
143
|
+
def skip_forgery_protection(options = {})
|
144
|
+
skip_before_action :verify_authenticity_token, options
|
145
|
+
end
|
146
|
+
|
109
147
|
private
|
110
148
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
149
|
+
def protection_method_class(name)
|
150
|
+
ActionController::RequestForgeryProtection::ProtectionMethods.const_get(name.to_s.classify)
|
151
|
+
rescue NameError
|
152
|
+
raise ArgumentError, "Invalid request forgery protection method, use :null_session, :exception, or :reset_session"
|
153
|
+
end
|
116
154
|
end
|
117
155
|
|
118
156
|
module ProtectionMethods
|
@@ -124,42 +162,34 @@ module ActionController #:nodoc:
|
|
124
162
|
# This is the method that defines the application behavior when a request is found to be unverified.
|
125
163
|
def handle_unverified_request
|
126
164
|
request = @controller.request
|
127
|
-
request.session = NullSessionHash.new(request
|
128
|
-
request.
|
129
|
-
request.
|
130
|
-
request.
|
165
|
+
request.session = NullSessionHash.new(request)
|
166
|
+
request.flash = nil
|
167
|
+
request.session_options = { skip: true }
|
168
|
+
request.cookie_jar = NullCookieJar.build(request, {})
|
131
169
|
end
|
132
170
|
|
133
|
-
|
134
|
-
|
135
|
-
class NullSessionHash < Rack::Session::Abstract::SessionHash #:nodoc:
|
136
|
-
def initialize(env)
|
137
|
-
super(nil, env)
|
138
|
-
@data = {}
|
139
|
-
@loaded = true
|
140
|
-
end
|
141
|
-
|
142
|
-
# no-op
|
143
|
-
def destroy; end
|
171
|
+
private
|
144
172
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
173
|
+
class NullSessionHash < Rack::Session::Abstract::SessionHash #:nodoc:
|
174
|
+
def initialize(req)
|
175
|
+
super(nil, req)
|
176
|
+
@data = {}
|
177
|
+
@loaded = true
|
178
|
+
end
|
149
179
|
|
150
|
-
|
151
|
-
|
152
|
-
key_generator = request.env[ActionDispatch::Cookies::GENERATOR_KEY]
|
153
|
-
host = request.host
|
154
|
-
secure = request.ssl?
|
180
|
+
# no-op
|
181
|
+
def destroy; end
|
155
182
|
|
156
|
-
|
183
|
+
def exists?
|
184
|
+
true
|
185
|
+
end
|
157
186
|
end
|
158
187
|
|
159
|
-
|
160
|
-
|
188
|
+
class NullCookieJar < ActionDispatch::Cookies::CookieJar #:nodoc:
|
189
|
+
def write(*)
|
190
|
+
# nothing
|
191
|
+
end
|
161
192
|
end
|
162
|
-
end
|
163
193
|
end
|
164
194
|
|
165
195
|
class ResetSession
|
@@ -183,29 +213,33 @@ module ActionController #:nodoc:
|
|
183
213
|
end
|
184
214
|
end
|
185
215
|
|
186
|
-
|
216
|
+
private
|
187
217
|
# The actual before_action that is used to verify the CSRF token.
|
188
218
|
# Don't override this directly. Provide your own forgery protection
|
189
219
|
# strategy instead. If you override, you'll disable same-origin
|
190
|
-
#
|
220
|
+
# <tt><script></tt> verification.
|
191
221
|
#
|
192
222
|
# Lean on the protect_from_forgery declaration to mark which actions are
|
193
223
|
# due for same-origin request verification. If protect_from_forgery is
|
194
224
|
# enabled on an action, this before_action flags its after_action to
|
195
225
|
# verify that JavaScript responses are for XHR requests, ensuring they
|
196
226
|
# follow the browser's same-origin policy.
|
197
|
-
def verify_authenticity_token
|
227
|
+
def verify_authenticity_token # :doc:
|
198
228
|
mark_for_same_origin_verification!
|
199
229
|
|
200
230
|
if !verified_request?
|
201
231
|
if logger && log_warning_on_csrf_failure
|
202
|
-
|
232
|
+
if valid_request_origin?
|
233
|
+
logger.warn "Can't verify CSRF token authenticity."
|
234
|
+
else
|
235
|
+
logger.warn "HTTP Origin header (#{request.origin}) didn't match request.base_url (#{request.base_url})"
|
236
|
+
end
|
203
237
|
end
|
204
238
|
handle_unverified_request
|
205
239
|
end
|
206
240
|
end
|
207
241
|
|
208
|
-
def handle_unverified_request
|
242
|
+
def handle_unverified_request # :doc:
|
209
243
|
forgery_protection_strategy.new(self).handle_unverified_request
|
210
244
|
end
|
211
245
|
|
@@ -215,30 +249,33 @@ module ActionController #:nodoc:
|
|
215
249
|
"If you know what you're doing, go ahead and disable forgery " \
|
216
250
|
"protection on this action to permit cross-origin JavaScript embedding."
|
217
251
|
private_constant :CROSS_ORIGIN_JAVASCRIPT_WARNING
|
252
|
+
# :startdoc:
|
218
253
|
|
219
|
-
# If
|
254
|
+
# If +verify_authenticity_token+ was run (indicating that we have
|
220
255
|
# forgery protection enabled for this request) then also verify that
|
221
256
|
# we aren't serving an unauthorized cross-origin response.
|
222
|
-
def verify_same_origin_request
|
257
|
+
def verify_same_origin_request # :doc:
|
223
258
|
if marked_for_same_origin_verification? && non_xhr_javascript_response?
|
224
|
-
logger
|
259
|
+
if logger && log_warning_on_csrf_failure
|
260
|
+
logger.warn CROSS_ORIGIN_JAVASCRIPT_WARNING
|
261
|
+
end
|
225
262
|
raise ActionController::InvalidCrossOriginRequest, CROSS_ORIGIN_JAVASCRIPT_WARNING
|
226
263
|
end
|
227
264
|
end
|
228
265
|
|
229
266
|
# GET requests are checked for cross-origin JavaScript after rendering.
|
230
|
-
def mark_for_same_origin_verification!
|
267
|
+
def mark_for_same_origin_verification! # :doc:
|
231
268
|
@marked_for_same_origin_verification = request.get?
|
232
269
|
end
|
233
270
|
|
234
|
-
# If the
|
271
|
+
# If the +verify_authenticity_token+ before_action ran, verify that
|
235
272
|
# JavaScript responses are only served to same-origin GET requests.
|
236
|
-
def marked_for_same_origin_verification?
|
273
|
+
def marked_for_same_origin_verification? # :doc:
|
237
274
|
@marked_for_same_origin_verification ||= false
|
238
275
|
end
|
239
276
|
|
240
277
|
# Check for cross-origin JavaScript responses.
|
241
|
-
def non_xhr_javascript_response?
|
278
|
+
def non_xhr_javascript_response? # :doc:
|
242
279
|
content_type =~ %r(\Atext/javascript) && !request.xhr?
|
243
280
|
end
|
244
281
|
|
@@ -246,34 +283,56 @@ module ActionController #:nodoc:
|
|
246
283
|
|
247
284
|
# Returns true or false if a request is verified. Checks:
|
248
285
|
#
|
249
|
-
# *
|
286
|
+
# * Is it a GET or HEAD request? GETs should be safe and idempotent
|
250
287
|
# * Does the form_authenticity_token match the given token value from the params?
|
251
|
-
# * Does the X-CSRF-Token header match the form_authenticity_token
|
252
|
-
def verified_request?
|
288
|
+
# * Does the X-CSRF-Token header match the form_authenticity_token?
|
289
|
+
def verified_request? # :doc:
|
253
290
|
!protect_against_forgery? || request.get? || request.head? ||
|
254
|
-
|
255
|
-
|
291
|
+
(valid_request_origin? && any_authenticity_token_valid?)
|
292
|
+
end
|
293
|
+
|
294
|
+
# Checks if any of the authenticity tokens from the request are valid.
|
295
|
+
def any_authenticity_token_valid? # :doc:
|
296
|
+
request_authenticity_tokens.any? do |token|
|
297
|
+
valid_authenticity_token?(session, token)
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
# Possible authenticity tokens sent in the request.
|
302
|
+
def request_authenticity_tokens # :doc:
|
303
|
+
[form_authenticity_param, request.x_csrf_token]
|
256
304
|
end
|
257
305
|
|
258
306
|
# Sets the token value for the current session.
|
259
|
-
def form_authenticity_token
|
260
|
-
masked_authenticity_token(session)
|
307
|
+
def form_authenticity_token(form_options: {})
|
308
|
+
masked_authenticity_token(session, form_options: form_options)
|
261
309
|
end
|
262
310
|
|
263
311
|
# Creates a masked version of the authenticity token that varies
|
264
312
|
# on each request. The masking is used to mitigate SSL attacks
|
265
313
|
# like BREACH.
|
266
|
-
def masked_authenticity_token(session)
|
314
|
+
def masked_authenticity_token(session, form_options: {}) # :doc:
|
315
|
+
action, method = form_options.values_at(:action, :method)
|
316
|
+
|
317
|
+
raw_token = if per_form_csrf_tokens && action && method
|
318
|
+
action_path = normalize_action_path(action)
|
319
|
+
per_form_csrf_token(session, action_path, method)
|
320
|
+
else
|
321
|
+
global_csrf_token(session)
|
322
|
+
end
|
323
|
+
|
267
324
|
one_time_pad = SecureRandom.random_bytes(AUTHENTICITY_TOKEN_LENGTH)
|
268
|
-
encrypted_csrf_token = xor_byte_strings(one_time_pad,
|
325
|
+
encrypted_csrf_token = xor_byte_strings(one_time_pad, raw_token)
|
269
326
|
masked_token = one_time_pad + encrypted_csrf_token
|
270
|
-
Base64.
|
327
|
+
Base64.urlsafe_encode64(masked_token, padding: false)
|
328
|
+
|
329
|
+
mask_token(raw_token)
|
271
330
|
end
|
272
331
|
|
273
332
|
# Checks the client's masked token to see if it matches the
|
274
333
|
# session token. Essentially the inverse of
|
275
334
|
# +masked_authenticity_token+.
|
276
|
-
def valid_authenticity_token?(session, encoded_masked_token)
|
335
|
+
def valid_authenticity_token?(session, encoded_masked_token) # :doc:
|
277
336
|
if encoded_masked_token.nil? || encoded_masked_token.empty? || !encoded_masked_token.is_a?(String)
|
278
337
|
return false
|
279
338
|
end
|
@@ -291,44 +350,121 @@ module ActionController #:nodoc:
|
|
291
350
|
if masked_token.length == AUTHENTICITY_TOKEN_LENGTH
|
292
351
|
# This is actually an unmasked token. This is expected if
|
293
352
|
# you have just upgraded to masked tokens, but should stop
|
294
|
-
# happening shortly after installing this gem
|
353
|
+
# happening shortly after installing this gem.
|
295
354
|
compare_with_real_token masked_token, session
|
296
355
|
|
297
356
|
elsif masked_token.length == AUTHENTICITY_TOKEN_LENGTH * 2
|
298
|
-
|
299
|
-
# value and decrypt it
|
300
|
-
one_time_pad = masked_token[0...AUTHENTICITY_TOKEN_LENGTH]
|
301
|
-
encrypted_csrf_token = masked_token[AUTHENTICITY_TOKEN_LENGTH..-1]
|
302
|
-
csrf_token = xor_byte_strings(one_time_pad, encrypted_csrf_token)
|
303
|
-
|
304
|
-
compare_with_real_token csrf_token, session
|
357
|
+
csrf_token = unmask_token(masked_token)
|
305
358
|
|
359
|
+
compare_with_global_token(csrf_token, session) ||
|
360
|
+
compare_with_real_token(csrf_token, session) ||
|
361
|
+
valid_per_form_csrf_token?(csrf_token, session)
|
306
362
|
else
|
307
|
-
false # Token is malformed
|
363
|
+
false # Token is malformed.
|
308
364
|
end
|
309
365
|
end
|
310
366
|
|
311
|
-
def
|
312
|
-
|
367
|
+
def unmask_token(masked_token) # :doc:
|
368
|
+
# Split the token into the one-time pad and the encrypted
|
369
|
+
# value and decrypt it.
|
370
|
+
one_time_pad = masked_token[0...AUTHENTICITY_TOKEN_LENGTH]
|
371
|
+
encrypted_csrf_token = masked_token[AUTHENTICITY_TOKEN_LENGTH..-1]
|
372
|
+
xor_byte_strings(one_time_pad, encrypted_csrf_token)
|
313
373
|
end
|
314
374
|
|
315
|
-
def
|
375
|
+
def mask_token(raw_token) # :doc:
|
376
|
+
one_time_pad = SecureRandom.random_bytes(AUTHENTICITY_TOKEN_LENGTH)
|
377
|
+
encrypted_csrf_token = xor_byte_strings(one_time_pad, raw_token)
|
378
|
+
masked_token = one_time_pad + encrypted_csrf_token
|
379
|
+
Base64.strict_encode64(masked_token)
|
380
|
+
end
|
381
|
+
|
382
|
+
def compare_with_real_token(token, session) # :doc:
|
383
|
+
ActiveSupport::SecurityUtils.fixed_length_secure_compare(token, real_csrf_token(session))
|
384
|
+
end
|
385
|
+
|
386
|
+
def compare_with_global_token(token, session) # :doc:
|
387
|
+
ActiveSupport::SecurityUtils.fixed_length_secure_compare(token, global_csrf_token(session))
|
388
|
+
end
|
389
|
+
|
390
|
+
def valid_per_form_csrf_token?(token, session) # :doc:
|
391
|
+
if per_form_csrf_tokens
|
392
|
+
correct_token = per_form_csrf_token(
|
393
|
+
session,
|
394
|
+
normalize_action_path(request.fullpath),
|
395
|
+
request.request_method
|
396
|
+
)
|
397
|
+
|
398
|
+
ActiveSupport::SecurityUtils.fixed_length_secure_compare(token, correct_token)
|
399
|
+
else
|
400
|
+
false
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
def real_csrf_token(session) # :doc:
|
316
405
|
session[:_csrf_token] ||= SecureRandom.base64(AUTHENTICITY_TOKEN_LENGTH)
|
317
406
|
Base64.strict_decode64(session[:_csrf_token])
|
318
407
|
end
|
319
408
|
|
320
|
-
def
|
321
|
-
|
409
|
+
def per_form_csrf_token(session, action_path, method) # :doc:
|
410
|
+
csrf_token_hmac(session, [action_path, method.downcase].join("#"))
|
411
|
+
end
|
412
|
+
|
413
|
+
GLOBAL_CSRF_TOKEN_IDENTIFIER = "!real_csrf_token"
|
414
|
+
private_constant :GLOBAL_CSRF_TOKEN_IDENTIFIER
|
415
|
+
|
416
|
+
def global_csrf_token(session) # :doc:
|
417
|
+
csrf_token_hmac(session, GLOBAL_CSRF_TOKEN_IDENTIFIER)
|
418
|
+
end
|
419
|
+
|
420
|
+
def csrf_token_hmac(session, identifier) # :doc:
|
421
|
+
OpenSSL::HMAC.digest(
|
422
|
+
OpenSSL::Digest::SHA256.new,
|
423
|
+
real_csrf_token(session),
|
424
|
+
identifier
|
425
|
+
)
|
426
|
+
end
|
427
|
+
|
428
|
+
def xor_byte_strings(s1, s2) # :doc:
|
429
|
+
s2_bytes = s2.bytes
|
430
|
+
s1.each_byte.with_index { |c1, i| s2_bytes[i] ^= c1 }
|
431
|
+
s2_bytes.pack("C*")
|
322
432
|
end
|
323
433
|
|
324
434
|
# The form's authenticity parameter. Override to provide your own.
|
325
|
-
def form_authenticity_param
|
435
|
+
def form_authenticity_param # :doc:
|
326
436
|
params[request_forgery_protection_token]
|
327
437
|
end
|
328
438
|
|
329
439
|
# Checks if the controller allows forgery protection.
|
330
|
-
def protect_against_forgery?
|
440
|
+
def protect_against_forgery? # :doc:
|
331
441
|
allow_forgery_protection
|
332
442
|
end
|
443
|
+
|
444
|
+
NULL_ORIGIN_MESSAGE = <<-MSG.strip_heredoc
|
445
|
+
The browser returned a 'null' origin for a request with origin-based forgery protection turned on. This usually
|
446
|
+
means you have the 'no-referrer' Referrer-Policy header enabled, or that the request came from a site that
|
447
|
+
refused to give its origin. This makes it impossible for Rails to verify the source of the requests. Likely the
|
448
|
+
best solution is to change your referrer policy to something less strict like same-origin or strict-same-origin.
|
449
|
+
If you cannot change the referrer policy, you can disable origin checking with the
|
450
|
+
Rails.application.config.action_controller.forgery_protection_origin_check setting.
|
451
|
+
MSG
|
452
|
+
|
453
|
+
# Checks if the request originated from the same origin by looking at the
|
454
|
+
# Origin header.
|
455
|
+
def valid_request_origin? # :doc:
|
456
|
+
if forgery_protection_origin_check
|
457
|
+
# We accept blank origin headers because some user agents don't send it.
|
458
|
+
raise InvalidAuthenticityToken, NULL_ORIGIN_MESSAGE if request.origin == "null"
|
459
|
+
request.origin.nil? || request.origin == request.base_url
|
460
|
+
else
|
461
|
+
true
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
def normalize_action_path(action_path) # :doc:
|
466
|
+
uri = URI.parse(action_path)
|
467
|
+
uri.path.chomp("/")
|
468
|
+
end
|
333
469
|
end
|
334
470
|
end
|