dalli 3.2.14 → 3.2.16
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 +4 -4
- data/CHANGELOG.md +22 -1
- data/README.md +25 -0
- data/lib/dalli/client.rb +6 -0
- data/lib/dalli/pipelined_getter.rb +7 -0
- data/lib/dalli/protocol/base.rb +27 -0
- data/lib/dalli/protocol/binary/response_processor.rb +20 -6
- data/lib/dalli/protocol/binary.rb +4 -1
- data/lib/dalli/protocol/connection_manager.rb +35 -6
- data/lib/dalli/protocol/meta/response_processor.rb +36 -6
- data/lib/dalli/protocol/meta.rb +4 -1
- data/lib/dalli/protocol/value_compressor.rb +6 -4
- data/lib/dalli/protocol/value_marshaller.rb +4 -0
- data/lib/dalli/protocol.rb +7 -0
- data/lib/dalli/socket.rb +55 -5
- data/lib/dalli/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 12a8b4270f527df2231bb1c56ce5bb035c477ec752ac1315f6afb0aa8afadcfa
|
|
4
|
+
data.tar.gz: bd82350ebd1b7c924be97b5f057e96b0d2b33b6e8ee34767f11a5b4bd58ef776
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2014fc711d0e8165dafa5fc2d18c45f68788bc78e243f2cdee411ed64a3863ab9a810b89460bad89cf92f567283537cad4be06ad09c6e7bbb8557613f97d1893
|
|
7
|
+
data.tar.gz: e4474495d492a13e360a00b85830694767e0441e28f69471e00b1a468c1fa139d92660ea95d1ee845e541610ecf25fa1353ea69bc03e93f18c77c3744c7eb151
|
data/CHANGELOG.md
CHANGED
|
@@ -1,9 +1,30 @@
|
|
|
1
1
|
Dalli Changelog
|
|
2
2
|
=====================
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
3.2.16
|
|
5
5
|
==========
|
|
6
6
|
|
|
7
|
+
Security:
|
|
8
|
+
|
|
9
|
+
- Bound memcached reply headers, incoming value sizes, and response counts, and enforce TLS timeouts for blocking operations (GHSA-5wpc-45jc-6ccc)
|
|
10
|
+
|
|
11
|
+
3.2.15
|
|
12
|
+
==========
|
|
13
|
+
|
|
14
|
+
- Keep a connection shared by several threads or fibers in step during a `quiet` block (GHSA-5xvx-r8rg-5qm2)
|
|
15
|
+
- A `quiet` (or `multi`) block is quiet only for the thread or fiber that runs it. memcached still replies to a quiet request that fails (for example an `add` of a key that exists), and another thread or fiber using the same client while the block was open read that reply as its own, so its later requests could return another key's value. A non-quiet request now reads any pending quiet replies first
|
|
16
|
+
- Affects every supported line, with both protocols; with the binary protocol the connection stayed out of step after the block ended. Fixed in 5.2.4, 5.1.6, 5.0.12, 4.3.9 and 3.2.15
|
|
17
|
+
- With `protocol: :meta`, read values by their size instead of up to the first CRLF (GHSA-5xvx-r8rg-5qm2)
|
|
18
|
+
- `get`, `gat` and `cas` read a value as one line, so a value containing CR/LF was cut short and the rest was parsed as the next reply. A stored value containing a crafted reply was then returned for a later read of a different key, with flags that could cause it to be deserialized
|
|
19
|
+
- Affects 3.2.0 to 3.2.14 with `protocol: :meta`; fixed in 3.2.15. The binary protocol (3.2's default) and 4.0 and later aren't affected
|
|
20
|
+
- A per-request `raw: true` write no longer compresses the value (backport of #1213)
|
|
21
|
+
- Values at or above the compression threshold (4 KB by default) were compressed and flagged even with `raw: true`. Raw reads ignore flags, so reading them back with `raw: true` returned the compressed bytes
|
|
22
|
+
- Raw values are now stored exactly as given. Callers that pass `raw: true` without `compress: false` store large values uncompressed; Rails' `MemCacheStore` passes `compress: false` and isn't affected
|
|
23
|
+
- Handle failed TLS handshakes like other connection failures, and warn when TLS verification is off (backport of #1215)
|
|
24
|
+
- A failed or timed-out handshake left the TCP socket open until GC and raised `OpenSSL::SSL::SSLError` directly, so the server was never marked down. The socket is now closed, and the error goes through the usual retry and `socket_max_failures` handling, raising `Dalli::NetworkError` (or `Dalli::RingError` once no server is available)
|
|
25
|
+
- Certificate and hostname checks come only from the `ssl_context` you pass, and a bare `OpenSSL::SSL::SSLContext.new` checks neither. Dalli now logs a warning once per process when `verify_mode` doesn't include `VERIFY_PEER` or `verify_hostname` is false; see the new TLS section in the README
|
|
26
|
+
- A reply line longer than 8 KiB without a CRLF, or one cut short by the connection closing, is now a network error that closes the connection. Before, the line had no length limit (backport of #1216)
|
|
27
|
+
|
|
7
28
|
3.2.14
|
|
8
29
|
==========
|
|
9
30
|
|
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
|
|
@@ -41,6 +47,25 @@ Per-request `raw: true` returns the stored bytes without deserializing or decomp
|
|
|
41
47
|
|
|
42
48
|
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 (with `protocol: :meta`). Since 3.2.14, 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.
|
|
43
49
|
|
|
50
|
+
## TLS
|
|
51
|
+
|
|
52
|
+
Pass an `OpenSSL::SSL::SSLContext` as `ssl_context:` to connect over TLS. Certificate and hostname verification come only from that context: Dalli adds no checks of its own. A bare `OpenSSL::SSL::SSLContext.new` verifies nothing, so it accepts any certificate from any server. Configure the context to verify both:
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
ssl_context = OpenSSL::SSL::SSLContext.new
|
|
56
|
+
ssl_context.set_params(
|
|
57
|
+
verify_mode: OpenSSL::SSL::VERIFY_PEER,
|
|
58
|
+
verify_hostname: true,
|
|
59
|
+
ca_file: '/path/to/ca.crt' # omit to use the system's trusted certificates
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
Dalli::Client.new('memcached.example.com:11211', ssl_context: ssl_context)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Dalli logs a warning once per process when the context's `verify_mode` is `VERIFY_NONE`, or when it is `VERIFY_PEER` but `verify_hostname` is false.
|
|
66
|
+
|
|
67
|
+
A failed TLS handshake counts as a failed connection attempt, like a refused connection: once `socket_max_failures` attempts have failed, the server is marked down and requests fail over to the other servers.
|
|
68
|
+
|
|
44
69
|
## Development
|
|
45
70
|
|
|
46
71
|
After checking out the repo, run `bin/setup` to install dependencies. You can run `bin/console` for an interactive prompt that will allow you to experiment.
|
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.
|
|
@@ -192,6 +193,11 @@ module Dalli
|
|
|
192
193
|
# Any error replies will be discarded at the end of the block, and
|
|
193
194
|
# Dalli client methods invoked inside the block will not
|
|
194
195
|
# have return values
|
|
196
|
+
#
|
|
197
|
+
# The block is quiet only for the thread or fiber that runs it. Another
|
|
198
|
+
# thread or fiber sharing this client can make requests meanwhile; a
|
|
199
|
+
# non-quiet one first reads the block's pending replies with a noop and
|
|
200
|
+
# discards them, so it reads only its own.
|
|
195
201
|
def quiet
|
|
196
202
|
old = Thread.current[::Dalli::QUIET]
|
|
197
203
|
Thread.current[::Dalli::QUIET] = 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) }
|
data/lib/dalli/protocol/base.rb
CHANGED
|
@@ -41,7 +41,11 @@ module Dalli
|
|
|
41
41
|
|
|
42
42
|
begin
|
|
43
43
|
@connection_manager.start_request!
|
|
44
|
+
drain_pending_quiet_responses(opkey)
|
|
44
45
|
response = send(opkey, *args)
|
|
46
|
+
# Quiet requests can leave replies on the socket; remember that so a
|
|
47
|
+
# later non-quiet request knows to read them first
|
|
48
|
+
@connection_manager.quiet_responses_pending = true if quiet? && opkey != :noop
|
|
45
49
|
|
|
46
50
|
# pipelined_get emit query but doesn't read the response(s)
|
|
47
51
|
@connection_manager.finish_request! unless opkey == :pipelined_get
|
|
@@ -105,6 +109,8 @@ module Dalli
|
|
|
105
109
|
# to the noop at the end of the pipeline
|
|
106
110
|
finish_pipeline && break if status && key.nil?
|
|
107
111
|
|
|
112
|
+
check_pipeline_response_count!
|
|
113
|
+
|
|
108
114
|
# If the status is ok and the key is not nil, then this is a
|
|
109
115
|
# getkq response with a value that we want to set in the response hash
|
|
110
116
|
break unless keep_reading?(values, key, value, cas)
|
|
@@ -165,6 +171,18 @@ module Dalli
|
|
|
165
171
|
private
|
|
166
172
|
|
|
167
173
|
ALLOWED_QUIET_OPS = %i[add replace set delete incr decr append prepend flush noop].freeze
|
|
174
|
+
|
|
175
|
+
# Reads the replies still pending from quiet requests before any
|
|
176
|
+
# non-quiet request, which reads a reply of its own. A quiet block reads
|
|
177
|
+
# them at its end, but the connection is shared: another thread or fiber
|
|
178
|
+
# (quiet? is fiber-local) can make a request while the block is still
|
|
179
|
+
# open, and would otherwise read the block's replies as its own.
|
|
180
|
+
def drain_pending_quiet_responses(opkey)
|
|
181
|
+
return unless @connection_manager.quiet_responses_pending? && opkey != :noop && !quiet?
|
|
182
|
+
|
|
183
|
+
noop
|
|
184
|
+
end
|
|
185
|
+
|
|
168
186
|
def verify_allowed_quiet!(opkey)
|
|
169
187
|
return if ALLOWED_QUIET_OPS.include?(opkey)
|
|
170
188
|
|
|
@@ -243,9 +261,18 @@ module Dalli
|
|
|
243
261
|
# requested is how that shows up. Only with the verify_reply_keys option,
|
|
244
262
|
# since building the set costs a multi-get about 15%.
|
|
245
263
|
def expect_reply_keys(keys)
|
|
264
|
+
@expected_response_count = keys.size
|
|
265
|
+
@pipeline_response_count = 0
|
|
246
266
|
@expected_reply_keys = (keys.to_set { |k| reply_key_form(k) } if @verify_reply_keys)
|
|
247
267
|
end
|
|
248
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
|
+
|
|
249
276
|
# Stores a getkq reply's value and returns true, unless its key wasn't
|
|
250
277
|
# requested: then the replies are out of step, so what was read so far is
|
|
251
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!(
|
|
221
|
-
|
|
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
|
|
@@ -139,7 +139,10 @@ module Dalli
|
|
|
139
139
|
# We need to read all the responses at once.
|
|
140
140
|
def noop
|
|
141
141
|
write_noop
|
|
142
|
-
response_processor.consume_all_responses_until_noop
|
|
142
|
+
result = response_processor.consume_all_responses_until_noop
|
|
143
|
+
# Everything up to and including the noop reply has been read
|
|
144
|
+
@connection_manager.quiet_responses_pending = false
|
|
145
|
+
result
|
|
143
146
|
end
|
|
144
147
|
|
|
145
148
|
def stats(info = '')
|
|
@@ -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,
|
|
@@ -28,6 +28,7 @@ module Dalli
|
|
|
28
28
|
|
|
29
29
|
attr_accessor :hostname, :port, :socket_type, :options
|
|
30
30
|
attr_reader :sock
|
|
31
|
+
attr_writer :quiet_responses_pending
|
|
31
32
|
|
|
32
33
|
def initialize(hostname, port, socket_type, client_options)
|
|
33
34
|
@hostname = hostname
|
|
@@ -35,6 +36,7 @@ module Dalli
|
|
|
35
36
|
@socket_type = socket_type
|
|
36
37
|
@options = DEFAULTS.merge(client_options)
|
|
37
38
|
@request_in_progress = false
|
|
39
|
+
@quiet_responses_pending = false
|
|
38
40
|
@sock = nil
|
|
39
41
|
@pid = nil
|
|
40
42
|
|
|
@@ -56,8 +58,9 @@ module Dalli
|
|
|
56
58
|
@sock = memcached_socket
|
|
57
59
|
@pid = Process.pid
|
|
58
60
|
@request_in_progress = false
|
|
59
|
-
|
|
60
|
-
|
|
61
|
+
@quiet_responses_pending = false
|
|
62
|
+
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError, SocketError => e
|
|
63
|
+
# SocketError = DNS resolution failure; SSL_ERRORS = failed TLS handshake
|
|
61
64
|
error_on_request!(e)
|
|
62
65
|
end
|
|
63
66
|
|
|
@@ -122,6 +125,7 @@ module Dalli
|
|
|
122
125
|
end
|
|
123
126
|
@sock = nil
|
|
124
127
|
@pid = nil
|
|
128
|
+
@quiet_responses_pending = false
|
|
125
129
|
abort_request!
|
|
126
130
|
end
|
|
127
131
|
|
|
@@ -136,6 +140,13 @@ module Dalli
|
|
|
136
140
|
!@sock.nil?
|
|
137
141
|
end
|
|
138
142
|
|
|
143
|
+
# True when a quiet request was written on this connection and its
|
|
144
|
+
# replies (memcached only suppresses some of them) haven't been
|
|
145
|
+
# consumed by a noop yet.
|
|
146
|
+
def quiet_responses_pending?
|
|
147
|
+
@quiet_responses_pending
|
|
148
|
+
end
|
|
149
|
+
|
|
139
150
|
def request_in_progress?
|
|
140
151
|
@request_in_progress
|
|
141
152
|
end
|
|
@@ -161,9 +172,21 @@ module Dalli
|
|
|
161
172
|
@request_in_progress = false
|
|
162
173
|
end
|
|
163
174
|
|
|
175
|
+
# The longest reply line Dalli reads, in bytes, CRLF included. A meta
|
|
176
|
+
# reply header is at most about 500 bytes: "VA <size>" plus its flags,
|
|
177
|
+
# the largest being a 250-byte key base64-encoded to 336 bytes, a
|
|
178
|
+
# 20-digit CAS, a 32-byte opaque and a few numeric fields. The longest
|
|
179
|
+
# stats line (stats settings) carries a file path such as ext_path, up
|
|
180
|
+
# to PATH_MAX (4096). 8 KiB leaves room for both, while a server that
|
|
181
|
+
# never sends CRLF can't grow the read buffer without bound.
|
|
182
|
+
MAX_LINE_BYTES = 8 * 1024
|
|
183
|
+
|
|
164
184
|
def read_line
|
|
165
|
-
data = @sock.gets("\r\n")
|
|
185
|
+
data = with_tls_timeout { @sock.gets("\r\n", MAX_LINE_BYTES) }
|
|
166
186
|
error_on_request!('EOF in read_line') if data.nil?
|
|
187
|
+
# Without its CRLF the line either ran past MAX_LINE_BYTES or was cut
|
|
188
|
+
# short by EOF; either way the connection is out of step
|
|
189
|
+
error_on_request!("Reply line without CRLF in the first #{MAX_LINE_BYTES} bytes") unless data.end_with?("\r\n")
|
|
167
190
|
data
|
|
168
191
|
rescue SystemCallError, *TIMEOUT_ERRORS, EOFError => e
|
|
169
192
|
error_on_request!(e)
|
|
@@ -182,13 +205,13 @@ module Dalli
|
|
|
182
205
|
|
|
183
206
|
def read(count)
|
|
184
207
|
check_read_size!(count)
|
|
185
|
-
@sock.readfull(count)
|
|
208
|
+
with_tls_timeout { @sock.readfull(count) }
|
|
186
209
|
rescue SystemCallError, *TIMEOUT_ERRORS, EOFError => e
|
|
187
210
|
error_on_request!(e)
|
|
188
211
|
end
|
|
189
212
|
|
|
190
213
|
def write(bytes)
|
|
191
|
-
@sock.write(bytes)
|
|
214
|
+
with_tls_timeout { @sock.write(bytes) }
|
|
192
215
|
rescue SystemCallError, *TIMEOUT_ERRORS => e
|
|
193
216
|
error_on_request!(e)
|
|
194
217
|
end
|
|
@@ -199,6 +222,12 @@ module Dalli
|
|
|
199
222
|
@sock.read_available
|
|
200
223
|
end
|
|
201
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
|
+
|
|
202
231
|
def max_allowed_failures
|
|
203
232
|
@max_allowed_failures ||= @options[:socket_max_failures] || 2
|
|
204
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'
|
|
@@ -34,7 +37,7 @@ module Dalli
|
|
|
34
37
|
return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN
|
|
35
38
|
return true unless tokens.first == VA
|
|
36
39
|
|
|
37
|
-
value =
|
|
40
|
+
value = read_data(tokens[1].to_i)
|
|
38
41
|
raw ? value : @value_marshaller.retrieve(value, bitflags_from_tokens(tokens))
|
|
39
42
|
end
|
|
40
43
|
|
|
@@ -46,7 +49,7 @@ module Dalli
|
|
|
46
49
|
cas = cas_from_tokens(tokens)
|
|
47
50
|
return [nil, cas] unless tokens.first == VA
|
|
48
51
|
|
|
49
|
-
value =
|
|
52
|
+
value = read_data(tokens[1].to_i)
|
|
50
53
|
[raw ? value : @value_marshaller.retrieve(value, bitflags_from_tokens(tokens)), cas]
|
|
51
54
|
end
|
|
52
55
|
|
|
@@ -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
|
-
|
|
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
|
|
|
@@ -180,7 +193,8 @@ module Dalli
|
|
|
180
193
|
end
|
|
181
194
|
|
|
182
195
|
def check_value_size!(size)
|
|
183
|
-
|
|
196
|
+
limit = @value_marshaller.max_response_bytes
|
|
197
|
+
return if size >= 0 && size <= MAX_VALUE_BYTES && size <= limit
|
|
184
198
|
|
|
185
199
|
raise Dalli::DalliError, "Reply value size #{size} is out of range"
|
|
186
200
|
end
|
|
@@ -191,7 +205,14 @@ module Dalli
|
|
|
191
205
|
end
|
|
192
206
|
|
|
193
207
|
def contains_header?(buf)
|
|
194
|
-
buf
|
|
208
|
+
return false unless buf
|
|
209
|
+
|
|
210
|
+
term_idx = buf.index(TERMINATOR)
|
|
211
|
+
if (term_idx || buf.bytesize) + TERMINATOR.bytesize > MAX_HEADER_BYTES
|
|
212
|
+
raise Dalli::DalliError, 'Reply header exceeds 8 KiB'
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
!term_idx.nil?
|
|
195
216
|
end
|
|
196
217
|
|
|
197
218
|
def header_from_buffer(buf)
|
|
@@ -242,6 +263,15 @@ module Dalli
|
|
|
242
263
|
line = read_line
|
|
243
264
|
line&.split || []
|
|
244
265
|
end
|
|
266
|
+
|
|
267
|
+
# Values are read by the size in the VA line, not up to the next CRLF:
|
|
268
|
+
# a value can contain CRLF and can be longer than MAX_LINE_BYTES
|
|
269
|
+
def read_data(data_size)
|
|
270
|
+
# Checked before the terminator is added: -1 or -2 would otherwise
|
|
271
|
+
# read 1 or 0 bytes and leave the connection out of step
|
|
272
|
+
check_value_size!(data_size)
|
|
273
|
+
@io_source.read(data_size + TERMINATOR.bytesize)&.chomp!(TERMINATOR)
|
|
274
|
+
end
|
|
245
275
|
end
|
|
246
276
|
end
|
|
247
277
|
end
|
data/lib/dalli/protocol/meta.rb
CHANGED
|
@@ -176,7 +176,10 @@ module Dalli
|
|
|
176
176
|
# We need to read all the responses at once.
|
|
177
177
|
def noop
|
|
178
178
|
write_noop
|
|
179
|
-
response_processor.consume_all_responses_until_mn
|
|
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
|
|
180
183
|
end
|
|
181
184
|
|
|
182
185
|
def stats(info = nil)
|
|
@@ -97,11 +97,13 @@ module Dalli
|
|
|
97
97
|
end
|
|
98
98
|
|
|
99
99
|
# Checks whether we should apply compression when serializing a value
|
|
100
|
-
# based on the specified options. Returns false
|
|
101
|
-
#
|
|
102
|
-
#
|
|
103
|
-
#
|
|
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.
|
|
104
105
|
def compress_value?(value, req_options)
|
|
106
|
+
return false if req_options&.dig(:raw)
|
|
105
107
|
return false unless value.bytesize >= compression_min_size
|
|
106
108
|
return compress_by_default? unless req_options && !req_options[:compress].nil?
|
|
107
109
|
|
|
@@ -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/protocol.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'openssl'
|
|
3
4
|
require 'timeout'
|
|
4
5
|
|
|
5
6
|
module Dalli
|
|
@@ -15,5 +16,11 @@ module Dalli
|
|
|
15
16
|
else
|
|
16
17
|
[Timeout::Error]
|
|
17
18
|
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
|
|
18
25
|
end
|
|
19
26
|
end
|
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
|
-
|
|
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
|
-
|
|
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
|
|
|
@@ -132,12 +152,42 @@ module Dalli
|
|
|
132
152
|
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, timeval)
|
|
133
153
|
end
|
|
134
154
|
|
|
135
|
-
|
|
155
|
+
# Closes the TCP socket if the handshake fails or times out, so a failed
|
|
156
|
+
# attempt doesn't hold a file descriptor until GC.
|
|
157
|
+
def self.wrapping_ssl_socket(tcp_socket, host, ssl_context, socket_timeout = nil)
|
|
158
|
+
warn_if_unverified(ssl_context)
|
|
159
|
+
connected = false
|
|
136
160
|
ssl_socket = Dalli::Socket::SSLSocket.new(tcp_socket, ssl_context)
|
|
137
161
|
ssl_socket.hostname = host
|
|
138
162
|
ssl_socket.sync_close = true
|
|
139
|
-
ssl_socket.connect
|
|
163
|
+
Timeout.timeout(socket_timeout) { ssl_socket.connect }
|
|
164
|
+
connected = true
|
|
140
165
|
ssl_socket
|
|
166
|
+
ensure
|
|
167
|
+
tcp_socket.close unless connected
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Verification comes entirely from the caller's ssl_context, and a bare
|
|
171
|
+
# SSLContext.new verifies nothing, so warn (once per process) when the
|
|
172
|
+
# certificate or the hostname won't be checked.
|
|
173
|
+
def self.warn_if_unverified(ssl_context)
|
|
174
|
+
return if @ssl_verification_warning_logged
|
|
175
|
+
|
|
176
|
+
problem = unverified_problem(ssl_context)
|
|
177
|
+
return unless problem
|
|
178
|
+
|
|
179
|
+
Dalli.logger.warn "SECURITY WARNING: Dalli's ssl_context #{problem}. " \
|
|
180
|
+
'Use ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER, ' \
|
|
181
|
+
'verify_hostname: true) to verify the server.'
|
|
182
|
+
@ssl_verification_warning_logged = true
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def self.unverified_problem(ssl_context)
|
|
186
|
+
if !ssl_context.verify_mode.to_i.anybits?(::OpenSSL::SSL::VERIFY_PEER)
|
|
187
|
+
'verify_mode is VERIFY_NONE, so the server certificate is not checked'
|
|
188
|
+
elsif !ssl_context.verify_hostname
|
|
189
|
+
'verify_hostname is false, so the certificate is not checked against the hostname'
|
|
190
|
+
end
|
|
141
191
|
end
|
|
142
192
|
end
|
|
143
193
|
|
data/lib/dalli/version.rb
CHANGED
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: 3.2.
|
|
4
|
+
version: 3.2.16
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Peter M. Goldstein
|
|
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
90
90
|
- !ruby/object:Gem::Version
|
|
91
91
|
version: '0'
|
|
92
92
|
requirements: []
|
|
93
|
-
rubygems_version: 4.0.
|
|
93
|
+
rubygems_version: 4.0.24
|
|
94
94
|
specification_version: 4
|
|
95
95
|
summary: High performance memcached client for Ruby
|
|
96
96
|
test_files: []
|