Fix caching logic with regards to Accept-Language, Cookie, and Signature (#24604)
This commit is contained in:
parent
5dc3173ef8
commit
58a1b2e330
12 changed files with 62 additions and 45 deletions
|
@ -163,6 +163,20 @@ module CacheConcern
|
|||
end
|
||||
end
|
||||
|
||||
included do
|
||||
after_action :enforce_cache_control!
|
||||
end
|
||||
|
||||
# Prevents high-entropy headers such as `Cookie`, `Signature` or `Authorization`
|
||||
# from being used as cache keys, while allowing to `Vary` on them (to not serve
|
||||
# anonymous cached data to authenticated requests when authentication matters)
|
||||
def enforce_cache_control!
|
||||
vary = response.headers['Vary']&.split&.map { |x| x.strip.downcase }
|
||||
return unless vary.present? && %w(cookie authorization signature).any? { |header| vary.include?(header) && request.headers[header].present? }
|
||||
|
||||
response.cache_control.replace(private: true, no_store: true)
|
||||
end
|
||||
|
||||
def render_with_cache(**options)
|
||||
raise ArgumentError, 'Only JSON render calls are supported' unless options.key?(:json) || block_given?
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue