Problem with sessions
denny opened this issue · 4 comments
Hi; I've got this in routes.rb, and I haven't overridden any settings so far:
mount Sail::Engine, at: '/admin/settings'
When I try to load http://localhost:3000/admin/settings from a test I get this backtrace:
TypeError:
can't cast Rack::Session::SessionId
# /home/denny/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:388:in `commit_session'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:268:in `context'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:260:in `call'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/railties-6.1.0/lib/rails/rack/logger.rb:37:in `call_app'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/railties-6.1.0/lib/rails/rack/logger.rb:26:in `block in call'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/railties-6.1.0/lib/rails/rack/logger.rb:26:in `call'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/ahoy_matey-3.1.0/lib/ahoy/engine.rb:22:in `call_with_quiet_ahoy'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/request_store-1.5.0/lib/request_store/middleware.rb:19:in `call'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/method_override.rb:24:in `call'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/runtime.rb:22:in `call'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/sendfile.rb:110:in `call'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/railties-6.1.0/lib/rails/engine.rb:539:in `call'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
# /home/denny/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/test.rb:119:in `request'
# ./spec/requests/admin/sail_settings_spec.rb:20:in `block (3 levels) in <top (required)>'
I'm a bit out of my depth here, but I was wondering if it was because I'm using ActiveRecord::SessionStore and you're using ActionDispatch::Session::CookieStore ?
If I hit the URL with a browser, I get a longer backtrace in the logs that mentions both...
Started GET "/admin/settings" for ::1 at 2021-01-04 20:39:22 +0000
(0.5ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
TypeError (can't cast Rack::Session::SessionId):
activerecord (6.1.0) lib/active_record/connection_adapters/abstract/quoting.rb:246:in `_type_cast'
activerecord (6.1.0) lib/active_record/connection_adapters/postgresql/quoting.rb:162:in `_type_cast'
activerecord (6.1.0) lib/active_record/connection_adapters/abstract/quoting.rb:43:in `type_cast'
activerecord (6.1.0) lib/active_record/connection_adapters/abstract/quoting.rb:205:in `block in type_casted_binds'
activerecord (6.1.0) lib/active_record/connection_adapters/abstract/quoting.rb:203:in `map'
activerecord (6.1.0) lib/active_record/connection_adapters/abstract/quoting.rb:203:in `type_casted_binds'
activerecord (6.1.0) lib/active_record/connection_adapters/postgresql_adapter.rb:689:in `exec_cache'
activerecord (6.1.0) lib/active_record/connection_adapters/postgresql_adapter.rb:657:in `execute_and_clear'
activerecord (6.1.0) lib/active_record/connection_adapters/postgresql/database_statements.rb:53:in `exec_query'
activerecord (6.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:536:in `select_prepared'
activerecord (6.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:67:in `select_all'
activerecord (6.1.0) lib/active_record/connection_adapters/abstract/query_cache.rb:101:in `block in select_all'
activerecord (6.1.0) lib/active_record/connection_adapters/abstract/query_cache.rb:118:in `block in cache_sql'
activesupport (6.1.0) lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
activesupport (6.1.0) lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
activesupport (6.1.0) lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
activesupport (6.1.0) lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
activesupport (6.1.0) lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
activerecord (6.1.0) lib/active_record/connection_adapters/abstract/query_cache.rb:109:in `cache_sql'
activerecord (6.1.0) lib/active_record/connection_adapters/abstract/query_cache.rb:101:in `select_all'
activerecord (6.1.0) lib/active_record/querying.rb:47:in `find_by_sql'
activerecord (6.1.0) lib/active_record/relation.rb:850:in `block in exec_queries'
activerecord (6.1.0) lib/active_record/relation.rb:868:in `skip_query_cache_if_necessary'
activerecord (6.1.0) lib/active_record/relation.rb:835:in `exec_queries'
activerecord (6.1.0) lib/active_record/relation.rb:638:in `load'
activerecord (6.1.0) lib/active_record/relation.rb:249:in `records'
activerecord (6.1.0) lib/active_record/relation.rb:244:in `to_ary'
activerecord (6.1.0) lib/active_record/relation/finder_methods.rb:553:in `find_nth_with_limit'
activerecord (6.1.0) lib/active_record/relation/finder_methods.rb:538:in `find_nth'
activerecord (6.1.0) lib/active_record/relation/finder_methods.rb:122:in `first'
activerecord-session_store (f188efbc49a5) lib/active_record/session_store/session.rb:51:in `find_by_session_id'
activerecord-session_store (f188efbc49a5) lib/active_record/session_store/session.rb:28:in `find_by_session_id'
activerecord-session_store (f188efbc49a5) lib/action_dispatch/session/active_record_store.rb:120:in `block in get_session_model'
activesupport (6.1.0) lib/active_support/logger_silence.rb:18:in `block in silence'
activesupport (6.1.0) lib/active_support/logger_thread_safe_level.rb:52:in `log_at'
activesupport (6.1.0) lib/active_support/logger_silence.rb:18:in `silence'
activesupport (6.1.0) lib/active_support/logger.rb:64:in `block (3 levels) in broadcast'
activesupport (6.1.0) lib/active_support/logger_silence.rb:18:in `block in silence'
activesupport (6.1.0) lib/active_support/logger_thread_safe_level.rb:52:in `log_at'
activesupport (6.1.0) lib/active_support/logger_silence.rb:18:in `silence'
activesupport (6.1.0) lib/active_support/logger.rb:62:in `block (2 levels) in broadcast'
activerecord-session_store (f188efbc49a5) lib/action_dispatch/session/active_record_store.rb:119:in `get_session_model'
activerecord-session_store (f188efbc49a5) lib/action_dispatch/session/active_record_store.rb:136:in `find_session'
rack (2.2.3) lib/rack/session/abstract/id.rb:314:in `load_session'
actionpack (6.1.0) lib/action_dispatch/middleware/session/abstract_store.rb:45:in `block in load_session'
actionpack (6.1.0) lib/action_dispatch/middleware/session/abstract_store.rb:53:in `stale_session_check!'
actionpack (6.1.0) lib/action_dispatch/middleware/session/abstract_store.rb:45:in `load_session'
actionpack (6.1.0) lib/action_dispatch/request/session.rb:234:in `load!'
actionpack (6.1.0) lib/action_dispatch/request/session.rb:226:in `load_for_read!'
actionpack (6.1.0) lib/action_dispatch/request/session.rb:144:in `to_hash'
actionpack (6.1.0) lib/action_dispatch/request/session.rb:217:in `merge!'
actionpack (6.1.0) lib/action_dispatch/request/session.rb:217:in `merge!'
actionpack (6.1.0) lib/action_dispatch/request/session.rb:19:in `create'
actionpack (6.1.0) lib/action_dispatch/middleware/session/abstract_store.rb:71:in `prepare_session'
rack (2.2.3) lib/rack/session/abstract/id.rb:265:in `context'
rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (6.1.0) lib/action_dispatch/middleware/cookies.rb:689:in `call'
railties (6.1.0) lib/rails/engine.rb:539:in `call'
railties (6.1.0) lib/rails/railtie.rb:207:in `public_send'
railties (6.1.0) lib/rails/railtie.rb:207:in `method_missing'
actionpack (6.1.0) lib/action_dispatch/routing/mapper.rb:20:in `block in <class:Constraints>'
actionpack (6.1.0) lib/action_dispatch/routing/mapper.rb:49:in `serve'
actionpack (6.1.0) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (6.1.0) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.1.0) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.1.0) lib/action_dispatch/routing/route_set.rb:842:in `call'
warden (1.2.9) lib/warden/manager.rb:36:in `block in call'
warden (1.2.9) lib/warden/manager.rb:34:in `catch'
warden (1.2.9) lib/warden/manager.rb:34:in `call'
rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.3) lib/rack/etag.rb:27:in `call'
rack (2.2.3) lib/rack/conditional_get.rb:27:in `call'
rack (2.2.3) lib/rack/head.rb:12:in `call'
actionpack (6.1.0) lib/action_dispatch/http/permissions_policy.rb:22:in `call'
actionpack (6.1.0) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.2.3) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (6.1.0) lib/action_dispatch/middleware/cookies.rb:689:in `call'
activerecord (6.1.0) lib/active_record/migration.rb:601:in `call'
actionpack (6.1.0) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.1.0) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (6.1.0) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.1.0) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.1.0) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
actionpack (6.1.0) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call'
actionpack (6.1.0) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.1.0) lib/rails/rack/logger.rb:37:in `call_app'
railties (6.1.0) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.1.0) lib/active_support/tagged_logging.rb:99:in `block in tagged'
activesupport (6.1.0) lib/active_support/tagged_logging.rb:37:in `tagged'
activesupport (6.1.0) lib/active_support/tagged_logging.rb:99:in `tagged'
railties (6.1.0) lib/rails/rack/logger.rb:26:in `call'
ahoy_matey (3.1.0) lib/ahoy/engine.rb:22:in `call_with_quiet_ahoy'
sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.1.0) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
request_store (1.5.0) lib/request_store/middleware.rb:19:in `call'
actionpack (6.1.0) lib/action_dispatch/middleware/request_id.rb:26:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
activesupport (6.1.0) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.1.0) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.1.0) lib/action_dispatch/middleware/static.rb:24:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (6.1.0) lib/action_dispatch/middleware/host_authorization.rb:98:in `call'
webpacker (5.2.1) lib/webpacker/dev_server_proxy.rb:25:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.1.0) lib/rails/engine.rb:539:in `call'
puma (5.1.1) lib/puma/configuration.rb:246:in `call'
puma (5.1.1) lib/puma/request.rb:76:in `block in handle_request'
puma (5.1.1) lib/puma/thread_pool.rb:337:in `with_force_shutdown'
puma (5.1.1) lib/puma/request.rb:75:in `handle_request'
puma (5.1.1) lib/puma/server.rb:431:in `process_client'
puma (5.1.1) lib/puma/thread_pool.rb:145:in `block in spawn_thread'
Let me know if I'm doing something stupid :) or if there's any useful info I can provide otherwise.
Hello! Thank you for the bug report. I believe you are correct, Sail includes the cookie store middleware here. This is done so that Rails applications created in API mode can still use the dashboard.
We'll need to figure out a way of handling different session stores. I think the best option would be to somehow inherit from the parent application and if not defined use cookie store. I'll investigate if that's feasible and how to do it.
In the mean time, could you help me verify that this is indeed the issue? You should be able to test it out by cloning the repo, pointing to the local version of Sail in your project and then changing the line that uses cookie middleware to use SessionStore.
I can confirm that changing line 17 in engine.rb from:
config.middleware.use ActionDispatch::Session::CookieStore
to:
config.middleware.use ActionDispatch::Session::ActiveRecordStore
... does result in Sail loading as expected.
... actually, just getting rid of the cookie store seems adequate, I don't need to explicitly load the active record store in its place.