Skip to content

Commit 0a7ce52

Browse files
dylanahsmithtenderlove
authored andcommitted
activesupport: Avoid Marshal.load on raw cache value in MemCacheStore
Dalli is already being used for marshalling, so we should also rely on it for unmarshalling. Since Dalli tags the cache value as marshalled it can avoid unmarshalling a raw string which might have come from an untrusted source. [CVE-2020-8165]
1 parent b3230c5 commit 0a7ce52

2 files changed

Lines changed: 4 additions & 14 deletions

File tree

activesupport/lib/active_support/cache/mem_cache_store.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
raise e
88
end
99

10-
require "active_support/core_ext/marshal"
1110
require "active_support/core_ext/array/extract_options"
1211

1312
module ActiveSupport
@@ -28,14 +27,6 @@ class MemCacheStore < Store
2827
# Provide support for raw values in the local cache strategy.
2928
module LocalCacheWithRaw # :nodoc:
3029
private
31-
def read_entry(key, **options)
32-
entry = super
33-
if options[:raw] && local_cache && entry
34-
entry = deserialize_entry(entry.value)
35-
end
36-
entry
37-
end
38-
3930
def write_entry(key, entry, **options)
4031
if options[:raw] && local_cache
4132
raw_entry = Entry.new(entry.value.to_s)
@@ -194,9 +185,8 @@ def normalize_key(key, options)
194185
key
195186
end
196187

197-
def deserialize_entry(raw_value)
198-
if raw_value
199-
entry = Marshal.load(raw_value) rescue raw_value
188+
def deserialize_entry(entry)
189+
if entry
200190
entry.is_a?(Entry) ? entry : Entry.new(entry)
201191
end
202192
end

activesupport/test/cache/stores/mem_cache_store_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_raw_values_with_marshal
6767
cache = ActiveSupport::Cache.lookup_store(*store, raw: true)
6868
cache.clear
6969
cache.write("foo", Marshal.dump([]))
70-
assert_equal [], cache.read("foo")
70+
assert_equal Marshal.dump([]), cache.read("foo")
7171
end
7272

7373
def test_local_cache_raw_values
@@ -100,7 +100,7 @@ def test_local_cache_raw_values_with_marshal
100100
cache.clear
101101
cache.with_local_cache do
102102
cache.write("foo", Marshal.dump([]))
103-
assert_equal [], cache.read("foo")
103+
assert_equal Marshal.dump([]), cache.read("foo")
104104
end
105105
end
106106

0 commit comments

Comments
 (0)