Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sessions to work across subdomains #8090

Merged
merged 3 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
end

require_relative "../lib/open_food_network/i18n_config"

require_relative '../lib/spree/core/environment'
require_relative '../lib/spree/core/mail_interceptor'
require_relative "../lib/session_cookie_upgrader"

if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Expand All @@ -34,6 +34,15 @@

module Openfoodnetwork
class Application < Rails::Application
config.middleware.insert_before(
ActionDispatch::Cookies,
SessionCookieUpgrader, {
old_key: "_session_id",
new_key: "_ofn_session_id",
domain: "." + ENV["SITE_URL"].delete_prefix("www.")
}
) if Rails.env.staging? || Rails.env.production?

config.after_initialize do
# We need this here because the test env file loads before the Spree engine is loaded
Spree::Core::Engine.routes.default_url_options[:host] = 'test.host' if Rails.env == 'test'
Expand Down
11 changes: 6 additions & 5 deletions config/initializers/session_store.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Be sure to restart your server when you modify this file.

# The cookie_store can be too small for very long URLs stored by Devise.
# The maximum size of cookies is 4096 bytes.
#Openfoodnetwork::Application.config.session_store :cookie_store, key: '_openfoodnetwork_session'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented code 👃 🔥


# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rails generate session_migration")
Openfoodnetwork::Application.config.session_store :active_record_store
Openfoodnetwork::Application.config.session_store(
:active_record_store,
key: "_ofn_session_id",
domain: :all,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the line that does the magic, correct? By passing these additional arguments it shares cookies across all subdomains?

Copy link
Contributor Author

@Matt-Yorkley Matt-Yorkley Aug 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the domain: :all and tld_length: 2 options mean the session cookie will work across any subdomains.

The name change is necessary here, as you can't modify the primary attributes of an existing cookie with the same name. If we didn't do it the changes wouldn't take effect until the user fully logged out and back in, and we don't know when that would happen.

tld_length: 2
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
= t 'legal.cookies_policy.essential_cookies_desc'

%table{ng: { controller:"CookiesPolicyModalCtrl"}}
= render_cookie_entry( "_session_id", t( "legal.cookies_policy.cookie_session_desc" ) )
= render_cookie_entry( "_ofn_session_id", t( "legal.cookies_policy.cookie_session_desc" ) )
= render_cookie_entry( "cookies_consent", t( "legal.cookies_policy.cookie_consent_desc" ) )
= render_cookie_entry( "remember_spree_user_token", t( "legal.cookies_policy.cookie_remember_me_desc" ) )
= render_cookie_entry( "qos_token", t( "legal.cookies_policy.cookie_openstreemap_desc" ), "openstreetmap.org" )
Expand Down
2 changes: 1 addition & 1 deletion engines/web/spec/features/consumer/cookies_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@

scenario "shows session_id cookies description with correct instance domain" do
visit '/#/policies/cookies'
expect(page).to have_content('_session_id')
expect(page).to have_content('_ofn_session_id')
.and have_content('127.0.0.1')
end

Expand Down
35 changes: 35 additions & 0 deletions lib/session_cookie_upgrader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

class SessionCookieUpgrader
def initialize(app, options = {})
@app = app
@options = options
end

def call(env)
request = ::Rack::Request.new(env)
cookies = request.cookies
old_key = @options[:old_key]
new_key = @options[:new_key]

# Set the session id for this request from the old session cookie (if present)
# This must be done before @app.call(env) or a new session will be initialized
cookies[new_key] = cookies[old_key] if cookies[old_key]

status, headers, body = @app.call(env)

if cookies[old_key]
# Create new session cookie with pre-existing session id
Rack::Utils.set_cookie_header!(
headers,
new_key,
{ value: cookies[old_key], path: "/", domain: @options[:domain] }
)

# Delete old session cookie
Rack::Utils.delete_cookie_header!(headers, old_key)
end

[status, headers, body]
end
end