Sha256: 01357dc09990bb62d033d4443715059ffe31ebd0966da585fc0de162c97f00a6

Contents?: true

Size: 912 Bytes

Versions: 18

Compression:

Stored size: 912 Bytes

Contents

# frozen_string_literal: true
module ShopifyApp
  class SameSiteCookieMiddleware
    COOKIE_SEPARATOR = "\n"

    def initialize(app)
      @app = app
    end

    def call(env)
      status, headers, body = @app.call(env)
      user_agent = env['HTTP_USER_AGENT']

      if headers && headers['Set-Cookie'] &&
          BrowserSniffer.new(user_agent).same_site_none_compatible? &&
          ShopifyApp.configuration.enable_same_site_none &&
          Rack::Request.new(env).ssl?

        set_cookies = headers['Set-Cookie']
          .split(COOKIE_SEPARATOR)
          .compact
          .map do |cookie|
            cookie << '; Secure' unless cookie =~ /;\s*secure/i
            cookie << '; SameSite=None' if ShopifyApp.configuration.embedded_app?
            cookie
          end

        headers['Set-Cookie'] = set_cookies.join(COOKIE_SEPARATOR)
      end

      [status, headers, body]
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
ruby_shopify_app-1.3.3 lib/ruby_shopify_app/middleware/same_site_cookie_middleware.rb
ruby_shopify_app-1.3.2 lib/ruby_shopify_app/middleware/same_site_cookie_middleware.rb
ruby_shopify_app-1.3.1 lib/ruby_shopify_app/middleware/same_site_cookie_middleware.rb
ruby_shopify_app-1.3.0 lib/ruby_shopify_app/middleware/same_site_cookie_middleware.rb
ruby_shopify_app-1.2.0 lib/ruby_shopify_app/middleware/same_site_cookie_middleware.rb
ruby_shopify_app-1.1.0 lib/ruby_shopify_app/middleware/same_site_cookie_middleware.rb
ruby_shopify_app-1.0.0 lib/shopify_app/middleware/same_site_cookie_middleware.rb
shopify_app-18.1.3 lib/shopify_app/middleware/same_site_cookie_middleware.rb
shopify_app-18.1.2 lib/shopify_app/middleware/same_site_cookie_middleware.rb
shopify_app-18.1.1 lib/shopify_app/middleware/same_site_cookie_middleware.rb
shopify_app-18.1.0 lib/shopify_app/middleware/same_site_cookie_middleware.rb
shopify_app-18.0.4 lib/shopify_app/middleware/same_site_cookie_middleware.rb
shopify_app-18.0.3 lib/shopify_app/middleware/same_site_cookie_middleware.rb
shopify_app-18.0.2 lib/shopify_app/middleware/same_site_cookie_middleware.rb
shopify_app-18.0.1 lib/shopify_app/middleware/same_site_cookie_middleware.rb
shopify_app-18.0.0 lib/shopify_app/middleware/same_site_cookie_middleware.rb
shopify_app-17.2.1 lib/shopify_app/middleware/same_site_cookie_middleware.rb
shopify_app-17.2.0 lib/shopify_app/middleware/same_site_cookie_middleware.rb