Class: Gitlab::Middleware::SameSiteCookies

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/middleware/same_site_cookies.rb

Constant Summary collapse

"\n"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ SameSiteCookies

Returns a new instance of SameSiteCookies.


22
23
24
# File 'lib/gitlab/middleware/same_site_cookies.rb', line 22

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object


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
# File 'lib/gitlab/middleware/same_site_cookies.rb', line 26

def call(env)
  status, headers, body = @app.call(env)
  result = [status, headers, body]

  set_cookie = headers['Set-Cookie']&.strip

  return result if set_cookie.blank? || !ssl?
  return result if same_site_none_incompatible?(env['HTTP_USER_AGENT'])

  cookies = set_cookie.split(COOKIE_SEPARATOR)

  cookies.each do |cookie|
    next if cookie.blank?

    # Chrome will drop SameSite=None cookies without the Secure
    # flag. If we remove this middleware, we may need to ensure
    # that all cookies set this flag.
    unless SECURE_REGEX.match?(cookie)
      cookie << '; Secure'
    end

    unless SAME_SITE_REGEX.match?(cookie)
      cookie << '; SameSite=None'
    end
  end

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

  result
end