提交 fd487860 编写于 作者: G Guillermo Iguaran

Modify the session serializer implementation

Rename allowed options to :marshal and :json, for custom serializers
only allow the use of custom classes.
上级 db5d6bf7
* Add `:serializer` option for `config.session_store :cookie_store`. This * Add `:serializer` option for `config.session_store :cookie_store`. This
changes default serializer when using `:cookie_store` to changes default serializer when using `:cookie_store`.
`ActionDispatch::Session::MarshalSerializer` which is wrapper on Marshal.
It is also possible to pass: It is possible to pass:
* `:json_serializer` which is secure wrapper on JSON using `JSON.parse` and * `:json` which is a secure wrapper on JSON using `JSON.parse` and
`JSON.generate` methods with quirks mode; `JSON.generate` methods with quirks mode;
* any other Symbol or String like `:my_custom_serializer` which will be * `:marshal` which is a wrapper on Marshal;
camelized and constantized in `ActionDispatch::Session` namespace; * serializer class with `load` and `dump` methods defined.
* serializer object with `load` and `dump` methods defined.
For new apps `:json` option is added by default and :marshal is used
when no option is specified.
*Łukasz Sarnacki + Matt Aimonetti* *Łukasz Sarnacki + Matt Aimonetti*
......
...@@ -466,10 +466,12 @@ def decrypt_and_verify(encrypted_message) ...@@ -466,10 +466,12 @@ def decrypt_and_verify(encrypted_message)
end end
def serializer def serializer
serializer = @options[:session_serializer] || :marshal_serializer serializer = @options[:session_serializer] || :marshal
case serializer case serializer
when Symbol, String when :marshal
ActionDispatch::Session.const_get(serializer.to_s.camelize) ActionDispatch::Session::MarshalSerializer
when :json
ActionDispatch::Session::JsonSerializer
else else
serializer serializer
end end
......
...@@ -379,7 +379,7 @@ def test_encrypted_cookie ...@@ -379,7 +379,7 @@ def test_encrypted_cookie
assert_equal 'bar', cookies.encrypted[:foo] assert_equal 'bar', cookies.encrypted[:foo]
end end
class ActionDispatch::Session::CustomJsonSerializer class CustomJsonSerializer
def self.load(value) def self.load(value)
JSON.load(value) + " and loaded" JSON.load(value) + " and loaded"
end end
...@@ -389,20 +389,14 @@ def self.dump(value) ...@@ -389,20 +389,14 @@ def self.dump(value)
end end
end end
def test_encrypted_cookie_using_custom_json_serializer
@request.env["action_dispatch.session_serializer"] = :custom_json_serializer
get :set_encrypted_cookie
assert_equal 'bar was dumped and loaded', cookies.encrypted[:foo]
end
def test_encrypted_cookie_using_serializer_object def test_encrypted_cookie_using_serializer_object
@request.env["action_dispatch.session_serializer"] = ActionDispatch::Session::CustomJsonSerializer @request.env["action_dispatch.session_serializer"] = CustomJsonSerializer
get :set_encrypted_cookie get :set_encrypted_cookie
assert_equal 'bar was dumped and loaded', cookies.encrypted[:foo] assert_equal 'bar was dumped and loaded', cookies.encrypted[:foo]
end end
def test_encrypted_cookie_using_json_serializer def test_encrypted_cookie_using_json_serializer
@request.env["action_dispatch.session_serializer"] = :json_serializer @request.env["action_dispatch.session_serializer"] = :json
get :set_encrypted_cookie get :set_encrypted_cookie
cookies = @controller.send :cookies cookies = @controller.send :cookies
assert_not_equal 'bar', cookies[:foo] assert_not_equal 'bar', cookies[:foo]
......
...@@ -384,20 +384,14 @@ YourApp::Application.config.session_store :cookie_store, key: '_your_app_session ...@@ -384,20 +384,14 @@ YourApp::Application.config.session_store :cookie_store, key: '_your_app_session
You can pass `:serializer` key to specify serializer for serializing session: You can pass `:serializer` key to specify serializer for serializing session:
```ruby ```ruby
YourApp::Application.config.session_store :cookie_store, key: '_your_app_session', serializer: :json_serializer YourApp::Application.config.session_store :cookie_store, key: '_your_app_session', serializer: :json
``` ```
Default serializer is `:marshal_serializer`. When Symbol or String is passed it The default serializer for new application is `:json`. For compatibility with
will look for appropriate class in `ActionDispatch::Session` namespace, so old applications `:marshal` is used when `serializer` option is not specified.
passing `:my_custom_serializer` would load
`ActionDispatch::Session::MyCustomSerializer`.
```ruby It is also possible to pass a custom serializer class with `load` and `dump`
YourApp::Application.config.session_store :cookie_store, key: '_your_app_session', serializer: :my_custom_serializer public methods defined:
```
It is also possible to pass serializer object with defined `load` and `dump`
public methods:
```ruby ```ruby
YourApp::Application.config.session_store :cookie_store, key: '_your_app_session', serializer: MyCustomSerializer YourApp::Application.config.session_store :cookie_store, key: '_your_app_session', serializer: MyCustomSerializer
......
# Be sure to restart your server when you modify this file. # Be sure to restart your server when you modify this file.
Rails.application.config.session_store :cookie_store, key: <%= "'_#{app_name}_session'" %>, serializer: :json_serializer Rails.application.config.session_store :cookie_store, key: <%= "'_#{app_name}_session'" %>, serializer: :json
...@@ -433,7 +433,7 @@ def test_no_active_record_or_test_unit_if_skips_given ...@@ -433,7 +433,7 @@ def test_no_active_record_or_test_unit_if_skips_given
def test_new_hash_style def test_new_hash_style
run_generator [destination_root] run_generator [destination_root]
assert_file "config/initializers/session_store.rb" do |file| assert_file "config/initializers/session_store.rb" do |file|
assert_match(/config.session_store :cookie_store, key: '_.+_session', serializer: :json_serializer/, file) assert_match(/config.session_store :cookie_store, key: '_.+_session', serializer: :json/, file)
end end
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册