2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-21 02:52:54 -04:00
|
|
|
# A module to check CSRF tokens in requests.
|
|
|
|
# It's used in API helpers and OmniAuth.
|
|
|
|
# Usage: GitLab::RequestForgeryProtection.call(env)
|
2015-04-24 11:03:18 -04:00
|
|
|
|
2017-06-22 01:19:14 -04:00
|
|
|
module Gitlab
|
2015-12-08 08:41:19 -05:00
|
|
|
module RequestForgeryProtection
|
|
|
|
class Controller < ActionController::Base
|
2018-06-21 06:44:31 -04:00
|
|
|
protect_from_forgery with: :exception, prepend: true
|
2015-04-24 11:03:18 -04:00
|
|
|
|
2015-12-08 08:41:19 -05:00
|
|
|
def index
|
|
|
|
head :ok
|
2015-04-24 11:03:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-08 08:41:19 -05:00
|
|
|
def self.app
|
|
|
|
@app ||= Controller.action(:index)
|
2015-04-24 11:03:18 -04:00
|
|
|
end
|
|
|
|
|
2015-12-08 08:41:19 -05:00
|
|
|
def self.call(env)
|
|
|
|
app.call(env)
|
2015-04-24 11:03:18 -04:00
|
|
|
end
|
2017-07-26 05:25:10 -04:00
|
|
|
|
|
|
|
def self.verified?(env)
|
2021-02-10 13:09:02 -05:00
|
|
|
minimal_env = env.slice('REQUEST_METHOD', 'rack.session', 'HTTP_X_CSRF_TOKEN')
|
|
|
|
.merge('rack.input' => '')
|
|
|
|
call(minimal_env)
|
2017-07-26 05:25:10 -04:00
|
|
|
|
|
|
|
true
|
|
|
|
rescue ActionController::InvalidAuthenticityToken
|
|
|
|
false
|
|
|
|
end
|
2015-04-24 11:03:18 -04:00
|
|
|
end
|
|
|
|
end
|