Skip to content

Commit

Permalink
OTWO-7218 Add CSP headers[report only] (#1785)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-sig authored Apr 18, 2024
1 parent 2935f22 commit 29a6ef8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/controllers/csp_violation_reports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class CspViolationReportsController < ApplicationController
skip_before_action :verify_authenticity_token

def report
notify_airbrake(request.raw_post)
head :ok
end
end
15 changes: 15 additions & 0 deletions config/initializers/content_security_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

allowed_script_sources = %w[www.google.com cdn.firebase.com www.gstatic.com s7.addthis.com cdnjs.cloudflare.com]

Rails.application.config.content_security_policy do |policy|
policy.default_src :self, :https
policy.font_src :self, :https, :data
policy.img_src :self, :https, :data
policy.object_src :none
policy.script_src :self, :https, :unsafe_eval, *allowed_script_sources
policy.style_src :self, :https, :unsafe_inline
policy.report_uri '/csp-violation-report'
end

Rails.application.config.content_security_policy_report_only = true
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
post 'sessions' => 'sessions#create', as: :oh_sessions
get 'health' => 'sessions#health'

post '/csp-violation-report' => 'csp_violation_reports#report'

resources :stack_entries, only: :new

resources :activation_resends, only: %i[new create]
Expand Down
10 changes: 10 additions & 0 deletions test/controllers/csp_violation_reports_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require 'test_helper'

class CspViolationReportsControllerTest < ActionController::TestCase
it 'must notify errbit' do
@controller.expects(:notify_airbrake).once
post :report
end
end

0 comments on commit 29a6ef8

Please sign in to comment.