dalli 3.2.17 → 4.0.0

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.
@@ -9,9 +9,6 @@ module Dalli
9
9
  # and parsing into local values. Handles errors on unexpected values.
10
10
  ##
11
11
  class ResponseProcessor
12
- MAX_HEADER_BYTES = 8 * 1024
13
- MAX_STATS_ENTRIES = 10_000
14
- MAX_QUIET_RESPONSES = 100_000
15
12
  EN = 'EN'
16
13
  END_TOKEN = 'END'
17
14
  EX = 'EX'
@@ -24,33 +21,29 @@ module Dalli
24
21
  STAT = 'STAT'
25
22
  VA = 'VA'
26
23
  VERSION = 'VERSION'
24
+ SERVER_ERROR = 'SERVER_ERROR'
27
25
 
28
26
  def initialize(io_source, value_marshaller)
29
27
  @io_source = io_source
30
28
  @value_marshaller = value_marshaller
31
29
  end
32
30
 
33
- # raw: true returns the value as stored, without deserializing or
34
- # decompressing it, whatever flags it was stored with
35
- def meta_get_with_value(cache_nils: false, raw: false)
31
+ def meta_get_with_value(cache_nils: false)
36
32
  tokens = error_on_unexpected!([VA, EN, HD])
37
33
  return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN
38
34
  return true unless tokens.first == VA
39
35
 
40
- value = read_data(tokens[1].to_i)
41
- raw ? value : @value_marshaller.retrieve(value, bitflags_from_tokens(tokens))
36
+ @value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens))
42
37
  end
43
38
 
44
- # raw: true returns the value as stored; see #meta_get_with_value
45
- def meta_get_with_value_and_cas(raw: false)
39
+ def meta_get_with_value_and_cas
46
40
  tokens = error_on_unexpected!([VA, EN, HD])
47
41
  return [nil, 0] if tokens.first == EN
48
42
 
49
43
  cas = cas_from_tokens(tokens)
50
44
  return [nil, cas] unless tokens.first == VA
51
45
 
52
- value = read_data(tokens[1].to_i)
53
- [raw ? value : @value_marshaller.retrieve(value, bitflags_from_tokens(tokens)), cas]
46
+ [@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens)), cas]
54
47
  end
55
48
 
56
49
  def meta_get_without_value
@@ -88,11 +81,7 @@ module Dalli
88
81
  def stats
89
82
  tokens = error_on_unexpected!([END_TOKEN, STAT])
90
83
  values = {}
91
- replies = 0
92
84
  while tokens.first != END_TOKEN
93
- replies += 1
94
- raise Dalli::DalliError, 'Too many stats replies' if replies > MAX_STATS_ENTRIES
95
-
96
85
  values[tokens[1]] = tokens[2]
97
86
  tokens = next_line_to_tokens
98
87
  end
@@ -116,16 +105,10 @@ module Dalli
116
105
  tokens.last
117
106
  end
118
107
 
119
- def consume_all_responses_until_mn(max_responses = MAX_QUIET_RESPONSES)
108
+ def consume_all_responses_until_mn
120
109
  tokens = next_line_to_tokens
121
110
 
122
- replies = 0
123
- while tokens.first != MN
124
- replies += 1
125
- raise Dalli::DalliError, 'Too many quiet replies' if replies > max_responses
126
-
127
- tokens = next_line_to_tokens
128
- end
111
+ tokens = next_line_to_tokens while tokens.first != MN
129
112
  true
130
113
  end
131
114
 
@@ -138,10 +121,6 @@ module Dalli
138
121
  end
139
122
 
140
123
  def full_response_from_buffer(tokens, body, resp_size)
141
- # A hit with no key can't be matched to a request: skip it (false
142
- # status) rather than yield it, or take it for the end of the pipeline
143
- return [resp_size, false, nil, nil, nil] unless tokens.any? { |t| t.start_with?('k') }
144
-
145
124
  value = @value_marshaller.retrieve(body, bitflags_from_tokens(tokens))
146
125
  [resp_size, tokens.first == VA, cas_from_tokens(tokens), key_from_tokens(tokens), value]
147
126
  end
@@ -162,60 +141,25 @@ module Dalli
162
141
 
163
142
  tokens, header_len, body_len = tokens_from_header_buffer(buf)
164
143
 
165
- # A complete response with no body. Only the terminating noop's MN
166
- # ends the pipeline. Any other bodyless reply (CLIENT_ERROR,
167
- # SERVER_ERROR, ...) answers one key's request: report it with a
168
- # false status so the caller skips it and keeps reading. Treating it
169
- # as the end would leave the remaining replies on the connection for
170
- # later commands to read as their own. A hit on an empty value (VA 0)
171
- # still has a body -- just its terminator -- so it's parsed below.
172
- return [header_len, tokens.first == MN, nil, nil, nil] if no_body?(tokens, body_len)
144
+ # We have a complete response that has no body.
145
+ # This is either the response to the terminating
146
+ # noop or, if the status is not MN, an intermediate
147
+ # error response that needs to be discarded.
148
+ return [header_len, true, nil, nil, nil] if body_len.zero?
173
149
 
174
150
  resp_size = header_len + body_len + TERMINATOR.length
175
151
  # The header is in the buffer, but the body is not. As we don't have
176
152
  # a complete response, don't advance the buffer
177
153
  return [0, nil, nil, nil, nil] unless buf.bytesize >= resp_size
178
154
 
179
- term = header_len + body_len
180
- raise_bad_value_terminator unless buf.getbyte(term) == 13 && buf.getbyte(term + 1) == 10
181
-
182
155
  # The full response is in our buffer, so parse it and return
183
156
  # the values
184
157
  body = buf.slice(header_len, body_len)
185
158
  full_response_from_buffer(tokens, body, resp_size)
186
159
  end
187
160
 
188
- # memcached can't store an item larger than 1 GiB (its -I maximum). A
189
- # pipelined reply claiming an impossible size would otherwise have the
190
- # buffer wait for (and accumulate) that many bytes.
191
- MAX_VALUE_BYTES = 1024 * 1024 * 1024
192
-
193
- # The size given right after "VA", or 0 when it isn't a number
194
- def positional_size(tokens)
195
- tokens.first == VA && tokens[1]&.match?(/\A\d+\z/) ? tokens[1].to_i : 0
196
- end
197
-
198
- def check_value_size!(size)
199
- limit = @value_marshaller.max_response_bytes
200
- return if size >= 0 && size <= MAX_VALUE_BYTES && size <= limit
201
-
202
- raise Dalli::DalliError, "Reply value size #{size} is out of range"
203
- end
204
-
205
- # A zero-size reply has no body unless it's a VA (a hit on an empty value)
206
- def no_body?(tokens, body_len)
207
- body_len.zero? && tokens.first != VA
208
- end
209
-
210
161
  def contains_header?(buf)
211
- return false unless buf
212
-
213
- term_idx = buf.index(TERMINATOR)
214
- if (term_idx || buf.bytesize) + TERMINATOR.bytesize > MAX_HEADER_BYTES
215
- raise Dalli::DalliError, 'Reply header exceeds 8 KiB'
216
- end
217
-
218
- !term_idx.nil?
162
+ buf.include?(TERMINATOR)
219
163
  end
220
164
 
221
165
  def header_from_buffer(buf)
@@ -224,9 +168,12 @@ module Dalli
224
168
 
225
169
  def error_on_unexpected!(expected_codes)
226
170
  tokens = next_line_to_tokens
227
- raise Dalli::DalliError, "Response error: #{tokens.first}" unless expected_codes.include?(tokens.first)
228
171
 
229
- tokens
172
+ return tokens if expected_codes.include?(tokens.first)
173
+
174
+ raise Dalli::ServerError, tokens.join(' ').to_s if tokens.first == SERVER_ERROR
175
+
176
+ raise Dalli::DalliError, "Response error: #{tokens.first}"
230
177
  end
231
178
 
232
179
  def bitflags_from_tokens(tokens)
@@ -244,11 +191,7 @@ module Dalli
244
191
  end
245
192
 
246
193
  def body_len_from_tokens(tokens)
247
- # memcached also gives the size positionally ("VA <size> ..."); use it
248
- # when there's no s flag rather than reading the value as replies
249
- size = tokens.any? { |t| t.start_with?('s') } ? value_from_tokens(tokens, 's')&.to_i : positional_size(tokens)
250
- check_value_size!(size) if size
251
- size
194
+ value_from_tokens(tokens, 's')&.to_i
252
195
  end
253
196
 
254
197
  def value_from_tokens(tokens, flag)
@@ -267,21 +210,8 @@ module Dalli
267
210
  line&.split || []
268
211
  end
269
212
 
270
- # Values are read by the size in the VA line, not up to the next CRLF:
271
- # a value can contain CRLF and can be longer than MAX_LINE_BYTES
272
- def strip_value_terminator!(data)
273
- data&.chomp!(TERMINATOR) || raise_bad_value_terminator
274
- end
275
-
276
- def raise_bad_value_terminator
277
- raise Dalli::DalliError, "Reply value isn't followed by CRLF, so its size doesn't match the value"
278
- end
279
-
280
213
  def read_data(data_size)
281
- # Checked before the terminator is added: -1 or -2 would otherwise
282
- # read 1 or 0 bytes and leave the connection out of step
283
- check_value_size!(data_size)
284
- strip_value_terminator!(@io_source.read(data_size + TERMINATOR.bytesize))
214
+ @io_source.read(data_size + TERMINATOR.bytesize)&.chomp!(TERMINATOR)
285
215
  end
286
216
  end
287
217
  end
@@ -13,7 +13,6 @@ module Dalli
13
13
  ##
14
14
  class Meta < Base
15
15
  TERMINATOR = "\r\n"
16
- COUNTER_ADD_OPTIONS = { raw: true }.freeze # counters are stored raw
17
16
 
18
17
  def response_processor
19
18
  @response_processor ||= ResponseProcessor.new(@connection_manager, @value_marshaller)
@@ -28,7 +27,7 @@ module Dalli
28
27
  encoded_key, base64 = KeyRegularizer.encode(key)
29
28
  req = RequestFormatter.meta_get(key: encoded_key, base64: base64)
30
29
  write(req)
31
- response_processor.meta_get_with_value(cache_nils: cache_nils?(options), raw: raw_request?(options))
30
+ response_processor.meta_get_with_value(cache_nils: cache_nils?(options))
32
31
  end
33
32
 
34
33
  def quiet_get_request(key)
@@ -41,7 +40,7 @@ module Dalli
41
40
  encoded_key, base64 = KeyRegularizer.encode(key)
42
41
  req = RequestFormatter.meta_get(key: encoded_key, ttl: ttl, base64: base64)
43
42
  write(req)
44
- response_processor.meta_get_with_value(cache_nils: cache_nils?(options), raw: raw_request?(options))
43
+ response_processor.meta_get_with_value(cache_nils: cache_nils?(options))
45
44
  end
46
45
 
47
46
  def touch(key, ttl)
@@ -54,11 +53,11 @@ module Dalli
54
53
 
55
54
  # TODO: This is confusing, as there's a cas command in memcached
56
55
  # and this isn't it. Maybe rename? Maybe eliminate?
57
- def cas(key, options = nil)
56
+ def cas(key)
58
57
  encoded_key, base64 = KeyRegularizer.encode(key)
59
58
  req = RequestFormatter.meta_get(key: encoded_key, value: true, return_cas: true, base64: base64)
60
59
  write(req)
61
- response_processor.meta_get_with_value_and_cas(raw: raw_request?(options))
60
+ response_processor.meta_get_with_value_and_cas
62
61
  end
63
62
 
64
63
  # Storage Commands
@@ -86,6 +85,8 @@ module Dalli
86
85
  bitflags: bitflags, cas: cas,
87
86
  ttl: ttl, mode: mode, quiet: quiet?, base64: base64)
88
87
  write(req)
88
+ write(value)
89
+ write(TERMINATOR)
89
90
  end
90
91
  # rubocop:enable Metrics/ParameterLists
91
92
 
@@ -106,6 +107,8 @@ module Dalli
106
107
  req = RequestFormatter.meta_set(key: encoded_key, value: value, base64: base64,
107
108
  cas: cas, ttl: ttl, mode: mode, quiet: quiet?)
108
109
  write(req)
110
+ write(value)
111
+ write(TERMINATOR)
109
112
  end
110
113
  # rubocop:enable Metrics/ParameterLists
111
114
 
@@ -130,42 +133,11 @@ module Dalli
130
133
  def decr_incr(incr, key, delta, ttl, initial)
131
134
  ttl = initial ? TtlSanitizer.sanitize(ttl) : nil # Only set a TTL if we want to set a value on miss
132
135
  encoded_key, base64 = KeyRegularizer.encode(key)
133
- # memcached stores an item created by ma's auto-create (N/J) for a
134
- # base64-encoded key without its binary-key flag, and later replies
135
- # name it with the raw key bytes. Create such counters with an add.
136
- return decr_incr_with_add(incr, key, delta, ttl, initial) if initial && base64
137
-
138
136
  write(RequestFormatter.meta_arithmetic(key: encoded_key, delta: delta, initial: initial, incr: incr, ttl: ttl,
139
137
  quiet: quiet?, base64: base64))
140
138
  response_processor.decr_incr unless quiet?
141
139
  end
142
140
 
143
- # Like decr_incr with an initial value, without memcached's auto-create:
144
- # on a miss the counter is added with the initial value (which is
145
- # returned, as auto-create would), and the add losing a race to another
146
- # client means the counter now exists, so the incr is tried again. In a
147
- # quiet block both requests are sent and whichever applies takes effect.
148
- def decr_incr_with_add(incr, key, delta, ttl, initial)
149
- return quiet_decr_incr_with_add(incr, key, delta, ttl, initial) if quiet?
150
-
151
- 2.times do
152
- value = decr_incr(incr, key, delta, nil, nil)
153
- return value unless value.nil?
154
-
155
- write_storage_req(:add, key, initial.to_s, ttl, nil, COUNTER_ADD_OPTIONS)
156
- return initial if response_processor.meta_set_with_cas
157
- end
158
- decr_incr(incr, key, delta, nil, nil)
159
- end
160
-
161
- def quiet_decr_incr_with_add(incr, key, delta, ttl, initial)
162
- encoded_key, base64 = KeyRegularizer.encode(key)
163
- write(RequestFormatter.meta_arithmetic(key: encoded_key, delta: delta, initial: nil, incr: incr, ttl: nil,
164
- quiet: true, base64: base64))
165
- # Sent with the rest of the quiet block, by its closing noop
166
- write_storage_req(:add, key, initial.to_s, ttl, nil, COUNTER_ADD_OPTIONS)
167
- end
168
-
169
141
  # Other Commands
170
142
  def flush(delay = 0)
171
143
  write(RequestFormatter.flush(delay: delay))
@@ -176,10 +148,7 @@ module Dalli
176
148
  # We need to read all the responses at once.
177
149
  def noop
178
150
  write_noop
179
- result = response_processor.consume_all_responses_until_mn
180
- # Everything up to and including the MN has been read
181
- @connection_manager.quiet_responses_pending = false
182
- result
151
+ response_processor.consume_all_responses_until_mn
183
152
  end
184
153
 
185
154
  def stats(info = nil)
@@ -16,7 +16,7 @@ module Dalli
16
16
  # can limit character set to LDH + '.'. Hex digit section
17
17
  # is there to support IPv6 addresses, which need to be specified with
18
18
  # a bounding []
19
- SERVER_CONFIG_REGEXP = /\A(\[([\h:]+)\]|[^:]+)(?::(\d+))?(?::(\d+))?\z/.freeze
19
+ SERVER_CONFIG_REGEXP = /\A(\[([\h:]+)\]|[^:]+)(?::(\d+))?(?::(\d+))?\z/
20
20
 
21
21
  DEFAULT_PORT = 11_211
22
22
  DEFAULT_WEIGHT = 1
@@ -31,7 +31,7 @@ module Dalli
31
31
  return ttl_as_i if ttl_as_i > now # Already a timestamp
32
32
 
33
33
  Dalli.logger.debug "Expiration interval (#{ttl_as_i}) too long for Memcached " \
34
- 'and too short to be a future timestamp,' \
34
+ 'and too short to be a future timestamp, ' \
35
35
  'converting to an expiration timestamp'
36
36
  now + ttl_as_i
37
37
  end
@@ -15,33 +15,18 @@ module Dalli
15
15
  compress: true,
16
16
  compressor: ::Dalli::Compressor,
17
17
  # min byte size to attempt compression
18
- compression_min_size: 4 * 1024, # 4K
19
- # max size a stored value may decompress to (nil: no limit). Guards
20
- # against a small compressed value expanding into gigabytes on read.
21
- decompressed_max_bytes: 128 * 1024 * 1024 # 128 MiB
18
+ compression_min_size: 4 * 1024 # 4K
22
19
  }.freeze
23
20
 
24
21
  OPTIONS = DEFAULTS.keys.freeze
25
22
 
26
- KEYWORD_PARAMETER_TYPES = %i[key keyreq].freeze
27
- private_constant :KEYWORD_PARAMETER_TYPES
28
-
29
23
  # https://www.hjp.at/zettel/m/memcached_flags.rxml
30
24
  # Looks like most clients use bit 1 to indicate gzip compression.
31
25
  FLAG_COMPRESSED = 0x2
32
26
 
33
27
  def initialize(client_options)
34
- # Support the deprecated compression option, but don't allow it to override
35
- # an explicit compress
36
- # Remove this with 4.0
37
- if client_options.key?(:compression) && !client_options.key?(:compress)
38
- Dalli.logger.warn "DEPRECATED: Dalli's :compression option is now just 'compress: true'. " \
39
- 'Please update your configuration.'
40
- client_options[:compress] = client_options.delete(:compression)
41
- end
42
-
43
28
  @compression_options =
44
- DEFAULTS.merge(client_options.select { |k, _| OPTIONS.include?(k) })
29
+ DEFAULTS.merge(client_options.slice(*OPTIONS))
45
30
  end
46
31
 
47
32
  def store(value, req_options, bitflags)
@@ -53,16 +38,8 @@ module Dalli
53
38
  end
54
39
 
55
40
  def retrieve(value, bitflags)
56
- return value unless bitflags.anybits?(FLAG_COMPRESSED)
57
-
58
- max_bytes = @compression_options[:decompressed_max_bytes]
59
- # Custom compressors that only define decompress(data) keep working,
60
- # without the limit
61
- if max_bytes && compressor_accepts_max_bytes?
62
- compressor.decompress(value, max_bytes: max_bytes)
63
- else
64
- compressor.decompress(value)
65
- end
41
+ compressed = bitflags.anybits?(FLAG_COMPRESSED)
42
+ compressed ? compressor.decompress(value) : value
66
43
 
67
44
  # TODO: We likely want to move this rescue into the Dalli::Compressor / Dalli::GzipCompressor
68
45
  # itself, since not all compressors necessarily use Zlib. For now keep it here, so the behavior
@@ -71,19 +48,6 @@ module Dalli
71
48
  raise UnmarshalError, "Unable to uncompress value: #{$ERROR_INFO.message}"
72
49
  end
73
50
 
74
- def compressor_accepts_max_bytes?
75
- return @compressor_accepts_max_bytes if defined?(@compressor_accepts_max_bytes)
76
-
77
- @compressor_accepts_max_bytes =
78
- begin
79
- compressor.method(:decompress).parameters.any? do |type, name|
80
- name == :max_bytes && KEYWORD_PARAMETER_TYPES.include?(type)
81
- end
82
- rescue NameError # an object without a reflectable decompress method
83
- false
84
- end
85
- end
86
-
87
51
  def compress_by_default?
88
52
  @compression_options[:compress]
89
53
  end
@@ -97,13 +61,11 @@ module Dalli
97
61
  end
98
62
 
99
63
  # Checks whether we should apply compression when serializing a value
100
- # based on the specified options. Returns false for a raw request
101
- # (raw reads ignore bitflags, so the value must be stored as given) or
102
- # unless the value is greater than the minimum compression size.
103
- # Otherwise returns based on a method-level option if specified,
104
- # falling back to the server default.
64
+ # based on the specified options. Returns false unless the value
65
+ # is greater than the minimum compression size. Otherwise returns
66
+ # based on a method-level option if specified, falling back to the
67
+ # server default.
105
68
  def compress_value?(value, req_options)
106
- return false if req_options&.dig(:raw)
107
69
  return false unless value.bytesize >= compression_min_size
108
70
  return compress_by_default? unless req_options && !req_options[:compress].nil?
109
71
 
@@ -27,9 +27,7 @@ module Dalli
27
27
  @value_compressor = ValueCompressor.new(client_options)
28
28
 
29
29
  @marshal_options =
30
- DEFAULTS.merge(client_options.select { |k, _| OPTIONS.include?(k) })
31
- @max_response_bytes = Integer(client_options.fetch(:max_response_bytes, value_max_bytes))
32
- raise ArgumentError, 'max_response_bytes must be positive' unless @max_response_bytes.positive?
30
+ DEFAULTS.merge(client_options.slice(*OPTIONS))
33
31
  end
34
32
 
35
33
  def store(key, value, options = nil)
@@ -50,8 +48,6 @@ module Dalli
50
48
  @marshal_options[:value_max_bytes]
51
49
  end
52
50
 
53
- attr_reader :max_response_bytes
54
-
55
51
  def error_if_over_max_value_bytes(key, value)
56
52
  return if value.bytesize <= value_max_bytes
57
53
 
@@ -18,57 +18,53 @@ module Dalli
18
18
  # https://www.hjp.at/zettel/m/memcached_flags.rxml
19
19
  # Looks like most clients use bit 0 to indicate native language serialization
20
20
  FLAG_SERIALIZED = 0x1
21
+ FLAG_UTF8 = 0x2
22
+
23
+ # Class variable to track whether the Marshal warning has been logged
24
+ @@marshal_warning_logged = false # rubocop:disable Style/ClassVars
21
25
 
22
26
  attr_accessor :serialization_options
23
27
 
24
28
  def initialize(protocol_options)
25
29
  @serialization_options =
26
- DEFAULTS.merge(protocol_options.select { |k, _| OPTIONS.include?(k) })
30
+ DEFAULTS.merge(protocol_options.slice(*OPTIONS))
31
+ warn_if_marshal_default(protocol_options) unless protocol_options[:silence_marshal_warning]
27
32
  end
28
33
 
29
34
  def store(value, req_options, bitflags)
30
- do_serialize = !(req_options && req_options[:raw])
31
- store_value = do_serialize ? serialize_value(value) : value.to_s
32
- bitflags |= FLAG_SERIALIZED if do_serialize
33
- [store_value, bitflags]
35
+ if req_options
36
+ return [value.to_s, bitflags] if req_options[:raw]
37
+
38
+ # If the value is a simple string, going through serialization is costly
39
+ # for no benefit other than preserving encoding.
40
+ # Assuming most strings are either UTF-8 or BINARY we can just store
41
+ # that information in the bitflags.
42
+ if req_options[:string_fastpath] && value.instance_of?(String)
43
+ case value.encoding
44
+ when Encoding::BINARY
45
+ return [value, bitflags]
46
+ when Encoding::UTF_8
47
+ return [value, bitflags | FLAG_UTF8]
48
+ end
49
+ end
50
+ end
51
+
52
+ [serialize_value(value), bitflags | FLAG_SERIALIZED]
34
53
  end
35
54
 
36
- # TODO: Some of these error messages need to be validated. It's not obvious
37
- # that all of them are actually generated by the invoked code
38
- # in current systems
39
- # rubocop:disable Layout/LineLength
40
- TYPE_ERR_REGEXP = %r{needs to have method `_load'|exception class/object expected|instance of IO needed|incompatible marshal file format}.freeze
41
- ARGUMENT_ERR_REGEXP = /undefined class|marshal data too short/.freeze
42
- NAME_ERR_STR = 'uninitialized constant'
43
- # rubocop:enable Layout/LineLength
44
-
45
55
  def retrieve(value, bitflags)
46
- serialized = (bitflags & FLAG_SERIALIZED) != 0
47
- serialized ? serializer.load(value) : value
48
- rescue TypeError => e
49
- filter_type_error(e)
50
- rescue ArgumentError => e
51
- filter_argument_error(e)
52
- rescue NameError => e
53
- filter_name_error(e)
54
- end
55
-
56
- def filter_type_error(err)
57
- raise err unless TYPE_ERR_REGEXP.match?(err.message)
58
-
59
- raise UnmarshalError, "Unable to unmarshal value: #{err.message}"
60
- end
61
-
62
- def filter_argument_error(err)
63
- raise err unless ARGUMENT_ERR_REGEXP.match?(err.message)
64
-
65
- raise UnmarshalError, "Unable to unmarshal value: #{err.message}"
66
- end
67
-
68
- def filter_name_error(err)
69
- raise err unless err.message.include?(NAME_ERR_STR)
70
-
71
- raise UnmarshalError, "Unable to unmarshal value: #{err.message}"
56
+ serialized = bitflags.anybits?(FLAG_SERIALIZED)
57
+ if serialized
58
+ begin
59
+ serializer.load(value)
60
+ rescue StandardError
61
+ raise UnmarshalError, 'Unable to unmarshal value'
62
+ end
63
+ elsif bitflags.anybits?(FLAG_UTF8)
64
+ value.force_encoding(Encoding::UTF_8)
65
+ else
66
+ value
67
+ end
72
68
  end
73
69
 
74
70
  def serializer
@@ -86,6 +82,19 @@ module Dalli
86
82
  exc.set_backtrace e.backtrace
87
83
  raise exc
88
84
  end
85
+
86
+ private
87
+
88
+ def warn_if_marshal_default(protocol_options)
89
+ return if protocol_options.key?(:serializer)
90
+ return if @@marshal_warning_logged
91
+
92
+ Dalli.logger.warn 'SECURITY WARNING: Dalli is using Marshal for serialization. ' \
93
+ 'Marshal can execute arbitrary code during deserialization. ' \
94
+ 'If your memcached server could be compromised, consider using ' \
95
+ 'a safer serializer like JSON: Dalli::Client.new(servers, serializer: JSON)'
96
+ @@marshal_warning_logged = true # rubocop:disable Style/ClassVars
97
+ end
89
98
  end
90
99
  end
91
100
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'openssl'
4
3
  require 'timeout'
5
4
 
6
5
  module Dalli
@@ -16,11 +15,5 @@ module Dalli
16
15
  else
17
16
  [Timeout::Error]
18
17
  end
19
-
20
- # SSL errors during the TLS handshake count as a failed request: they
21
- # trigger reconnection and, after socket_max_failures, mark the server
22
- # down. openssl is required above because this file loads before
23
- # dalli/socket, so OpenSSL may not be defined yet.
24
- SSL_ERRORS = [OpenSSL::SSL::SSLError].freeze
25
18
  end
26
19
  end