From 7e94fc1395aa1140ffe5991e2878698928dc3cb4 Mon Sep 17 00:00:00 2001 From: Evan Raffel Date: Sun, 25 Feb 2024 01:46:45 -0500 Subject: [PATCH 1/2] Purge cache_key if response headers do not include content digest --- lib/rack/cache/meta_store.rb | 3 ++- lib/rack/cache/version.rb | 2 +- test/meta_store_test.rb | 13 +++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/rack/cache/meta_store.rb b/lib/rack/cache/meta_store.rb index 7fc63a9..ec37f35 100644 --- a/lib/rack/cache/meta_store.rb +++ b/lib/rack/cache/meta_store.rb @@ -38,7 +38,8 @@ def lookup(request, entity_store) return nil if match.nil? _, res = match - if body = entity_store.open(res['x-content-digest']) + entity_key = res['x-content-digest'] + if entity_key && body = entity_store.open(entity_key) restore_response(res, body) else # the metastore referenced an entity that doesn't exist in diff --git a/lib/rack/cache/version.rb b/lib/rack/cache/version.rb index cff0c2d..c47eb2d 100644 --- a/lib/rack/cache/version.rb +++ b/lib/rack/cache/version.rb @@ -1,5 +1,5 @@ module Rack module Cache - VERSION = '1.15.0' + VERSION = '1.15.1' end end diff --git a/test/meta_store_test.rb b/test/meta_store_test.rb index 91f735d..7369550 100644 --- a/test/meta_store_test.rb +++ b/test/meta_store_test.rb @@ -190,6 +190,19 @@ def self.call(request); request.path_info.reverse end mock.verify end + it 'purges meta store entry when the entry does not contain the digest header' do + cache_key = store_simple_entry + meta_entry = @store.read(cache_key) + meta_entry.select { |e| e.is_a?(Array) }.flatten.each { |h| h.is_a?(Hash) && h.delete('x-content-digest') } + @store.write(cache_key, meta_entry) + mock = Minitest::Mock.new + mock.expect :call, nil, [@store.cache_key(@request)] + @store.stub(:purge, mock) do + @store.lookup(@request, nil) + end + mock.verify + end + it 'warns once if purge is not implemented' do store_simple_entry assert @response.headers['x-content-digest'] From 70282c9eedd182a74df78ea198b24ffe88ab9b4f Mon Sep 17 00:00:00 2001 From: Evan Raffel Date: Mon, 26 Feb 2024 10:54:47 -0500 Subject: [PATCH 2/2] code style fix and revert version change --- lib/rack/cache/version.rb | 2 +- test/meta_store_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rack/cache/version.rb b/lib/rack/cache/version.rb index c47eb2d..cff0c2d 100644 --- a/lib/rack/cache/version.rb +++ b/lib/rack/cache/version.rb @@ -1,5 +1,5 @@ module Rack module Cache - VERSION = '1.15.1' + VERSION = '1.15.0' end end diff --git a/test/meta_store_test.rb b/test/meta_store_test.rb index 7369550..be54434 100644 --- a/test/meta_store_test.rb +++ b/test/meta_store_test.rb @@ -193,7 +193,7 @@ def self.call(request); request.path_info.reverse end it 'purges meta store entry when the entry does not contain the digest header' do cache_key = store_simple_entry meta_entry = @store.read(cache_key) - meta_entry.select { |e| e.is_a?(Array) }.flatten.each { |h| h.is_a?(Hash) && h.delete('x-content-digest') } + meta_entry.grep(Array).flatten.each { |h| h.is_a?(Hash) && h.delete('x-content-digest') } @store.write(cache_key, meta_entry) mock = Minitest::Mock.new mock.expect :call, nil, [@store.cache_key(@request)]