dalli 5.0.10 → 5.0.11

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64bc18f470e5b97d87b6531a968a9252c4ff46ba715d927c74b66e52f7549683
4
- data.tar.gz: 70fc9db7148b82932b9f3323ac74a2c59dd7ed8b72bd9543dc160c5026d33880
3
+ metadata.gz: 43164df9e3e36d747346b72c44ebe1a7a3be0dee501c83b2ef813a39d14d9bb9
4
+ data.tar.gz: e8e1ecbf3d019891da3935c2d0c805f09bfc8a9746df14d9a6954f8cdecfdc10
5
5
  SHA512:
6
- metadata.gz: a2c3def53ce7c5c4bf4d929bc45c5d1ff7f000db6b59abea3ac9ef3eb81e7f706ab8d0b01021b7002ff2b2153014b17c018c5060935e022f867b4981318afc06
7
- data.tar.gz: f63a580cc08950f6da91213f0c4b9ecd2cb75961fe4467e06c11ecb434d2b13ff5950fceb31d43f8b8c42d8997fec3fd1bd40eebc1aa59dda41b2c1092a5e253
6
+ metadata.gz: e5b73ad3b6a075030a75f7f7903a097611cfd862f65a80a549d11f1c34c900205ac4ed4783392be4ee139c598b6de346ddaf782460c0ec11e8fc31847b4cb4aa
7
+ data.tar.gz: 5978a35d174e82c4cdc1dafdd439395f64035517f5a4592ee0ad1724e6b9b4241551c5b5990e6a6823d433e68604697bb43940e8bf3b82b15952325264e8b446
data/CHANGELOG.md CHANGED
@@ -4,6 +4,17 @@ Dalli Changelog
4
4
  Unreleased
5
5
  ==========
6
6
 
7
+ 5.0.11
8
+ ==========
9
+
10
+ Security:
11
+
12
+ - Don't rely on memcached's auto-create for keys that need base64 encoding (GHSA-r47x-6p6r-mvcp)
13
+ - memcached stores an item made by auto-create (`ma` with `N`/`J`, or `mg` with `N`) for a base64-encoded key without its binary-key flag, and later replies name the item by its raw key bytes. A key containing spaces or CR/LF could then make a multi-get read the replies after it as different ones
14
+ - `incr` and `decr` with an initial value now create such counters with an add instead. Return values are unchanged
15
+ - The new `verify_reply_keys` option makes `get_multi` and `get_multi_cas` ignore replies for keys they didn't request, which guards against items created by older Dalli versions or other clients until memcached is upgraded. It's off by default, since it costs a multi-get about 15%
16
+ - Affects the meta protocol since 3.2.0; fixed in 5.2.3, 5.1.5, 5.0.11, 4.3.8 and 3.2.14. memcached 1.6.45 and earlier still name these items by their raw key bytes
17
+
7
18
  5.0.10
8
19
  ==========
9
20
 
data/README.md CHANGED
@@ -58,6 +58,10 @@ Per-request `raw: true` returns the stored bytes without deserializing or decomp
58
58
 
59
59
  See the [5.0-Upgrade.md](5.0-Upgrade.md) guide for upgrade information.
60
60
 
61
+ ### Counters and verify_reply_keys
62
+
63
+ memcached names an item made by auto-create for a base64-encoded key (one with whitespace, control or non-ASCII characters) by its raw key bytes in later replies. Since 5.0.11, Dalli creates counters for such keys with an add rather than auto-create. Items created before then, or by other clients, can still be named that way until memcached releases a fix (1.6.45 and earlier behave this way). If keys passed to `incr` or `decr` with an initial value can contain untrusted input, set `verify_reply_keys: true` until then: multi-gets then ignore replies for keys they didn't request, at a cost of about 15% per multi-get.
64
+
61
65
  ## OpenTelemetry Tracing
62
66
 
63
67
  Dalli automatically instruments operations with [OpenTelemetry](https://opentelemetry.io/) when the SDK is present. No configuration is required - just add the OpenTelemetry gems to your application:
data/lib/dalli/client.rb CHANGED
@@ -50,6 +50,11 @@ module Dalli
50
50
  # +:include+ logs the full operation and key(s), +:obfuscate+ replaces keys with "?",
51
51
  # +nil+ (default) omits the attribute entirely.
52
52
  # - :otel_peer_service - when set, adds a +peer.service+ span attribute with this value for logical service naming.
53
+ # - :verify_reply_keys - defaults to false. When true, get_multi, get_multi_cas and get_multi_with_metadata only
54
+ # accept replies for the keys they requested; after a reply naming another key, the rest of
55
+ # that server's replies are treated as misses. memcached can name an item by its raw key
56
+ # bytes when the item was created by auto-create from a base64-encoded key, which Dalli no
57
+ # longer does. Costs a multi-get about 15%.
53
58
  #
54
59
  def initialize(servers = nil, options = {})
55
60
  @normalized_servers = ::Dalli::ServersArgNormalizer.normalize_servers(servers)
@@ -572,6 +577,9 @@ module Dalli
572
577
  result
573
578
  rescue Dalli::NetworkError
574
579
  {}
580
+ rescue Dalli::UnexpectedReplyKeyError => e
581
+ Dalli.logger.warn { e.message }
582
+ {}
575
583
  end
576
584
 
577
585
  def single_server_set_multi(hash, ttl, req_options)
@@ -25,6 +25,7 @@ module Dalli
25
25
  warn_uri_credentials(user_creds)
26
26
  @options = client_options.merge(user_creds)
27
27
  @raw_mode = client_options[:raw]
28
+ @verify_reply_keys = client_options[:verify_reply_keys] ? true : false
28
29
  @value_marshaller = @raw_mode ? StringMarshaller.new(@options) : ValueMarshaller.new(@options)
29
30
  @connection_manager = ConnectionManager.new(hostname, port, socket_type, @options)
30
31
  end
@@ -118,6 +119,8 @@ module Dalli
118
119
  # avoiding intermediate Hash allocation. Returns nil.
119
120
  # Without a block, returns a Hash of { key => [value, cas] }.
120
121
  def pipeline_next_responses(&block)
122
+ return abandon_pipeline if @pipeline_out_of_step
123
+
121
124
  reconnect_on_pipeline_complete!
122
125
  values = nil
123
126
 
@@ -139,6 +142,13 @@ module Dalli
139
142
  # to the noop at the end of the pipeline
140
143
  finish_pipeline && break if status && key.nil?
141
144
 
145
+ # A key that wasn't requested means the replies are out of step
146
+ # (see expect_reply_keys): keep what was read so far, drop the rest
147
+ if unexpected_reply_key?(key)
148
+ abandon_pipeline
149
+ break
150
+ end
151
+
142
152
  # If the status is ok and the key is not nil, then this is a
143
153
  # getkq response with a value that we want to set in the response hash
144
154
  unless key.nil?
@@ -265,6 +275,7 @@ module Dalli
265
275
  # Use clear (not reset) to keep pipeline_complete? = true, which is
266
276
  # the expected state before pipeline_response_setup is called.
267
277
  response_buffer.clear
278
+ expect_reply_keys(keys)
268
279
 
269
280
  req = +''
270
281
  keys.each do |key|
@@ -280,6 +291,7 @@ module Dalli
280
291
  def pipelined_get_interleaved(keys, chunk_size, results)
281
292
  # Initialize the response buffer for draining during send phase
282
293
  response_buffer.ensure_ready
294
+ expect_reply_keys(keys)
283
295
 
284
296
  keys.each_slice(chunk_size) do |chunk|
285
297
  # Build and write this chunk of requests
@@ -312,6 +324,12 @@ module Dalli
312
324
  status, cas, key, value = response_buffer.process_single_getk_response
313
325
  break if status.nil? # No complete response available
314
326
 
327
+ # Handled by the next pipeline_next_responses, once sending is done
328
+ if unexpected_reply_key?(key)
329
+ @pipeline_out_of_step = true
330
+ break
331
+ end
332
+
315
333
  results[key] = [value, cas] unless key.nil?
316
334
  end
317
335
  rescue SystemCallError, Dalli::NetworkError
@@ -323,6 +341,42 @@ module Dalli
323
341
  @response_buffer ||= ResponseBuffer.new(@connection_manager, response_processor)
324
342
  end
325
343
 
344
+ # A multi-get only accepts replies for the keys it asked for. memcached
345
+ # names an item created by auto-create (ma or mg with N) for a
346
+ # base64-encoded key by its raw bytes, without the b flag; such a key
347
+ # can contain spaces or CR/LF, which split the reply header and make the
348
+ # replies after it parse as different ones. A key that wasn't requested
349
+ # is how that shows up.
350
+ # Only with the verify_reply_keys option, since building the set costs a
351
+ # multi-get about 15%. Dalli itself no longer creates such items (see
352
+ # Meta#decr_incr); the check guards against ones that older versions or
353
+ # other clients created, on memcached versions with the behavior.
354
+ def expect_reply_keys(keys)
355
+ @pipeline_out_of_step = false
356
+ @expected_reply_keys = (keys.to_set { |k| reply_key_form(k) } if @verify_reply_keys)
357
+ end
358
+
359
+ def unexpected_reply_key?(key)
360
+ !key.nil? && @expected_reply_keys && !@expected_reply_keys.include?(reply_key_form(key))
361
+ end
362
+
363
+ # Keys compared as bytes; ASCII keys compare equal in any encoding as is
364
+ def reply_key_form(key)
365
+ key.ascii_only? ? key : key.b
366
+ end
367
+
368
+ # Keeps what was read, drops the rest of this server's replies, and
369
+ # closes the connection so nothing unread is left on it. Not a socket
370
+ # failure, so it doesn't count toward socket_max_failures; the keys not
371
+ # read are simply missing from the result.
372
+ def abandon_pipeline
373
+ Dalli.logger.warn { "#{name} replied to a get_multi with a key that wasn't requested; closing the connection" }
374
+ @pipeline_out_of_step = false
375
+ response_buffer.clear
376
+ @connection_manager.close
377
+ {}
378
+ end
379
+
326
380
  # Called after the noop response is received at the end of a set
327
381
  # of pipelined gets
328
382
  def finish_pipeline
@@ -193,12 +193,44 @@ module Dalli
193
193
 
194
194
  def decr_incr(incr, key, delta, ttl, initial)
195
195
  ttl = initial ? TtlSanitizer.sanitize(ttl) : nil # Only set a TTL if we want to set a value on miss
196
+ # memcached stores an item created by ma's auto-create (N/J) for a
197
+ # base64-encoded key without its binary-key flag, and later replies
198
+ # name it with the raw key bytes. Create such counters with an add.
199
+ return decr_incr_with_add(incr, key, delta, ttl, initial) if initial && KeyRegularizer.required?(key)
200
+
196
201
  write(RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: initial, incr: incr, ttl: ttl,
197
202
  quiet: quiet?))
198
203
  @connection_manager.flush unless quiet?
199
204
  response_processor.decr_incr unless quiet?
200
205
  end
201
206
 
207
+ # Like decr_incr with an initial value, without memcached's auto-create:
208
+ # on a miss the counter is added with the initial value (which is
209
+ # returned, as auto-create would), and the add losing a race to another
210
+ # client means the counter now exists, so the incr is tried again. In a
211
+ # quiet block both requests are sent and whichever applies takes effect.
212
+ def decr_incr_with_add(incr, key, delta, ttl, initial)
213
+ add_options = { raw: true } # counters are stored raw
214
+ return quiet_decr_incr_with_add(incr, key, delta, ttl, initial, add_options) if quiet?
215
+
216
+ 2.times do
217
+ value = decr_incr(incr, key, delta, nil, nil)
218
+ return value unless value.nil?
219
+
220
+ write_storage_req(:add, key, initial.to_s, ttl, nil, add_options, quiet: false)
221
+ return initial if response_processor.meta_set_with_cas
222
+ end
223
+ decr_incr(incr, key, delta, nil, nil)
224
+ end
225
+
226
+ # rubocop:disable-next Metrics/ParameterLists
227
+ def quiet_decr_incr_with_add(incr, key, delta, ttl, initial, add_options)
228
+ write(RequestFormatter.meta_arithmetic(key: key, delta: delta, initial: nil, incr: incr, ttl: nil,
229
+ quiet: true))
230
+ # Sent with the rest of the quiet block, by its closing noop
231
+ write_storage_req(:add, key, initial.to_s, ttl, nil, add_options, quiet: true)
232
+ end
233
+
202
234
  # Other Commands
203
235
  def flush(delay = 0)
204
236
  write(RequestFormatter.flush(delay: delay))
@@ -240,10 +272,11 @@ module Dalli
240
272
  buffer = RequestFormatter.multi_meta_get(keys, skip_flags: is_raw)
241
273
  flushed_write(buffer)
242
274
  buffer.clear
243
- read_multi_get_responses(is_raw)
275
+ read_multi_get_responses(is_raw, keys)
244
276
  end
245
277
 
246
- def read_multi_get_responses(is_raw)
278
+ def read_multi_get_responses(is_raw, keys)
279
+ expect_reply_keys(keys)
247
280
  hash = {}
248
281
  key_index = is_raw ? 2 : 3
249
282
  while (line = @connection_manager.read_line)
@@ -251,11 +284,19 @@ module Dalli
251
284
  next unless line.start_with?('VA ')
252
285
 
253
286
  key, value = parse_multi_get_value(line, key_index, is_raw)
287
+ raise_unexpected_reply_key(key) if unexpected_reply_key?(key)
254
288
  hash[key] = value if key
255
289
  end
256
290
  hash
257
291
  end
258
292
 
293
+ # The replies after a key that wasn't requested can't be trusted (see
294
+ # Base#expect_reply_keys). Raising ends the request, which closes the
295
+ # connection; the caller treats this server's keys as missing.
296
+ def raise_unexpected_reply_key(key)
297
+ raise Dalli::UnexpectedReplyKeyError, "#{name} replied with a key that wasn't requested: #{key.inspect}"
298
+ end
299
+
259
300
  def parse_multi_get_value(line, key_index, is_raw)
260
301
  tokens = line.chomp!(TERMINATOR).split
261
302
  size = tokens[1].to_i
data/lib/dalli/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dalli
4
- VERSION = '5.0.10'
4
+ VERSION = '5.0.11'
5
5
 
6
6
  MIN_SUPPORTED_MEMCACHED_VERSION = '1.6'
7
7
  end
data/lib/dalli.rb CHANGED
@@ -31,6 +31,10 @@ module Dalli
31
31
  # socket/server communication error that can be retried
32
32
  class RetryableNetworkError < NetworkError; end
33
33
 
34
+ # a multi-get reply named a key that wasn't requested, so the rest of that
35
+ # server's replies can't be trusted
36
+ class UnexpectedReplyKeyError < DalliError; end
37
+
34
38
  # Implements the NullObject pattern to store an application-defined value for 'Key not found' responses.
35
39
  class NilObject; end # rubocop:disable Lint/EmptyClass
36
40
  NOT_FOUND = NilObject.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dalli
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.10
4
+ version: 5.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter M. Goldstein
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  requirements: []
91
- rubygems_version: 4.0.21
91
+ rubygems_version: 4.0.23
92
92
  specification_version: 4
93
93
  summary: High performance memcached client for Ruby
94
94
  test_files: []