Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relaxed 304 If-None-Match test to support weak key #65

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/response_bank/response_cache_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ def serving_from_noncurrent_but_recent_version_acceptable?
end

def serve_from_browser_cache(cache_key_hash)
if @env["HTTP_IF_NONE_MATCH"] == cache_key_hash
# Support for Etag variations including:
# If-None-Match: abc
# If-None-Match: "abc"
# If-None-Match: W/"abc"
# If-None-Match: "abc", "def"
if !@env["HTTP_IF_NONE_MATCH"].nil? && @env["HTTP_IF_NONE_MATCH"].include?(cache_key_hash)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that matching way too lose?

cache_key_hash = "b"

If-None-Match: W/"abc" shouldn't match IMO.

I think this method need actual parsing of the header, String#include? doesn't.

@env['cacheable.miss'] = false
@env['cacheable.store'] = 'client'

Expand Down
12 changes: 12 additions & 0 deletions test/response_cache_handler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ def test_client_cache_hit
assert_env(false, 'client')
end

def test_client_cache_hit_quoted
controller.request.env['HTTP_IF_NONE_MATCH'] = "\"#{handler.versioned_key_hash}\""
handler.run!
assert_env(false, 'client')
end

def test_client_cache_hit_weak
controller.request.env['HTTP_IF_NONE_MATCH'] = "W/\"#{handler.versioned_key_hash}\""
handler.run!
assert_env(false, 'client')
end

def test_server_cache_hit
controller.request.env['gzip'] = false
@cache_store.expects(:read).with(handler.versioned_key_hash, raw: true).returns(page_serialized)
Expand Down