Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions app/api/api_root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ class ApiRoot < Grape::API
format :json

before do
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'

Thread.current.thread_variable_set(:ip, request.ip)
end

Expand Down
21 changes: 19 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,27 @@ def self.fetch_boolean_env(name)
Rails.root.join('app/models/d2l')

# CORS config
# Configure a strict allowlist. Override per environment via:
# CORS_ALLOWED_ORIGINS="http://localhost:4200,https://frontend.example.edu"
default_cors_origins = [
'http://localhost:4200',
"https://#{config.institution[:host]}"
].uniq
allowed_cors_origins = ENV.fetch('CORS_ALLOWED_ORIGINS', default_cors_origins.join(','))
.split(',')
.map(&:strip)
.reject(&:empty?)
.uniq

config.middleware.insert_before Warden::Manager, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: %i(get post put delete options)
origins do |source, _env|
allowed_cors_origins.include?(source)
end

resource '*',
headers: %w[Content-Type Authorization Accept],
methods: %i[get post put delete options]
end
end

Expand Down