Module: Dalli::Protocol::Meta::KeyRegularizer
- Defined in:
- lib/dalli/protocol/key_regularizer.rb
Overview
The meta protocol requires that keys be ASCII only, so Unicode keys are not supported. In addition, the use of whitespace in the key is not allowed. memcached supports the use of base64 hashes for keys containing whitespace or non-ASCII characters, provided the 'b' flag is included in the request.
Class Method Summary collapse
- .decode(encoded_key) ⇒ Object
- .encode(key) ⇒ Object
-
.required?(key) ⇒ Boolean
protocol.txt requires that a key "must not include control characters or whitespace" -- \pCntrl is C0 (0x00-0x1F) plus DEL (0x7F).
Class Method Details
.decode(encoded_key) ⇒ Object
40 41 42 43 |
# File 'lib/dalli/protocol/key_regularizer.rb', line 40 def decode(encoded_key) strict_base64_decoded = encoded_key.unpack1('m0') strict_base64_decoded.force_encoding(Encoding::UTF_8) end |
.encode(key) ⇒ Object
36 37 38 |
# File 'lib/dalli/protocol/key_regularizer.rb', line 36 def encode(key) [key].pack('m0') end |
.required?(key) ⇒ Boolean
protocol.txt requires that a key "must not include control characters or whitespace" -- \pCntrl is C0 (0x00-0x1F) plus DEL (0x7F). \s alone misses NUL and the rest of that range: a key containing one of those bytes but no whitespace is ASCII-only, so it would otherwise be written to the wire unencoded. Not a protocol-injection risk (the text protocol splits on CRLF, not other control bytes), but a downstream consumer that treats the key specially at one of those bytes (a C string terminating at NUL, a terminal or log line interpreting an escape byte) could silently act on a different key than Dalli believes it sent.
Written as \pCntrl rather than the POSIX [:cntrl:] bracket class: \s and [:cntrl:] overlap (tab, newline, CR are in both), and Ruby warns "character class has duplicated range" when they're combined in one -- fatal here, since this suite's -w run treats warnings as errors (see test_strict_warnings.rb). \pCntrl matches the same bytes without the overlap warning.
32 33 34 |
# File 'lib/dalli/protocol/key_regularizer.rb', line 32 def required?(key) !key.ascii_only? || /[\p{Cntrl}\s]/.match?(key) end |