actionpack 7.2.3.1 → 8.1.4.1

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.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +606 -120
  3. data/lib/abstract_controller/asset_paths.rb +4 -2
  4. data/lib/abstract_controller/base.rb +11 -5
  5. data/lib/abstract_controller/caching.rb +6 -3
  6. data/lib/abstract_controller/callbacks.rb +6 -0
  7. data/lib/abstract_controller/logger.rb +2 -1
  8. data/lib/abstract_controller/rendering.rb +0 -1
  9. data/lib/action_controller/api.rb +1 -0
  10. data/lib/action_controller/base.rb +3 -2
  11. data/lib/action_controller/caching.rb +1 -2
  12. data/lib/action_controller/form_builder.rb +4 -4
  13. data/lib/action_controller/log_subscriber.rb +22 -3
  14. data/lib/action_controller/metal/allow_browser.rb +12 -2
  15. data/lib/action_controller/metal/conditional_get.rb +30 -1
  16. data/lib/action_controller/metal/data_streaming.rb +5 -5
  17. data/lib/action_controller/metal/exceptions.rb +5 -0
  18. data/lib/action_controller/metal/flash.rb +1 -4
  19. data/lib/action_controller/metal/head.rb +3 -1
  20. data/lib/action_controller/metal/http_authentication.rb +1 -1
  21. data/lib/action_controller/metal/instrumentation.rb +1 -2
  22. data/lib/action_controller/metal/live.rb +83 -31
  23. data/lib/action_controller/metal/permissions_policy.rb +9 -0
  24. data/lib/action_controller/metal/rate_limiting.rb +39 -9
  25. data/lib/action_controller/metal/redirecting.rb +107 -14
  26. data/lib/action_controller/metal/renderers.rb +29 -9
  27. data/lib/action_controller/metal/rendering.rb +16 -3
  28. data/lib/action_controller/metal/request_forgery_protection.rb +18 -10
  29. data/lib/action_controller/metal/rescue.rb +9 -0
  30. data/lib/action_controller/metal/streaming.rb +5 -84
  31. data/lib/action_controller/metal/strong_parameters.rb +294 -98
  32. data/lib/action_controller/railtie.rb +35 -15
  33. data/lib/action_controller/structured_event_subscriber.rb +116 -0
  34. data/lib/action_controller/test_case.rb +12 -2
  35. data/lib/action_dispatch/http/cache.rb +143 -12
  36. data/lib/action_dispatch/http/content_security_policy.rb +14 -1
  37. data/lib/action_dispatch/http/filter_parameters.rb +5 -3
  38. data/lib/action_dispatch/http/mime_negotiation.rb +66 -2
  39. data/lib/action_dispatch/http/mime_types.rb +1 -0
  40. data/lib/action_dispatch/http/param_builder.rb +187 -0
  41. data/lib/action_dispatch/http/param_error.rb +26 -0
  42. data/lib/action_dispatch/http/parameters.rb +3 -3
  43. data/lib/action_dispatch/http/permissions_policy.rb +8 -1
  44. data/lib/action_dispatch/http/query_parser.rb +55 -0
  45. data/lib/action_dispatch/http/request.rb +70 -21
  46. data/lib/action_dispatch/http/response.rb +52 -17
  47. data/lib/action_dispatch/http/url.rb +112 -16
  48. data/lib/action_dispatch/journey/formatter.rb +5 -0
  49. data/lib/action_dispatch/journey/gtg/simulator.rb +33 -12
  50. data/lib/action_dispatch/journey/gtg/transition_table.rb +33 -41
  51. data/lib/action_dispatch/journey/nodes/node.rb +2 -1
  52. data/lib/action_dispatch/journey/parser.rb +99 -196
  53. data/lib/action_dispatch/journey/route.rb +45 -31
  54. data/lib/action_dispatch/journey/router/utils.rb +9 -15
  55. data/lib/action_dispatch/journey/router.rb +59 -81
  56. data/lib/action_dispatch/journey/routes.rb +7 -0
  57. data/lib/action_dispatch/journey/scanner.rb +44 -42
  58. data/lib/action_dispatch/journey/visitors.rb +55 -23
  59. data/lib/action_dispatch/journey/visualizer/fsm.js +4 -6
  60. data/lib/action_dispatch/log_subscriber.rb +7 -3
  61. data/lib/action_dispatch/middleware/cookies.rb +24 -5
  62. data/lib/action_dispatch/middleware/debug_exceptions.rb +24 -5
  63. data/lib/action_dispatch/middleware/debug_view.rb +11 -5
  64. data/lib/action_dispatch/middleware/exception_wrapper.rb +12 -12
  65. data/lib/action_dispatch/middleware/executor.rb +12 -2
  66. data/lib/action_dispatch/middleware/host_authorization.rb +3 -2
  67. data/lib/action_dispatch/middleware/public_exceptions.rb +1 -5
  68. data/lib/action_dispatch/middleware/remote_ip.rb +61 -24
  69. data/lib/action_dispatch/middleware/request_id.rb +2 -1
  70. data/lib/action_dispatch/middleware/session/abstract_store.rb +5 -0
  71. data/lib/action_dispatch/middleware/session/cache_store.rb +44 -4
  72. data/lib/action_dispatch/middleware/session/cookie_store.rb +0 -3
  73. data/lib/action_dispatch/middleware/ssl.rb +13 -3
  74. data/lib/action_dispatch/middleware/templates/rescues/_copy_button.html.erb +1 -0
  75. data/lib/action_dispatch/middleware/templates/rescues/_source.html.erb +3 -5
  76. data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +9 -5
  77. data/lib/action_dispatch/middleware/templates/rescues/blocked_host.html.erb +1 -0
  78. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb +1 -0
  79. data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.html.erb +4 -0
  80. data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.text.erb +3 -0
  81. data/lib/action_dispatch/middleware/templates/rescues/layout.erb +50 -0
  82. data/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.html.erb +1 -0
  83. data/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb +1 -0
  84. data/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb +7 -8
  85. data/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb +1 -1
  86. data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +1 -0
  87. data/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb +1 -0
  88. data/lib/action_dispatch/railtie.rb +21 -0
  89. data/lib/action_dispatch/request/session.rb +1 -0
  90. data/lib/action_dispatch/request/utils.rb +9 -3
  91. data/lib/action_dispatch/routing/inspector.rb +80 -57
  92. data/lib/action_dispatch/routing/mapper.rb +404 -223
  93. data/lib/action_dispatch/routing/polymorphic_routes.rb +2 -2
  94. data/lib/action_dispatch/routing/redirection.rb +10 -7
  95. data/lib/action_dispatch/routing/route_set.rb +21 -12
  96. data/lib/action_dispatch/routing/routes_proxy.rb +1 -0
  97. data/lib/action_dispatch/structured_event_subscriber.rb +20 -0
  98. data/lib/action_dispatch/system_test_case.rb +3 -3
  99. data/lib/action_dispatch/system_testing/browser.rb +15 -22
  100. data/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +2 -2
  101. data/lib/action_dispatch/testing/assertions/response.rb +26 -2
  102. data/lib/action_dispatch/testing/assertions/routing.rb +27 -15
  103. data/lib/action_dispatch/testing/integration.rb +18 -7
  104. data/lib/action_dispatch/testing/test_helpers/page_dump_helper.rb +1 -1
  105. data/lib/action_dispatch.rb +14 -4
  106. data/lib/action_pack/gem_version.rb +3 -3
  107. metadata +18 -48
  108. data/lib/action_dispatch/journey/parser.y +0 -50
  109. data/lib/action_dispatch/journey/parser_extras.rb +0 -33
@@ -12,9 +12,11 @@ require "action_view/railtie"
12
12
  module ActionController
13
13
  class Railtie < Rails::Railtie # :nodoc:
14
14
  config.action_controller = ActiveSupport::OrderedOptions.new
15
- config.action_controller.raise_on_open_redirects = false
15
+ config.action_controller.action_on_open_redirect = :log
16
+ config.action_controller.action_on_path_relative_redirect = :log
16
17
  config.action_controller.log_query_tags_around_actions = true
17
18
  config.action_controller.wrap_parameters_by_default = false
19
+ config.action_controller.allowed_redirect_hosts = []
18
20
 
19
21
  config.eager_load_namespaces << AbstractController
20
22
  config.eager_load_namespaces << ActionController
@@ -31,6 +33,12 @@ module ActionController
31
33
  ActionController::Helpers.helpers_path = app.helpers_paths
32
34
  end
33
35
 
36
+ initializer "action_controller.live_streaming_excluded_keys" do |app|
37
+ ActiveSupport.on_load(:action_controller_live) do
38
+ ActionController::Live.live_streaming_excluded_keys = app.config.action_controller.live_streaming_excluded_keys
39
+ end
40
+ end
41
+
34
42
  initializer "action_controller.parameters_config" do |app|
35
43
  options = app.config.action_controller
36
44
 
@@ -48,11 +56,6 @@ module ActionController
48
56
  end
49
57
 
50
58
  ActionController::Parameters.action_on_unpermitted_parameters = action_on_unpermitted_parameters
51
-
52
- unless options.allow_deprecated_parameters_hash_equality.nil?
53
- ActionController::Parameters.allow_deprecated_parameters_hash_equality =
54
- options.allow_deprecated_parameters_hash_equality
55
- end
56
59
  end
57
60
  end
58
61
 
@@ -60,7 +63,8 @@ module ActionController
60
63
  paths = app.config.paths
61
64
  options = app.config.action_controller
62
65
 
63
- options.logger ||= Rails.logger
66
+ options.logger = options.fetch(:logger, Rails.logger)
67
+
64
68
  options.cache_store ||= Rails.cache
65
69
 
66
70
  options.javascripts_dir ||= paths["public/javascripts"].first
@@ -85,7 +89,7 @@ module ActionController
85
89
  :action_on_unpermitted_parameters,
86
90
  :always_permitted_parameters,
87
91
  :wrap_parameters_by_default,
88
- :allow_deprecated_parameters_hash_equality
92
+ :live_streaming_excluded_keys
89
93
  )
90
94
 
91
95
  filtered_options.each do |k, v|
@@ -99,12 +103,6 @@ module ActionController
99
103
  end
100
104
  end
101
105
 
102
- initializer "action_controller.compile_config_methods" do
103
- ActiveSupport.on_load(:action_controller) do
104
- config.compile_methods! if config.respond_to?(:compile_methods!)
105
- end
106
- end
107
-
108
106
  initializer "action_controller.request_forgery_protection" do |app|
109
107
  ActiveSupport.on_load(:action_controller_base) do
110
108
  if app.config.action_controller.default_protect_from_forgery
@@ -113,6 +111,22 @@ module ActionController
113
111
  end
114
112
  end
115
113
 
114
+ initializer "action_controller.open_redirects" do |app|
115
+ ActiveSupport.on_load(:action_controller, run_once: true) do
116
+ if app.config.action_controller.has_key?(:raise_on_open_redirects)
117
+ ActiveSupport.deprecator.warn(<<~MSG.squish)
118
+ `raise_on_open_redirects` is deprecated and will be removed in a future Rails version.
119
+ Use `config.action_controller.action_on_open_redirect = :raise` instead.
120
+ MSG
121
+
122
+ # Fallback to the default behavior in case of `load_default` set `action_on_open_redirect`, but apps set `raise_on_open_redirects`.
123
+ if app.config.action_controller.raise_on_open_redirects == false && app.config.action_controller.action_on_open_redirect == :raise
124
+ self.action_on_open_redirect = :log
125
+ end
126
+ end
127
+ end
128
+ end
129
+
116
130
  initializer "action_controller.query_log_tags" do |app|
117
131
  query_logs_tags_enabled = app.config.respond_to?(:active_record) &&
118
132
  app.config.active_record.query_log_tags_enabled &&
@@ -123,7 +137,7 @@ module ActionController
123
137
  app.config.active_record.query_log_tags |= [:action]
124
138
 
125
139
  ActiveSupport.on_load(:active_record) do
126
- ActiveRecord::QueryLogs.taggings.merge!(
140
+ ActiveRecord::QueryLogs.taggings = ActiveRecord::QueryLogs.taggings.merge(
127
141
  controller: ->(context) { context[:controller]&.controller_name },
128
142
  action: ->(context) { context[:controller]&.action_name },
129
143
  namespaced_controller: ->(context) {
@@ -145,5 +159,11 @@ module ActionController
145
159
  ActionController::TestCase.executor_around_each_request = app.config.active_support.executor_around_test_case
146
160
  end
147
161
  end
162
+
163
+ initializer "action_controller.backtrace_cleaner" do
164
+ ActiveSupport.on_load(:action_controller) do
165
+ ActionController::LogSubscriber.backtrace_cleaner = Rails.backtrace_cleaner
166
+ end
167
+ end
148
168
  end
149
169
  end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionController
4
+ class StructuredEventSubscriber < ActiveSupport::StructuredEventSubscriber # :nodoc:
5
+ INTERNAL_PARAMS = %w(controller action format _method only_path)
6
+
7
+ def start_processing(event)
8
+ payload = event.payload
9
+ params = {}
10
+ payload[:params].each_pair do |k, v|
11
+ params[k] = v unless INTERNAL_PARAMS.include?(k)
12
+ end
13
+ format = payload[:format]
14
+ format = format.to_s.upcase if format.is_a?(Symbol)
15
+ format = "*/*" if format.nil?
16
+
17
+ emit_event("action_controller.request_started",
18
+ controller: payload[:controller],
19
+ action: payload[:action],
20
+ format:,
21
+ params:,
22
+ )
23
+ end
24
+
25
+ def process_action(event)
26
+ payload = event.payload
27
+ status = payload[:status]
28
+
29
+ if status.nil? && (exception_class_name = payload[:exception]&.first)
30
+ status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
31
+ end
32
+
33
+ emit_event("action_controller.request_completed", {
34
+ controller: payload[:controller],
35
+ action: payload[:action],
36
+ status: status,
37
+ **additions_for(payload),
38
+ duration_ms: event.duration.round(2),
39
+ gc_time_ms: event.gc_time.round(1),
40
+ }.compact)
41
+ end
42
+
43
+ def halted_callback(event)
44
+ emit_event("action_controller.callback_halted", filter: event.payload[:filter])
45
+ end
46
+
47
+ def rescue_from_callback(event)
48
+ exception = event.payload[:exception]
49
+
50
+ exception_backtrace = exception.backtrace&.first
51
+ exception_backtrace = exception_backtrace&.delete_prefix("#{Rails.root}/") if defined?(Rails.root) && Rails.root
52
+
53
+ emit_event("action_controller.rescue_from_handled",
54
+ exception_class: exception.class.name,
55
+ exception_message: exception.message,
56
+ exception_backtrace:
57
+ )
58
+ end
59
+
60
+ def send_file(event)
61
+ emit_event("action_controller.file_sent", path: event.payload[:path], duration_ms: event.duration.round(1))
62
+ end
63
+
64
+ def redirect_to(event)
65
+ emit_event("action_controller.redirected", location: event.payload[:location])
66
+ end
67
+
68
+ def send_data(event)
69
+ emit_event("action_controller.data_sent", filename: event.payload[:filename], duration_ms: event.duration.round(1))
70
+ end
71
+
72
+ def unpermitted_parameters(event)
73
+ unpermitted_keys = event.payload[:keys]
74
+ context = event.payload[:context]
75
+
76
+ emit_debug_event("action_controller.unpermitted_parameters",
77
+ unpermitted_keys:,
78
+ context: context.except(:request)
79
+ )
80
+ end
81
+ debug_only :unpermitted_parameters
82
+
83
+ def write_fragment(event)
84
+ fragment_cache(__method__, event)
85
+ end
86
+
87
+ def read_fragment(event)
88
+ fragment_cache(__method__, event)
89
+ end
90
+
91
+ def exist_fragment?(event)
92
+ fragment_cache(__method__, event)
93
+ end
94
+
95
+ def expire_fragment(event)
96
+ fragment_cache(__method__, event)
97
+ end
98
+
99
+ private
100
+ def fragment_cache(method_name, event)
101
+ key = ActiveSupport::Cache.expand_cache_key(event.payload[:key] || event.payload[:path])
102
+
103
+ emit_event("action_controller.fragment_cache",
104
+ method: "#{method_name}",
105
+ key: key,
106
+ duration_ms: event.duration.round(1)
107
+ )
108
+ end
109
+
110
+ def additions_for(payload)
111
+ payload.slice(:view_runtime, :db_runtime, :queries_count, :cached_queries_count)
112
+ end
113
+ end
114
+ end
115
+
116
+ ActionController::StructuredEventSubscriber.attach_to :action_controller
@@ -22,11 +22,21 @@ module ActionController
22
22
  # database on the main thread, so they could open a txn, then the controller
23
23
  # thread will open a new connection and try to access data that's only visible
24
24
  # to the main thread's txn. This is the problem in #23483.
25
+ alias_method :original_new_controller_thread, :new_controller_thread
26
+
25
27
  silence_redefinition_of_method :new_controller_thread
26
28
  def new_controller_thread # :nodoc:
27
29
  yield
28
30
  end
29
31
 
32
+ # Because of the above, we need to prevent the clearing of thread locals, since
33
+ # no new thread is actually spawned in the test environment.
34
+ alias_method :original_clean_up_thread_locals, :clean_up_thread_locals
35
+
36
+ silence_redefinition_of_method :clean_up_thread_locals
37
+ def clean_up_thread_locals(*args) # :nodoc:
38
+ end
39
+
30
40
  # Avoid a deadlock from the queue filling up
31
41
  Buffer.queue_size = nil
32
42
  end
@@ -106,7 +116,7 @@ module ActionController
106
116
  set_header k, "application/x-www-form-urlencoded"
107
117
  end
108
118
 
109
- case content_mime_type.to_sym
119
+ case content_mime_type&.to_sym
110
120
  when nil
111
121
  raise "Unknown Content-Type: #{content_type}"
112
122
  when :json
@@ -121,7 +131,7 @@ module ActionController
121
131
  end
122
132
  end
123
133
 
124
- data_stream = StringIO.new(data)
134
+ data_stream = StringIO.new(data.b)
125
135
  set_header "CONTENT_LENGTH", data_stream.length.to_s
126
136
  set_header "rack.input", data_stream
127
137
  end
@@ -9,9 +9,15 @@ module ActionDispatch
9
9
  HTTP_IF_MODIFIED_SINCE = "HTTP_IF_MODIFIED_SINCE"
10
10
  HTTP_IF_NONE_MATCH = "HTTP_IF_NONE_MATCH"
11
11
 
12
+ mattr_accessor :strict_freshness, default: false
13
+
12
14
  def if_modified_since
13
15
  if since = get_header(HTTP_IF_MODIFIED_SINCE)
14
- Time.rfc2822(since) rescue nil
16
+ # `If-Modified-Since` carries an HTTP-date, which per RFC 9110 may be in
17
+ # any of the three legal formats (IMF-fixdate, RFC 850, or asctime).
18
+ # `Time.httpdate` accepts all three; it also matches how the response side
19
+ # parses `Last-Modified`/`Date`. `Time.rfc2822` only accepted IMF-fixdate.
20
+ Time.httpdate(since) rescue nil
15
21
  end
16
22
  end
17
23
 
@@ -34,19 +40,140 @@ module ActionDispatch
34
40
  end
35
41
  end
36
42
 
37
- # Check response freshness (`Last-Modified` and ETag) against request
38
- # `If-Modified-Since` and `If-None-Match` conditions. If both headers are
39
- # supplied, both must match, or the request is not considered fresh.
43
+ # Check response freshness (`Last-Modified` and `ETag`) against request
44
+ # `If-Modified-Since` and `If-None-Match` conditions.
45
+ # If both headers are supplied, based on configuration, either `ETag` is preferred over `Last-Modified`
46
+ # or both are considered equally. You can adjust the preference with
47
+ # `config.action_dispatch.strict_freshness`.
48
+ # Reference: http://tools.ietf.org/html/rfc7232#section-6
40
49
  def fresh?(response)
41
- last_modified = if_modified_since
42
- etag = if_none_match
50
+ if Request.strict_freshness
51
+ if if_none_match
52
+ etag_matches?(response.etag)
53
+ elsif if_modified_since
54
+ not_modified?(response.last_modified)
55
+ else
56
+ false
57
+ end
58
+ else
59
+ last_modified = if_modified_since
60
+ etag = if_none_match
61
+
62
+ return false unless last_modified || etag
63
+
64
+ success = true
65
+ success &&= not_modified?(response.last_modified) if last_modified
66
+ success &&= etag_matches?(response.etag) if etag
67
+ success
68
+ end
69
+ end
70
+
71
+ def cache_control_directives
72
+ @cache_control_directives ||= CacheControlDirectives.new(get_header("HTTP_CACHE_CONTROL"))
73
+ end
74
+
75
+ # Represents the HTTP Cache-Control header for requests,
76
+ # providing methods to access various cache control directives
77
+ # Reference: https://www.rfc-editor.org/rfc/rfc9111.html#name-request-directives
78
+ class CacheControlDirectives
79
+ def initialize(cache_control_header)
80
+ @only_if_cached = false
81
+ @no_cache = false
82
+ @no_store = false
83
+ @no_transform = false
84
+ @max_age = nil
85
+ @max_stale = nil
86
+ @min_fresh = nil
87
+ @stale_if_error = false
88
+ parse_directives(cache_control_header)
89
+ end
90
+
91
+ # Returns true if the only-if-cached directive is present.
92
+ # This directive indicates that the client only wishes to obtain a
93
+ # stored response. If a valid stored response is not available,
94
+ # the server should respond with a 504 (Gateway Timeout) status.
95
+ def only_if_cached?
96
+ @only_if_cached
97
+ end
98
+
99
+ # Returns true if the no-cache directive is present.
100
+ # This directive indicates that a cache must not use the response
101
+ # to satisfy subsequent requests without successful validation on the origin server.
102
+ def no_cache?
103
+ @no_cache
104
+ end
105
+
106
+ # Returns true if the no-store directive is present.
107
+ # This directive indicates that a cache must not store any part of the
108
+ # request or response.
109
+ def no_store?
110
+ @no_store
111
+ end
112
+
113
+ # Returns true if the no-transform directive is present.
114
+ # This directive indicates that a cache or proxy must not transform the payload.
115
+ def no_transform?
116
+ @no_transform
117
+ end
43
118
 
44
- return false unless last_modified || etag
119
+ # Returns the value of the max-age directive.
120
+ # This directive indicates that the client is willing to accept a response
121
+ # whose age is no greater than the specified number of seconds.
122
+ attr_reader :max_age
45
123
 
46
- success = true
47
- success &&= not_modified?(response.last_modified) if last_modified
48
- success &&= etag_matches?(response.etag) if etag
49
- success
124
+ # Returns the value of the max-stale directive.
125
+ # When max-stale is present with a value, returns that integer value.
126
+ # When max-stale is present without a value, returns true (unlimited staleness).
127
+ # When max-stale is not present, returns nil.
128
+ attr_reader :max_stale
129
+
130
+ # Returns true if max-stale directive is present (with or without a value)
131
+ def max_stale?
132
+ !@max_stale.nil?
133
+ end
134
+
135
+ # Returns true if max-stale directive is present without a value (unlimited staleness)
136
+ def max_stale_unlimited?
137
+ @max_stale == true
138
+ end
139
+
140
+ # Returns the value of the min-fresh directive.
141
+ # This directive indicates that the client is willing to accept a response
142
+ # whose freshness lifetime is no less than its current age plus the specified time in seconds.
143
+ attr_reader :min_fresh
144
+
145
+ # Returns the value of the stale-if-error directive.
146
+ # This directive indicates that the client is willing to accept a stale response
147
+ # if the check for a fresh one fails with an error for the specified number of seconds.
148
+ attr_reader :stale_if_error
149
+
150
+ private
151
+ def parse_directives(header_value)
152
+ return unless header_value
153
+
154
+ header_value.delete(" ").downcase.split(",").each do |directive|
155
+ name, value = directive.split("=", 2)
156
+
157
+ case name
158
+ when "max-age"
159
+ @max_age = value.to_i
160
+ when "min-fresh"
161
+ @min_fresh = value.to_i
162
+ when "stale-if-error"
163
+ @stale_if_error = value.to_i
164
+ when "no-cache"
165
+ @no_cache = true
166
+ when "no-store"
167
+ @no_store = true
168
+ when "no-transform"
169
+ @no_transform = true
170
+ when "only-if-cached"
171
+ @only_if_cached = true
172
+ when "max-stale"
173
+ @max_stale = value ? value.to_i : true
174
+ end
175
+ end
176
+ end
50
177
  end
51
178
  end
52
179
 
@@ -127,7 +254,7 @@ module ActionDispatch
127
254
  private
128
255
  DATE = "Date"
129
256
  LAST_MODIFIED = "Last-Modified"
130
- SPECIAL_KEYS = Set.new(%w[extras no-store no-cache max-age public private must-revalidate])
257
+ SPECIAL_KEYS = Set.new(%w[extras no-store no-cache max-age public private must-revalidate must-understand])
131
258
 
132
259
  def generate_weak_etag(validators)
133
260
  "W/#{generate_strong_etag(validators)}"
@@ -171,6 +298,8 @@ module ActionDispatch
171
298
  PUBLIC = "public"
172
299
  PRIVATE = "private"
173
300
  MUST_REVALIDATE = "must-revalidate"
301
+ IMMUTABLE = "immutable"
302
+ MUST_UNDERSTAND = "must-understand"
174
303
 
175
304
  def handle_conditional_get!
176
305
  # Normally default cache control setting is handled by ETag middleware. But, if
@@ -205,6 +334,7 @@ module ActionDispatch
205
334
 
206
335
  if control[:no_store]
207
336
  options << PRIVATE if control[:private]
337
+ options << MUST_UNDERSTAND if control[:must_understand]
208
338
  options << NO_STORE
209
339
  elsif control[:no_cache]
210
340
  options << PUBLIC if control[:public]
@@ -221,6 +351,7 @@ module ActionDispatch
221
351
  options << MUST_REVALIDATE if control[:must_revalidate]
222
352
  options << "stale-while-revalidate=#{stale_while_revalidate.to_i}" if stale_while_revalidate
223
353
  options << "stale-if-error=#{stale_if_error.to_i}" if stale_if_error
354
+ options << IMMUTABLE if control[:immutable]
224
355
  options.concat(extras) if extras
225
356
  end
226
357
 
@@ -128,6 +128,7 @@ module ActionDispatch # :nodoc:
128
128
  MAPPINGS = {
129
129
  self: "'self'",
130
130
  unsafe_eval: "'unsafe-eval'",
131
+ wasm_unsafe_eval: "'wasm-unsafe-eval'",
131
132
  unsafe_hashes: "'unsafe-hashes'",
132
133
  unsafe_inline: "'unsafe-inline'",
133
134
  none: "'none'",
@@ -170,6 +171,8 @@ module ActionDispatch # :nodoc:
170
171
  worker_src: "worker-src"
171
172
  }.freeze
172
173
 
174
+ HASH_SOURCE_ALGORITHM_PREFIXES = ["sha256-", "sha384-", "sha512-"].freeze
175
+
173
176
  DEFAULT_NONCE_DIRECTIVES = %w[script-src style-src].freeze
174
177
 
175
178
  private_constant :MAPPINGS, :DIRECTIVES, :DEFAULT_NONCE_DIRECTIVES
@@ -304,7 +307,13 @@ module ActionDispatch # :nodoc:
304
307
  case source
305
308
  when Symbol
306
309
  apply_mapping(source)
307
- when String, Proc
310
+ when String
311
+ if hash_source?(source)
312
+ "'#{source}'"
313
+ else
314
+ source
315
+ end
316
+ when Proc
308
317
  source
309
318
  else
310
319
  raise ArgumentError, "Invalid content security policy source: #{source.inspect}"
@@ -373,5 +382,9 @@ module ActionDispatch # :nodoc:
373
382
  def nonce_directive?(directive, nonce_directives)
374
383
  nonce_directives.include?(directive)
375
384
  end
385
+
386
+ def hash_source?(source)
387
+ source.start_with?(*HASH_SOURCE_ALGORITHM_PREFIXES)
388
+ end
376
389
  end
377
390
  end
@@ -17,9 +17,11 @@ module ActionDispatch
17
17
  # For more information about filter behavior, see
18
18
  # ActiveSupport::ParameterFilter.
19
19
  module FilterParameters
20
- ENV_MATCH = [/RAW_POST_DATA/, "rack.request.form_vars"] # :nodoc:
21
- NULL_PARAM_FILTER = ActiveSupport::ParameterFilter.new # :nodoc:
22
- NULL_ENV_FILTER = ActiveSupport::ParameterFilter.new ENV_MATCH # :nodoc:
20
+ # :stopdoc:
21
+ ENV_MATCH = [/RAW_POST_DATA/, "rack.request.form_vars"]
22
+ NULL_PARAM_FILTER = ActiveSupport::ParameterFilter.new
23
+ NULL_ENV_FILTER = ActiveSupport::ParameterFilter.new ENV_MATCH
24
+ # :startdoc:
23
25
 
24
26
  def initialize
25
27
  super
@@ -91,7 +91,49 @@ module ActionDispatch
91
91
  end
92
92
  end
93
93
 
94
- # Sets the variant for template.
94
+ # Sets the \variant for the response template.
95
+ #
96
+ # When determining which template to render, Action View will incorporate
97
+ # all variants from the request. For example, if an
98
+ # `ArticlesController#index` action needs to respond to
99
+ # `request.variant = [:ios, :turbo_native]`, it will render the
100
+ # first template file it can find in the following list:
101
+ #
102
+ # - `app/views/articles/index.html+ios.erb`
103
+ # - `app/views/articles/index.html+turbo_native.erb`
104
+ # - `app/views/articles/index.html.erb`
105
+ #
106
+ # Variants add context to the requests that views render appropriately.
107
+ # Variant names are arbitrary, and can communicate anything from the
108
+ # request's platform (`:android`, `:ios`, `:linux`, `:macos`, `:windows`)
109
+ # to its browser (`:chrome`, `:edge`, `:firefox`, `:safari`), to the type
110
+ # of user (`:admin`, `:guest`, `:user`).
111
+ #
112
+ # Note: Adding many new variant templates with similarities to existing
113
+ # template files can make maintaining your view code more difficult.
114
+ #
115
+ # #### Parameters
116
+ #
117
+ # * `variant` - a symbol name or an array of symbol names for variants
118
+ # used to render the response template
119
+ #
120
+ # #### Examples
121
+ #
122
+ # class ApplicationController < ActionController::Base
123
+ # before_action :determine_variants
124
+ #
125
+ # private
126
+ # def determine_variants
127
+ # variants = []
128
+ #
129
+ # # some code to determine the variant(s) to use
130
+ #
131
+ # variants << :ios if request.user_agent.include?("iOS")
132
+ # variants << :turbo_native if request.user_agent.include?("Turbo Native")
133
+ #
134
+ # request.variant = variants
135
+ # end
136
+ # end
95
137
  def variant=(variant)
96
138
  variant = Array(variant)
97
139
 
@@ -102,6 +144,18 @@ module ActionDispatch
102
144
  end
103
145
  end
104
146
 
147
+ # Returns the \variant for the response template as an instance of
148
+ # ActiveSupport::ArrayInquirer.
149
+ #
150
+ # request.variant = :phone
151
+ # request.variant.phone? # => true
152
+ # request.variant.tablet? # => false
153
+ #
154
+ # request.variant = [:phone, :tablet]
155
+ # request.variant.phone? # => true
156
+ # request.variant.desktop? # => false
157
+ # request.variant.any?(:phone, :desktop) # => true
158
+ # request.variant.any?(:desktop, :watch) # => false
105
159
  def variant
106
160
  @variant ||= ActiveSupport::ArrayInquirer.new
107
161
  end
@@ -158,7 +212,17 @@ module ActionDispatch
158
212
  end
159
213
 
160
214
  def should_apply_vary_header?
161
- !params_readable? && use_accept_header && valid_accept_header
215
+ !params_readable?
216
+ end
217
+
218
+ def vary_headers # :nodoc:
219
+ if use_accept_header
220
+ headers = ["Accept", "X-Requested-With"]
221
+ headers.insert(1, "Content-Type") if xhr? && accept.blank?
222
+ headers
223
+ else
224
+ ["X-Requested-With"]
225
+ end
162
226
  end
163
227
 
164
228
  private
@@ -13,6 +13,7 @@ Mime::Type.register "text/calendar", :ics
13
13
  Mime::Type.register "text/csv", :csv
14
14
  Mime::Type.register "text/vcard", :vcf
15
15
  Mime::Type.register "text/vtt", :vtt, %w(vtt)
16
+ Mime::Type.register "text/markdown", :md, [], %w(md markdown)
16
17
 
17
18
  Mime::Type.register "image/png", :png, [], %w(png)
18
19
  Mime::Type.register "image/jpeg", :jpeg, [], %w(jpg jpeg jpe pjpeg)