dalli 3.2.15 → 3.2.17

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: 1282508d83c234919dfb2c8a05c08d7069186663c83d4e3aaf040e6f503a2e9f
4
- data.tar.gz: cb10108ea1d22c25630b5cfa36b6e038515e3f053e2354e3921718050138535b
3
+ metadata.gz: 85605d05fac6cb78fd5b6a3505bf92bb3df72f80a8aecd1fc581896e0bb6bcf5
4
+ data.tar.gz: 3e1d6612ea7c0c8987bc573bc3686d0d29d184b6af2edbd737f14ec6619fc6f0
5
5
  SHA512:
6
- metadata.gz: 614a4eebdaa9cee1d9dc6afdfd45c96ff1ec7eda12f471a12c6c1eca7866af971d907a7c288aeb5a72734b3f4f72bfb685ba58337da2ac50146ed74a018c0d0a
7
- data.tar.gz: 8f577fef557f593d9cb6327a54ec1b5d3df8e554a5935ba7a7faadf86259248a60f29204b3ad5a295024b676ebc481e925f81f330095a73e6649e11d182b1127
6
+ metadata.gz: 6c037ec54baa774897ada2bbfce9a99f927eca9e9d7655bdfec55b56118939d6ba212057ebcaada26dc7d6761a5e2926dcbe2b018e3eb0ba249bb650a1f864ce
7
+ data.tar.gz: 2cc9822465193504397dc0af9130eabf49b08887a77ca4dd37c86160e9043b6611eb4c3eddc91275dc4a341330d4d3430bff04a7087c4272c89da5c0e31a9270
data/CHANGELOG.md CHANGED
@@ -1,9 +1,20 @@
1
1
  Dalli Changelog
2
2
  =====================
3
3
 
4
- Unreleased
4
+ 3.2.17
5
5
  ==========
6
6
 
7
+ Security:
8
+
9
+ - Security fixes; details will follow in the advisory.
10
+
11
+ 3.2.16
12
+ ==========
13
+
14
+ Security:
15
+
16
+ - Bound memcached reply headers, incoming value sizes, and response counts, and enforce TLS timeouts for blocking operations (GHSA-5wpc-45jc-6ccc)
17
+
7
18
  3.2.15
8
19
  ==========
9
20
 
data/README.md CHANGED
@@ -27,6 +27,12 @@ The name is a variant of Salvador Dali for his famous painting [The Persistence
27
27
 
28
28
  ## Security Note
29
29
 
30
+ Incoming values are limited to `value_max_bytes` (1 MB by default). Set `max_response_bytes` to a positive byte count if an existing memcached deployment stores larger values:
31
+
32
+ ```ruby
33
+ Dalli::Client.new('localhost:11211', max_response_bytes: 2 * 1024 * 1024)
34
+ ```
35
+
30
36
  By default, Dalli uses Ruby's Marshal for serialization. Deserializing untrusted data with Marshal can lead to remote code execution. If you cache user-controlled data, or your memcached server could be written to by someone else, consider using a safer serializer:
31
37
 
32
38
  ```ruby
data/lib/dalli/client.rb CHANGED
@@ -28,6 +28,7 @@ module Dalli
28
28
  # the server configuration.
29
29
  #
30
30
  # Options:
31
+ # - :max_response_bytes - largest value accepted from a server. Defaults to :value_max_bytes (1 MB by default).
31
32
  # - :namespace - prepend each key with this value to provide simple namespacing.
32
33
  # - :failover - if a server is down, look for and store values on another server in the ring. Default: true.
33
34
  # - :threadsafe - ensure that only one thread is actively using a socket at a time. Default: true.
@@ -147,6 +147,9 @@ module Dalli
147
147
  def servers_with_response(servers, timeout)
148
148
  return [] if servers.empty?
149
149
 
150
+ buffered = pending_tls_servers(servers)
151
+ return buffered unless buffered.empty?
152
+
150
153
  # TODO: - This is a bit challenging. Essentially the PipelinedGetter
151
154
  # is a reactor, but without the benefit of a Fiber or separate thread.
152
155
  # My suspicion is that we may want to try and push this down into the
@@ -161,6 +164,10 @@ module Dalli
161
164
  readable.map { |sock| server_map[sock] }
162
165
  end
163
166
 
167
+ def pending_tls_servers(servers)
168
+ servers.select { |server| server.sock.respond_to?(:pending_data?) && server.sock.pending_data? }
169
+ end
170
+
164
171
  def groups_for_keys(*keys)
165
172
  keys.flatten!
166
173
  keys.map! { |a| @key_manager.validate_key(a.to_s) }
@@ -109,6 +109,8 @@ module Dalli
109
109
  # to the noop at the end of the pipeline
110
110
  finish_pipeline && break if status && key.nil?
111
111
 
112
+ check_pipeline_response_count!
113
+
112
114
  # If the status is ok and the key is not nil, then this is a
113
115
  # getkq response with a value that we want to set in the response hash
114
116
  break unless keep_reading?(values, key, value, cas)
@@ -259,9 +261,18 @@ module Dalli
259
261
  # requested is how that shows up. Only with the verify_reply_keys option,
260
262
  # since building the set costs a multi-get about 15%.
261
263
  def expect_reply_keys(keys)
264
+ @expected_response_count = keys.size
265
+ @pipeline_response_count = 0
262
266
  @expected_reply_keys = (keys.to_set { |k| reply_key_form(k) } if @verify_reply_keys)
263
267
  end
264
268
 
269
+ def check_pipeline_response_count!
270
+ @pipeline_response_count += 1
271
+ return if @pipeline_response_count <= @expected_response_count
272
+
273
+ raise Dalli::DalliError, 'Too many get_multi replies'
274
+ end
275
+
265
276
  # Stores a getkq reply's value and returns true, unless its key wasn't
266
277
  # requested: then the replies are out of step, so what was read so far is
267
278
  # kept, the rest dropped, and false returned to stop reading
@@ -9,6 +9,8 @@ module Dalli
9
9
  # and parsing into local values. Handles errors on unexpected values.
10
10
  ##
11
11
  class ResponseProcessor
12
+ MAX_STATS_ENTRIES = 10_000
13
+ MAX_QUIET_RESPONSES = 100_000
12
14
  # Response codes taken from:
13
15
  # https://github.com/memcached/memcached/wiki/BinaryProtocolRevamped#response-status
14
16
  RESPONSE_CODES = {
@@ -42,6 +44,7 @@ module Dalli
42
44
 
43
45
  def read_response
44
46
  resp_header = ResponseHeader.new(read_header)
47
+ check_body_size!(resp_header)
45
48
  body = read(resp_header.body_len) if resp_header.body_len.positive?
46
49
  [resp_header, body]
47
50
  end
@@ -120,13 +123,17 @@ module Dalli
120
123
  body ? body.unpack1('Q>') : body
121
124
  end
122
125
 
123
- def stats
126
+ def stats # rubocop:disable Metrics/MethodLength
124
127
  hash = {}
128
+ replies = 0
125
129
  loop do
126
130
  resp_header, body = read_response
127
131
  # This is the response to the terminating noop / end of stat
128
132
  return hash if resp_header.ok? && resp_header.key_len.zero?
129
133
 
134
+ replies += 1
135
+ raise Dalli::DalliError, 'Too many stats replies' if replies > MAX_STATS_ENTRIES
136
+
130
137
  # Ignore any responses with non-zero status codes,
131
138
  # such as errors from set operations. That allows
132
139
  # this code to be used at the end of a multi
@@ -150,11 +157,15 @@ module Dalli
150
157
  generic_response
151
158
  end
152
159
 
153
- def consume_all_responses_until_noop
160
+ def consume_all_responses_until_noop(max_responses = MAX_QUIET_RESPONSES)
161
+ replies = 0
154
162
  loop do
155
163
  resp_header, = read_response
156
164
  # This is the response to the terminating noop / end of stat
157
165
  return true if resp_header.ok? && resp_header.key_len.zero?
166
+
167
+ replies += 1
168
+ raise Dalli::DalliError, 'Too many quiet replies' if replies > max_responses
158
169
  end
159
170
  end
160
171
 
@@ -188,6 +199,7 @@ module Dalli
188
199
  resp_header = ResponseHeader.new(buf)
189
200
  body_len = resp_header.body_len
190
201
  validate_auth_format(resp_header.extra_len, body_len)
202
+ check_body_size!(resp_header)
191
203
  content = read(body_len) if body_len.positive?
192
204
  [resp_header.status, content]
193
205
  end
@@ -217,8 +229,11 @@ module Dalli
217
229
  # buffer wait for (and accumulate) that many bytes.
218
230
  MAX_BODY_BYTES = 1024 * 1024 * 1024
219
231
 
220
- def check_body_size!(size)
221
- return if size <= MAX_BODY_BYTES
232
+ def check_body_size!(header)
233
+ size = header.body_len
234
+ value_size = size - header.extra_len - header.key_len
235
+ limit = [MAX_BODY_BYTES, @value_marshaller.max_response_bytes].min
236
+ return if size <= MAX_BODY_BYTES && value_size.between?(0, limit)
222
237
 
223
238
  raise Dalli::DalliError, "Reply body size #{size} is out of range"
224
239
  end
@@ -229,6 +244,7 @@ module Dalli
229
244
 
230
245
  resp_header = response_header_from_buffer(buf)
231
246
  body_len = resp_header.body_len
247
+ check_body_size!(resp_header)
232
248
 
233
249
  # We have a complete response that has no body.
234
250
  # This is either the response to the terminating
@@ -236,8 +252,6 @@ module Dalli
236
252
  # error response that needs to be discarded.
237
253
  return [ResponseHeader::SIZE, resp_header.ok?, resp_header.cas, nil, nil] if body_len.zero?
238
254
 
239
- check_body_size!(body_len)
240
-
241
255
  resp_size = ResponseHeader::SIZE + body_len
242
256
  # The header is in the buffer, but the body is not. As we don't have
243
257
  # a complete response, don't advance the buffer
@@ -12,7 +12,7 @@ module Dalli
12
12
  # Manages the socket connection to the server, including ensuring liveness
13
13
  # and retries.
14
14
  ##
15
- class ConnectionManager
15
+ class ConnectionManager # rubocop:disable Metrics/ClassLength
16
16
  DEFAULTS = {
17
17
  # seconds between trying to contact a remote server
18
18
  down_retry_delay: 30,
@@ -182,7 +182,7 @@ module Dalli
182
182
  MAX_LINE_BYTES = 8 * 1024
183
183
 
184
184
  def read_line
185
- data = @sock.gets("\r\n", MAX_LINE_BYTES)
185
+ data = with_tls_timeout { @sock.gets("\r\n", MAX_LINE_BYTES) }
186
186
  error_on_request!('EOF in read_line') if data.nil?
187
187
  # Without its CRLF the line either ran past MAX_LINE_BYTES or was cut
188
188
  # short by EOF; either way the connection is out of step
@@ -205,13 +205,13 @@ module Dalli
205
205
 
206
206
  def read(count)
207
207
  check_read_size!(count)
208
- @sock.readfull(count)
208
+ with_tls_timeout { @sock.readfull(count) }
209
209
  rescue SystemCallError, *TIMEOUT_ERRORS, EOFError => e
210
210
  error_on_request!(e)
211
211
  end
212
212
 
213
213
  def write(bytes)
214
- @sock.write(bytes)
214
+ with_tls_timeout { @sock.write(bytes) }
215
215
  rescue SystemCallError, *TIMEOUT_ERRORS => e
216
216
  error_on_request!(e)
217
217
  end
@@ -222,6 +222,12 @@ module Dalli
222
222
  @sock.read_available
223
223
  end
224
224
 
225
+ def with_tls_timeout
226
+ return yield unless @sock.is_a?(Dalli::Socket::SSLSocket)
227
+
228
+ Timeout.timeout(socket_timeout) { yield } # rubocop:disable Style/ExplicitBlockArgument
229
+ end
230
+
225
231
  def max_allowed_failures
226
232
  @max_allowed_failures ||= @options[:socket_max_failures] || 2
227
233
  end
@@ -9,6 +9,9 @@ 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
12
15
  EN = 'EN'
13
16
  END_TOKEN = 'END'
14
17
  EX = 'EX'
@@ -85,7 +88,11 @@ module Dalli
85
88
  def stats
86
89
  tokens = error_on_unexpected!([END_TOKEN, STAT])
87
90
  values = {}
91
+ replies = 0
88
92
  while tokens.first != END_TOKEN
93
+ replies += 1
94
+ raise Dalli::DalliError, 'Too many stats replies' if replies > MAX_STATS_ENTRIES
95
+
89
96
  values[tokens[1]] = tokens[2]
90
97
  tokens = next_line_to_tokens
91
98
  end
@@ -109,10 +116,16 @@ module Dalli
109
116
  tokens.last
110
117
  end
111
118
 
112
- def consume_all_responses_until_mn
119
+ def consume_all_responses_until_mn(max_responses = MAX_QUIET_RESPONSES)
113
120
  tokens = next_line_to_tokens
114
121
 
115
- tokens = next_line_to_tokens while tokens.first != MN
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
116
129
  true
117
130
  end
118
131
 
@@ -163,6 +176,9 @@ module Dalli
163
176
  # a complete response, don't advance the buffer
164
177
  return [0, nil, nil, nil, nil] unless buf.bytesize >= resp_size
165
178
 
179
+ term = header_len + body_len
180
+ raise_bad_value_terminator unless buf.getbyte(term) == 13 && buf.getbyte(term + 1) == 10
181
+
166
182
  # The full response is in our buffer, so parse it and return
167
183
  # the values
168
184
  body = buf.slice(header_len, body_len)
@@ -180,7 +196,8 @@ module Dalli
180
196
  end
181
197
 
182
198
  def check_value_size!(size)
183
- return if size.between?(0, MAX_VALUE_BYTES)
199
+ limit = @value_marshaller.max_response_bytes
200
+ return if size >= 0 && size <= MAX_VALUE_BYTES && size <= limit
184
201
 
185
202
  raise Dalli::DalliError, "Reply value size #{size} is out of range"
186
203
  end
@@ -191,7 +208,14 @@ module Dalli
191
208
  end
192
209
 
193
210
  def contains_header?(buf)
194
- buf.include?(TERMINATOR)
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?
195
219
  end
196
220
 
197
221
  def header_from_buffer(buf)
@@ -245,11 +269,19 @@ module Dalli
245
269
 
246
270
  # Values are read by the size in the VA line, not up to the next CRLF:
247
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
+
248
280
  def read_data(data_size)
249
281
  # Checked before the terminator is added: -1 or -2 would otherwise
250
282
  # read 1 or 0 bytes and leave the connection out of step
251
283
  check_value_size!(data_size)
252
- @io_source.read(data_size + TERMINATOR.bytesize)&.chomp!(TERMINATOR)
284
+ strip_value_terminator!(@io_source.read(data_size + TERMINATOR.bytesize))
253
285
  end
254
286
  end
255
287
  end
@@ -28,6 +28,8 @@ module Dalli
28
28
 
29
29
  @marshal_options =
30
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?
31
33
  end
32
34
 
33
35
  def store(key, value, options = nil)
@@ -48,6 +50,8 @@ module Dalli
48
50
  @marshal_options[:value_max_bytes]
49
51
  end
50
52
 
53
+ attr_reader :max_response_bytes
54
+
51
55
  def error_if_over_max_value_bytes(key, value)
52
56
  return if value.bytesize <= value_max_bytes
53
57
 
data/lib/dalli/socket.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'openssl'
4
4
  require 'rbconfig'
5
+ require 'timeout'
5
6
 
6
7
  module Dalli
7
8
  ##
@@ -12,6 +13,8 @@ module Dalli
12
13
  # Common methods for all socket implementations.
13
14
  ##
14
15
  module InstanceMethods
16
+ MAX_AVAILABLE_BYTES = 8196 * 8
17
+
15
18
  def readfull(count)
16
19
  value = String.new(capacity: count + 1)
17
20
  loop do
@@ -24,16 +27,21 @@ module Dalli
24
27
 
25
28
  def read_available
26
29
  value = +''
27
- loop do
28
- result = read_nonblock(8196, exception: false)
30
+ while value.bytesize < MAX_AVAILABLE_BYTES
31
+ result = read_nonblock([8196, MAX_AVAILABLE_BYTES - value.bytesize].min, exception: false)
29
32
  break if WAIT_RCS.include?(result)
30
33
  raise Errno::ECONNRESET, "Connection reset: #{logged_options.inspect}" unless result
31
34
 
32
35
  value << result
36
+ break if result.bytesize < 8196 && !buffered_data?
33
37
  end
34
38
  value
35
39
  end
36
40
 
41
+ def buffered_data?
42
+ false
43
+ end
44
+
37
45
  WAIT_RCS = %i[wait_writable wait_readable].freeze
38
46
 
39
47
  def append_to_buffer?(result)
@@ -63,6 +71,14 @@ module Dalli
63
71
  ##
64
72
  class SSLSocket < ::OpenSSL::SSL::SSLSocket
65
73
  include Dalli::Socket::InstanceMethods
74
+ def pending_data?
75
+ pending.to_i.positive? || (@rbuffer && !@rbuffer.empty?)
76
+ end
77
+
78
+ def buffered_data?
79
+ RUBY_ENGINE == 'jruby' || pending_data?
80
+ end
81
+
66
82
  def options
67
83
  io.options
68
84
  end
@@ -93,7 +109,11 @@ module Dalli
93
109
  sock.options = { host: host, port: port }.merge(options)
94
110
  init_socket_options(sock, options)
95
111
 
96
- options[:ssl_context] ? wrapping_ssl_socket(sock, host, options[:ssl_context]) : sock
112
+ if options[:ssl_context]
113
+ wrapping_ssl_socket(sock, host, options[:ssl_context], options[:socket_timeout])
114
+ else
115
+ sock
116
+ end
97
117
  end
98
118
  end
99
119
 
@@ -134,13 +154,13 @@ module Dalli
134
154
 
135
155
  # Closes the TCP socket if the handshake fails or times out, so a failed
136
156
  # attempt doesn't hold a file descriptor until GC.
137
- def self.wrapping_ssl_socket(tcp_socket, host, ssl_context)
157
+ def self.wrapping_ssl_socket(tcp_socket, host, ssl_context, socket_timeout = nil)
138
158
  warn_if_unverified(ssl_context)
139
159
  connected = false
140
160
  ssl_socket = Dalli::Socket::SSLSocket.new(tcp_socket, ssl_context)
141
161
  ssl_socket.hostname = host
142
162
  ssl_socket.sync_close = true
143
- ssl_socket.connect
163
+ Timeout.timeout(socket_timeout) { ssl_socket.connect }
144
164
  connected = true
145
165
  ssl_socket
146
166
  ensure
data/lib/dalli/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dalli
4
- VERSION = '3.2.15'
4
+ VERSION = '3.2.17'
5
5
 
6
6
  MIN_SUPPORTED_MEMCACHED_VERSION = '1.4'
7
7
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dalli
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.15
4
+ version: 3.2.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter M. Goldstein
8
8
  - Mike Perham
9
+ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 1980-01-02 00:00:00.000000000 Z
12
+ date: 2026-11-14 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: logger
@@ -76,6 +77,7 @@ metadata:
76
77
  bug_tracker_uri: https://github.com/petergoldstein/dalli/issues
77
78
  changelog_uri: https://github.com/petergoldstein/dalli/blob/main/CHANGELOG.md
78
79
  rubygems_mfa_required: 'true'
80
+ post_install_message:
79
81
  rdoc_options: []
80
82
  require_paths:
81
83
  - lib
@@ -90,7 +92,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
92
  - !ruby/object:Gem::Version
91
93
  version: '0'
92
94
  requirements: []
93
- rubygems_version: 4.0.23
95
+ rubygems_version: 3.5.22
96
+ signing_key:
94
97
  specification_version: 4
95
98
  summary: High performance memcached client for Ruby
96
99
  test_files: []