1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
|
Description: Replace marshalling with pluggable serializers
Author: Tom Scott <tscott@weblinc.com>
Bug: https://github.com/redis-store/redis-store/issues/289
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=882034
Applied-Upstream: https://github.com/redis-store/redis-store/commit/e0c1398d54a9661c8c70267c3a925ba6b192142e
Origin: upstream
Last-Update: Tue, 15 Aug 2017 11:07:07 -0400
This is in response to a vulnerability warning we received on Friday,
August 11th, 2017. While most users will not be affected by this
change, we recommend that developers of new applications use a different
serializer other than `Marshal`. This, along with the removal of the
`:marshalling` option, will enforce "sane defaults" in terms of securely
serializing/de-serializing data.
- Add `:serializer` option and deprecate `:marshalling`. Although you
will still be able to enable/disable serialization with Marshal using
`:marshalling` in the 1.x series, this will be removed by 2.0.
- Rename `Redis::Store::Marshalling` to `Redis::Store::Serialization` to
reflect its new purpose.
Fixes #289
---
lib/redis-store.rb | 12 -------
lib/redis/store.rb | 28 +++++++++++++--
lib/redis/store/factory.rb | 9 ++++-
lib/redis/store/namespace.rb | 4 +--
.../store/{marshalling.rb => serialization.rb} | 6 ++--
test/redis/store/factory_test.rb | 40 ++++++++++++++++++++--
test/redis/store/namespace_test.rb | 4 +--
.../{marshalling_test.rb => serialization_test.rb} | 4 +--
8 files changed, 80 insertions(+), 27 deletions(-)
rename lib/redis/store/{marshalling.rb => serialization.rb} (90%)
rename test/redis/store/{marshalling_test.rb => serialization_test.rb} (98%)
--- a/lib/redis-store.rb
+++ b/lib/redis-store.rb
@@ -1,12 +1 @@
-require 'redis'
require 'redis/store'
-require 'redis/store/factory'
-require 'redis/distributed_store'
-require 'redis/store/namespace'
-require 'redis/store/marshalling'
-require 'redis/store/version'
-
-class Redis
- class Store < self
- end
-end
--- a/lib/redis/store.rb
+++ b/lib/redis/store.rb
@@ -1,3 +1,9 @@
+require 'redis'
+require 'redis/store/factory'
+require 'redis/distributed_store'
+require 'redis/store/namespace'
+require 'redis/store/serialization'
+require 'redis/store/version'
require 'redis/store/ttl'
require 'redis/store/interface'
@@ -7,6 +13,24 @@
def initialize(options = { })
super
+
+ unless options[:marshalling].nil?
+ puts %(
+ DEPRECATED: You are passing the :marshalling option, which has been
+ replaced with `serializer: Marshal` to support pluggable serialization
+ backends. To disable serialization (much like disabling marshalling),
+ pass `serializer: nil` in your configuration.
+
+ The :marshalling option will be removed for redis-store 2.0.
+ )
+ end
+
+ @serializer = options.key?(:serializer) ? options[:serializer] : Marshal
+
+ unless options[:marshalling].nil?
+ @serializer = options[:marshalling] ? Marshal : nil
+ end
+
_extend_marshalling options
_extend_namespace options
end
@@ -22,8 +46,7 @@
private
def _extend_marshalling(options)
- @marshalling = !(options[:marshalling] === false) # HACK - TODO delegate to Factory
- extend Marshalling if @marshalling
+ extend Serialization unless @serializer.nil?
end
def _extend_namespace(options)
--- a/lib/redis/store/marshalling.rb
+++ /dev/null
@@ -1,56 +0,0 @@
-class Redis
- class Store < self
- module Marshalling
- def set(key, value, options = nil)
- _marshal(value, options) { |value| super encode(key), encode(value), options }
- end
-
- def setnx(key, value, options = nil)
- _marshal(value, options) { |value| super encode(key), encode(value), options }
- end
-
- def setex(key, expiry, value, options = nil)
- _marshal(value, options) { |value| super encode(key), expiry, encode(value), options }
- end
-
- def get(key, options = nil)
- _unmarshal super(key), options
- end
-
- def mget(*keys)
- options = keys.pop if keys.last.is_a?(Hash)
- super(*keys).map do |result|
- _unmarshal result, options
- end
- end
-
- private
- def _marshal(val, options)
- yield marshal?(options) ? Marshal.dump(val) : val
- end
-
- def _unmarshal(val, options)
- unmarshal?(val, options) ? Marshal.load(val) : val
- end
-
- def marshal?(options)
- !(options && options[:raw])
- end
-
- def unmarshal?(result, options)
- result && result.size > 0 && marshal?(options)
- end
-
- if defined?(Encoding)
- def encode(string)
- key = string.to_s.dup
- key.force_encoding(Encoding::BINARY)
- end
- else
- def encode(string)
- string
- end
- end
- end
- end
-end
--- /dev/null
+++ b/lib/redis/store/serialization.rb
@@ -0,0 +1,56 @@
+class Redis
+ class Store < self
+ module Serialization
+ def set(key, value, options = nil)
+ _marshal(value, options) { |value| super encode(key), encode(value), options }
+ end
+
+ def setnx(key, value, options = nil)
+ _marshal(value, options) { |value| super encode(key), encode(value), options }
+ end
+
+ def setex(key, expiry, value, options = nil)
+ _marshal(value, options) { |value| super encode(key), expiry, encode(value), options }
+ end
+
+ def get(key, options = nil)
+ _unmarshal super(key), options
+ end
+
+ def mget(*keys)
+ options = keys.pop if keys.last.is_a?(Hash)
+ super(*keys).map do |result|
+ _unmarshal result, options
+ end
+ end
+
+ private
+ def _marshal(val, options)
+ yield marshal?(options) ? @serializer.dump(val) : val
+ end
+
+ def _unmarshal(val, options)
+ unmarshal?(val, options) ? @serializer.load(val) : val
+ end
+
+ def marshal?(options)
+ !(options && options[:raw])
+ end
+
+ def unmarshal?(result, options)
+ result && result.size > 0 && marshal?(options)
+ end
+
+ if defined?(Encoding)
+ def encode(string)
+ key = string.to_s.dup
+ key.force_encoding(Encoding::BINARY)
+ end
+ else
+ def encode(string)
+ string
+ end
+ end
+ end
+ end
+end
--- a/test/redis/store/factory_test.rb
+++ b/test/redis/store/factory_test.rb
@@ -1,4 +1,5 @@
require 'test_helper'
+require 'json'
describe "Redis::Store::Factory" do
describe ".create" do
@@ -41,9 +42,11 @@
store.instance_variable_get(:@client).password.must_equal("secret")
end
- it "allows/disable marshalling" do
- store = Redis::Store::Factory.create :marshalling => false
- store.instance_variable_get(:@marshalling).must_equal(false)
+
+ it "disables serialization" do
+ store = Redis::Store::Factory.create :serializer => nil
+ store.instance_variable_get(:@serializer).must_be_nil
+ store.instance_variable_get(:@options)[:raw].must_equal(true)
end
it "should instantiate a Redis::DistributedStore store" do
--- a/test/redis/store/namespace_test.rb
+++ b/test/redis/store/namespace_test.rb
@@ -3,7 +3,7 @@
describe "Redis::Store::Namespace" do
def setup
@namespace = "theplaylist"
- @store = Redis::Store.new :namespace => @namespace, :marshalling => false # TODO remove mashalling option
+ @store = Redis::Store.new :namespace => @namespace, :serializer => nil
@client = @store.instance_variable_get(:@client)
@rabbit = "bunny"
@default_store = Redis::Store.new
@@ -77,7 +77,7 @@
end
describe 'method calls' do
- let(:store){Redis::Store.new :namespace => @namespace, :marshalling => false}
+ let(:store){Redis::Store.new :namespace => @namespace, :serializer => nil}
let(:client){store.instance_variable_get(:@client)}
it "should namespace get" do
--- a/test/redis/store/marshalling_test.rb
+++ /dev/null
@@ -1,128 +0,0 @@
-require 'test_helper'
-
-describe "Redis::Marshalling" do
- def setup
- @store = Redis::Store.new :marshalling => true
- @rabbit = OpenStruct.new :name => "bunny"
- @white_rabbit = OpenStruct.new :color => "white"
- @store.set "rabbit", @rabbit
- @store.del "rabbit2"
- end
-
- def teardown
- @store.flushdb
- @store.quit
- end
-
- it "unmarshals on get" do
- @store.get("rabbit").must_equal(@rabbit)
- end
-
- it "marshals on set" do
- @store.set "rabbit", @white_rabbit
- @store.get("rabbit").must_equal(@white_rabbit)
- end
-
- if RUBY_VERSION.match /1\.9/
- it "doesn't unmarshal on get if raw option is true" do
- @store.get("rabbit", :raw => true).must_equal("\x04\bU:\x0FOpenStruct{\x06:\tnameI\"\nbunny\x06:\x06EF")
- end
- else
- it "doesn't unmarshal on get if raw option is true" do
- @store.get("rabbit", :raw => true).must_include("\x04\bU:\x0FOpenStruct{\x06:\tname")
- end
- end
-
- it "doesn't marshal set if raw option is true" do
- @store.set "rabbit", @white_rabbit, :raw => true
- @store.get("rabbit", :raw => true).must_equal(%(#<OpenStruct color="white">))
- end
-
- it "doesn't unmarshal if get returns an empty string" do
- @store.set "empty_string", ""
- @store.get("empty_string").must_equal("")
- # TODO use a meaningful Exception
- # lambda { @store.get("empty_string").must_equal("") }.wont_raise Exception
- end
-
- it "doesn't set an object if already exist" do
- @store.setnx "rabbit", @white_rabbit
- @store.get("rabbit").must_equal(@rabbit)
- end
-
- it "marshals on set unless exists" do
- @store.setnx "rabbit2", @white_rabbit
- @store.get("rabbit2").must_equal(@white_rabbit)
- end
-
- it "doesn't marshal on set unless exists if raw option is true" do
- @store.setnx "rabbit2", @white_rabbit, :raw => true
- @store.get("rabbit2", :raw => true).must_equal(%(#<OpenStruct color="white">))
- end
-
- it "marshals on set expire" do
- @store.setex "rabbit2", 1, @white_rabbit
- @store.get("rabbit2").must_equal(@white_rabbit)
- sleep 2
- @store.get("rabbit2").must_be_nil
- end
-
- it "doesn't unmarshal on multi get" do
- @store.set "rabbit2", @white_rabbit
- rabbit, rabbit2 = @store.mget "rabbit", "rabbit2"
- rabbit.must_equal(@rabbit)
- rabbit2.must_equal(@white_rabbit)
- end
-
- if RUBY_VERSION.match /1\.9/
- it "doesn't unmarshal on multi get if raw option is true" do
- @store.set "rabbit2", @white_rabbit
- rabbit, rabbit2 = @store.mget "rabbit", "rabbit2", :raw => true
- rabbit.must_equal("\x04\bU:\x0FOpenStruct{\x06:\tnameI\"\nbunny\x06:\x06EF")
- rabbit2.must_equal("\x04\bU:\x0FOpenStruct{\x06:\ncolorI\"\nwhite\x06:\x06EF")
- end
- else
- it "doesn't unmarshal on multi get if raw option is true" do
- @store.set "rabbit2", @white_rabbit
- rabbit, rabbit2 = @store.mget "rabbit", "rabbit2", :raw => true
- rabbit.must_include("\x04\bU:\x0FOpenStruct{\x06:\tname")
- rabbit2.must_include("\x04\bU:\x0FOpenStruct{\x06:\ncolor")
- end
- end
-
- describe "binary safety" do
- it "marshals objects" do
- utf8_key = [51339].pack("U*")
- ascii_rabbit = OpenStruct.new(:name => [128].pack("C*"))
-
- @store.set(utf8_key, ascii_rabbit)
- @store.get(utf8_key).must_equal(ascii_rabbit)
- end
-
- it "gets and sets raw values" do
- utf8_key = [51339].pack("U*")
- ascii_string = [128].pack("C*")
-
- @store.set(utf8_key, ascii_string, :raw => true)
- @store.get(utf8_key, :raw => true).bytes.to_a.must_equal(ascii_string.bytes.to_a)
- end
-
- it "marshals objects on setnx" do
- utf8_key = [51339].pack("U*")
- ascii_rabbit = OpenStruct.new(:name => [128].pack("C*"))
-
- @store.del(utf8_key)
- @store.setnx(utf8_key, ascii_rabbit)
- @store.get(utf8_key).must_equal(ascii_rabbit)
- end
-
- it "gets and sets raw values on setnx" do
- utf8_key = [51339].pack("U*")
- ascii_string = [128].pack("C*")
-
- @store.del(utf8_key)
- @store.setnx(utf8_key, ascii_string, :raw => true)
- @store.get(utf8_key, :raw => true).bytes.to_a.must_equal(ascii_string.bytes.to_a)
- end
- end if defined?(Encoding)
-end
--- /dev/null
+++ b/test/redis/store/serialization_test.rb
@@ -0,0 +1,128 @@
+require 'test_helper'
+
+describe "Redis::Serialization" do
+ def setup
+ @store = Redis::Store.new serializer: Marshal
+ @rabbit = OpenStruct.new :name => "bunny"
+ @white_rabbit = OpenStruct.new :color => "white"
+ @store.set "rabbit", @rabbit
+ @store.del "rabbit2"
+ end
+
+ def teardown
+ @store.flushdb
+ @store.quit
+ end
+
+ it "unmarshals on get" do
+ @store.get("rabbit").must_equal(@rabbit)
+ end
+
+ it "marshals on set" do
+ @store.set "rabbit", @white_rabbit
+ @store.get("rabbit").must_equal(@white_rabbit)
+ end
+
+ if RUBY_VERSION.match /1\.9/
+ it "doesn't unmarshal on get if raw option is true" do
+ @store.get("rabbit", :raw => true).must_equal("\x04\bU:\x0FOpenStruct{\x06:\tnameI\"\nbunny\x06:\x06EF")
+ end
+ else
+ it "doesn't unmarshal on get if raw option is true" do
+ @store.get("rabbit", :raw => true).must_include("\x04\bU:\x0FOpenStruct{\x06:\tname")
+ end
+ end
+
+ it "doesn't marshal set if raw option is true" do
+ @store.set "rabbit", @white_rabbit, :raw => true
+ @store.get("rabbit", :raw => true).must_equal(%(#<OpenStruct color="white">))
+ end
+
+ it "doesn't unmarshal if get returns an empty string" do
+ @store.set "empty_string", ""
+ @store.get("empty_string").must_equal("")
+ # TODO use a meaningful Exception
+ # lambda { @store.get("empty_string").must_equal("") }.wont_raise Exception
+ end
+
+ it "doesn't set an object if already exist" do
+ @store.setnx "rabbit", @white_rabbit
+ @store.get("rabbit").must_equal(@rabbit)
+ end
+
+ it "marshals on set unless exists" do
+ @store.setnx "rabbit2", @white_rabbit
+ @store.get("rabbit2").must_equal(@white_rabbit)
+ end
+
+ it "doesn't marshal on set unless exists if raw option is true" do
+ @store.setnx "rabbit2", @white_rabbit, :raw => true
+ @store.get("rabbit2", :raw => true).must_equal(%(#<OpenStruct color="white">))
+ end
+
+ it "marshals on set expire" do
+ @store.setex "rabbit2", 1, @white_rabbit
+ @store.get("rabbit2").must_equal(@white_rabbit)
+ sleep 2
+ @store.get("rabbit2").must_be_nil
+ end
+
+ it "doesn't unmarshal on multi get" do
+ @store.set "rabbit2", @white_rabbit
+ rabbit, rabbit2 = @store.mget "rabbit", "rabbit2"
+ rabbit.must_equal(@rabbit)
+ rabbit2.must_equal(@white_rabbit)
+ end
+
+ if RUBY_VERSION.match /1\.9/
+ it "doesn't unmarshal on multi get if raw option is true" do
+ @store.set "rabbit2", @white_rabbit
+ rabbit, rabbit2 = @store.mget "rabbit", "rabbit2", :raw => true
+ rabbit.must_equal("\x04\bU:\x0FOpenStruct{\x06:\tnameI\"\nbunny\x06:\x06EF")
+ rabbit2.must_equal("\x04\bU:\x0FOpenStruct{\x06:\ncolorI\"\nwhite\x06:\x06EF")
+ end
+ else
+ it "doesn't unmarshal on multi get if raw option is true" do
+ @store.set "rabbit2", @white_rabbit
+ rabbit, rabbit2 = @store.mget "rabbit", "rabbit2", :raw => true
+ rabbit.must_include("\x04\bU:\x0FOpenStruct{\x06:\tname")
+ rabbit2.must_include("\x04\bU:\x0FOpenStruct{\x06:\ncolor")
+ end
+ end
+
+ describe "binary safety" do
+ it "marshals objects" do
+ utf8_key = [51339].pack("U*")
+ ascii_rabbit = OpenStruct.new(:name => [128].pack("C*"))
+
+ @store.set(utf8_key, ascii_rabbit)
+ @store.get(utf8_key).must_equal(ascii_rabbit)
+ end
+
+ it "gets and sets raw values" do
+ utf8_key = [51339].pack("U*")
+ ascii_string = [128].pack("C*")
+
+ @store.set(utf8_key, ascii_string, :raw => true)
+ @store.get(utf8_key, :raw => true).bytes.to_a.must_equal(ascii_string.bytes.to_a)
+ end
+
+ it "marshals objects on setnx" do
+ utf8_key = [51339].pack("U*")
+ ascii_rabbit = OpenStruct.new(:name => [128].pack("C*"))
+
+ @store.del(utf8_key)
+ @store.setnx(utf8_key, ascii_rabbit)
+ @store.get(utf8_key).must_equal(ascii_rabbit)
+ end
+
+ it "gets and sets raw values on setnx" do
+ utf8_key = [51339].pack("U*")
+ ascii_string = [128].pack("C*")
+
+ @store.del(utf8_key)
+ @store.setnx(utf8_key, ascii_string, :raw => true)
+ @store.get(utf8_key, :raw => true).bytes.to_a.must_equal(ascii_string.bytes.to_a)
+ end
+ end if defined?(Encoding)
+end
--- a/lib/redis/store/factory.rb
+++ b/lib/redis/store/factory.rb
@@ -50,6 +50,14 @@
if options.key?(:key_prefix) && !options.key?(:namespace)
options[:namespace] = options.delete(:key_prefix) # RailsSessionStore
end
+ options[:raw] = case
+ when options.key?(:serializer)
+ options[:serializer].nil?
+ when options.key?(:marshalling)
+ !options[:marshalling]
+ else
+ false
+ end
options
end
--- a/lib/redis/store/namespace.rb
+++ b/lib/redis/store/namespace.rb
@@ -44,8 +44,8 @@
def mget(*keys)
options = keys.pop if keys.last.is_a? Hash
if keys.any?
- # Marshalling gets extended before Namespace does, so we need to pass options further
- if singleton_class.ancestors.include? Marshalling
+ # Serialization gets extended before Namespace does, so we need to pass options further
+ if singleton_class.ancestors.include? Serialization
super *keys.map {|key| interpolate(key) }, options
else
super *keys.map {|key| interpolate(key) }
|