Skip to content

Commit

Permalink
Merge pull request #89 from aplijobs/feature/cors_webwidget
Browse files Browse the repository at this point in the history
Security | Adding CORS domain restriction based on webwidget url and widget filtering
  • Loading branch information
cmm-apli authored May 25, 2023
2 parents 3b44e23 + f241973 commit 3f89111
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
8 changes: 8 additions & 0 deletions app/controllers/widgets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class WidgetsController < ActionController::Base
before_action :set_token
before_action :set_contact
before_action :build_contact
before_action :check_domain
after_action :allow_iframe_requests

private
Expand All @@ -24,6 +25,13 @@ def set_web_widget
render json: { error: 'web widget does not exist' }, status: :not_found
end

def check_domain
return if request.base_url.downcase.start_with? @web_widget.website_url.downcase

Rails.logger.error('web widget does not match with expected domain')
render json: { error: 'web widget does not match with expected domain' }, status: :not_found
end

def set_token
@token = permitted_params[:cw_conversation]
@auth_token_params = if @token.present?
Expand Down
64 changes: 52 additions & 12 deletions config/initializers/cors.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
# config/initializers/cors.rb
# ref: https://github.com/cyu/rack-cors

# font cors issue with CDN
# Ref: https://stackoverflow.com/questions/56960709/rails-font-cors-policy
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '/packs/*', headers: :any, methods: [:get, :options]
resource '/audio/*', headers: :any, methods: [:get, :options]
# Make the public endpoints accessible to the frontend
resource '/public/api/*', headers: :any, methods: :any

if ActiveModel::Type::Boolean.new.cast(ENV.fetch('CW_API_ONLY_SERVER', false)) || Rails.env.development?
resource '*', headers: :any, methods: :any, expose: %w[access-token client uid expiry]
# rubocop:disable Rails/ApplicationRecord
class WebWidget < ActiveRecord::Base
self.table_name = 'channel_web_widgets'
end
# rubocop:enable Rails/ApplicationRecord

unless ENV.fetch('FRONTEND_URL', nil).nil?
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins ENV.fetch('FRONTEND_URL', '')
resource '/packs/*', headers: :any, methods: [:get, :options]
resource '/audio/*', headers: :any, methods: [:get, :options]
# Make the public endpoints accessible to the frontend
resource '/public/api/*', headers: :any, methods: :any

if ActiveModel::Type::Boolean.new.cast(ENV.fetch('CW_API_ONLY_SERVER', false)) || Rails.env.development?
resource '*', headers: :any, methods: :any, expose: %w[access-token client uid expiry]
end
end
end
end

if ENV.fetch('CORS_WIDGET_CONFIG', true)
WebWidget.all.each do |widget|
# font cors issue with CDN
# Ref: https://stackoverflow.com/questions/56960709/rails-font-cors-policy
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins widget.website_url
resource '/packs/*', headers: :any, methods: [:get, :options]
resource '/audio/*', headers: :any, methods: [:get, :options]
# Make the public endpoints accessible to the frontend
resource '/public/api/*', headers: :any, methods: :any

if ActiveModel::Type::Boolean.new.cast(ENV.fetch('CW_API_ONLY_SERVER', false)) || Rails.env.development?
resource '*', headers: :any, methods: :any, expose: %w[access-token client uid expiry]
end
end
end
end
else
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '/packs/*', headers: :any, methods: [:get, :options]
resource '/audio/*', headers: :any, methods: [:get, :options]
# Make the public endpoints accessible to the frontend
resource '/public/api/*', headers: :any, methods: :any

if ActiveModel::Type::Boolean.new.cast(ENV.fetch('CW_API_ONLY_SERVER', false)) || Rails.env.development?
resource '*', headers: :any, methods: :any, expose: %w[access-token client uid expiry]
end
end
end
end
Expand Down

0 comments on commit 3f89111

Please sign in to comment.