Commit 7029aa51 authored by Axel García's avatar Axel García

Address Content Security Policy concerns

Moves CSP from the default/global level to the
controllers, so the exception gets less risky.
parent aa00d221
...@@ -15,6 +15,16 @@ class RegistrationsController < Devise::RegistrationsController ...@@ -15,6 +15,16 @@ class RegistrationsController < Devise::RegistrationsController
feature_category :authentication_and_authorization feature_category :authentication_and_authorization
content_security_policy do |policy|
next if policy.directives.blank?
script_src_values = Array.wrap(policy.directives['script-src']) | ['https://cdn.cookielaw.org https://*.onetrust.com']
policy.script_src(*script_src_values)
connect_src_values = Array.wrap(policy.directives['connect-src']) | ['https://cdn.cookielaw.org']
policy.connect_src(*connect_src_values)
end
def new def new
@resource = build_resource @resource = build_resource
end end
......
...@@ -55,6 +55,16 @@ class SessionsController < Devise::SessionsController ...@@ -55,6 +55,16 @@ class SessionsController < Devise::SessionsController
CAPTCHA_HEADER = 'X-GitLab-Show-Login-Captcha' CAPTCHA_HEADER = 'X-GitLab-Show-Login-Captcha'
MAX_FAILED_LOGIN_ATTEMPTS = 5 MAX_FAILED_LOGIN_ATTEMPTS = 5
content_security_policy do |policy|
next if policy.directives.blank?
script_src_values = Array.wrap(policy.directives['script-src']) | ['https://cdn.cookielaw.org https://*.onetrust.com']
policy.script_src(*script_src_values)
connect_src_values = Array.wrap(policy.directives['connect-src']) | ['https://cdn.cookielaw.org']
policy.connect_src(*connect_src_values)
end
def new def new
set_minimum_password_length set_minimum_password_length
......
- if one_trust_enabled? - if one_trust_enabled?
<!-- OneTrust --> <!-- OneTrust -->
= javascript_include_tag "https://cdn.cookielaw.org/consent/#{extra_config.one_trust_id}/OtAutoBlock.js" = javascript_include_tag "https://cdn.cookielaw.org/consent/#{extra_config.one_trust_id}/OtAutoBlock.js"
%script{ :src => "https://cdn.cookielaw.org/scripttemplates/otSDKStub.js", :charset => "UTF-8", :"data-domain-script" => extra_config.one_trust_id, :defer => true, :nonce => true } %script{ :src => "https://cdn.cookielaw.org/scripttemplates/otSDKStub.js", :charset => "UTF-8", :"data-domain-script" => extra_config.one_trust_id, :defer => true, :nonce => content_security_policy_nonce }
= javascript_tag nonce: true do = javascript_tag nonce: content_security_policy_nonce do
:plain :plain
function OptanonWrapper() { } function OptanonWrapper() { }
...@@ -12,6 +12,16 @@ class TrialRegistrationsController < RegistrationsController ...@@ -12,6 +12,16 @@ class TrialRegistrationsController < RegistrationsController
before_action :check_if_gl_com_or_dev before_action :check_if_gl_com_or_dev
before_action :set_redirect_url, only: [:new] before_action :set_redirect_url, only: [:new]
content_security_policy do |policy|
next if policy.directives.blank?
script_src_values = Array.wrap(policy.directives['script-src']) | ['https://cdn.cookielaw.org https://*.onetrust.com']
policy.script_src(*script_src_values)
connect_src_values = Array.wrap(policy.directives['connect-src']) | ['https://cdn.cookielaw.org']
policy.connect_src(*connect_src_values)
end
def new def new
end end
......
...@@ -15,7 +15,7 @@ module Gitlab ...@@ -15,7 +15,7 @@ module Gitlab
directives = { directives = {
'default_src' => "'self'", 'default_src' => "'self'",
'base_uri' => "'self'", 'base_uri' => "'self'",
'connect_src' => "'self' https://cdn.cookielaw.org", 'connect_src' => "'self'",
'font_src' => "'self'", 'font_src' => "'self'",
'form_action' => "'self' https: http:", 'form_action' => "'self' https: http:",
'frame_ancestors' => "'self'", 'frame_ancestors' => "'self'",
...@@ -23,7 +23,7 @@ module Gitlab ...@@ -23,7 +23,7 @@ module Gitlab
'img_src' => "'self' data: blob: http: https:", 'img_src' => "'self' data: blob: http: https:",
'manifest_src' => "'self'", 'manifest_src' => "'self'",
'media_src' => "'self'", 'media_src' => "'self'",
'script_src' => "'strict-dynamic' 'self' 'unsafe-inline' 'unsafe-eval' https://www.google.com/recaptcha/ https://www.recaptcha.net https://apis.google.com https://cdn.cookielaw.org", 'script_src' => "'strict-dynamic' 'self' 'unsafe-inline' 'unsafe-eval' https://www.google.com/recaptcha/ https://www.recaptcha.net https://apis.google.com",
'style_src' => "'self' 'unsafe-inline'", 'style_src' => "'self' 'unsafe-inline'",
'worker_src' => "'self' blob: data:", 'worker_src' => "'self' blob: data:",
'object_src' => "'none'", 'object_src' => "'none'",
......
...@@ -56,22 +56,22 @@ RSpec.describe Gitlab::ContentSecurityPolicy::ConfigLoader do ...@@ -56,22 +56,22 @@ RSpec.describe Gitlab::ContentSecurityPolicy::ConfigLoader do
context 'adds all websocket origins to support Safari' do context 'adds all websocket origins to support Safari' do
it 'with insecure domain' do it 'with insecure domain' do
stub_config_setting(host: 'example.com', https: false) stub_config_setting(host: 'example.com', https: false)
expect(directives['connect_src']).to eq("'self' https://cdn.cookielaw.org ws://example.com") expect(directives['connect_src']).to eq("'self' ws://example.com")
end end
it 'with secure domain' do it 'with secure domain' do
stub_config_setting(host: 'example.com', https: true) stub_config_setting(host: 'example.com', https: true)
expect(directives['connect_src']).to eq("'self' https://cdn.cookielaw.org wss://example.com") expect(directives['connect_src']).to eq("'self' wss://example.com")
end end
it 'with custom port' do it 'with custom port' do
stub_config_setting(host: 'example.com', port: '1234') stub_config_setting(host: 'example.com', port: '1234')
expect(directives['connect_src']).to eq("'self' https://cdn.cookielaw.org ws://example.com:1234") expect(directives['connect_src']).to eq("'self' ws://example.com:1234")
end end
it 'with custom port and secure domain' do it 'with custom port and secure domain' do
stub_config_setting(host: 'example.com', https: true, port: '1234') stub_config_setting(host: 'example.com', https: true, port: '1234')
expect(directives['connect_src']).to eq("'self' https://cdn.cookielaw.org wss://example.com:1234") expect(directives['connect_src']).to eq("'self' wss://example.com:1234")
end end
end end
...@@ -81,7 +81,7 @@ RSpec.describe Gitlab::ContentSecurityPolicy::ConfigLoader do ...@@ -81,7 +81,7 @@ RSpec.describe Gitlab::ContentSecurityPolicy::ConfigLoader do
end end
it 'adds CDN host to CSP' do it 'adds CDN host to CSP' do
expect(directives['script_src']).to eq("'strict-dynamic' 'self' 'unsafe-inline' 'unsafe-eval' https://www.google.com/recaptcha/ https://www.recaptcha.net https://apis.google.com https://cdn.cookielaw.org https://example.com") expect(directives['script_src']).to eq("'strict-dynamic' 'self' 'unsafe-inline' 'unsafe-eval' https://www.google.com/recaptcha/ https://www.recaptcha.net https://apis.google.com https://example.com")
expect(directives['style_src']).to eq("'self' 'unsafe-inline' https://example.com") expect(directives['style_src']).to eq("'self' 'unsafe-inline' https://example.com")
expect(directives['font_src']).to eq("'self' https://example.com") expect(directives['font_src']).to eq("'self' https://example.com")
end end
...@@ -94,7 +94,7 @@ RSpec.describe Gitlab::ContentSecurityPolicy::ConfigLoader do ...@@ -94,7 +94,7 @@ RSpec.describe Gitlab::ContentSecurityPolicy::ConfigLoader do
end end
it 'adds sentry path to CSP without user' do it 'adds sentry path to CSP without user' do
expect(directives['connect_src']).to eq("'self' https://cdn.cookielaw.org ws://example.com dummy://example.com/43") expect(directives['connect_src']).to eq("'self' ws://example.com dummy://example.com/43")
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment